Write a program to measure the analog voltage on the PCO(ADCO) pin using the ADC
ID: 3810829 • Letter: W
Question
Write a program to measure the analog voltage on the PCO(ADCO) pin using the ADC peripheral with V_ref selected to be AV_cc. Using a voltage source, apply a DC voltage to ADC0 and record measurements at 20 different input voltages between 0 and 5 volts. Use a multimeter to measure the actual input voltage V_in. Measure the supply voltage AV_cc on the 5V pin. Provide a table that includes the measured (by multimeter) voltage V_in, the expected ADC value for V_in, the recorded ADC value (a 10-bit unsigned integer), and the voltage that the ADC value represents. Do your measurements match your expectations? How do you expect the measurement from Part 1 to affect these results? Explain your answer. Plot the recorded ADC value (y-axis) versus the measured input voltage V_in (x-axis).Explanation / Answer
Ans:
// program for reading and displaying
#include <avr/io.h>
// includes lcd.h
#include "lcd.h"
void InitADC()
{
ADMUX=(1<<REFS0); // For Aref=AVcc;
ADCSRA=(1<<ADEN)|(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0); //Rrescalar div factor =128
}
uint16_t ReadADC(uint8_t ch)
{
ch=ch&0b00000111;
ADMUX|=ch;
ADCSRA|=(1<<ADSC);
while(!(ADCSRA & (1<<ADIF)));
ADCSRA|=(1<<ADIF);
return(ADC);
}
void Wait()
{
uint8_t i;
for(i=0;i<20;i++)
_delay_loop_2(0);
}
void main()
{
uint16_t adc_result;
LCDInit(LS_BLINK|LS_ULINE);
LCDClear();
InitADC();
LCDWriteString("ADC Test");
LCDWriteStringXY(0,1,"ADC=");
while(1)
{
// for reading values, channel-0
adc_result=ReadADC(0);
// lcd values
LCDWriteIntXY(4,1,adc_result,4);
Wait();
}
}
// for measuring
#define BATTERY_VOLTAGE(v)
do {
ADCCON2 = 0x2F;
ADCCON1 = 0x76;
while(!(ADCCON1 & 0x80));
v = ADCL;
// values
v |= (((unsigned int)ADCH) << 5);
} while(0)
#define CONST 0.0183195
unsigned char getBatteryVoltage(void) {
unsigned int adcValue;
SAMPLE_BATTERY_VOLTAGE(adcValue);
adcValue >>= 4;
return CONST * adcValue;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.