Code for excersise year 2022, Exercises of Programming Languages

Code for some other exercise in year end 2021 and year 2022

Typology: Exercises

2020/2021

Uploaded on 01/13/2022

pham-minh-khoi
pham-minh-khoi 🇻🇳

5

(1)

1 document

1 / 17

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Microcontroller Lab EEIT-2019
Student Lab Report
Name: Phm Minh Khôi Student ID: 15795
Email: [email protected] EEIT Class: EEIT2019 Class
**** PLEASE DO ALL YOUR LAB EXPERIMENT BY YOURSELF, ANY CHEATING WILL FAIL THE LAB
1. Lab Experiment 1. Input-output ports
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 LED7
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
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Partial preview of the text

Download Code for excersise year 2022 and more Exercises Programming Languages in PDF only on Docsity!

Microcontroller Lab EEIT-

Student Lab Report

Name: Phạm Minh Khôi Student ID: 15795

Email: [email protected] EEIT Class: EEIT2019 Class

****** PLEASE DO ALL YOUR LAB EXPERIMENT BY YOURSELF, ANY CHEATING WILL FAIL THE LAB**

1. Lab Experiment 1. Input-output ports

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

include

include

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. Declare the LED PB4 as an output in order to blink
  2. In the running process, we have to a. Declare that port to High Logic Level = Turned on light b. Declare that port to Low Logic Level = Turned off light c. Write delay function for each transition by 1000ms = 1s Difficulty At the very beginning, I have followed the code in the instruction which resulted in a very different result. Instead of declaring the port to work with only LED PB4, the code was made to make all LEDs blink at the same time. So I made 2 videos to demonstrate both cases where all LEDs blink (misunderstood) and only PB4 LED blink with a delay of 1s Conclusion Through this experiment, I have learned how to work with PORT in the ATmega2560 and also the way that the program navigates specific pin in each PORT. Through this project is only conducted through a simulation, the proteus demonstrates quite clear how the code would behave in such a virtual environment.

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. Declare the LED PB4, PB5, PB6 as an output in order to blink
  2. In the running process, we have to d. Declare that those ports to High Logic Level = Turned on light e. Declare that those ports to Low Logic Level = Turned off light f. Write delay function for each transition by 500ms = 0.5s Difficulty Conclusion This experiment extends the understanding of how AVR code syntax navigates multiple ports simultaneously. That action is completed by the use of the vertical bar “ | ” when setting the direction of the data register

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. Declare all LEDs in PORTB as output in order to blink
  2. In the running process, we have to g. Declare that those ports to High Logic Level = Turned on light h. Declare that those ports to Low Logic Level = Turned off light i. Write delay function for each transition by 500ms = 0.2s Difficulty At first, I could not understand clearly why we need to declare all pins to 0xff to set them as output. After some online research, I understood that 0xff is actually the binary for 1111 1111 - which means that all are active ‘1’ state Conclusion Instead of declaring specific port to output, we, through this project, learned what is the syntax for setting the data direction which is ‘0xff’. This syntax helps my code much cleaner and easier to understand than specifying each pins

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. Declare all LEDs in PORTB as output in order to blink
  2. Declare the switches in PORTD as input
  3. In the running process, we have to j. Assign the state value of PORTB pins equal to the PIND Difficulty Conclusion Through this experiment, we learned the logic of how to assign the logic state level to one another

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

  • We notice in the video that when we push too fast, the program can’t instantly proceed with the code
  • We need to hold the button for a while
  • Furthermore, when we switch from 3 to 4 in (binary number system), we can notice that there is a blank push. In other words, we have to push the button 2 times to either increase from 3 to 4 or decrease from 4 to 3
  • This might be because of the transition time delay** https://drive.google.co m/file/d/1kJgLzC1oaT-a JbdPnt51S6y--Ul0u34t/ view?usp=sharing

Task Description This lab task tells us to decrease and increase the value of the variable by pressing 2 different buttons. The task includes

  1. Declare a variable to store the value of increment/decrement
  2. Declare the LED in PORTB as output to display the value - in this case, we represent the value in binary system
  3. Set the switch 1 and 8 to use as input conditions for increase/decrease
  4. In the running program k. Check which switch is pressed i. If switch 1 is pressed => decrease the value ii. If switch 8 is pressed => increase the value iii. Reset the count if out of bound Difficulty At first, I do not notice the condition if the value is increased or decreased over the range 0-255. Then after noticing this special case, I added the condition for the value to be reset whenever it goes over the interval 0-255 (in decimal and 0000 0000 - 1111 1111 in binary) Conclusion Through this experiment, I have learned to perform a more complicated task with LED and button usage. Furthermore, this experiment shows how we can implement the use of external self-defined variable into the program