Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
14 views8 pages

Lab 3 Tasks - Sajjad

Uploaded by

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

Lab 3 Tasks - Sajjad

Uploaded by

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

Sajjad Ali EE-21232

LAB: 03 – TASKS
Task: 01: To test the Example 1 on ATmega328P and verify working of ADC
using an external D/A Converter.
1. Create a new AVR project and build the code given in the Example 1. Test the obtained
digital output by varying the output from potentiometer. Observe the on/off status of
LEDs (used as indicators for digital output) and verify the digital voltage by calculation
for 8-bit ADC at given reference voltage.

CODE:
#include <avr/io.h>

int main()
{
DDRD = 0xFF;
DDRC = 0x00;
ADCSRA = 0x87;
ADMUX = 0x60;
while(1)
{
ADCSRA |= (1 << ADSC);
while(ADCSRA & (1 << ADIF) == 0);
PORTD = ADCH;
}
return 0;
}

TINKERCAD SIMULATION:
• INPUT = 1V:
Sajjad Ali EE-21232

• INPUT = 2V:

• INPUT = 3V:

• INPUT = 4V:
Sajjad Ali EE-21232

• INPUT = 5V:

VERIFICATION:
The maximum value 8 bits of binary data can represent is 255, thus by dividing the output of
ADC to 255 and then multiplying the result by 5 (maximum voltage the potentiometer can pass
on), we can verify if our ADC is working properly. We are simply calculating the percentage of
the ADC output and then applying that percentage on the 5V range of the potentiometer.
• For 1V Input:
𝐿𝑒𝑑 𝑂𝑢𝑡𝑝𝑢𝑡 = 00110011
𝐷𝑒𝑐𝑖𝑚𝑎𝑙 𝑂𝑢𝑡𝑝𝑢𝑡 = 51
𝐶𝑜𝑣𝑒𝑟𝑡𝑖𝑛𝑔, 𝑤𝑒 𝑔𝑒𝑡:
51
∗ 5 = 1𝑉
255
• For 2V Input:
𝐿𝑒𝑑 𝑂𝑢𝑡𝑝𝑢𝑡 = 01100110
𝐷𝑒𝑐𝑖𝑚𝑎𝑙 𝑂𝑢𝑡𝑝𝑢𝑡 = 102
𝐶𝑜𝑣𝑒𝑟𝑡𝑖𝑛𝑔, 𝑤𝑒 𝑔𝑒𝑡:
Sajjad Ali EE-21232

102
∗ 5 = 2𝑉
255
• For 3V Input:
𝐿𝑒𝑑 𝑂𝑢𝑡𝑝𝑢𝑡 = 10011001
𝐷𝑒𝑐𝑖𝑚𝑎𝑙 𝑂𝑢𝑡𝑝𝑢𝑡 = 153
𝐶𝑜𝑣𝑒𝑟𝑡𝑖𝑛𝑔, 𝑤𝑒 𝑔𝑒𝑡:
153
∗ 5 = 3𝑉
255

• For 4V Input:
𝐿𝑒𝑑 𝑂𝑢𝑡𝑝𝑢𝑡 = 11001100
𝐷𝑒𝑐𝑖𝑚𝑎𝑙 𝑂𝑢𝑡𝑝𝑢𝑡 = 204
𝐶𝑜𝑣𝑒𝑟𝑡𝑖𝑛𝑔, 𝑤𝑒 𝑔𝑒𝑡:
204
∗ 5 = 4𝑉
255
• For 5V Input:
𝐿𝑒𝑑 𝑂𝑢𝑡𝑝𝑢𝑡 = 11111111
𝐷𝑒𝑐𝑖𝑚𝑎𝑙 𝑂𝑢𝑡𝑝𝑢𝑡 = 255
𝐶𝑜𝑣𝑒𝑟𝑡𝑖𝑛𝑔, 𝑤𝑒 𝑔𝑒𝑡:
255
∗ 5 = 5𝑉
255
2. Now, remove the LEDs and provide the 8-bit digital output to an external DAC circuit i.e., convert
the digital output to analog for verification. You can use a simple R-2R circuit of Figure 8 or use
DAC0808 IC. For DAC0808 IC, refer to its datasheet for more information.
Sajjad Ali EE-21232

TINKERCAD SIMULATION:
• INPUT = 500mV:

• INPUT = 1.8V:
Sajjad Ali EE-21232

• INPUT = 4.2V:

3. Measure the analog input given through potentiometer and the analog output reproduced
by the DAC. Compare both and verify the ADC and DAC.
• INPUT = 500mV:
Analog Input = 500mV
Analog Output = 508mV
• INPUT = 1.8V:
Analog Input = 1.8V
Analog Output = 1.82V
• INPUT = 4.2V:
Analog Input = 4.2V
Analog Output = 4.16V
Sajjad Ali EE-21232

Task: 02: To control the status of an LED based on the value of input analog
voltage.
Modify the previous task or, example to read 10-bit ADC value of voltage across
potentiometer instead of 8-bit result. Map the obtained 10-bit result with voltage
level using the step-size. Connect an LED to indicate the voltage level. Use some
conditions to build the following logic for controlling the LED status.
- If voltage is above 2.5V, the LED turns ON.
- If voltage is below 2.5V, the LED red turns OFF.

CODE:
#include <avr/io.h>

int main(void) {

DDRD |= 0x04;
DDRC &= 0xFE;
ADCSRA |= 10000111;
ADMUX |= (1 << REFS0);
int digital_output;
float voltage;
while (1) {
ADCSRA |= (1 << ADSC);

while (ADCSRA & (1 << ADSC));

digital_output = ADCL | (ADCH << 8);


voltage = (digital_output / 1023.0) * 5;

if (voltage > 2.5) {


PORTD |= 0x04;
} else {
PORTD &= ~(0x04);
}
}

return 0;
}
Sajjad Ali EE-21232

TINKERCAD SIMULATION:
• INPUT VOLTAGE = 1.3V, LED DOES NOT GLOW

• INPUT VOLTAGE = 3.8V, LED GLOWS

You might also like