delay.asm 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #ifndef F_CPU
  2. #error "F_CPU must be defined!"
  3. #endif
  4. #if F_CPU < 4000000
  5. #warning "F_CPU too low, possible wrong delay"
  6. #endif
  7. #define CYCLES_PER_US (F_CPU/1000000)
  8. #define C4PUS (CYCLES_PER_US/4)
  9. #define DVUS(x) (C4PUS*x)
  10. .macro DELAY_uS
  11. ldi XH, HIGH(DVUS(@0))
  12. ldi XL, LOW(DVUS(@0))
  13. rcall Wait4xCycles
  14. .endm
  15. .macro DELAY_mS
  16. ldi YL,low(@0)
  17. ldi YH,high(@0)
  18. rcall WaitMiliseconds
  19. .endm
  20. ;------------------------------------------------------------------------------
  21. ; Input : XH:XL - number of CPU cycles to wait (divided by four)
  22. ;------------------------------------------------------------------------------
  23. Wait4xCycles:
  24. sbiw XH:XL, 1
  25. brne Wait4xCycles
  26. ret
  27. ;------------------------------------------------------------------------------
  28. ; Input : temp - number of miliseconds to wait
  29. ;------------------------------------------------------------------------------
  30. ;WaitMiliseconds:
  31. ; push temp
  32. ;WaitMsLoop:
  33. ; ldi XH,HIGH(DVUS(500))
  34. ; ldi XL,LOW(DVUS(500))
  35. ; rcall Wait4xCycles
  36. ; ldi XH,HIGH(DVUS(500))
  37. ; ldi XL,LOW(DVUS(500))
  38. ; rcall Wait4xCycles
  39. ; dec temp
  40. ; brne WaitMsLoop
  41. ; pop temp
  42. ; ret
  43. ;------------------------------------------------------------------------------
  44. ; Input : YH:YL - number of miliseconds to wait, использует Temp
  45. ; таймер каждую милисекунду отнимает 1 от Y, пока тот не достигнет 0
  46. ;------------------------------------------------------------------------------
  47. WaitMiliSeconds:
  48. ldi temp,1<<SE
  49. out MCUCR,temp ; IDLE SLEEP
  50. WMS_SLL:
  51. sleep ; спим.
  52. tst YL
  53. brne WMS_SLL ; спим до сл. прерывания
  54. tst YH
  55. brne WMS_SLL ; спим до сл. прерывания
  56. clr temp
  57. out MCUCR,temp ; со сна
  58. ret