This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
en:examples:timer:delay [2015/11/05 10:19] – heikopikner | en:examples:timer:delay [2020/07/20 09:00] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ~~PB~~ | + | < |
====== Delay ====== | ====== Delay ====== | ||
Line 63: | Line 63: | ||
<code c> | <code c> | ||
- | // Software delay in milliseconds. | + | // Software delay in milliseconds |
void sw_delay_ms(unsigned short count) | void sw_delay_ms(unsigned short count) | ||
{ | { | ||
Line 69: | Line 69: | ||
while (count-- > 0) | while (count-- > 0) | ||
{ | { | ||
- | // 1ms delay with a special function. | + | // 1ms delay with a special function |
_delay_ms(1); | _delay_ms(1); | ||
} | } | ||
Line 78: | Line 78: | ||
<code c> | <code c> | ||
- | // The demonstration program of the software delay of the HomeLab. | + | // The demonstration program of the software delay of the HomeLab |
- | // The program is blinking a LED for a moment after ~1 second. | + | // The program is blinking a LED for a moment after ~1 second |
#include < | #include < | ||
#include < | #include < | ||
Line 86: | Line 86: | ||
int main(void) | int main(void) | ||
{ | { | ||
- | // Setting the pin of the LED as output. | + | // Setting the pin of the LED as output |
pin_setup_output(led_debug); | pin_setup_output(led_debug); | ||
Line 101: | Line 101: | ||
pin_set(led_debug); | pin_set(led_debug); | ||
- | // Software delay for 900 milliseconds. | + | // Software delay for 900 milliseconds |
sw_delay_ms(900); | sw_delay_ms(900); | ||
} | } | ||
Line 115: | Line 115: | ||
<code c> | <code c> | ||
- | // Hardware delay in milliseconds. | + | // Hardware delay in milliseconds |
void hw_delay_ms(unsigned short count) | void hw_delay_ms(unsigned short count) | ||
{ | { | ||
- | // Calculating the initial value of the timer. | + | // Calculating the initial value of the timer |
register unsigned char timer_start = 256 - F_CPU / 1000 / 64; | register unsigned char timer_start = 256 - F_CPU / 1000 / 64; | ||
- | // Starting the timer. | + | // Starting the timer |
timer0_init_normal(TIMER0_PRESCALE_64); | timer0_init_normal(TIMER0_PRESCALE_64); | ||
- | // Counting the variable of the delay to the 0. | + | // Counting the variable of the delay to the 0 |
while (count-- > 0) | while (count-- > 0) | ||
{ | { | ||
- | // Initializing the timer. | + | // Initializing the timer |
timer0_set_value(timer_start); | timer0_set_value(timer_start); | ||
- | // Zeroing the overflow flag. | + | // Zeroing the overflow flag |
timer0_overflow_flag_clear(); | timer0_overflow_flag_clear(); | ||
- | // Waiting for overflow. | + | // Waiting for overflow |
while (!timer0_overflow_flag_is_set()) | while (!timer0_overflow_flag_is_set()) | ||
{ | { | ||
Line 140: | Line 140: | ||
} | } | ||
- | // Zeroing the overflow flag. | + | // Zeroing the overflow flag |
timer0_overflow_flag_clear(); | timer0_overflow_flag_clear(); | ||
- | // Stoping the timer. | + | // Stoping the timer |
timer0_stop(); | timer0_stop(); | ||
} | } | ||
Line 198: | Line 198: | ||
<code c> | <code c> | ||
- | // Demonstration program of hardware delay of the HomeLab. | + | // Demonstration program of hardware delay of the HomeLab |
- | // The Program blinks LED for a moment after every ~1 second. | + | // The Program blinks LED for a moment after every ~1 second |
#include < | #include < | ||
#include < | #include < | ||
- | // Main program. | + | // Main program |
int main(void) | int main(void) | ||
{ | { | ||
- | // Setting the pin of the LED as output. | + | // Setting the pin of the LED as output |
pin_setup_output(led_debug); | pin_setup_output(led_debug); | ||
- | // Endless loop. | + | // Endless loop |
while (true) | while (true) | ||
{ | { | ||
- | // Lighting the LED. | + | // Lighting the LED |
pin_clear(led_debug); | pin_clear(led_debug); | ||
- | // Hardware delay for 100 milliseconds. | + | // Hardware delay for 100 milliseconds |
hw_delay_ms(100); | hw_delay_ms(100); | ||
- | // Switch off of the LED. | + | // Switch off of the LED |
pin_set(led_debug); | pin_set(led_debug); | ||
- | // Hardware delay for 900 milliseconds. | + | // Hardware delay for 900 milliseconds |
hw_delay_ms(900); | hw_delay_ms(900); | ||
} | } |