Getting Started

(lily) #1

Chapter 9 – Digital Meets Analog – ADC and DAC


ADC_temp += (ADCH << 8); // read out ADCH register


// accumulate result (8 samples) for later averaging
ADCr += ADC_temp;
}


ADCr = ADCr >> 3; // average the 8 samples


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


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


return ADCr;
}


Light Meter


The Butterfly has a Light Dependent Resistor, LDR, connected to ADC channel 2.
The resistance of the LDR decreases as the light increases, so the voltage
measured will decrease as light decreases.
We write the getLight function:


void getLight()
{
char light[]= {'0','0','0','\0'};
i r


// Initialize the ADC to the light sensor channel
ADC_init(2);


ADCresult = ADC_read();


itoa(ADCresult, light, 10);


// Send the temperature to the PC
sendString("The light reading is ");
sendString(light);
sendString(" somethings.\r");


}


This is straightforward and returns a value for the light. The light units
‘somethings’ is a precise scientific measure that means: ‘I don’t have a clue as to


nt ADC esult = 0;
Free download pdf