Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

2. Complete the function stub below. The function preforms a single analog to di

ID: 2250098 • Letter: 2

Question

2. Complete the function stub below. The function preforms a single analog to digital conversion on the ATmega32 using the specified parameter “channel” which is ADC channel on which the conversion is to be preformed. Assume the AVR is running at 8 MHz. Use the |=, &=, and << operators for all register configurations! (20 Points)

/* * This function:

* Sets VRef = AVCC

* Right justifies the ADC result

* Sets the ADC channel using the function parameter

“channel” * which can be 0-7 in value * Enables the ADC

* Selects a clock source so that the ADC clock is < 200KHz

* Starts a single conversion

* Waits for the conversion to finish

* Returns the 10-bit ADC value

*/

uint16_t readADC(uint8_t channel) {

Explanation / Answer

#include<avr/io.h>

#include"lcd.h"

void IntADC()

{

ADMUX=(1<<REFSO); //this is for Vref=AVcc

ADCSRA=(1<<ADEN)||(1<<ADPS2)|(1<<ADPS1)|(1<<ADPS0); //this is for setting clock frequency between 50-200kHz

}

unit 16_t readADC(uint 8_t channel)

{

channel=channel&0b00000111;

ADMUX |=channel;

// Starting single conversion

ADCSRA |=(1<<ADSC);

//waiting for end of conversion

while(!(ADCSRA&(1<<ADIF)));

ADCSRA |=(1<<ADIF);

Return (ADC);

}

void wait()

{

uint 8_t i;

for(i=0;i<20;i++)

-delay-loop-2(0);

}

void main()

{

uint 6_t adc_result;

//displaying in LCD by initializing it

LCDInit(LS_BLINK | LS_ULINE);

LCDClear();

InitADC();

LCD WriteString("ADC result");

LCD WriteString xy(0,1,"ADC=");

while(1)

{

adc_result=ReadADC(0);

LCD WriteInt xy(4,1,adc_result,4); //displaying output

wait();

}}