Advertisement
Atdiy

Wireless Compass Rev 0.01

Jul 24th, 2012
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.24 KB | None | 0 0
  1. CON
  2. _clkmode = xtal1 + pll16x
  3. _xinfreq = 5_000_000
  4.  
  5. XB_Rx = 0 ' XBee DOUT
  6. XB_Tx = 1 ' XBee DIN
  7. XB_Baud = 9600
  8. CR = 13 ' Carriage Return value
  9. datapin = 4 'SDA
  10. clockPin = 5 'SCL
  11.  
  12. WRITE_DATA = $3C 'Used to perform a Write operation
  13. READ_DATA = $3D 'Used to perform a Read operation
  14.  
  15. CNFG_A = $00 'Read/Write Register, Sets Data Output Rate. Default = 15Hz & 8 samples per measurement
  16. '160Hz can be achieved by monitoring DRDY pin in single measurement mode.
  17. CNFG_B = $01 'Read/Write Register, Sets the Device Gain(230-1370 Gauss). Default = 1090 Gauss
  18. MODE = $02 'Read/Write Register, Selects the operating mode. Default = Single measurement
  19. 'Send $3C $02 $00 on power up to change to continuous measurement mode.
  20. OUTPUT_X_MSB = $03 'Read Register, Output of X MSB 8-bit value. (Will read -4096 if math overflow during bias measurement)
  21. OUTPUT_X_LSB = $04 'Read Register, Output of X LSB 8-bit value. (Will read -4096 if math overflow during bias measurement)
  22. OUTPUT_Z_MSB = $05 'Read Register, Output of Z MSB 8-bit value. (Will read -4096 if math overflow during bias measurement)
  23. OUTPUT_Z_LSB = $06 'Read Register, Output of Z LSB 8-bit value. (Will read -4096 if math overflow during bias measurement)
  24. OUTPUT_Y_MSB = $07 'Read Register, Output of Y MSB 8-bit value. (Will read -4096 if math overflow during bias measurement)
  25. OUTPUT_Y_LSB = $08 'Read Register, Output of Y LSB 8-bit value. (Will read -4096 if math overflow during bias measurement)
  26. STATUS = $09 'Read Register, indicates device status.
  27. ID_A = $0A 'Read Register, (ASCII value H)
  28. ID_B = $0B 'Read Register, (ASCII value 4)
  29. ID_C = $0C 'Read Register, (ASCII value 3)
  30.  
  31. OBJ
  32. XB : "FullDuplexSerial"
  33. math : "SL32_INTEngine_2"
  34.  
  35. VAR
  36. long CompassStack[1000]
  37. long GotoAngleStack[100]
  38. long Angle
  39. long x
  40. long y
  41. long z
  42.  
  43. byte NE
  44. byte SE
  45. byte SW
  46. byte NW
  47.  
  48. Pub Start | range
  49. XB.start(XB_Rx, XB_Tx, 0, XB_Baud) ' Initialize comms for XBee
  50. cognew(Compass, @CompassStack)
  51.  
  52. Repeat
  53. XB.str(string("Heading in Degrees:", 32))
  54. XB.dec(Angle)
  55. XB.tx(13)
  56.  
  57. PUB Compass
  58.  
  59. waitcnt(clkfreq/100_000 + cnt) 'Wait while compass has time to startup.
  60.  
  61. setcont 'sets measurements to go continuously
  62.  
  63. repeat 'Repeat indefinitely
  64. setpointer(OUTPUT_X_MSB) 'Start with Register OUT_X_MSB
  65. getRaw 'Gather raw data from compass
  66. Angle := aziadjust
  67. waitcnt(clkfreq/80 + cnt)
  68.  
  69. PUB aziadjust : value
  70.  
  71. {{ Converts the Azimuth to Degrees from 0 - 360. }}
  72.  
  73. NW~
  74. NE~
  75. SE~
  76. SW~
  77.  
  78. if x =< 0
  79. if azimuth =< 0
  80. value := AZ_A
  81. NW := 1
  82. else
  83. NW~
  84.  
  85. if azimuth > 0
  86. value := AZ_D
  87. SW := 1
  88. else
  89. SW~
  90.  
  91. if x > 0
  92. if azimuth =< 0
  93. value := AZ_B
  94. NE := 1
  95. else
  96. NE~
  97.  
  98.  
  99. if azimuth > 0
  100. value := AZ_C
  101. SE := 1
  102. else
  103. SE~
  104.  
  105. value := 1 #> value <# 360
  106.  
  107. value := (value + 270) // 360
  108.  
  109. PUB SetCont
  110.  
  111. {{ Sets the Compass to Continuous output mode.}}
  112.  
  113. start
  114. send(WRITE_DATA)
  115. send(MODE)
  116. send($00)
  117. stop
  118.  
  119. PUB SetPointer(Register)
  120.  
  121. {{ Start pointer at user specified Register. }}
  122.  
  123. start
  124. send(WRITE_DATA)
  125. send(Register)
  126. stop
  127.  
  128. PUB GetRaw
  129.  
  130. {{ Get raw data from continous output.}}
  131.  
  132. start
  133. send(READ_DATA)
  134. x := ((receive(true) << 8) | receive(true)) 'RegisterA and RegisterB
  135. z := ((receive(true) << 8) | receive(true))
  136. y := ((receive(true) << 8) | receive(false))
  137. stop
  138. ~~x
  139. ~~z
  140. ~~y
  141. x := x
  142. z := z
  143. y := y
  144.  
  145. PRI Azimuth
  146.  
  147. 'Azimuth = arcTan(y/x)
  148.  
  149. result := math.arctan(y,x)
  150.  
  151. PRI AZ_A 'NE
  152.  
  153. result := ||azimuth
  154.  
  155. PRI AZ_B 'SE
  156.  
  157. result := azimuth + 180
  158.  
  159. PRI AZ_C 'SW
  160.  
  161. result := azimuth + 180
  162.  
  163. PRI AZ_D 'NW
  164.  
  165. result := -azimuth + 360
  166.  
  167. PRI send(value) ' I²C Send data - 4 Stack Longs
  168.  
  169. value := ((!value) >< 8)
  170.  
  171. repeat 8
  172. dira[dataPin] := value
  173. dira[clockPin] := false
  174. dira[clockPin] := true
  175. value >>= 1
  176.  
  177. dira[dataPin] := false
  178. dira[clockPin] := false
  179. result := !(ina[dataPin])
  180. dira[clockPin] := true
  181. dira[dataPin] := true
  182.  
  183. PRI receive(aknowledge) ' I²C receive data - 4 Stack Longs
  184.  
  185. dira[dataPin] := false
  186.  
  187. repeat 8
  188. result <<= 1
  189. dira[clockPin] := false
  190. result |= ina[dataPin]
  191. dira[clockPin] := true
  192.  
  193. dira[dataPin] := aknowledge
  194. dira[clockPin] := false
  195. dira[clockPin] := true
  196. dira[dataPin] := true
  197.  
  198. PRI start ' 3 Stack Longs
  199.  
  200. outa[dataPin] := false
  201. outa[clockPin] := false
  202. dira[dataPin] := true
  203. dira[clockPin] := true
  204.  
  205. PRI stop ' 3 Stack Longs
  206.  
  207. dira[clockPin] := false
  208. dira[dataPin] := false
  209.  
  210. DAT
  211.  
  212. Azm byte "Azimuth = ",0
  213. XRaw byte "X = ",0
  214. YRaw byte "Y = ",0
  215. ZRaw byte "Z = ",0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement