Advertisement
Guest User

I2C

a guest
Apr 8th, 2011
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. ; TXD P1.0 = CLOCK
  2. ; RXD P1.1 = DATA
  3.  
  4. $NOMOD51 ; importes
  5. #include "reg932.h" ; for
  6. ; at
  7. prog SEGMENT CODE ; undgå
  8. RSEG prog ; fejl
  9. ;
  10. ?C_START: ;
  11. public ?C_START ;
  12.  
  13. jmp 80H ;
  14. org 80H ; hvad gør disse "!!!!"
  15.  
  16. mov divm,#9 ;(120KHz)
  17. mov p1m1,#0 ;
  18. mov p1m2,#0 ; sætter portene op
  19. ; mov p1,#0H ;
  20. ; mov scon, #0 ; SKAL BRUGES DA DER ARBEJDES MED MODE-0
  21.  
  22. call i2cinit
  23.  
  24. //******************************
  25. call startc
  26. mov a,#10010000b
  27. call send
  28. call ack
  29. mov a,#10101100b
  30. call send
  31. call ack
  32. mov a,#00000000b
  33. call send
  34. call ack
  35. call stop
  36. //******************************
  37.  
  38. call DEELAY
  39.  
  40. //******************************
  41. call startc
  42. mov a,#10010000b
  43. call send
  44. call ack
  45. mov a,#10100001b
  46. call send
  47. call ack
  48. mov a,#00111001b
  49. call send
  50. call ack
  51. mov a,#00000000b
  52. call send
  53. call ack
  54. call stop
  55. //******************************
  56.  
  57. call DEELAY
  58.  
  59. //******************************
  60. call startc
  61. mov a,#10010000b
  62. call send
  63. call ack
  64. mov a,#10100010b
  65. call send
  66. call ack
  67. mov a,#00001010b
  68. call send
  69. call ack
  70. mov a,#00000000b
  71. call send
  72. call ack
  73. call stop
  74. //******************************
  75.  
  76. call DEELAY
  77.  
  78. //******************************
  79. call startc
  80. mov a,#10010000b
  81. call send
  82. call ack
  83. mov a,#10101010b
  84. call send
  85. call ack
  86. call stop
  87. //******************************
  88. jmp $
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95. ;***************************************
  96. ;Ports Used for I2C Communication
  97. ;***************************************
  98. PDA equ P1.0
  99. PCL equ P1.1
  100.  
  101.  
  102. ;***************************************
  103. ;Initializing I2C Bus Communication
  104. ;***************************************
  105. i2cinit:
  106. setb PDA
  107. setb PCL
  108. ret
  109.  
  110. ;****************************************
  111. ;ReStart Condition for I2C Communication
  112. ;****************************************
  113. rstart:
  114. clr PCL
  115. setb PDA
  116. setb PCL
  117. clr PDA
  118. ret
  119.  
  120. ;****************************************
  121. ;Start Condition for I2C Communication
  122. ;****************************************
  123. startc:
  124. setb PCL
  125. clr PDA
  126. clr PCL
  127. ret
  128.  
  129. ;*****************************************
  130. ;Stop Condition For I2C Bus
  131. ;*****************************************
  132. stop:
  133. clr PCL
  134. clr PDA
  135. setb PCL
  136. setb PDA
  137. ret
  138.  
  139. ;*****************************************
  140. ;Sending Data to slave on I2C bus
  141. ;*****************************************
  142. send:
  143. mov r7,#08
  144. back:
  145. clr PCL
  146. rlc a
  147. mov PDA,c
  148. setb PCL
  149. djnz r7,back
  150. clr PCL
  151. setb PDA
  152. ret
  153.  
  154. ;*****************************************
  155. ;ACK and NAK for I2C Bus
  156. ;*****************************************
  157. ack:
  158. clr PDA
  159. setb PCL
  160. nop
  161. nop
  162. clr PCL
  163. setb PDA
  164. ret
  165.  
  166. nak:
  167. setb PDA
  168. setb PCL
  169. clr PCL
  170. setb PCL
  171. ret
  172.  
  173. ;*****************************************
  174. ;Receiving Data from slave on I2C bus
  175. ;*****************************************
  176. recv:
  177. mov r7,#08
  178. back2:
  179. clr PCL
  180. setb PCL
  181. mov c,PDA
  182. rlc A
  183. djnz r7,back2
  184. clr PCL
  185. nop
  186. nop
  187. ;setb PDA
  188. ret
  189.  
  190. DEELAY:
  191. mov r4,#50
  192.  
  193. DELL:
  194. djnz r3,DELL
  195. djnz r4,DELL
  196. ret
  197.  
  198.  
  199.  
  200.  
  201. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement