Advertisement
Guest User

delay.h

a guest
Oct 18th, 2017
577
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.85 KB | None | 0 0
  1. /*
  2.  * delay utilite for STM8 family
  3.  * COSMIC and SDCC
  4.  * Terentiev Oleg
  5.  */
  6.  
  7. #ifndef _UTIL_DELAY_H_
  8. #define _UTIL_DELAY_H_ 1
  9.  
  10. #ifndef F_CPU
  11. #error F_CPU is not defined!
  12. #endif
  13.  
  14.  
  15.  
  16. #define T_COUNT(x) ((((unsigned long long)F_CPU * (unsigned long long)x / 1000000UL))/7)
  17. /*
  18.  * Func delayed N cycles, where N = 3 + ( ticks * 3 )
  19.  * so, ticks = ( N - 3 ) / 3, minimum delay is 6 CLK
  20.  * when tick = 1, because 0 equels 65535
  21.  */
  22.  
  23. /*(( F_CPU * x / 1000000UL )-5)/5)*/
  24.  
  25. void inline _delay_cycl(uint16_t __ticks )
  26. {
  27.     __asm__("nop\n nop\n");
  28.     do
  29.     {
  30.     __ticks--;
  31.   }
  32.   while ( __ticks );
  33.     __asm__("nop\n");
  34.  
  35. }
  36.  
  37. void inline _delay_us(uint16_t __us )
  38. {
  39.     _delay_cycl( (uint16_t) T_COUNT(__us) );
  40. }
  41.  
  42. void inline _delay_ms( volatile uint16_t __ms )
  43. {
  44.     while ( __ms-- )
  45.     {
  46.         _delay_us( 1000 );
  47.     }
  48. }
  49.  
  50. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement