prjbrook

usi2h.asm Rx and Tx both seem to work at 600baud

Oct 17th, 2014
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.89 KB | None | 0 0
  1. .include "tn85def.inc" ;usi2h: Want to tidy up usiTx into one call. Done
  2. ;fixed some problems. The Rx was interfeering with the next tx so I've made the Tx pin (PB1)
  3. ; an input unless it's actually just about to transmit then it's an output
  4. again:
  5. ldi r16, low(RAMEND)
  6. out SPL, r16
  7. ldi r16,high(RAMEND)
  8. out SPH, r16
  9. ;rjmp split62
  10. ;ldi r17,17
  11. ;ldi r18,18
  12. ;ldi r16,$a6
  13. ;rcall split62
  14. ;nop
  15. ;rcall reverseBits
  16. ;ag1:
  17. ;rjmp ag1
  18. ldi r16,$ff
  19. out DDRB,r16
  20. out PORTB,r16
  21. ldi r19,(1<<USIWM0)|(0<<USICS0) ;need this otherwise msb not initially joined to D0
  22. out USICR,r19
  23. ;rjmp test_usiTx
  24. rjmp test_usiRx0
  25.  
  26. ldi r16,$a7 ;!!<--- this works. Comes out as $a6, after reversing and splitiing, on terminal at about 1Hz
  27. rcall reverseBits
  28. rcall split62
  29. rcall SPITransfer_Fast2
  30. mov r16,r17
  31. rcall SPITransfer_Fast2
  32. rjmp here
  33.  
  34.  
  35.  
  36. ldi r16,$a5
  37. rcall SPITransfer_Fast2
  38. ldi r16,$bf
  39. rcall SPITransfer_Fast2
  40.  
  41. ;rcall SPITransfer
  42. here:
  43. rcall oneSec
  44. rcall oneSec
  45. rcall oneSec
  46. rjmp again
  47.  
  48.  
  49.  
  50. ;-----------------------------------
  51. SPITransfer_Fast2:
  52. out USIDR,r16
  53. ;fin2:
  54. ;rjmp fin2
  55. ;ldi r16,(1<<USIWM0)|(0<<USICS0)|(1<<USITC)
  56. ldi r19,(1<<USIWM0)|(0<<USICS0)|(1<<USITC)|(1<<USICLK)
  57. ldi r18,8
  58. upt2:
  59. ;out USICR,r16 ; MSB
  60. rcall oneBitTime
  61. ;rcall oneSec
  62. ;rjmp upt2
  63. out USICR,r19
  64. up3:
  65. ;rjmp up3
  66. dec r18
  67. brne upt2
  68.  
  69. ret
  70. ;---------------------------------------
  71.  
  72. halfBitTime: ;better name for this delay. Half of 1/600
  73. ;myDelay1200:
  74. ;ldi r21,13 ; 13 works for m328 at 16Mhz
  75. push r20
  76. push r21
  77. ldi r21,7 ;try 7 for tiny85 at 8Hmz
  78. ldi r20,130 ;r20,21 at 130,7 give 833uS. Good for 600baud at 8Mhz
  79. starthbt:
  80. inc r20
  81. nop
  82. brne starthbt
  83. dec r21
  84. brne starthbt
  85. pop r21
  86. pop r20
  87. ret
  88. ;--------------------------------------------------
  89. oneBitTime:
  90. rcall halfBitTime
  91. rcall halfBitTime
  92. ret
  93. ;---------------------------------
  94. delay100ms: ;handy; delay for about 0.1 sec = 100 ms
  95. ;header endif_1,10,"delay100ms"
  96. ;delay100ms:
  97. ;.ifdef testing
  98. ; ldi r16,1
  99. ;.else
  100. push r16
  101. ldi r16,60
  102. .;endif
  103. upd100:
  104. rcall oneBitTime
  105. dec r16
  106. brne upd100
  107. pop r16
  108. ret ;after about a tenth of a second
  109. ;------------------------
  110. oneSec:
  111. ;jmp finsec ;take out when not simulating
  112. push r17
  113. ldi r17,5
  114. upones:
  115. rcall delay100ms
  116. dec r17
  117. brne upones
  118. pop r17
  119. finsec:
  120. ret
  121. ;-----------------------------------
  122. reverseBits: ;r16 gets reversed
  123. push r17
  124. push r18
  125. ldi r18,8
  126. ;ldi r16,$a6
  127. ldi r17,0
  128. uprb:
  129.  
  130. lsl r16
  131. ror r17
  132. dec r18
  133. brne uprb
  134. uprb2:
  135. mov r16,r17
  136. pop r18
  137. pop r17
  138. ret
  139. ;-----------------------
  140. split62: ;split r16 into two bytes, r16 and r17 where r16 contains first 6 bits preceded by
  141. ; by 10, the last stop bit then start bit. Last two bits go into r17 followed by 6 1's. ie 6 stop bits.
  142. ;ldi r16,$f0. Wrecks r16,17
  143. ldi r17,$ff
  144. clc
  145. ror r16
  146. ror r17
  147. sec
  148. ror r16
  149. ror r17
  150. ret
  151. rjmp split62
  152. ;-----------------------------------
  153. usiTx: ;this is going to be THE tx routine for usi tx. Assume byte to be transferred is in r16
  154. ldi r17,$fe ;make r1 an output as this stage. Can interfere with Rx
  155. out DDRB,r17
  156.  
  157. rcall reverseBits ;needed
  158. rcall split62 ;now have (10 + 6lsbs) + (2 msbs + 6Stops) in r116,r17
  159. rcall SPITransfer_Fast2 ;there's the r16 gone
  160. mov r16,r17
  161. rcall SPITransfer_Fast2 ;and the r17.
  162. ret ;with r16 having been sent via USI Tx
  163. ;-------------------------------------------
  164. test_usiTx: ;works
  165. ldi r16,$61
  166. rcall usiTx
  167. ldi r16, $62
  168. rcall usiTx
  169. rcall oneSec
  170. rjmp test_usiTx
  171. ;--------------------------------------------------------
  172.  
  173.  
  174. usiRx0: ;try this for usi rx
  175. ;make PB0 = D0 an input
  176. ldi r16,$fc
  177. out DDRB,r16
  178. ldi r16,$ff
  179. out PORTB,r16
  180. out USIDR,r16 ;fill up usidr with only stop bits. Gruadually push them out by inpit bits
  181. ;ldi r16,(1<<USIWM0)|(0<<USICS0)|(1<<USITC)
  182.  
  183. ldi r17,(1<<USIWM0)|(0<<USICS0)|(1<<USITC)|(1<<USICLK)
  184.  
  185. rcall waitForPin0High
  186. rcall waitForPin0Low
  187. rcall halfBitTime ;go to middle of start bit then..
  188. rcall oneBitTime ;go to middle of first real bit
  189. ldi r18,8
  190. ;out USICR,r16 ; MSB
  191. get1bit:
  192. out USICR,r17
  193. rcall oneBitTime
  194. dec r18
  195. brne get1bit
  196. rjmp fin
  197.  
  198. out USICR,r16
  199. out USICR,r17
  200. out USICR,r16
  201. out USICR,r17
  202. out USICR,r16
  203. out USICR,r17
  204. out USICR,r16
  205. out USICR,r17
  206. out USICR,r16
  207. out USICR,r17
  208. out USICR,r16
  209. out USICR,r17
  210. out USICR,r16 ; LSB
  211. out USICR,r17
  212. fin:
  213. in r16,USIDR
  214. rcall reverseBits ;needed
  215.  
  216. ret
  217. ;---------------------
  218. test_usiRx0:
  219. rcall usiRx0
  220. ; rcall oneSec
  221. ; ldi r17,$fe ;make r1 an output as this stage
  222. ; out DDRB,r17
  223. rcall usiTx ;print what we found coming in
  224. ; rcall oneSec
  225. rjmp test_usiRx0
  226. ;--------------------------------
  227. waitForPin0Low:
  228. ; ldi zl,0x36
  229. ; clr zh
  230. ; ld r16,z
  231. ;rcall d16
  232. ; rcall spacecode
  233. sbic PINB,0
  234. rjmp waitForPin0Low
  235. ret ;when pin PB1 goes low
  236. ;------------------------
  237.  
  238. waitForPin0High:
  239. sbis PINB,0
  240. rjmp waitForPin0High
  241. ret ;when pin PB1 goes high
Advertisement
Add Comment
Please, Sign In to add comment