prjbrook

Tiny85 600baud tx works OK at 8Mhz

Jun 30th, 2014
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.29 KB | None | 0 0
  1. ;serial85_0. Is just the tiny85 version of m328p below. Works for tx. Got halfbittime delay going.
  2. ;Serial01. Got both Tx and Rx routines working out of Arduino
  3. ; pins 8 and 10 resp.Bit holdup due to not noticing PINB was the
  4. ; place to look for inputs, not PORTB. Now going to send and receive longer strings
  5. ; of bytes. 22June14, 15:13.
  6. ;SerialInOut0. Builds on SerialOut below
  7. ;Serial Out0. Send byte in r16 out soft serial TX_PIN in port b
  8. ; out at 600 baud.
  9. ; get a delay of exactly 1/1200 sec for 600 baud softserial
  10. ;initially for Arduino then for Tiny85
  11. ;1/1200 sec is 833.3 uSec
  12. ;use r20,21,22
  13. ;This subroutine works for delay of 833.13uS
  14.  
  15. ;defs, then equ then inc, theb .dseg and eseg then eseg
  16. .def serialByteReg = r16
  17. .def rxByte = r18
  18. .def counterReg = r17
  19.  
  20. .equ tab_size = 64
  21. .equ TX_PIN = 0
  22. .equ RX_PIN = 2 ; Tx,Rx pins are PB0 and PB2 resp
  23.  
  24. .include "tn85def.inc"
  25. ;.include "m328pdef.inc"
  26.  
  27. .DSEG
  28. var1: .BYTE 1 ; reserve 1 byte to var1
  29. table: .BYTE tab_size ; reserve tab_size bytes
  30.  
  31. .ESEG
  32. .DB "PeterB's serial program"
  33. .CSEG
  34. ldi r30,low(var1) ; Load Z register low
  35. ldi r31,high(var1) ; Load Z register high
  36. ld r1,Z ; Load VAR1 into register 1
  37.  
  38.  
  39. ;.DB 1,2,3, 4
  40.  
  41. test:
  42. ldi r16, low(RAMEND)
  43. out SPL, r16
  44. ldi r16,high(RAMEND)
  45. out SPH, r16
  46. ldi r16, 0xf9
  47. out DDRB,r16 ;
  48. nop
  49. ldi r16, $ff
  50. out PORTB,r16
  51.  
  52.  
  53. ; rjmp serialTest1
  54. rjmp serialTest0
  55.  
  56. here1:
  57. rjmp here1
  58. ;----------------------------------------------------------------
  59. halfBitTime: ;better name for this delay. Half of 1/600
  60. ;myDelay1200:
  61. ;ldi r21,13 ; 13 works for m328 at 16Mhz
  62. ldi r21,7 ;try 7 for tiny85 at 8Hmz
  63. ldi r20,130 ;r20,21 at 130,7 give 833uS. Good for 600baud at 8Mhz
  64. start:
  65. inc r20
  66. nop
  67. brne start
  68. dec r21
  69. brne start
  70. ret
  71. ;--------------------------------------------------
  72. oneBitTime:
  73. rcall halfBitTime
  74. rcall halfBitTime
  75. ret
  76. ;-------------------------------------------------
  77. sendAZero:
  78. ;output 0 on Tx pin
  79. cbi PORTB,TX_PIN ; send a zero out PB0
  80. ret
  81. ;-----------------------------------------------------
  82.  
  83. sendAOne:
  84. ;output 1 on Tx pin
  85. sbi PORTB,TX_PIN ; send a zero out PB0
  86. ret
  87. ;-----------------------------------------------------
  88. sendStartBit:
  89. ; send a 0 for one bit time
  90. rcall sendAZero
  91. rcall oneBitTime
  92. ret
  93. ;-------------------------------------------------------
  94. sendNextDataBit: ;main output routine for serial tx
  95. lsr serialByteReg ;push high bit into carry flag then inspect it
  96. ;originally did lsl but found lsb first.
  97. brcc gotzero ;if it's a 0 do nothing
  98. rcall sendAOne ;must have been a 1 in carry
  99. rjmp down
  100. gotzero:
  101. rcall sendAZero ;if here carry was a zero
  102. down:
  103. rcall oneBitTime ;so that 1 or 0 lasts 1/600 sec
  104. ret
  105. ;-------------------------------------------------------------
  106. send8DataBits: ; send all bits in serialByteReg
  107. ldi counterReg,8 ;8 data bits
  108. sendBit:
  109. rcall sendNextDataBit
  110. dec counterReg
  111. brne sendBit
  112. ret
  113. ;--------------------------------------------------------
  114. sendStopBit:
  115. ; send a 1 for one bit time
  116. rcall sendAOne
  117. rcall oneBitTime
  118. ret
  119. ;--------------------------------------------------------
  120. sendSerialByte: ;main routine. Byte in serialByteReg = r16
  121. rcall sendStartBit
  122. rcall send8DataBits
  123. rcall sendStopBit
  124. rcall sendStopBit ;two stops
  125. ret
  126. ;**************************************************************
  127. serialTest0: ;output series of 'AAAA..'s
  128. ldi serialByteReg, 0x41 ;0x41
  129. rcall sendSerialByte
  130. rcall oneBitTime ; take a rest
  131. rjmp serialTest0 ;continue forever
  132. ;---------------------------------------------------------
  133. ;---------Now do SerialRx routines-------------------
  134. waitForHigh: ;loop til RX is high
  135. sbis PINB,RX_PIN ;test that pin for set (PB2)
  136. rjmp waitForHigh ; loop if rx pin is low
  137. ret
  138. ;-----------------------------------------------
  139. waitForLow: ;PRONBLEMs loop til RX is low. FIXED.
  140. sbic PINB,2 ;test that pin for set (PB2)
  141. rjmp waitForLow ; loop if rx pin is high
  142. ret
  143. ;---------------------------------------------------
  144. waitForStartBit: ;loop til get a real start bit
  145. rcall waitForHigh ;should be marking at start
  146. rcall waitForLow ;gone low. might be noise
  147. rcall halfBitTime ;is it still low in middle of bit time
  148. sbic PINB,RX_PIN ;..well, is it?
  149. rjmp waitForStartBit ;loop if level gone back high. Not a start bit.
  150. ret ;we've got our start bit
  151. ;----------------------------------------------------
  152. checkForStopBit: ;at end, get carry flag to reflect level. Prob if c=0
  153. rcall oneBitTime ; go into stop bit frame, halfway
  154. sec ;should stay a 1 in C if stop bit OK
  155. sbis PINB,RX_PIN ;don't clc if bit is high
  156. clc ;but only if we have a weird low stop bit
  157. ret ;with carry flag = stop bit. Should be a 1
  158. ;-------------------------------------------------------------
  159. get8Bits: ;get the 8 data bits. No frame stuff
  160. clr rxbyte ;this will fill up with bits read from RX_PIN
  161. push counterReg ;going to use this so save contents for later
  162. ldi counterReg,8 ;because we're expecting 8 databits
  163. nextBit:
  164. rcall oneBitTime ;first enter here when mid-startbit
  165. rcall rxABit ;get one bit
  166. dec counterReg ;done?
  167. brne nextBit ;no, round again
  168. pop counterReg ;yes, finished, restor counter and get out
  169. ret
  170. ;---------------------------------------------------------------
  171. rxABit: ;big serial input routine for one bit
  172. clc ;assume a 0
  173. sbic PINB,RX_PIN ; skip nxt if pin low
  174. sec ;rx pin was high
  175. ror rxbyte ;carry flag rolls into msb first
  176. ret
  177. ;********************************
  178. getSerialByte: ;big routine. Serial ends up in rxByte
  179. rcall waitForStartBit ;**change
  180. rcall get8Bits
  181. rcall checkForStopBit
  182. ret ;with rxByte containing serial bye
  183. ;----------------------------------------------------
  184. serialTest1: ;output A then reflect input. Worked OK
  185. ldi serialByteReg, 0x36 ;0x41
  186. rcall sendSerialByte
  187. rcall oneBitTime ; take a rest
  188. rcall getSerialByte
  189. mov serialByteReg,rxByte ;output what's been read
  190. rcall sendSerialByte
  191. rjmp serialTest1
  192. ;--------------------------------------------------------
  193. ;----------Now doing buffer work. Want to and fro 64 bytes----------
  194. showBuf:
  195. ldi ZL,low(table) ;table is my buffer
  196. ldi ZH, high(table) ;Z now points to table
  197. ldi counterReg,64 ;64 bytes in buffer
  198. ; finish later
  199. ret
Advertisement
Add Comment
Please, Sign In to add comment