Advertisement
Atdiy

I2C Rev0.01

Aug 19th, 2012
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.13 KB | None | 0 0
  1. OBJ
  2.  
  3. pst : "FullDuplexSerial"
  4. alt : "altimeter_29124"
  5.  
  6. CON
  7.  
  8. _clkmode = xtal1 + pll16x
  9. _clkfreq = 80_000_000
  10.  
  11. SDApin = 0 ' SDA of gyro connected to P0
  12. SCLpin = 1 ' SCL of gyro connected to P1
  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. START_ALT = 200 ' Your starting altitude in feet
  29.  
  30. WRITE_DATA = $3C ' Requests Write operation
  31. READ_DATA = $3D ' Requests Read operation
  32. MODE = $02 ' Mode setting register
  33. OUTPUT_X_MSB = $03 ' X MSB data output register
  34.  
  35. VAR
  36.  
  37. long x
  38. long y
  39. long z
  40. long a
  41. long b
  42. long c
  43.  
  44. long cx
  45. long cy
  46. long cz
  47.  
  48. long ff_x
  49. long ff_y
  50. long ff_z
  51.  
  52. long multiBYTE[3]
  53.  
  54. PUB Main | last_ticks, d
  55.  
  56. pst.start(31, 30, 0, 115200)
  57.  
  58. ' Set modes
  59. Wrt_1B(CTRL_REG3, $08) ' Data ready signal
  60. Wrt_1B(CTRL_REG4, $80) ' Block data update
  61. Wrt_1B(CTRL_REG1, $1F) ' Enable all axes
  62.  
  63. last_ticks := cnt
  64. SetCont
  65. alt.start(alt#QUICKSTART, alt#BACKGROUND)
  66. alt.set_resolution(alt#HIGHEST)
  67. alt.set_altitude(alt.m_from_ft(START_ALT * 100))
  68.  
  69. repeat
  70. pst.tx(1) ' Set Terminal data
  71. WaitForDataReady ' at top of screen
  72. Read_MultiB(OUT_X_INC) ' Read XYZ bytes
  73. SetPointer(OUTPUT_X_MSB)
  74. getRaw ' Gather raw data from compass
  75. pst.tx(13)
  76. pst.tx(13)
  77. d := alt.altitude(alt.average_press)
  78. pst.str(string("Altitude:"))
  79. pst.str(alt.formatn(d, alt#METERS | alt#CECR, 8))
  80. pst.str(alt.formatn(d, alt#TO_FEET | alt#CECR, 17))
  81. ' Divide by 114 to reduce noise
  82. x := (x - cx) / 114
  83. y := (y - cy) / 114
  84. z := (z - cz) / 114
  85. pst.tx(13)
  86. RawXYZ
  87. WaitCnt(ClkFreq / 4 + Cnt) ' Delay before next loop
  88.  
  89. PUB RawXYZ 'gyroscope
  90.  
  91. pst.str(string("RAW X ",11))
  92. pst.dec(x)
  93. pst.str(string(13, "RAW Y ",11))
  94. pst.dec(y)
  95. pst.str(string(13, "RAW Z ",11))
  96. pst.dec(z)
  97.  
  98. PUB GetRaw 'compass
  99.  
  100. start
  101. send(READ_DATA)
  102. a := ((receive(true) << 8) | receive(true))
  103. b := ((receive(true) << 8) | receive(true))
  104. c := ((receive(true) << 8) | receive(false))
  105. stop
  106. ~~a
  107. ~~c
  108. ~~b
  109. a := a
  110. c := c
  111. b := b
  112. pst.str(string("X="))
  113. pst.dec(a)
  114. pst.str(string(", Y="))
  115. pst.dec(b)
  116. pst.str(string(", Z="))
  117. pst.dec(c)
  118. pst.str(string(" "))
  119.  
  120. PUB SetCont
  121. ' Sets compass to continuous output mode
  122.  
  123. start
  124. send(WRITE_DATA)
  125. send(MODE)
  126. send($00)
  127. stop
  128.  
  129. PUB SetPointer(Register)
  130. ' Start pointer at user specified register (OUT_X_MSB)
  131.  
  132. start
  133. send(WRITE_DATA)
  134. send(Register)
  135. stop
  136. PUB WaitForDataReady | status
  137. repeat
  138. status := Read_1B(STATUS_REG)
  139. if (status & $08) == $08
  140. quit
  141.  
  142. PUB Wrt_1B(SUB1, data)
  143. ''Write single byte to Gyroscope.
  144.  
  145. start
  146. send(WRITE)
  147. send(SUB1)
  148. send(data)
  149. stop
  150.  
  151. PUB Read_1B(SUB3) | rxd
  152. ''Read single byte from Gyroscope
  153.  
  154. start
  155. send(WRITE)
  156. send(SUB3)
  157. stop
  158.  
  159. start
  160. send(READ)
  161. rxd := receive(false)
  162. stop
  163.  
  164. result := rxd
  165.  
  166. PUB Read_MultiB(SUB3)
  167. ''Read multiple bytes from Gyroscope
  168.  
  169. start
  170. send(WRITE)
  171. send(SUB3)
  172. stop
  173.  
  174. start
  175. send(READ)
  176. multiBYTE[x_idx] := (receive(true)) | (receive(true)) << 8
  177. multiBYTE[y_idx] := (receive(true)) | (receive(true)) << 8
  178. multiBYTE[z_idx] := (receive(true)) | (receive(false)) << 8
  179. stop
  180.  
  181. x := ~~multiBYTE[x_idx]
  182. y := ~~multiBYTE[y_idx]
  183. z := ~~multiBYTE[z_idx]
  184.  
  185. PRI send(value)
  186.  
  187. value := ((!value) >< 8)
  188.  
  189. repeat 8
  190. dira[SDApin] := value
  191. dira[SCLpin] := false
  192. dira[SCLpin] := true
  193. value >>= 1
  194.  
  195. dira[SDApin] := false
  196. dira[SCLpin] := false
  197. result := not(ina[SDApin])
  198. dira[SCLpin] := true
  199. dira[SDApin] := true
  200.  
  201. PRI receive(acknowledge)
  202.  
  203. dira[SDApin] := false
  204.  
  205. repeat 8
  206. result <<= 1
  207. dira[SCLpin] := false
  208. result |= ina[SDApin]
  209. dira[SCLpin] := true
  210.  
  211. dira[SDApin] := (acknowledge)
  212. dira[SCLpin] := false
  213. dira[SCLpin] := true
  214. dira[SDApin] := true
  215.  
  216. PRI start
  217.  
  218. outa[SDApin] := false
  219. outa[SCLpin] := false
  220. dira[SDApin] := true
  221. dira[SCLpin] := true
  222.  
  223. PRI stop
  224.  
  225. dira[SCLpin] := false
  226. dira[SDApin] := false
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement