How delay function in Arduino IDE work.
The delay function in the Arduino IDE is a simple function that stops the program execution for a specified amount of time. The delay function takes one argument, which is the time to wait in milliseconds. For example, if you call delay(1000), the program execution will be stopped for 1000 milliseconds, or 1 second. The implementation of the delay function in the Arduino IDE is based on the use of timer interrupts. When you call the delay function, the Arduino library sets up a timer interrupt to occur after the specified number of milliseconds. The timer interrupt is generated by a hardware timer peripheral within the microcontroller, and it periodically increments a counter value. When the counter reaches the specified value, the timer interrupt is triggered and the delay function ends. While the delay function is running, the microcontroller is effectively idle, waiting for the timer interrupt to occur. This means that other tasks or functions in the program cannot run while the delay is active. If you need to perform other tasks while waiting for a delay, you can use other methods, such as the millis function or the delayMicroseconds function, which provides a finer resolution for shorter delays. In general, the delay function is a simple and easy-to-use function for adding pauses or delays to your Arduino program. However, it is important to keep in mind that the delay function stops the program execution and blocks other tasks from running, so you should use it carefully and avoid using long delays whenever possible
0-9 up counter design using a microcontroller (Arduino UNO)
To upload the code from Atmel Studio to the Arduino board follow the steps given below; Open Atmel Studio click in the menu "tools/external tools" we will see a dialog box asking for some parameters.
- In title: "Send to Arduino UNO"
- In command: "C:\Program Files(x86)\Arduino\hardware\tools\avr\bin\avrdude.exe" (this may vary according to the installation directory of the Arduino IDE. Check for avrdude.exe directory in your install.)
- In arguments: "-C "C:\Program Files (x86)\Arduino\hardware\tools\avr\etc\avrdude.conf" -p atmega328p -c arduino -P COM4 -b 115200 -U flash:w:"$(ProjectDir)Debug\$(TargetName).hex":i" (this can vary according to the installation directory of the Arduino IDE. You need to replace the COM4 with the actual COM port your board is using as we see in the device administrator.)
- Check "Use output window" box. Click OK
1. #define F_CPU 8000000UL2. #define BTN (PINB&0b00000010)3. #include <avr/io.h>4. #include <util/delay.h>5. #include <avr/sfr_defs.h>6. void decode(unsigned char count){7. //this is how decode for the 7 segment just lit the corresponding LEDs tothe8. //number9. if(count ==1 ){ PORTD = 0B00001100; }10. else if(count == 2){ PORTD = 0B00110111; }11. else if(count == 3){ PORTD = 0B00011111; }12. else if(count == 4){ PORTD = 0B01001101; }13. else if(count == 5){ PORTD = 0B01011011; }14. else if(count == 6){ PORTD = 0B01111011; }15. else if(count == 7){ PORTD = 0B00001110; }16. else if(count == 8){ PORTD = 0B01111111; }17. else if(count == 9){ PORTD = 0B01011111; }18. else { PORTD = 0B01111110; }19.}20.int main(void)21.{22. //port D is connected to 7 segment23. DDRD = 0B11111111;24. //PORTD= 0B11111111;25. //port B is connected to push button26. DDRB = 0B00000000;27. //PORTB= 0x0F;28. PORTD = 0B01111110;29. signed char x = 0;30. while (1)31. {32.33. if(BTN == 0)34. {35. x++; //increment counter36. if(x == 10){37. x=0; //to set counter to zero overflow on 1038. }39. decode(x); //decoding the number on 7 segment _delay_ms(800);40. _delay_ms(800);41. }42.43.44. }45.}
Here is a step-by-step explanation of the code:
Define F_CPU and BTN:
- The first line, #define F_CPU 8000000UL, sets the CPU clock frequency to 8 MHz. This is used to set the delay times in the code accurately.
- The second line, #define BTN (PINB & 0b00000010), sets a constant BTN that represents the state of the push button. The push button is connected to pin B1, and the value of BTN is 0 if the button is pressed and non-zero otherwise.
Include headers:
- The next three lines include the required headers for the AVR microcontroller.
provides definitions for the I/O ports and their registers. provides a delay function, _delay_ms, which takes a delay time in milliseconds. provides definitions for special function registers.
The decode function takes an argument count which is the count to be displayed on the 7-segment LED. The function uses a series of if statements to set the state of the I/O pins on PORTD, which are connected to the 7-segment LED. The state of each pin corresponds to a specific segment on the LED. For example, if count is 1, then PORTD is set to 0B00001100 to turn on the segments necessary to display the digit 1.
Main function:
The main function is the entry point of the program. First, the direction of the I/O pins on PORTD are set to output by writing 0B11111111 to DDRD. The direction of the I/O pins on PINB are set to input by writing 0B00000000 to DDRB. Next, the initial value of the 7-segment LED is set to the digit 0 by writing 0B01111110 to PORTD. A variable x is then initialized to 0, which will keep track of the count. The program then enters an infinite loop that checks the state of the push button by evaluating BTN. If the button is pressed, x is incremented, and if x is equal to 10, it is set back to 0 to implement the overflow behavior. The current count is then passed to the decode function to update the display. The program then waits for 800 milliseconds using _delay_ms before checking the button again.This code implements a simple counter that increments and displays the count on a 7-segment LED display when a push button is pressed. The code uses bit manipulation and delay functions to control the I/O pins and display the count
0 Comments