Guest User

Untitled

a guest
Jun 24th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. $MODDE2
  2.  
  3. org 0000H
  4. ljmp myprogram
  5.  
  6. Load32 MAC
  7. mov %0 + 0, #low(%1 % 10000H)
  8. mov %0 + 1, #high(%1 % 10000H)
  9. mov %0 + 2, #low(%1 / 10000H)
  10. mov %0 + 3, #high(%1 / 10000H)
  11. ENDMAC
  12.  
  13. SCLK EQU P1.1
  14. MOSI EQU P1.2
  15. MISO EQU P1.3
  16. CE_ADC EQU P1.4
  17.  
  18.  
  19.  
  20. cseg
  21. Name1:
  22. DB 'T: Celsius'
  23. DB 0
  24.  
  25. Name2:
  26. DB ' '
  27. DB 0
  28.  
  29. Wait40us:
  30. mov R5, #149
  31. X1:
  32. djnz R5, X1 ; 3 machine cycles-> 3*90ns*149=40us
  33. ret
  34.  
  35. INIT_SPI:
  36. orl P1MOD, #00010110b ; Set SCLK, MOSI, CE_ADC as outputs
  37. anl P1MOD, #11110111b ; Set MISO as input
  38. clr SCLK ; Mode 0,0 default
  39. ret
  40.  
  41. Delay:
  42. mov R3, #20
  43. Delay_loop:
  44. djnz R3, Delay_loop
  45. ret
  46.  
  47. DO_SPI_G:
  48. push acc
  49. mov R1, #0 ; Received byte stored in R1
  50. mov R2, #8 ; Loop counter (8-bits)
  51.  
  52. DO_SPI_G_LOOP:
  53. mov a, R0 ; Byte to write is in R0
  54. rlc a ; Carry flag has bit to write
  55. mov R0, a
  56. mov MOSI, c
  57. setb SCLK ; Transmit
  58. mov c, MISO ; Read received bit
  59. mov a, R1 ; Save received bit in R1
  60. rlc a
  61. mov R1, a
  62. clr SCLK
  63. djnz R2, DO_SPI_G_LOOP
  64. pop acc
  65. ret
  66.  
  67.  
  68. LCD_command:
  69. mov LCD_DATA, A
  70. clr LCD_RS
  71. setb LCD_EN
  72. clr LCD_EN
  73. ljmp Wait40us
  74. ret
  75.  
  76. LCD_put:
  77. mov LCD_DATA, A
  78. setb LCD_RS
  79. setb LCD_EN
  80. clr LCD_EN
  81. ljmp Wait40us
  82. ret
  83.  
  84. LCDON:
  85. setb LCD_ON
  86. lcall Wait40us
  87. mov LCD_MOD, #0xff ; Use LCD_DATA as output port
  88. clr LCD_RW ; Only writing to the LCD in this code.
  89. mov a, #0CH ; Display on command
  90. lcall LCD_command
  91. mov a, #38H ; 8-bits interface, 2 lines, 5x7 characters
  92. lcall LCD_command
  93. mov a, #01H ; Clear screen (Warning, very slow command!)
  94. lcall LCD_command
  95. ; Delay loop needed for 'clear screen'
  96. ; command above (1.6ms at least!)
  97. mov R1, #40
  98.  
  99. clr_loop:
  100. lcall Wait40us
  101. djnz R1, Clr_loop
  102. ret
  103.  
  104.  
  105. send_msg:
  106. clr a
  107. movc a, @a+dptr
  108. jz second
  109. inc dptr
  110. lcall LCD_put
  111. sjmp send_msg
  112. ret
  113.  
  114. send_msg2:
  115. clr a
  116. movc a, @a+dptr
  117. jz forever
  118. inc dptr
  119. lcall LCD_put
  120. sjmp send_msg2
  121. ret
  122.  
  123. myprogram:
  124. mov SP, #7FH
  125. mov LEDRA, #0 ; Turn off all LEDs
  126. mov LEDRB, #0
  127. mov LEDRC, #0
  128. mov LEDG, #0
  129. lcall INIT_SPI
  130. lcall LCDON
  131.  
  132. mov a,#80H
  133. lcall LCD_command
  134. mov dptr, #Name1
  135. lcall send_msg
  136.  
  137. second:
  138. mov a, #0C0H
  139. lcall LCD_command
  140. mov dptr, #Name2
  141. lcall send_msg2
  142.  
  143. forever:
  144. clr CE_ADC
  145. mov R0, #00000001B ; Start bit:1, Single:1, D2: 0
  146. lcall DO_SPI_G
  147.  
  148.  
  149. Read_Temp:
  150. mov R0, #10000000B ; Read channel 0; D1 (bit 7): 0, D0 (bit 6):0
  151. lcall DO_SPI_G
  152. mov a, R1 ; R1 contains bits 8 to 9
  153. anl a, #03H
  154. mov LEDRB, a
  155. mov R7, a
  156. mov R0, #55H ; It doesn't matter what we transmit...
  157. lcall DO_SPI_G
  158. mov LEDRA, R1 ; R1 contains bits 0 to 7
  159. mov a,R1
  160. mov R6,a
  161. lcall display_temp
  162. setb CE_ADC
  163. lcall Delay
  164.  
  165. ljmp forever
  166.  
  167. display_temp:
  168. mov a, #83H
  169. lcall LCD_command
  170. lcall ADC_to_Celcius
  171. ret
  172.  
  173. ADC_to_Celcius:
  174. ; [R7,R6] has the 10-bit value
  175. ; from the converter
  176. mov x+0, R6
  177. mov x+1, R7
  178. mov x+2, #0
  179. mov x+3, #0
  180. Load32 (y, 500000)
  181. lcall mul32
  182. Load32 (y, 1024)
  183. lcall div32
  184. Load32 (y, 50000)
  185. lcall sub32
  186. lcall hex2bcd
  187. ; Display temperature in LCD
  188. ;*******************************NEED TO FIGURE OUT THE SIGFIGS
  189. ;most sig digit
  190. mov a, bcd+2
  191. swap a
  192. anl a, #0FH
  193. add a, #30h
  194. orl a, #'0'
  195. lcall LCD_put
  196. ;2nd most sig
  197. mov a, bcd+2
  198. anl a, #0FH
  199. add a, #30h
  200. orl a, #'0'
  201. lcall LCD_put
  202. ; Decimal point
  203. mov a, #'.'
  204. lcall LCD_put
  205. ; 3rd
  206. mov a, bcd+1
  207. swap a
  208. anl a, #0FH
  209. add a, #30h
  210. orl a, #'0'
  211. lcall LCD_put
  212.  
  213. ret
  214.  
  215. ; start here
  216.  
  217. $include(math32.asm)
  218. END
Add Comment
Please, Sign In to add comment