ADC 1V 8Bit Timer2  ver.1.0
Maid D. 2016
start.cpp
Go to the documentation of this file.
1 
16 #include <Arduino.h>
17 
18 #define DEBUG_LCD
19 #include "_debug.h"
20 
21 void setup()
22 {
23  //kill Arduino Timer0 Interrupt; millis() not available!
24  TIMSK0 &=~_BV(TOIE0);
25  TCCR2A = 0; //clear flags set by arduino
26  TCCR1A = 0;
27  TCCR1B = 0;
28 
29  // To Select ADC0 the Mux Settings are 0-0-0-0 => not neccessary to set Zero...
30  ADMUX |=_BV(REFS0); // Vref -- ver.1.1 internal 1.1 Voltage Rererence would be better
31  ADCSRA|= _BV(ADEN); // Enable ADC
32  ADCSRA|=_BV(ADIE); // Interrupt Enable for ADC
33  ADCSRA |= _BV(ADPS0); // Set Clock Division Factor to 128 for better quality
34  ADCSRA |= _BV(ADPS1); //-------------------------,--------------------------
35  ADCSRA |= _BV(ADPS2); //-------------------------,--------------------------
36  TCCR2B |= _BV(CS22); //Set Prescaler of Timer2 to 64, timer starts now
37  TIMSK2 |= _BV(TOIE2); // Enable Flag for Timer2
38  TCNT2 = 6; // Set count to 6 to get an OVF every 1ms @16Mhz --> (256-6)x64x62.5ns=1ms
39  DDRB = 0xFF;
40  sei();
41 }
42 
43 void loop()
44 {
45  volatile static int dummy=0; dummy++; //needed for simulator if no additional lines of code
46 }
47 
52 ISR(ADC_vect)
53 {
54  // ADC comes up here when he done converting....
55  PORTB = ADCL; // in ADCL the first 8 bits are written (digital result of the ADC), write these bits to PORTB
56  //T("A")
57 }
58 
59 ISR(TIMER2_OVF_vect)
60 {
61  T("T")
62  TCNT2 = 6; // Set count to 6 again so we get an OFV in 1ms
63  ADCSRA |= _BV(ADSC); // Start single conversion when Timer Overflow
64 }
void loop()
Definition: start.cpp:43
#define T(x)
Definition: _debug.h:85
void setup()
Definition: start.cpp:21
ISR(ADC_vect)
Definition: start.cpp:52