Guest User

delay.h

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