/* * Analog-to-digital converter (ADC) Test Program * * This program reads and prints out the current voltage of the signal applied * to the PORTA:0 pin. * * This program uses VT100 escape sequences to control the cursor on the * Hyperterminal screen, rather than just blasting out line after line of output. * Terminal escape sequences allow you to do things like clearing the screen, * position the cursor before text output, and changing the text color. * Given that '\x1B' is the hex representation of the ASCII escape (ESC) character, * the sequence ESC[2J clears the terminal, and ESCc resets the terminal. * * One reference for VT100 escape codes is: * http://ascii-table.com/ansi-escape-sequences-vt-100.php * * Last Modified 2010/08/19 02:19 (ebw) */ #include // for PORTx, DDRx, AD*, etc. #include // for _delay_ms() #include // for printf() #include "uart.h" // for uart_init() /* functions to declare before their use */ void printWelcome(), printValue(); /* * Initialization consists of setting up the ADC and the UART, both of * which are described in detail in the ATmega16 datasheet. * * The ADC is first setup to use AVCC for the ADC voltage reference, then * the ADC enabled and the ADC clock is configured to prescale the system clock * down by 128 to be slow enough to get full 10-bit accuracy. */ void init() { ADMUX = _BV(REFS0); ADCSRA = _BV(ADEN) | _BV(ADPS2) | _BV(ADPS1) | _BV(ADPS0); uart_init(); printWelcome(); } int main() { uint16_t adc_level; // for holding ADC value (if needed multiple times) init(); while (1) { ADCSRA |= _BV(ADSC); // start ADC conversion while (ADCSRA & _BV(ADSC)) ; // wait until conversion done (bit cleared) adc_level = ADC; // capture the ADC value printValue(adc_level); // for debugging, if we need it _delay_ms(100); // delay 100msec } return 0; // never executed... } /* * Print the magic VT100 escape sequence to reset and clear the screen, * then the welcome banner. */ void printWelcome() { printf("\x1B[2J\x1B\x63"); printf("Welcome to the ADC test program!"); } /* * Print the magic VT100 escape sequences to move the cursor to , * erase the current line, then the requested value. */ void printValue(uint16_t value) { printf("\x1B[2;1H\x1B[2K"); printf("ADC value: %u", value); }