Getting Started

(lily) #1

Chapter 9 – Digital Meets Analog – ADC and DAC


We turn the sensors off to save power.


cbi(PORTF,PF3); // mt cbi(PORTF, PORTF3); // disable the VCP
cbi(DDRF,DDF3); // mt cbi(DDRF, PORTF3);


And we disable the ADC and return the calculated value.


cbi(ADCSRA, ADEN); // disable the ADC


return ADCr;


Giving us the ADC_read function:


int ADC_read(void)
{
char i;
int ADC_temp;
// mt int ADC = 0 ;
int ADCr = 0;


// To save power, the voltage over the LDR and the NTC is
// turned off when not used. T is is done by controlling the
// voltage from an I/O-pin (PORTF3)
sbi(PORTF, PF3); // Enable the VCP (VC-peripheral)
sbi(DDRF, DDF3); // sbi(DDRF, PORTF3);


sbi(ADCSRA, ADEN); // Enable the ADC


//do a dummy readout first
ADCSRA |= (1<<ADSC); // do single conversion


// wait for conversion done, ADIF flag active
while(!(ADCSRA & 0x10));


// do the ADC conversion 8 times for better accuracy
for(i=0;i<8;i++)
{
ADCSRA |= (1<<ADSC); // do single conversion


// wait for conversion done, ADIF flag active
while(!(AD


ADC_temp = ADCL; // read out ADCL register

h


CSRA & 0x10));
Free download pdf