Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
en:examples:delay [2009/04/01 11:01] raivo.sellen:examples:delay [2010/02/04 12:34] (current) – removed mikk.leini
Line 1: Line 1:
-====== Delays ====== 
  
-===== Simple delay ===== 
- 
-Check optimization: -O0 
-and frequency settings in AVRStudio: 14745600 
- 
-{{:et:failid:avrstudio_settings.jpg|}} 
- 
-<code c> 
-void delay1s(int count){ // ~0,09s 
- unsigned int i,j; 
- for(i=0;i<count;i++){ 
- for(j=0;j<65530;j++){asm volatile ("nop");}} 
-} 
- 
- 
-//call 
-delay1s(10); // ~0,9 sec. 
- 
-</code> 
- 
-===== 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 ("nop"); 
-  } 
-   
-  // clearing interrup bit (=1) 
-  TIFR |= _BV(TOV1); 
-} 
-</code> 
- 
-===== Delay using util/delay.h header file ===== 
- 
-The problem is max value the delay functions accept. Max delay: 262.14 ms / F_CPU in MHz. 
-Following example allows us 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); // 1ms delay 
- ms--;  // decreases value 
- } 
-} 
-</code> 
en/examples/delay.1238583676.txt.gz · Last modified: 2020/07/20 09:00 (external edit)
CC Attribution-Share Alike 4.0 International
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0