minimalmode3.asm 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. list p=18f25k22
  2. errorlevel -306 ; no page boundary warnings
  3. errorlevel -305 ; no default destination messages
  4. errorlevel -302 ; no bank 0 warnings
  5. errorlevel -202 ; no 'argument out of range' warnings
  6. include <p18f25k22.inc>
  7. ; Device configuration settings
  8. config FCMEN = OFF
  9. config IESO = OFF
  10. config PRICLKEN = ON
  11. config PLLCFG = OFF
  12. config FOSC = HSMP
  13. config BORV = 285
  14. config BOREN = OFF
  15. config PWRTEN = ON
  16. config WDTPS = 2048
  17. config WDTEN = OFF
  18. config CCP2MX = PORTB3
  19. config PBADEN = OFF
  20. config CCP3MX = PORTC6
  21. config HFOFST = OFF
  22. config T3CMX = PORTB5
  23. config P2BMX = PORTC0
  24. config MCLRE = INTMCLR
  25. config XINST = ON
  26. config DEBUG = OFF
  27. config LVP = OFF
  28. config STVREN = ON
  29. config CP0 = OFF
  30. config CP1 = OFF
  31. config CP2 = OFF
  32. config CP3 = OFF
  33. config CPD = OFF
  34. config CPB = OFF
  35. config WRT0 = OFF
  36. config WRT1 = OFF
  37. config WRT2 = OFF
  38. config WRT3 = OFF
  39. config WRTC = OFF
  40. config WRTB = OFF
  41. config WRTD = OFF
  42. config EBTR0 = OFF
  43. config EBTR1 = OFF
  44. config EBTR2 = OFF
  45. config EBTR3 = OFF
  46. config EBTRB = OFF
  47. CONTMR equ 000
  48. CPSTATE equ 001
  49. DLYCOUNT equ 002
  50. PILOT_12 equ 0F4
  51. PILOT_9 equ 0D2
  52. PILOT_6 equ 0B4
  53. DIODE_OK equ 0B
  54. CHARGEPWM equ 3C
  55. start
  56. ; Initialize Ports TRISA
  57. ; PORTA, 0 = CP input = analog in 1
  58. ; PORTA, 1 = PP input = analog in 1
  59. ; PORTA, 2 = Temperatuursensor = analog in 1
  60. ; PORTA, 3 = (niet aangesloten) 1
  61. ; PORTA, 4 = motor driver 1 = digital out 0
  62. ; PORTA, 5 = motor driver 2 = digital out 0
  63. ; PORTA, 6 = kristal 1 0
  64. ; PORTA, 7 = kristal 2 1
  65. movlb 0F ; Stel bank F in voor alle SFR's
  66. movlw b'10001111'
  67. movwf TRISA
  68. movlw b'00001111'
  69. movwf ANSELA
  70. ; Analog to Digital conversion setup
  71. bsf ADCON0, ADON ; Turn on ADC module
  72. movlw b'00100101' ; Settings for ADC: left justified, 8 cycles, Fosc/16
  73. movwf ADCON2
  74. movlw b'00000001'
  75. movwf ADCON0
  76. ; PORTB, 0 = display 1 = digital out
  77. ; PORTB, 1 = display 2 = digital out
  78. ; PORTB, 2 = display 3 = digital out
  79. ; PORTB, 3 = display 4 = digital out
  80. ; PORTB, 4 = Solid State Relais = digital out
  81. ; PORTB, 5 = Richtingsschakelaar voor RS485 = digital out
  82. ; PORTB, 6 = UART 2 TX (moet als input)
  83. ; PORTB, 7 = UART 2 RX (moet als input)
  84. movlw b'11000000'
  85. movwf TRISB
  86. ; PORTC, 0 = display 5 = digital out
  87. ; PORTC, 1 = Lock State = digital input
  88. ; PORTC, 2 = PWM output poort (moet als input geconfigureerd voor PWM operatie, pag 186)
  89. ; PORTC, 3 = display 6 = digital out
  90. ; PORTC, 4 = display 7 = digital out
  91. ; PORTC, 5 = display 8 = digital out
  92. ; PORTC, 6 = UART 1 TX, RS485 communicatie TX
  93. ; PORTC, 7 = UART 2 RX, RS485 communicatie RX
  94. movlw b'11000110'
  95. movwf TRISC
  96. ; Initialize PWM op poort C2
  97. bcf CCPTMRS0, C1TSEL0 ; Selecteer TMR1 en TMR2 voor PWM
  98. bcf CCPTMRS0, C1TSEL1
  99. bsf CCP1CON, CCP1M3 ; Stel in voor PWM op de CCP module
  100. bsf CCP1CON, CCP1M2
  101. movlw b'00000110' ; Stel 16x prescaler in port Timer 2
  102. movwf T2CON
  103. movlw 0F9h ; Stel periode in op 249 = 1 kHz bij 16 MHz
  104. movwf PR2
  105. movlw 0FA ; Stel 100% PWM in bij start
  106. movwf CCPR1L
  107. bcf PIR2, TMR2IF
  108. bsf T2CON, TMR2ON ; Start de timer
  109. bcf PIR1, TMR1IF
  110. bsf T1CON, TMR1ON ; Start TMR1
  111. bcf TRISC, 2 ; Enable output driver
  112. ; Stroom Duty Cycle CCPR1L:CCP1CON<5:4> CCPR1L CCP1CON<5:4>
  113. ; 6 10% 100 25 0
  114. ; 7 11.7% 117 29 1
  115. ; 8 13.3% 133 33 1
  116. ; 9 15% 150 37 2
  117. ; 10 16.7% 167 41 3
  118. ; 11 18.3% 183 45 3
  119. ; 12 20% 200 50 0
  120. ; 13 21.7% 217 54 1
  121. ; 14 23.3% 233 58 1
  122. ; 15 25% 250 62 2
  123. ; 16 26.7% 267 66 3
  124. ; Initialize TMR0 to use as a delay timer
  125. ; Set to 16 bits, instruction cycle clock, use prescaler, use 4x prescaler
  126. clrf TMR0L
  127. clrf TMR0H
  128. clrf T0CON
  129. ; Main application
  130. enter
  131. bsf PORTA, 4 ; Motor Standby
  132. bsf PORTA, 5
  133. bcf PORTB, 4 ; Power Off
  134. btfss PORTC, 1 ; Make sure the socket is unlocked
  135. goto unlock
  136. movlw 5 ; Reset CONTMR
  137. movwf CONTMR
  138. movlw 0FA ; Turn off PWM
  139. movwf CCPR1L
  140. idle
  141. call read_cp_high ; Check if there is a car
  142. sublw 0
  143. btfsc STATUS, Z
  144. goto enter
  145. call delay100 ; Delay 100 ms
  146. decfsz CONTMR ; 5 times
  147. goto idle ; Check the stuff in this loop again
  148. movlw 5
  149. movwf CONTMR
  150. goto connected ; Then go to Connected
  151. connected
  152. btfsc PORTC, 1 ; Check if the lock is closed
  153. goto lock
  154. movlw CHARGEPWM ; Start offering
  155. movwf CCPR1L
  156. ; Check if the diode is OK
  157. call read_cp_low ; Check the diode
  158. sublw 3
  159. btfsc STATUS, Z ; The return value must be 3, otherwise go back to enter.
  160. goto enter
  161. ; Check the S2 state of the vehicle
  162. call read_cp_high
  163. movwf CPSTATE
  164. sublw 1 ; Cable still connected?
  165. btfsc STATUS, Z
  166. goto connected
  167. movf CPSTATE, w
  168. sublw 2 ; Charging requested?
  169. btfsc STATUS, Z
  170. goto charging
  171. ; Cable must be disconnected, unlock and go back to idle.
  172. goto enter
  173. charging
  174. bsf PORTB, 4 ; Power On
  175. call read_cp_high
  176. movwf CPSTATE
  177. sublw 2 ; Still charging?
  178. btfsc STATUS, Z
  179. goto charging
  180. bcf PORTB, 4 ; Power Off
  181. goto connected
  182. read_cp_high
  183. ; Lees CP uit en return met een waarde in W (0 = los, 1 = connected, 2 = S2 gesloten)
  184. ; PILOT_12 = 0F4
  185. ; PILOT_9 = 0D2
  186. ; PILOT_6 = 0B4
  187. ; TMR2 loopt mee gedurende de PWM. Als TMR2 < CCPR1L dan zitten we nog in de hoog periode.
  188. ; De aquisitietijd voor de AD-conversie is 4 cycli. Als we 16 cycli voor het eind van de PWM-hoog periode beginnen met samplen, hebben we tijd genoeg.
  189. ; We willen niet direct aan het begin van de periode zitten, maar na 5%.
  190. ; Dus 12 < TMR2 < (CCPR1L - 16)
  191. ; Check if TMR2 > 12d
  192. movlw d'12'
  193. subwf TMR2, W ; TMR2 - 12: moet > dan nul zijn, STATUS,C = 1
  194. btfss STATUS, C ; Continue to next step if STATUS,C = 1
  195. goto read_cp_high
  196. ; Check if TMR2 < (CCPR1L - 16)
  197. movlw d'16' ; 16 -> W
  198. subwf CCPR1L, W ; (CCPR1L - 16) -> W
  199. subwf TMR2, W ; TMR2 - (CCPR1L - 16): moet < dan nul zijn, STATUS,C = 0
  200. btfsc STATUS, C ; Continue to next step if STATUS,C = 0
  201. goto read_cp_high
  202. goto take_sample
  203. read_cp_low
  204. ; Read the CP at the low point, return 3 if OK, 0 if not.
  205. ; (CCPR1L + 12) < TMR2 < (PR2 - 16)
  206. ; Check if TMR2 > CCPR1L + 12
  207. movf CCPR1L, W ; CCPR1L -> W
  208. addlw d'12' ; (CCPR1L + 12) -> W
  209. subwf TMR2, W ; TMR2 - (CCPR1L + 12): moet > dan nul, STATUS,C = 1
  210. btfss STATUS, C
  211. goto read_cp_low
  212. ; Check if TMR2 < (PR2 - 16)
  213. movlw d'16' ; 16 -> W
  214. subwf PR2, W ; (PR2 - 16) -> W
  215. subwf TMR2, W ; TMR2 - (PR2 - 16): moet < dan nul zijn, STATUS,C = 0
  216. btfsc STATUS, C
  217. goto read_cp_low
  218. goto take_sample
  219. take_sample
  220. bsf ADCON0, GO ; Start the sample
  221. wait_for_ad_result
  222. btfsc ADCON0, DONE
  223. goto wait_for_ad_result
  224. ; Check if 12V
  225. movf ADRESH, w ; Voltage -> W
  226. sublw PILOT_12 ; PILOT_12 - Voltage: moet < nul zijn, dus STATUS,C = 0
  227. btfss STATUS, C
  228. retlw 0
  229. ; Check if 9V
  230. movf ADRESH, w
  231. sublw PILOT_9
  232. btfss STATUS, C
  233. retlw 1
  234. ; Check if 6V
  235. movf ADRESH, w
  236. sublw PILOT_6
  237. btfss STATUS, C
  238. retlw 2
  239. ; Check if diode OK
  240. movf ADRES, w ; Voltage -> W
  241. sublw DIODE_OK ; DIODE_OK - Voltage: moet > 0, STATUS,C = 1
  242. btfsc STATUS, C ; Skip over this of the STATUS,C = 0
  243. retlw 3
  244. ; Otherwise return "0", meaning just go to idle and turn everything off
  245. retlw 0
  246. lock
  247. bcf PORTA, 4
  248. bsf PORTA, 5
  249. call delay100 ; Blocking 100ms delay
  250. bsf PORTA, 4 ; Stop the motor
  251. call delay1000
  252. goto idle
  253. unlock
  254. movlw 0FA ; Turn off PWM
  255. movwf CCPR1L
  256. bsf PORTA, 4
  257. bcf PORTA, 5
  258. call delay100 ; Blocking 100ms delay
  259. bsf PORTA, 5 ; Stop the motor
  260. call delay2000
  261. goto enter
  262. delay100
  263. ; Preset timer 0 value at 15533
  264. movlw 3C
  265. movwf TMR0H
  266. movlw 0AD
  267. movwf TMR0L
  268. ; Set prescaler to 8x
  269. bcf T0CON, T0PS0
  270. bsf T0CON, T0PS1
  271. bcf T0CON, T0PS2
  272. ; Start the timer
  273. bcf INTCON, TMR0IF ; Clear the interrupt
  274. bsf T0CON, TMR0ON ; Start the timer
  275. goto delay_wait
  276. delay_wait
  277. btfss INTCON, TMR0IF ; Wait for the timer to overflow
  278. goto delay_wait
  279. bcf T0CON, TMR0ON ; Stop the timer
  280. return
  281. delay500
  282. movlw d'5'
  283. movwf DLYCOUNT
  284. goto delay_repeat
  285. delay1000
  286. movlw d'10'
  287. movwf DLYCOUNT
  288. goto delay_repeat
  289. delay2000
  290. movlw d'20'
  291. movwf DLYCOUNT
  292. goto delay_repeat
  293. delay_repeat
  294. call delay100
  295. decfsz DLYCOUNT, f
  296. goto delay_repeat
  297. return
  298. end