









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Code for some other exercise in year end 2021 and year 2022
Typology: Exercises
1 / 17
This page cannot be seen from the preview
Don't miss anything!










****** PLEASE DO ALL YOUR LAB EXPERIMENT BY YOURSELF, ANY CHEATING WILL FAIL THE LAB**
Task 1.1: Blink single LED PB4 in Port B with delay in 1000 ms Task 1.2: Modify this code to blink the PB4, PB5, and PB6 with a delay of 500 ms. Another LED in Port B will shut down. Task 1.3: Blink all Port B with delay 200 ms. Task 1.4: Write a program that assigns each of the buttons SW0 to SW7 display via LED0 to LED Task 1.5: Write a program that will increase and decrease a variable when you press the button SW0 counting down when pressing SW7 counting up.
1.1. Solution Task 1.1:
1. Code Task 1.1: Blink single LED PB4 in Port B with delay in 1000 ms
int main ( void ) { //Set the Data Direction Register to PB4 (which in this case is Port B ) DDRB = (1< Task Description In this lab experiment, we need to execute the action blinking the LED that is labeled PB4 in PORTB with the delay time of 1000ms = 1s in each transition between turned ON and OFF. So the task we need to do include:
1.2 Solution Task 1.2:
2. Code Task 1.2: Modify this code to blink the PB4, PB5, and PB6 with a delay of 500 ms. Another LED in Port B will shut down. #include #include int main(void) { //Set the Data Direction Register to PB4, PB5, PB DDRB = (1< Task Description This lab experiment is actually the extended version of task 1.1 because instead of working with only 1 LED, we are now required to work simultaneously with 3 LEDs. The task for this experiment is
1.3 Solution Task 1.3:
3. Code Task 1.3: Blink all Port B with delay 200 ms. #include #include int main(void) { //Set the Data Direction Register to all the PIN in port B DDRB = 0xff; while(1) { PORTB = 0xff; //Set all pins PORTB to HIGH logic level _delay_ms(200); // Delay 0.2s PORTB = 0x00; //Set all pins to LOW logic level _delay_ms(200); // Delay 0.2s } return 1; }
Task Description This lab experiment happens to be mine misunderstood code in task 4.1 where we are required to blink all the LEDs in PORTB. The tasks include
1.4 Solution Task 1.4:
4. Code Task 1.4: Write a program that assigns each of the buttons SW0 to SW7 display via LED0 to LED #include #include void main(void) { // Set the data direction for PORTB - LED and PORTD - BUTTON DDRB = 0xff; // all pin of PORTB as Output PORTB = 0x00; DDRD = 0x00; // all pin of PORTD as INPUT PORTD = 0xff; while(1) { PORTB = PIND; // Set the value of the Swicth - neither ON or OFF - to LED of portB } }
Task Description This ones upgrades the difficulty into 1 level when we are demanded to turned on the LED with the condition that button in PORTD is pressed. The works are
1.5 Solution Task 1.5:
5. Code Task 1.5: Write a program that will increase and decrease a variable. When you press the button SW0 counting down; when pressing SW7 counting up. #ifndef F_CPU #define F_CPU 8000000UL //set clock to 8MHz #endif #include #include // For interrupts #include void main(void) { int count = 0; //Create a variable to store counting result. Start counting from 0 DDRB = 0xFF; // Set port pins B - LED to be output pins to display the counting result DDRD &= ~((1 << DDD7) | (1 << DDD0)) ; // Set switch at PD7 and PD0 as the input pins. while (1) { if (~PIND & (1 << PIND0)) // Check if the switch SW0 - correspond to PD0 - is pushed { count--; // Decrease count by 1 if (0 > count) { count = 0xFF; // If count is out of bound, count is reset to 255. } while (~PIND & (1 << PIND0)); // Restate the while loop again to avoid too long hold push } else if (~PIND & (1 << PIND7)) // Check if the switch SW7 - correspond to PD7 - is pushed { count++; // Increase count by 1 if (count > 0xFF)
**1.5. Demo Pictures Comment Result Video link
Task Description This lab task tells us to decrease and increase the value of the variable by pressing 2 different buttons. The task includes