2012年5月4日金曜日

ADC アナログ入力をデジタル変換する

sketchの勉強のためにアナログ入力をデジタル変換することを考える。
これは受信機のRITのために必要である。

Sketch


//DDS_TestProgram by JA1NHL  08 Feb 2012
// EncodeDisplayADCFeb08
//include the library code:
#include <LiquidCrystal.h>
//LiquidCrystal(rs, enable, d4, d5, d6, d7)
LiquidCrystal lcd(12, 11, 7, 6, 5, 4);
#include <Bounce.h>
  int ledPin=13;          //monitor
  int adcInputPin=17;    //RIT INPUT PIN
  int adcInputValue=0;
  long adcInputValue2=0;        // RIT work
  char adcStrings[ ]="1234";
  char ritStrings[ ]="1234";
  char TxRxState='R';
 void setup() {
  Serial.begin(9600);               // output for Debug
  lcd.begin(16, 2);                  // set up the LCD's number of columns and rows:
  // Input Output
  pinMode(ledPin,OUTPUT);      
  // turn on pullup resistors   内蔵プルアップ 抵抗は使用しない

 //setup the LCD's number of columns and rows;
  lcd.begin(16,2);                      
  lcd.clear();
}

// main loop, work is done by interrupt service routines, this one only prints stuff
void loop() {
 adcInputValue=analogRead(adcInputPin);
 adcInputValue=adcInputValue/10;
 adcInputValue=adcInputValue*10;
 adcInputValue=510-adcInputValue;
 String s =String(adcInputValue);
lcd.setCursor(1,1);
    lcd.print("* RIT: ");
 
    if (adcInputValue<-100 )  { //-510
      lcd.print(s.substring(0,2));
      lcd.print(".");
      lcd.print(s.substring(2,4));
  }  else if (adcInputValue>=-100 && adcInputValue<0 ) {  //-90
      lcd.print(s.substring(0,1));
      lcd.print("0.");
      lcd.print(s.substring(1,3));
  } else if (adcInputValue>=0 && adcInputValue<100 ) { //90
      lcd.print("+0.");
      lcd.print(s.substring(0,2));
  }  else if (adcInputValue>=100 ) { //510
      lcd.print("+");
      lcd.print(s.substring(0,1));
      lcd.print(".");
      lcd.print(s.substring(1,3));
  }
}






/*
http://www.arduino.cc/playground/Main/RotaryEncoders#Example1
*/
/*
Another Interrupt Library THAT
REALLY WORKS (the Encoder
interrupts the processor and
debounces like there is no
tomorrow).
by rafbuff
*/

/* interrupt routine for Rotary Encoders
   tested with Noble RE0124PVB 17.7FINB-24 http://www.nobleusa.com/pdf/xre.pdf - available at pollin.de
   and a few others, seems pretty universal
   The average rotary encoder has three pins, seen from front: A C B
   Clockwise rotation A(on)->B(on)->A(off)->B(off)
   CounterCW rotation B(on)->A(on)->B(off)->A(off)
   and may be a push switch with another two pins, pulled low at pin 8 in this case
   raf@synapps.de 20120107
*/

0 件のコメント:

コメントを投稿