0% found this document useful (0 votes)
9 views

AVR GPIO Programming With C

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

AVR GPIO Programming With C

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Post Lab Tasks

TASK 1: Explain what makes the Blinky.c blink?


The code we tested in Lab 01 is given below. Explain what makes this Blinky.c blink?
#include <avr/io.h>

#define BLINK_DELAY_MS 1000 // Define a delay time of 1000 milliseconds (1 second)

#include <util/delay.h>

int main (void)

DDRB |= 0B100000; // Set the 5th pin of Port B (PB5) as an output

while(1) {

PORTB |= 0B100000; // Set PB5 high (turn the LED on)

_delay_ms(BLINK_DELAY_MS); // Wait for the defined delay time

PORTB &= ~0B100000; // Set PB5 low (turn the LED off)

_delay_ms(BLINK_DELAY_MS); // Wait for the defined delay time

TASK 2: 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.
#include <avr/io.h>

int main (void) {

DDRB &= ~(1 << PB1); //Configure PB1 as input for the door sensor

DDRC |= (1 << PC5); //Configure PC5 as output for the LED

while (1) {

if (PINB & (1 << PB1)) { // Check if the door sensor (PB1) is open

PORTC |= (1 << PC5); // If the sensor is open, turn on the LED (set PC5 high)

else {

PORTC &= ~(1 << PC5); // If the sensor is closed, turn off the LED (set PC5 low)
Post Lab Tasks

return 0;

TASK 3: 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.
#include <avr/io.h>

int main (void) {

while (1) {

if (PINB & (1 << PB7)) { // Check if bit 7 of Port B (PB7) is 1

DDRB &= ~(1 << PB4); // If PB7 is 1, make bit 4 of Port B (PB4) an input

} else {

DDRB |= (1 << PB4); // If PB7 is 0, make bit 4 of Port B (PB4) an output

return 0;

TASK 4: 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.
#include <avr/io.h>

#include <util/delay.h>

int main (void) {

DDRD |= 0x0F; // Configure PD0 to PD3 as output for the first 4 LEDs

DDRD |= 0xF0; // Configure PD4 to PD7 as output for the remaining 4 LEDs

DDRB &= ~(1 << PB0); // Configure PB0 as input for the switch

while (1) {

if (PINB & (1 << PB0)) { // Check if the switch (PB0) is high


Post Lab Tasks
PORTD |= 0x0F; // If the switch is high, turn on the first 4 LEDs (PD0 to PD3)

} else {

PORTD &= ~0x0F; // If the switch is low, turn off the first 4 LEDs (PD0 to PD3)

PORTD ^= 0xF0; // Toggle the remaining 4 LEDs (PD4 to PD7)

_delay_ms(500); // Add a delay to make the toggling visible

return 0;

TASK 5: 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


0b 00 0b 000
0b 01 0b 011
0b 10 0b 101
0b 11 0b 111

#include <avr/io.h>
int main (void) {
DDRB &= ~((1 << PB0) | (1 << PB1)); // Configure PB0 and PB1 as input
DDRD |= (1 << PD0) | (1 << PD1) | (1 << PD2); // Configure PD0, PD1, and PD2 as output
while (1) {
// Read the status of PB0 and PB1
uint8_t input = PINB & 0x03; // Mask to get only the first 2 bits
switch (input) { // Update LEDs on PD0, PD1, and PD2 based on input
case 0b00:
PORTD &= ~((1 << PD0) | (1 << PD1) | (1 << PD2)); // Set PD[2:0] to 000
break;
case 0b01:
PORTD = (PORTD & ~((1 << PD0) | (1 << PD1) | (1 << PD2))) | (1 << PD0) | (1 <<
PD1); // Set PD[2:0] to 011
break;
Post Lab Tasks
case 0b10:
PORTD = (PORTD & ~((1 << PD0) | (1 << PD1) | (1 << PD2))) | (1 << PD2) | (1 << PD0);
// Set PD[2:0] to 101
break;
case 0b11:
PORTD |= (1 << PD0) | (1 << PD1) | (1 << PD2); // Set PD[2:0] to 111
break;
}
}
return 0;
}

You might also like