This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| en:examples:delay [2009/03/05 10:43] – raivo.sell | en:examples:delay [2010/02/04 12:34] (current) – removed mikk.leini | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== Delays ====== | ||
| - | ===== Simple delay ===== | ||
| - | |||
| - | Check optimization: | ||
| - | and frequency settings in AVRStudio: 14745600 | ||
| - | |||
| - | {{: | ||
| - | |||
| - | <code c> | ||
| - | void delay1s(int count){ // ~0,09s | ||
| - | unsigned int i,j; | ||
| - | for(i=0; | ||
| - | for(j=0; | ||
| - | } | ||
| - | |||
| - | |||
| - | //call | ||
| - | delay1s(10); | ||
| - | |||
| - | </ | ||
| - | |||
| - | ===== Delay with timer ===== | ||
| - | Next example is not influenced by optimization. | ||
| - | |||
| - | <code c> | ||
| - | // Delay in milliseconds | ||
| - | // Delay is created by timer 0 | ||
| - | // Max delay 4500 ms | ||
| - | // Precision 3% | ||
| - | |||
| - | void aeg(unsigned int t) | ||
| - | { | ||
| - | // Limiting max delay time | ||
| - | if (t > 4500) t = 4500; | ||
| - | | ||
| - | // Prescaler 1024 | ||
| - | TCCR1B = _BV(CS12) | _BV(CS10); | ||
| - | |||
| - | // Timer set up | ||
| - | TCNT1 = 0xFFFF - (unsigned int)((unsigned long)t * (unsigned long)144 / (unsigned long)10); | ||
| - | | ||
| - | // Endless loop until timer bit is set | ||
| - | while ((TIFR & _BV(TOV1)) == 0) | ||
| - | { | ||
| - | asm volatile (" | ||
| - | } | ||
| - | | ||
| - | // clearing interrup bit (=1) | ||
| - | TIFR |= _BV(TOV1); | ||
| - | } | ||
| - | </ | ||
| - | |||
| - | ===== Delay using util/ | ||
| - | |||
| - | The problem is max value the delay functions accept. Followin example allows to use much bigger values. | ||
| - | |||
| - | <code c> | ||
| - | void delay_ms(uint16_t ms) // Set's delay duration | ||
| - | { | ||
| - | while (ms) // until the vaule reaches zero | ||
| - | { | ||
| - | _delay_ms(1); | ||
| - | ms--; | ||
| - | } | ||
| - | } | ||
| - | </ | ||