- Wed Nov 05, 2008 6:00 pm
#58438
Hi there,
Finally, I learned enough about using ADC to come up with the following code. It works fine. I have a potentiometer that I got from Radioshak and it is from 0-10K. When I turn it from end to end the two different LEDs turn on and off, telling me that I got the ADC working. Now my question is:
Obviously, my objective is to determine real pot values. So how do I do that?
how can I understand that a value that is read from ADCH is the correct value and how to correlate the value to real value of potentiomenter?
In another words, How do I figure out the real value of pot, and display that correct value?
With the above code, I can read a value but how do I translated reading from ADCH into real value of pot? Sorry if I am not clear
Finally, I learned enough about using ADC to come up with the following code. It works fine. I have a potentiometer that I got from Radioshak and it is from 0-10K. When I turn it from end to end the two different LEDs turn on and off, telling me that I got the ADC working. Now my question is:
Obviously, my objective is to determine real pot values. So how do I do that?
how can I understand that a value that is read from ADCH is the correct value and how to correlate the value to real value of potentiomenter?
In another words, How do I figure out the real value of pot, and display that correct value?
With the above code, I can read a value but how do I translated reading from ADCH into real value of pot? Sorry if I am not clear
Code: Select all
#include <avr/io.h>
void InitADC(void);
int main(void)
{
DDRB = 0xFF;
InitADC();
while(1)
{
if(ADCH < 100)
{
PORTB = 0xFE; // LED0 on
PORTB = 0xFF; // LED1 off
}
else
{
PORTB = 0xFF; // LED0 off
PORTB = 0xFD; // LED1 on
}
}
}
void InitADC(void)
{
ADCSRA |= (1 << ADPS2) | (1 << ADPS1) |(1 << ADPS0); // Set ADC prescalar to 128 - 125KHz sample rate @ 16MHz
ADMUX |= (1 << REFS0); // Set ADC reference to AVCC
ADMUX |= (1 << ADLAR); //Left adjust ADC result to allow easy 8 bit reading
// No MuX values needed to be changed to use ADC0
ADCSRA |= (1 << ADATE); //Set ADC to Free-Running mode
ADCSRA |= (1 << ADEN); // Enable ADC
ADCSRA |= (1 << ADSC); // Start A2D conversions
}