Show Clearly All the steps An AVR microcontroller with two ports B and A is show
ID: 3547736 • Letter: S
Question
Show Clearly All the steps
An AVR microcontroller with two ports B and A is shown below. A 4-bit input is coming from sensors that determine Carbon Dioxide (CO2) level in a room. The shown configuration is part of a bigger system to monitor pollution level in that room. Three LEDs (LED1, LED2 and LED3) are interfaced with pins PA1, PA3 and PA7 as shown. The LEDs will indicate CO2 level in the room as follow: - If the reading is between 0 - 3 (inclusive) all LEDs are OFF - If the reading is between 4 - 8 (inclusive LED1 is ON and the rest are OFF - If the reading between 9 - 12 (inclusive) LED1 & LED2 are ON, LED3 is OFF - Otherwise, LED1, LED2 & LED3 are ON Write the main c code (without pre-processor directives) that continuously checks the sensors and indicates CO2 level. Hint: Logic 1 will turn and LED ON. Clear all unused output pins.Explanation / Answer
#include <avr/io.h>
int main()
{
DDRB = 0x00; // declaring PORTB as input port
DDRA = 0xff; // declaring PORTA as output port
while(1)
{
if((PINB == 0b00000000) || (PINB == 0b00001000) || (PINB == 0b00010000) || (PINB == 0b00011000))
PORTA = 0x00;
else if((PINB == 0b00100000) || (PINB == 0b00101000) || (PINB == 0b00110000) || (PINB == 0b00111000) || (PINB == 0b01000000))
PORTA = 0b00000010; // This will switch on the first led and other will remain in off state
else if((PINB == 0b01001000) || (PINB == 0b01010000) || (PINB == 0b01011000) || (PINB == 0b01100000))
PORTA = 0b00001010; // This will switch on the first and second led and other will remain in off state
else
PORTA = 0b10001010; // All leds will turned on.
}
}