The reading ADC value in the single ended conversion could be obtained from:
Reference voltage VREF is normally +5 V. Input voltage must not exceed +5 V.
To calculate the voltage, we can obtain it from:
In this example, I use ADC0 to read an analog input voltage fed from a POT. The POT varies an analog output voltage from 0 V to +5 V DC. The MCU displays the reading result of the voltage from 0.0 V to 5.0 V.
ADC0 read the input analog voltage. PortC and PortD drive a two-digit SSD. The voltage reading is from 0.0 V to 5.0 V. |
Source code is here:
#include <avr/io.h>
#define F_CPU 4000000UL
#include <util/delay.h>
int main(void)
{ float voltage;
char _voltage;
char ssd[16]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,
0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71};
//PortC as output
DDRC=0xFF;
//PortD as output
DDRD=0xFF;
//PA0 or ADC0 as an analog input
DDRA=0;
//Turn on the ADC module
ADCSRA|=(1<<ADEN);
while (1)
{
//Start the conversion
ADCSRA|=(1<<ADSC);
//Wait for the completion
while((ADCSRA&(1<<ADSC))==1);
/*Convert ADC to voltage*/
voltage=(ADCL+(ADCH<<8))*5.0/1024;
voltage*=10;
/*convert float to SSD data*/
_voltage=(char)voltage;
/*Displaying the Data*/
PORTD=0x00;
PORTC=ssd[_voltage/10]|0x80;
PORTD=0x01;
_delay_ms(10);
PORTD=0x00;
PORTC=ssd[_voltage%10];
PORTD=0x02;
_delay_ms(10);
}
}
The ADC reading is 2.5 V analog voltage. |
Back to main tutorial page ATMega32 tutorials in C with Atmel Studio 7.
No comments:
Post a Comment