0% found this document useful (0 votes)
23 views17 pages

Microcontroller Section #6

The document discusses microcontrollers and contains example questions and solutions related to analog to digital conversion and reading sensor values using an AVR microcontroller. It includes code examples to read an ADC using polling and interrupts and to read a temperature sensor value and display it on an output port.

Uploaded by

beshoyalks24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views17 pages

Microcontroller Section #6

The document discusses microcontrollers and contains example questions and solutions related to analog to digital conversion and reading sensor values using an AVR microcontroller. It includes code examples to read an ADC using polling and interrupts and to read a temperature sensor value and display it on an output port.

Uploaded by

beshoyalks24
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Microcontrollers

Section #6

Spring 23-24
Question #1

For the A/D of ATmega32, find the step size for Vref = 2.048 V

1
Question #1

For the A/D of ATmega32, find the step size for Vref = 2.048 V
Solution

 Step size = , Where n is number of bits


 For AVR ATmega32 bits , so

1
Question #2

With Vref = 2.56 V, find the Vin for the following outputs:
(a) D9–D0 = 1111111111 (b) D9–D0 = 1100110000

Solution

1
Question #2

With Vref = 2.56 V, find the Vin for the following outputs:
(a) D9–D0 = 1111111111 (b) D9–D0 = 1100110000

Solution
(a) D9–D0 = 1111111111

1
Question #2

With Vref = 2.56 V, find the Vin for the following outputs:
(a) D9–D0 = 1111111111 (b) D9–D0 = 1100110000

Solution
(b) D9–D0 = 1100110000

1
Question #2

Find the first conversion times for the following cases if XTAL = 4 MHz. Are
they are acceptable?
(d)
Solution

So, the conversion time for /64 is 16 microseconds.

1
Write an AVR C program to read the sensor and display it on Port D

1
Write an AVR C program to read the sensor and display it on Port D

Input (port A) ADCSRA Register()

ADMUX Register () 1
Write an AVR C program to read the sensor and display it on Port D

Vref

= zero (connected to ground )

Vref values (usually take standard Vref=2.56 ) 1


Write an AVR C program to read the sensor and display it on Port D

Shift Right

Shift Left

1
Write an AVR C program to read the sensor and display it on Port D

1
Write an AVR C program to read the sensor and display it on Port D

𝟏
2𝟐
2𝟑
2𝟒
2𝟓
2𝟔
2𝟕
2 1
Program #1
Reading ADC Using Polling Method in C
Solution:
#include <avr/io.h> //standard AVR header
int main (void)
{
DDRB = 0xFF; //make Port B an output
DDRD = 0xFF; //make Port D an output
DDRA = 0; //make Port A an input for ADC input
ADCSRA= 0x87; //make ADC enable and select ck/128
ADMUX= 0xC0; //2.56V Vref, ADC0 single ended input
//data will be right-justified
while (1)
{
ADCSRA|=(1<<ADSC); //start conversion
while((ADCSRA&(1<<ADIF))==0); //wait for conversion to finish
PORTD = ADCL; //give the low byte to PORTD
PORTB = ADCH; //give the high byte to PORTB
}
return 0;
}
2
Program #2
Reading ADC Using Interrupts in C
Solution:
#include <avr/io.h>
#include <avr/interrupt.h>
ISR(ADC_vect)
{
PORTD = ADCL; //give the low byte to PORTD
PORTB = ADCH; //give the high byte to PORTB
ADCSRA|=(1<<ADSC); //start conversion
}
int main (void)
{
DDRB = 0xFF; //make Port B an output
DDRD = 0xFF; //make Port D an output
DDRA = 0; //make Port A an input for ADC input
sei(); //enable interrupts
ADCSRA= 0x8F; //enable and interrupt select ck/128
ADMUX= 0xC0; //2.56V Vref and ADC0 single-ended
//input right-justified data
ADCSRA|=(1<<ADSC); //start conversion
while (1); //wait forever
return 0;
}
2
Program #3
Reading Temperature Sensor in C
Solution:
//this program reads the sensor and displays it on Port D
#include <avr/io.h> //standard AVR header
int main (void)
{
DDRD = 0xFF; //make Port D an output
DDRA = 0; //make Port A an input for ADC input
ADCSRA = 0x87; //make ADC enable and select ck/128
ADMUX = 0xE0; //2.56 V Vref and ADC0 single-ended
//data will be left-justified
while (1)
{
ADCSRA |= (1<<ADSC); //start conversion
while((ADCSRA&(1<<ADIF))==0); //wait for end of conversion
PORTB = ADCH; //give the high byte to PORTB
}
return 0;
}

You might also like