prjbrook

timingRoutines0.asm Some polling, some interrupt

Oct 6th, 2014
393
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.75 KB | None | 0 0
  1. ;---------------------------------------------------
  2. ;These timing routines worked (mostly) . There's interupt driven ones and polling. Finally
  3. ; they come up with about the same number of ticks for 1sec Arduino sq wave. Need tidying.
  4. ;----------some timer0 routines---------------------------
  5. blinkTimer:
  6. rcall setUp
  7. ;rcall showCounters
  8. rcall waitForPinHigh
  9.  
  10. ;rcall showCounters
  11. rcall waitForPinLow
  12. ;inc r17
  13. ;rcall showCounters
  14. rcall startTim0
  15. rcall chkInp
  16. rcall stopTim0
  17. rcall showCounters
  18. ; rcall waitForever
  19. rjmp blinkTimer
  20. ;--------------------------------------------
  21. setUp:
  22. CBI DDRB,1 ;clr PORTB1 FOR inPUT
  23. clr r17
  24. clr r18
  25. clr r19 ;counters
  26. ;clr r16
  27. out TCNT0,r17 ;always start with clean count
  28. ret
  29. ;----------------------------------------------
  30. startTim0:
  31. LDI r16,0b0000_0101 ;SET TIMER PRESCALER TO /1024, 03 is /64
  32. OUT TCCR0B,r16
  33. ret ;with timer now started
  34. ;-----------------------------------------------
  35. stopTim0:
  36. LDI r16,0b0000_0000 ;Stop TIMER
  37. OUT TCCR0B,r16
  38. ret ;with timer now stopped
  39. ;----------------------------------------------------------
  40. waitForPinHigh:
  41. sbis PINB,1
  42. rjmp waitForPinHigh
  43. ret ;when pin PB1 goes high
  44. ;--------------------------------------------------
  45.  
  46. waitForPinLow:
  47. ; ldi zl,0x36
  48. ; clr zh
  49. ; ld r16,z
  50. ;rcall d16
  51. ; rcall spacecode
  52. sbic PINB,1
  53. rjmp waitForPinLow
  54. ret ;when pin PB1 goes low
  55. ;-------------------------------------
  56. chkInp: ;main loop. Come here after pin gone low
  57. sbic PINB,1 ;loop until pin PB1 goes high
  58. rjmp outci
  59. in r16,TIFR ;TOV0 goes high when TCNT0 overflows
  60. andi r16, 0b0000_0010 ;TOV0
  61. breq chkInp ;mostly take this branch
  62. overflow:
  63. ldi r16,0b0000_0010
  64. out TIFR,r16 ;push TOV0 flag back down by writng 1 to it.
  65. inc r17 ;overflow of TCNT0, therefore, click counters
  66. brne chkInp ;r17 not overflowing so chk pin all over again
  67. inc r18 ;if r17 becomes ff +1 click r18
  68. brne chkInp ;no overflow so start again with loop
  69. inc r19 ;sometimes, might need this for very long delays.
  70. rjmp chkInp ;if r19 overflows, bad luck, do nothing
  71. outci:
  72. ret ;with counters full but need to stop clock soon
  73. ;-----------------------------------------
  74. showCounters: ;after clock has stopped need to see their values
  75. rcall CR
  76. in r16,TCNT0
  77. ;show r16,r17
  78. rcall d1617
  79. rcall space
  80. movw r16,r18
  81. ;show r16,r17
  82. rcall d1617
  83.  
  84. ret ; with TCNT0,r17,18,19 all showing.
  85. ;--------------------------------------------------
  86. waitForever:
  87. nop
  88. rjmp waitForever
  89. ret ;never taken. Jump on spot
  90. ;---------------------------------------------
  91. wdscode: ;list just a few words for testing purposes
  92. push r16
  93. push r17
  94. push r22
  95. push r23
  96. push r24
  97. push r6
  98. pushz
  99.  
  100. ldi r16,$0c ;r6 is counter for words
  101. mov r6,r16 ;stop after 12 words. Best for testing.
  102.  
  103.  
  104. rcall doLatest ;get first link into v
  105. upwrd:
  106. rcall jmpNextWord ;pnt to link part of next word
  107. lpm r23,z+
  108. lpm r22,z+ ;store link into v=r23,24
  109. lpm r16,z+ ;get len
  110. andi r16,$0f ;don't want eg $85 to be len when it means immediate len 5.
  111. clr r17 ;need eg 0006 on stk not 06 later
  112. mypush2 r16,r17 ;len byte now on mystk
  113. ;at this stage z points to the start of word name
  114. mypush2 zl,zh ;flash start adr of string now on mystack
  115. rcall swapp ; but wrong way round. Want len = TOS
  116. rcall Sdot ;print the string on the term
  117. rcall spacecode ;but add space after each word
  118.  
  119. dec r6 ;different from 'words'. Stop after 5
  120. breq outwds
  121.  
  122. tst vl
  123. brne upwrd ;if vl:vh = r23,24 = 0000 finish
  124. tst vh
  125. brne upwrd
  126. outwds:
  127. popz
  128. pop r6
  129. pop r24
  130. pop r23
  131. pop r22
  132. pop r17 ;TODO macro with multiple pops & pushes
  133. pop r16
  134. ret ;with all the words in dic printed
  135. ;-----------------------
  136. test_strout:
  137. rcall strout
  138. .dw $05
  139. .db "abcde"
  140. ret
  141. ;---------------------------------------------
  142. insertreti: ;semireti has to end new word with reti = $9518 opcode
  143. pushx ;both xl,xh saved for later
  144. movw xl,myhere ;myhere points to next available spot in ram dic
  145. ldi r16,$18
  146. st x+,r16 ;$18 part goes first
  147. ldi r16,$95
  148. st x+,r16 ;ret now in ram. Just tidy pointers
  149. movw myhere,xl
  150. popx ;so x back where it was and reti inserted.
  151. ret
  152. ;----------------------------------
  153. interrupt_0: ;experiment for interrupts
  154. ;global interrupt enable
  155. lds r16, $005b ;set PCIE, bit 5 of GMSK
  156. ori r16,0b0010_0000 ; in order to enable pin change ints
  157. sts $005b,r16 ;pin changes now enable
  158. sbi PCMSK,01 ;enable PINB1 for pin change int
  159. ;assume the vector for pin change interrupts is pointing to ISR yhat ..
  160. ; ends with reti. Then, when this is run we should see that routine invoked when pin changes.
  161. sei
  162. ret
  163. herei0:
  164. rjmp herei0
  165. ;----------------------------
  166. testT0_ISR0: ;take out later
  167. inc r18
  168. brne downt0
  169. inc r19
  170. brne downt0
  171. inc r20
  172. ;takemeout 'I'
  173. downt0:
  174. reti
  175. ;------------------------------------
  176. startT0_0: ;just experimenting with getting T0 interrupts
  177. sei ;need global int
  178. lds r16,$0059 ;0x39=TMSK(io), bit 1 controls timer0 overflow int
  179. ori r16,0b000_0010 ;bit 1 =1 => t0 over int enabled
  180. sts $0059, r16
  181.  
  182. rcall interrupt_0 ;set up pinchange interrupt
  183.  
  184. ldi zl,$60
  185. ldi zh,0 ;x points to buf1. Going to store values there
  186.  
  187. CBI DDRB,1 ;clr PORTB1 FOR inPUT
  188. clr r17
  189. clr r18
  190. clr r19 ;counters
  191.  
  192. out TCNT0,r17 ;always start with clean count
  193. ;startTim0:
  194. LDI r16,0b0000_0101 ;SET TIMER PRESCALER TO /1024, 03 is /64
  195. OUT TCCR0B,r16
  196. ;things have started and ISR will kick in every overflow. Plan: watch r18. It should
  197. ; .. climb to 0x20 about every second with 8Mhz clock and 1024 prescale.
  198. ;so if r18 =0x20, do something, like output a char. Reset counters too.
  199. ;takemeout 'A'
  200. chkr18:
  201. tst r6 ;is there a new val
  202. breq chkr18
  203. clr r6 ;if so print it (about once per sec)
  204. ld r16,z
  205. mov r17,r6
  206. ; rcall qmark
  207. ; rcall d1617
  208.  
  209. nop
  210. rjmp chkr18
  211. ret ; never taken
  212. ;------------------------------------------------
  213. pcISR2: ;pin change interrupt comes here for ISR
  214. ldi r16,$01
  215. mov r6,r16 ;a flag. There's a new value.
  216. lds r16,$0052 ;get TCNT0
  217. mov r17,r18 ;save where we got to do TCNT0 display later
  218. clr r18
  219. clr r19
  220. sts $0052,r18 ;clr TCNT0
  221. rcall d1617 ;show count
  222. rcall space
  223.  
  224. reti
  225. ;----------------------------------------------
  226. TOVO_ISR:
  227. ; cli
  228. inc r5
  229. ; cpi r18,$20
  230. ; breq showT
  231. ; ldi r16,0b0000_0010
  232. ; out TIFR,r16 ;push TOV0 flag back down by writng 1 to it.
  233. rjmp finT
  234. ; sei
  235. showT:
  236. ;rcall OK
  237. finT:
  238. reti
  239. ;--------------------------------------
  240. PC_change_ISR:
  241. ; cli
  242. rcall stopTim0
  243. sts $0070,r5 ;save the val of num of TOVOs
  244. in r16,TCNT0
  245. sts $0071,r16
  246. clr r5
  247. sts $0072,r5 ;flag = 0 then there's a pin change
  248. ; sts $0052,r18 ;clr counter0
  249. ; rcall qmark
  250.  
  251. ; mov r17,r18
  252.  
  253. ; in r16,TCNT0
  254. ; mov r16,r18
  255. ; rcall d16
  256. ; rcall d16
  257. ; rcall d1617
  258. ; rcall space
  259. clr r5
  260. out TCNT0,r5
  261. rcall startTim0
  262. ; clr r19
  263. ; inc r19 ;flag
  264. ; sei
  265. reti
  266. ;---------------------------------------------
  267. quickT0: ;trying to get fastest int driven timer
  268. ;rcall qmark
  269. ; lds r16,$0071 ;TCNT0
  270. ; rcall d16
  271. ldi r16,1
  272. sts $0072,r16 ;flag
  273. rcall setupqt
  274. ; in r16,TCNT0
  275. ; rcall d16
  276. lds r16,$0071
  277. rcall d16
  278. ldi r16,0
  279. out TCNT0,r16
  280.  
  281. loopqt:
  282. ; tst r19
  283. lds r16,$0072 ;flag
  284. cpi r16,0
  285. brne loopqt
  286. ; brne loopqt
  287. ; cli
  288.  
  289. lds r16,$0070
  290. rcall d16
  291.  
  292. ;rcall d16
  293. ;rcall d16
  294. ;rcall space
  295. ; rcall oneBitTime
  296. ; rcall qmark
  297. ; clr r19 ;push Pchange int flag back down
  298. ; sei
  299.  
  300. rjmp quickT0
  301. ret
  302. ;----------------------------------------
  303. setupqt:
  304. ; sei ;need global int
  305. lds r16,$0059 ;0x39=TMSK(io), bit 1 controls timer0 overflow int
  306. ori r16,0b000_0010 ;bit 1 =1 => t0 over int enabled
  307. sts $0059, r16
  308.  
  309. rcall interrupt_0 ;set up pinchange interrupt
  310. ret
Advertisement
Add Comment
Please, Sign In to add comment