ADU no interrupt  Ver.1.0.
kner 2016
start.cpp
Go to the documentation of this file.
1 
17 #include <Arduino.h>
18 //#define DEBUG_LCD
19 #include "_debug.h"
20 
21 #define CLK PORTB1
22 #define DATA PORTB2
23 #define TRIGGER PINB0
24 #define ADCINPUT 4
25 
26 //some macros for easier typing
27 #define DATA_LOW PORTB &= ~_BV(DATA);
28 #define DATA_HIGH PORTB |= _BV(DATA);
29 #define CLK_LOW PORTB &= ~_BV(CLK);
30 #define CLK_HIGH PORTB |= _BV(CLK);
31 
32 void setup()
33 {
34  DDRB |= _BV(CLK)|_BV(DATA); // Set CLK and DATA as output
35  PORTB |= _BV(TRIGGER); // Set PB0 as input with pullup
36 
37  // ADU 10 Bit
38  ADCSRA=(1<<ADEN); //enable
39  ADCSRA = (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0); //super slow for high precision
40  ADMUX=(0<<REFS1) | (1<<REFS0); // ADC Voltage Reference: AVCC pin
41  ADMUX |= (0<<ADLAR); //right adjust -- for docu only
42  ADMUX |= ADCINPUT; // ADC4 as input -- sets all 3 bits
43 // DDRB |= _BV(TRIGGER); //simulation only: start with TRIGGER = 1 to simulate falling edge
44 }
45 void onePulse(){
46  CLK_LOW DATA_HIGH CLK_HIGH DATA_HIGH CLK_LOW DATA_HIGH
47 }
48 void zeroPulse(){
49  CLK_LOW DATA_LOW CLK_HIGH DATA_LOW CLK_LOW DATA_LOW
50 }
51 
52 void serialout(uint16_t dat){
53  //data are right adjusted, so start with right bits
54  // 10 Bits max
55  for (int i=0; i<10; i++){
56  if (bit_is_set(dat,i))
57  onePulse();
58  else
59  zeroPulse();
60  }
61 }
62 
63 void loop()
64 {
65  loop_until_bit_is_clear(PINB,PINB0); //wait for falling edge
66 
67  ADCSRA|=(1<<ADSC); //start adu conversion
68  loop_until_bit_is_clear(ADCSRA,ADSC); //wait until conversion finished
69  // serialout(5);
70  serialout(ADC);
71 
72  loop_until_bit_is_set(PINB,PINB0); //wait for key released
73  //wait for 30ms debounce time
74 }
75 
76 
77