prjbrook

Tiny85. Serial buffer work. Goes

Jul 3rd, 2014
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.38 KB | None | 0 0
  1. ;Have buffer in out routines going. Save this then On to flash programming.
  2. ;Serial85_2. Now starting get a string from terminal at 600 baud ending with CR
  3. ; and get string with len in front. Both strings go into 'table'.
  4. ; Now doing showBuf stuff. And fillBuff
  5. ;Buffers in and out of serial streams
  6. ;serial85_1. Going to try to get both tx and rx going using software loops for delays.
  7. ; appears to be working, both tx and rx'
  8.  
  9. ;serial85_0. Is just the tiny85 version of m328p below.Works for tn85 tx
  10. ;Serial01. Got both Tx and Rx routines working out of Arduino
  11. ; pins 8 and 10 resp.Bit holdup due to not noticing PINB was the
  12. ; place to look for inputs, not PORTB. Now going to send and receive longer strings
  13. ; of bytes. 22June14, 15:13.
  14. ;SerialInOut0. Builds on SerialOut below
  15. ;Serial Out0. Send byte in r16 out soft serial TX_PIN in port b
  16. ; out at 600 baud.
  17. ; get a delay of exactly 1/1200 sec for 600 baud softserial
  18. ;initially for Arduino then for Tiny85
  19. ;1/1200 sec is 833.3 uSec
  20. ;use r20,21,22
  21. ;This subroutine works for delay of 833.13uS
  22.  
  23. ;defs, then equ then inc, theb .dseg and eseg then eseg
  24. .def serialByteReg = r16
  25. .def rxByte = r18
  26. .def counterReg = r17
  27.  
  28. .set testing = 1 ;comment out if not testing
  29.  
  30. .equ tab_size = 64
  31. .equ TX_PIN = 0
  32. .equ RX_PIN = 2 ; Tx,Rx pins are PB0 and PB2 resp
  33.  
  34. .include "tn85def.inc"
  35. ;.include "m328pdef.inc"
  36.  
  37. .DSEG
  38. var1: .BYTE 1 ; reserve 1 byte to var1
  39. table: .BYTE tab_size ; reserve tab_size bytes
  40.  
  41. .ESEG
  42. .DB "PeterB's serial program"
  43. .CSEG
  44. ldi r30,low(var1) ; Load Z register low
  45. ldi r31,high(var1) ; Load Z register high
  46. ld r1,Z ; Load VAR1 into register 1
  47.  
  48.  
  49. ;.DB 1,2,3, 4
  50.  
  51. test:
  52. ldi r16, low(RAMEND)
  53. out SPL, r16
  54. ldi r16,high(RAMEND)
  55. out SPH, r16
  56. ldi r16, 0xf9
  57. out DDRB,r16 ;
  58. nop
  59. ldi r16, $ff
  60. out PORTB,r16
  61.  
  62. ;===============================================================
  63. ;rjmp test2_rxStrEndCR
  64. rcall test_rsStrWithLen
  65. ;rjmp waitForDDump
  66. ;rjmp test_dumpTable
  67. ;rjmp waitForCharD
  68. ; rjmp serialTest1
  69. ; rjmp serialTest0
  70. ; rjmp fillBuf
  71. ;rjmp test_serialStrOut
  72. here1:
  73. rjmp here1
  74. ;----------------------------------------------------------------
  75. halfBitTime: ;better name for this delay. Half of 1/600
  76. ;myDelay1200:
  77. ;ldi r21,13 ; 13 works for m328 at 16Mhz
  78. ldi r21,7 ;try 7 for tiny85 at 8Hmz
  79. ldi r20,130 ;r20,21 at 130,7 give 833uS. Good for 600baud at 8Mhz
  80. start:
  81. inc r20
  82. nop
  83. brne start
  84. dec r21
  85. brne start
  86. ret
  87. ;--------------------------------------------------
  88. oneBitTime:
  89. rcall halfBitTime
  90. rcall halfBitTime
  91. ret
  92. ;-------------------------------------------------
  93. sendAZero:
  94. ;output 0 on Tx pin
  95. cbi PORTB,TX_PIN ; send a zero out PB0
  96. ret
  97. ;-----------------------------------------------------
  98.  
  99. sendAOne:
  100. ;output 1 on Tx pin
  101. sbi PORTB,TX_PIN ; send a zero out PB0
  102. ret
  103. ;-----------------------------------------------------
  104. sendStartBit:
  105. ; send a 0 for one bit time
  106. rcall sendAZero
  107. rcall oneBitTime
  108. ret
  109. ;-------------------------------------------------------
  110. sendNextDataBit: ;main output routine for serial tx
  111. lsr serialByteReg ;push high bit into carry flag then inspect it
  112. ;originally did lsl but found lsb first.
  113. brcc gotzero ;if it's a 0 do nothing
  114. rcall sendAOne ;must have been a 1 in carry
  115. rjmp down
  116. gotzero:
  117. rcall sendAZero ;if here carry was a zero
  118. down:
  119. rcall oneBitTime ;so that 1 or 0 lasts 1/600 sec
  120. ret
  121. ;-------------------------------------------------------------
  122. send8DataBits: ; send all bits in serialByteReg
  123. ldi counterReg,8 ;8 data bits
  124. sendBit:
  125. rcall sendNextDataBit
  126. dec counterReg
  127. brne sendBit
  128. ret
  129. ;--------------------------------------------------------
  130. sendStopBit:
  131. ; send a 1 for one bit time
  132. rcall sendAOne
  133. rcall oneBitTime
  134. ret
  135. ;--------------------------------------------------------
  136. sendSerialByte: ;main routine. Byte in serialByteReg = r16
  137. push counterReg
  138. rcall sendStartBit
  139. rcall send8DataBits
  140. rcall sendStopBit
  141. rcall sendStopBit ;two stops
  142. pop counterReg
  143. ret
  144. ;**************************************************************
  145. serialTest0: ;output series of 'AAAA..'s
  146. ldi serialByteReg, 0x42 ;0x41
  147. rcall sendSerialByte
  148. rcall oneBitTime ; take a rest
  149. rjmp serialTest0 ;continue forever
  150. ;---------------------------------------------------------
  151. ;---------Now do SerialRx routines-------------------
  152. waitForHigh: ;loop til RX is high
  153. sbis PINB,RX_PIN ;test that pin for set (PB2)
  154. rjmp waitForHigh ; loop if rx pin is low
  155. ret
  156. ;-----------------------------------------------
  157. waitForLow: ;PRONBLEMs loop til RX is low. FIXED.
  158. sbic PINB,2 ;test that pin for set (PB2)
  159. rjmp waitForLow ; loop if rx pin is high
  160. ret
  161. ;---------------------------------------------------
  162. waitForStartBit: ;loop til get a real start bit
  163. rcall waitForHigh ;should be marking at start
  164. rcall waitForLow ;gone low. might be noise
  165. rcall halfBitTime ;is it still low in middle of bit time
  166. sbic PINB,RX_PIN ;..well, is it?
  167. rjmp waitForStartBit ;loop if level gone back high. Not a start bit.
  168. ret ;we've got our start bit
  169. ;----------------------------------------------------
  170. checkForStopBit: ;at end, get carry flag to reflect level. Prob if c=0
  171. rcall oneBitTime ; go into stop bit frame, halfway
  172. sec ;should stay a 1 in C if stop bit OK
  173. sbis PINB,RX_PIN ;don't clc if bit is high
  174. clc ;but only if we have a weird low stop bit
  175. ret ;with carry flag = stop bit. Should be a 1
  176. ;-------------------------------------------------------------
  177. get8Bits: ;get the 8 data bits. No frame stuff
  178. clr rxbyte ;this will fill up with bits read from RX_PIN
  179. push counterReg ;going to use this so save contents for later
  180. ldi counterReg,8 ;because we're expecting 8 databits
  181. nextBit:
  182. rcall oneBitTime ;first enter here when mid-startbit
  183. rcall rxABit ;get one bit
  184. dec counterReg ;done?
  185. brne nextBit ;no, round again
  186. pop counterReg ;yes, finished, restor counter and get out
  187. ret
  188. ;---------------------------------------------------------------
  189. rxABit: ;big serial input routine for one bit
  190. clc ;assume a 0
  191. sbic PINB,RX_PIN ; skip nxt if pin low
  192. sec ;rx pin was high
  193. ror rxbyte ;carry flag rolls into msb first
  194. ret
  195. ;********************************
  196. getSerialByte: ;big routine. Serial ends up in rxByte
  197. push counterReg
  198. rcall waitForStartBit ;**change
  199. rcall get8Bits
  200. rcall checkForStopBit
  201. pop counterReg
  202. ret ;with rxByte containing serial bye
  203. ;----------------------------------------------------
  204. serialTest1: ;output A then reflect input. Worked OK
  205. ldi serialByteReg, 0x36 ;0x41
  206. rcall sendSerialByte
  207. rcall oneBitTime ; take a rest
  208. rcall getSerialByte
  209. mov serialByteReg,rxByte ;output what's been read
  210. rcall sendSerialByte
  211. rjmp serialTest1
  212. ;--------------------------------------------------------
  213. ;----------Now doing buffer work. Want to and from 64 bytes----------
  214. fillBuf:
  215. ldi ZL,low(table) ;table is my buffer
  216. ldi ZH, high(table) ;Z now points to table
  217. ldi counterReg,64 ;64 bytes in buffer
  218. ldi r16,$30
  219. storeB0:
  220. st z+,r16
  221. inc r16
  222. dec counterReg
  223. brne storeB0
  224. here:
  225. ; rjmp here
  226. ret
  227. ;----------------------------------------------------------
  228. serialStrOut: ;X points to start of string,r17 has length
  229. ld serialByteReg, x+
  230.  
  231. rcall sendSerialByte
  232. dec r17 ;got to end of string?
  233. brne serialStrOut
  234. ret
  235. ;----------------------------------
  236. test_serialStrOut:
  237. rcall fillBuf
  238. ldi XL,low(table) ;table start of str
  239. ldi XH, high(table)
  240. ldi r17,64 ;going to send len=r17 bytes
  241. rcall serialStrOut
  242. here2:
  243. rjmp here2
  244. ;--------------------------------------
  245. waitForCharD: ;wait til eg a 'D' is pressed then do something.
  246. ldi serialByteReg, '>' ;0x41
  247. rcall sendSerialByte
  248. rcall oneBitTime ; take a rest
  249. rcall getSerialByte
  250. mov serialByteReg,rxByte ;output what's been read
  251. cpi rxByte, 'D'
  252. brne waitForCharD
  253. ldi serialByteReg, '*'
  254. rcall sendSerialByte
  255. rjmp waitForCharD
  256. ;-----------------------------------------------------------
  257. dumpTable:
  258. ldi XL,low(table) ;table start of str
  259. ldi XH, high(table)
  260. ldi r17,64 ;going to send len=r17 bytes
  261. rcall serialStrOut
  262. ret
  263. ;-------------------------------------------------------------
  264. test_dumpTable:
  265. rcall fillBuf
  266. rcall getSerialByte ;any one will do.
  267. rcall dumpTable
  268. rjmp test_dumpTable
  269. ;----------------------------------------------------------
  270. waitForDDump: ;wait til eg a 'D' is pressed then dump table
  271. ldi serialByteReg, '>' ;0x41
  272. rcall sendSerialByte
  273. rcall oneBitTime ; take a rest
  274. rcall getSerialByte
  275. mov serialByteReg,rxByte ;output what's been read
  276. cpi rxByte, 'D'
  277. brne waitForDDump
  278. rcall dumpTable
  279. rjmp waitForCharD
  280. ;---------------------------------------------------------------
  281. rxStrEndCR: ;get a serial string that ends with CR
  282. clr counterReg
  283. ldi XL,low(table) ;table is where str will go
  284. ldi XH, high(table)
  285. upsec:
  286. rcall getSerialByte
  287. cpi rxByte,$0d ;is it CR = end of string?
  288. breq fin
  289. st x+, rxByte ;char goes into buffer="Table"
  290. inc counterReg ;don't go over 64 bytes
  291. cpi counterReg,64
  292. brne upsec ;not too long and not CR so keep going
  293. fin:
  294. ret
  295. ;---------------------------------------------
  296. test_rxStrEndCR: ;just a test of above
  297. rcall rxStrEndCR
  298. rcall waitForDDump
  299. rjmp test_rxStrEndCR
  300. ;------------------------------------------------------
  301. test2_rxStrEndCR: ;want a diagnostic dump if testing. Works with .IFDEF
  302. rcall rxStrEndCR
  303. .IFDEF testing
  304. rcall dumpTable
  305. .ENDIF
  306. rjmp test2_rxStrEndCR
  307. ;------------------------------------------------------------
  308. rxStrWithLen: ;expect len char char char.. for len chars
  309. push counterReg
  310. ldi XL,low(table) ;table is where str will go
  311. ldi XH, high(table)
  312. rcall getSerialByte ; get length bye Must be less than 65
  313. mov counterReg, rxByte ;save len in counter
  314. cpi counterReg,65 ;
  315. brlo allOK ;less than 65 so carry on. Branch if Lower
  316. ldi counterReg,64 ; if len>64 then len=64. Buffer = table only 64 bytes
  317. allOK:
  318. tst counterReg ;zero yet?
  319. breq finrs
  320. rcall getSerialByte ;next serial input byte
  321. st x+, rxByte ;put into buffer
  322. dec counterReg ;have we done len=counterReg bytes?
  323. rjmp allOK
  324. finrs:
  325. pop counterReg
  326. ret
  327. ;---------------------------------------------------------------
  328. test_rsStrWithLen: ;works ok with macro $05GHIJKLM. Sends GHIJK
  329. ldi r16, '#'
  330. rcall sendSerialByte
  331. rcall rxStrWithLen
  332. rcall dumpTable
  333. rjmp test_rsStrWithLen
Advertisement
Add Comment
Please, Sign In to add comment