Lab 02

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Shayan Sheikh EE-21246

EMBEDDED SYSTEMS – EE-354


LAB: 02 - TASKS
Task: 01: Explain what makes the Blinky.c blink?
The code we tested in Lab 01 is given below. Explain what makes this Blinky.c blink?
//Blinky.c from Lab01
#include <avr/io.h>
#define BLINK_DELAY_MS 1000
#include <util/delay.h>
int main (void)
{
DDRB |= 0B100000;
while(1) {
PORTB |= 0B100000;
_delay_ms(BLINK_DELAY_MS);
PORTB &= ~ 0B100000;
_delay_ms(BLINK_DELAY_MS);
}
}

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;

// Pin 5 of Port C is the output


DDRC |= 0b00100000;

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

• Door Opened (Switch is On):

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)

• Bit 1 of Port B – Low, Bit 4 of Port B – High (Bit 1 is the Output/Led


Does Not Glow)

5
Shayan Sheikh EE-21246

• Bit 1 of Port B – Low, Bit 4 of Port B – High (Bit 1 is the Output/Led


Does Not Glow)

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

PORTD &= 0x0F;


_delay_ms(1000);
}
else
{
PORTD |= 0xF0;
PORTD &= 0xF0;
_delay_ms(1000);
PORTD &= 0x00;
_delay_ms(1000);
}
}
return 0;
}

Simulation:
• Input Switch – Low (First Four Leds Do Not Glow)

7
Shayan Sheikh EE-21246

• Input Switch – High (First Four Leds Glow)

Task: 05: To control the output based on combination of 2 input pins.


Write an AVR C program to read pins 0 and 1 of Port B and update the LEDs at pin 0, 1 & 2 of
Port D according to the following logic. You can use switch-case structure.
Input Port B [1:0] Status Output Port D [2:0] Status
0b00 0b000
0b01 0b011
0b10 0b101
0b11 0b111

Arduino Code:
#include <avr/io.h>

int main() {
// Configure Port B as input (pins 0 and 1)
DDRB &= ~(1 << DDB0);
DDRB &= ~(1 << DDB1);

// Configure Port D (pins 0, 1, and 2) as output for LEDs

8
Shayan Sheikh EE-21246

DDRD |= (1 << DDD0);


DDRD |= (1 << DDD1);
DDRD |= (1 << DDD2);

while (1) {
// Read the state of pins 0 and 1 from Port B
uint8_t input = PINB & (1 << PB0 | 1 << PB1);

// Use a switch-case statement to control LEDs based on the


input value
switch (input) {
case 0b00000000:
PORTD = 0b00000000;
break;

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

• Input – 0b01, Output – 0b011

10
Shayan Sheikh EE-21246

• Input – 0b10, Output – 0b101

• Input – 0b11, Output – 0b111

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

You might also like