Getting Started

(lily) #1

Chapter 9 – Digital Meets Analog – ADC and DAC


Projects


We will write code to allow us to use HyperTerminal to request a reading from the
light, temperature and voltage sensors of the Butterfly. You’ve already seen the
debugging tale above so you know how much fun I had writing this stuff, so enjoy
it or else.


Initializing the ADC


The Butterfly has the ATmega169 pin 62, (AREF) connected to a bypass capacitor
to help lessen noise on the ADC, so we set the ADMUX bits 6 and 7 to 0 to select
the 'use external reference' option. We use the ‘input’ variable to set the
multiplexer. to connect the ADC to pin 61 (ADC0) using the ADMUX register
(data book p 207).


ADMUX = input; // external AREF and ADCx


Next we set the ADC Control and Status Register A. The ADEN bit enables the
ADC. The ADPSx bits select the prescaler.


// set ADC prescaler to , 1MHz / 8 = 125kHz
ADCSRA = (1<<ADEN) | (1<<ADPS1) | (1<<ADPS0);


ternal AREF and ADCx

, 1MHz / 8 = 125kHz
ADCSRA = (1<<ADEN) | (1<<ADPS1) | (1<<ADPS0);

input = ADC_read(); // clear hairballs
}

Finally we take a dummy reading, which basically allows the ADC to hack up any
hairballs before we take any real readings


input = ADC_read();

void ADC_init(char input)
{
ADMUX = input; // ex

// set ADC prescaler to
Free download pdf