Reads the four 8-bit analog inputs on TB-1 training board.
Toolkit:AVR Development System
Location:\bipom\devtools\Arduino\ATmega2560\Examples\tb1\tb1adc
#include "tb1adc.h" void setup() { Serial.begin(115200); // prints string with ending line break Serial.println("BiPOM Arduino"); Serial.println("TB1 ADC Example"); // ADC control and data pins are outputs pinMode(ADCCS, OUTPUT); pinMode(ADCCLK, OUTPUT); pinMode(ADCDIN, OUTPUT); } void loop() { byte i; delay(100); Serial.println( "\n" ); for( i=0; i<4; i++ ) { Serial.print( Convert(i), DEC ); Serial.print( " " ); } } byte Convert( byte channel) { byte value,i; byte state; // Make the data pin an output from the microcontroller so we can control it pinMode(ADCDIN, OUTPUT); /* Reset the A/D converter */ digitalWrite(ADCCS, HIGH); digitalWrite(ADCCLK, LOW); digitalWrite(ADCDIN, HIGH); /**** Start Conversion ****/ digitalWrite(ADCCS, LOW); delay(1); /* Start bit */ digitalWrite(ADCDIN, HIGH); Clock(); /* SGL/DIF */ digitalWrite(ADCDIN, HIGH); Clock(); if( channel & 0x01 ) { digitalWrite(ADCDIN, HIGH); } else { digitalWrite(ADCDIN, LOW); } Clock(); /* SELECT */ if( channel & 0x02 ) { digitalWrite(ADCDIN, HIGH); } else { digitalWrite(ADCDIN, LOW); } Clock(); /* 1 millisecond delay for the Multiplexer to settle */ delay(1); Clock(); value = 0; digitalWrite(ADCDIN, HIGH); // Make the data pin an input to microcontroller now pinMode(ADCDOUT, INPUT); /* Clock out the 8-bit data from A/D converter */ for( i=0; i<8; i++ ) { state = digitalRead(ADCDOUT); if( state ) value |= 1; Clock(); if( i < 7 ) value = value << 1; } // 8 dummy clocks for( i=0; i<8; i++ ) Clock(); return value; } void Clock() { digitalWrite(ADCCLK, HIGH); digitalWrite(ADCCLK, HIGH); /* A little delay */ digitalWrite(ADCCLK, LOW); }