Advertisement
Guest User

Wireless Gyro Rev 0.01

a guest
Aug 3rd, 2012
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. CON
  2.  
  3. _clkmode = xtal1 + pll16x
  4. _clkfreq = 80_000_000
  5.  
  6. SDApin = 0 ' SDA of gyro connected to P0
  7. SCLpin = 1 ' SCL of gyro connected to P1
  8. XB_Rx = 6 ' XBee DOUT
  9. XB_Tx = 7 ' XBee DIN
  10.  
  11. XB_Baud = 9600
  12. CR = 13 ' Carriage Return value
  13. WRITE = $D2 ' Request Write operation
  14. READ = $D3 ' Request Read operation
  15.  
  16. ' Control registers
  17. CTRL_REG1 = $20
  18. CTRL_REG2 = $21
  19. CTRL_REG3 = $22
  20. CTRL_REG4 = $23
  21. STATUS_REG = $27
  22. OUT_X_INC = $A8
  23.  
  24. x_idx = 0
  25. y_idx = 1
  26. z_idx = 2
  27.  
  28. VAR
  29.  
  30. long x
  31. long y
  32. long z
  33.  
  34. long cx
  35. long cy
  36. long cz
  37.  
  38. long ff_x
  39. long ff_y
  40. long ff_z
  41.  
  42. long multiBYTE[3]
  43. OBJ
  44.  
  45. XB : "FullDuplexSerial"
  46.  
  47. PUB Go | last_ticks
  48. XB.start(XB_Rx, XB_Tx, 0, XB_Baud) ' Initialize comms for XBee
  49.  
  50. ' Set modes
  51. Wrt_1B(CTRL_REG3, $08) ' Data ready signal
  52. Wrt_1B(CTRL_REG4, $80) ' Block data update
  53. Wrt_1B(CTRL_REG1, $1F) ' Enable all axes
  54.  
  55. last_ticks := cnt
  56.  
  57. repeat
  58. XB.tx(1) ' Set Terminal data
  59. WaitForDataReady ' at top of screen
  60. Read_MultiB(OUT_X_INC) ' Read XYZ bytes
  61.  
  62. ' Divide by 114 to reduce noise
  63. x := (x - cx) / 114
  64. y := (y - cy) / 114
  65. z := (z - cz) / 114
  66.  
  67. RawXYZ
  68. WaitCnt(ClkFreq / 4 + Cnt) ' Delay before next loop
  69.  
  70. PUB RawXYZ
  71. 'Display Raw X,Y,Z data
  72.  
  73. XB.str(string("RAW X ",11))
  74. XB.dec(x)
  75.  
  76. XB.str(string(13, "RAW Y ",11))
  77. XB.dec(y)
  78.  
  79. XB.str(string(13, "RAW Z ",11))
  80. XB.dec(z)
  81. XB.tx(13)
  82. XB.tx(13)
  83.  
  84. '' Below here routines to support I2C interfacing
  85.  
  86. PUB WaitForDataReady | status
  87. repeat
  88. status := Read_1B(STATUS_REG)
  89. if (status & $08) == $08
  90. quit
  91.  
  92. PUB Wrt_1B(SUB1, data)
  93. ''Write single byte to Gyroscope.
  94.  
  95. start
  96. send(WRITE)
  97. send(SUB1)
  98. send(data)
  99. stop
  100.  
  101. PUB Read_1B(SUB3) | rxd
  102. ''Read single byte from Gyroscope
  103.  
  104. start
  105. send(WRITE)
  106. send(SUB3)
  107. stop
  108.  
  109. start
  110. send(READ)
  111. rxd := receive(false)
  112. stop
  113.  
  114. result := rxd
  115.  
  116. PUB Read_MultiB(SUB3)
  117. ''Read multiple bytes from Gyroscope
  118.  
  119. start
  120. send(WRITE)
  121. send(SUB3)
  122. stop
  123.  
  124. start
  125. send(READ)
  126. multiBYTE[x_idx] := (receive(true)) | (receive(true)) << 8
  127. multiBYTE[y_idx] := (receive(true)) | (receive(true)) << 8
  128. multiBYTE[z_idx] := (receive(true)) | (receive(false)) << 8
  129. stop
  130.  
  131. x := ~~multiBYTE[x_idx]
  132. y := ~~multiBYTE[y_idx]
  133. z := ~~multiBYTE[z_idx]
  134.  
  135. PRI send(value)
  136.  
  137. value := ((!value) >< 8)
  138.  
  139. repeat 8
  140. dira[SDApin] := value
  141. dira[SCLpin] := false
  142. dira[SCLpin] := true
  143. value >>= 1
  144.  
  145. dira[SDApin] := false
  146. dira[SCLpin] := false
  147. result := not(ina[SDApin])
  148. dira[SCLpin] := true
  149. dira[SDApin] := true
  150.  
  151. PRI receive(aknowledge)
  152.  
  153. dira[SDApin] := false
  154.  
  155. repeat 8
  156. result <<= 1
  157. dira[SCLpin] := false
  158. result |= ina[SDApin]
  159. dira[SCLpin] := true
  160.  
  161. dira[SDApin] := (aknowledge)
  162. dira[SCLpin] := false
  163. dira[SCLpin] := true
  164. dira[SDApin] := true
  165.  
  166. PRI start
  167.  
  168. outa[SDApin] := false
  169. outa[SCLpin] := false
  170. dira[SDApin] := true
  171. dira[SCLpin] := true
  172.  
  173. PRI stop
  174.  
  175. dira[SCLpin] := false
  176. dira[SDApin] := false
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement