Professional Documents
Culture Documents
Lab 02
Lab 02
Lab 02
Answer:
In Blinky.c, a LED connected to pin 5 (bit 5) of Port B is made to blink with a delay of 1000
microseconds (1 second) by first turning on the LED using “PORTB |= 0B100000;” which
essentially ORs the 5th Pin of Port B and then after 1 second it turns off the LED using
“PORTB &= ~ 0B100000;” which first inverts the whole term and then makes the 5th pin of
Port B zero and then delay is added so that the LED takes some time to turn back on again
otherwise the LED would turn off for a very short period of time which might not be visible to
the human eye.
Task: 02: To check and indicate the status of a sensor using the specified ports
and bits of ATmega328P.
A door sensor (here, assume the switch) is connected to pin 1 of Port B, and an LED is connected
to pin 5 of Port C. Write an AVR C program to monitor the door sensor and, when it opens, turn
on the LED.
Arduino Code:
#include <avr/io.h>
int main(void)
1
Shayan Sheikh EE-21246
{
// First bit of Port B is the input
DDRB &= 0b11111101;
while (1)
{
if (PINB & 0b00000010)
{
// Door is open, turn on the LED
PORTC |= 0b00100000;
}
else
{
// Door is closed, turn off the LED
PORTC &= ~(0b00100000);
}
}
return 0;
}
Simulation:
• Door Closed (Switch is Off):
2
Shayan Sheikh EE-21246
Task: 03: To use the general I/O pins of ATmega328P as input or output pin
based on the given condition.
Write an AVR C program to monitor bit 7 of Port B. If it is 1, make bit 4 of Port B input;
otherwise, change pin 4 of Port B to output.
Note:
Bit 1 of Port B is used instead of bit 7 of Port B.
Arduino Code:
#include <avr/io.h>
int main(void)
{
DDRB &= ~((1 << PB4) | (1 << PB1));
DDRC |= (1 << PC5);
while (1)
{
uint8_t bit1 = PINB & (1 << PB1);
if (bit1)
{
uint8_t bit2 = PINB & (1 << PB4);
3
Shayan Sheikh EE-21246
if (bit2)
{
PORTC |= (1 << 5);
}
else
{
PORTC &= (0 << 5);
}
}
else
{
DDRB |= (1 << PB4);
}
}
return 0;
}
Simulation:
• Bit 1 of Port B – High, Bit 4 of Port B – Low (Led Glows)
4
Shayan Sheikh EE-21246
• Bit 1 of Port B – High, Bit 4 of Port B – Low (Led Does Not Glow)
5
Shayan Sheikh EE-21246
Task: 04: To control the specified pins of a given port without disturbing the
rest of the pins.
Write an AVR C program to control a set of 8 LEDs connected to port D such that the first 4
LED glow when input from a switch is high, and remain off when the input from switch is low.
The remaining 4 LED toggle continuously without disturbing the rest of the pins of port D.
Arduino Code:
#include <avr/io.h>
#include <util/delay.h>
int main(void)
{
DDRD |= 0xFF;
DDRB &= 0x02;
while (1)
{
if (PINB & 0x02)
{
PORTD |= 0xFF;
_delay_ms(1000);
6
Shayan Sheikh EE-21246
Simulation:
• Input Switch – Low (First Four Leds Do Not Glow)
7
Shayan Sheikh EE-21246
Arduino Code:
#include <avr/io.h>
int main() {
// Configure Port B as input (pins 0 and 1)
DDRB &= ~(1 << DDB0);
DDRB &= ~(1 << DDB1);
8
Shayan Sheikh EE-21246
while (1) {
// Read the state of pins 0 and 1 from Port B
uint8_t input = PINB & (1 << PB0 | 1 << PB1);
case 0b00000001:
PORTD = 0b00000011;
break;
case 0b00000010:
PORTD = 0b00000101;
break;
case 0b00000011:
PORTD = 0b00000111;
break;
}
}
return 0;
}
9
Shayan Sheikh EE-21246
Simulation:
• Input – 0b00, Output – 0b000
10
Shayan Sheikh EE-21246
Links:
• https://www.tinkercad.com/things/lyeE8HqWCKt-
lab2task2?sharecode=jSVPdcaopxpCKIQW7kbsOzI7tLvA_-ketL5NYxh_fvw (Task-2
Simulation)
11
Shayan Sheikh EE-21246
• https://www.tinkercad.com/things/dQHrlJGUJlR-
lab2task3?sharecode=zjmGbq64QEbphz-GPF7oTJffc_Zy3bdWlHcVWgMWhSk (Task-3
Simulation)
• https://www.tinkercad.com/things/drZBowqhOxa-
lab2task4?sharecode=stqj2NkY9WD2FQVDZ9lQ1hKHjBr73jUBAdI5bsqnpfY (Task-4
Simulation)
• https://www.tinkercad.com/things/aUkBV5kFLyq-
lab2task5?sharecode=0NPqNIg4XluOfZEk2kn4pPyiODf0-0oeifOLKWe02Pk (Task-5
Simulation)
12