minimalmode3.asm 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. ; Always attempt to unlock on reset
  130. call unlock
  131. ; Main application
  132. enter
  133. bsf PORTA, 4 ; Motor Standby
  134. bsf PORTA, 5
  135. bcf PORTB, 4 ; Power Off
  136. btfss PORTC, 1 ; Make sure the socket is unlocked
  137. goto unlock
  138. movlw 5 ; Reset CONTMR
  139. movwf CONTMR
  140. movlw 0FA ; Turn off PWM
  141. movwf CCPR1L
  142. idle
  143. call read_cp_high ; Check if there is a car
  144. sublw 0
  145. btfsc STATUS, Z
  146. goto enter
  147. call delay100 ; Delay 100 ms
  148. decfsz CONTMR ; 5 times
  149. goto idle ; Check the stuff in this loop again
  150. movlw 5
  151. movwf CONTMR
  152. goto connected ; Then go to Connected
  153. connected
  154. btfsc PORTC, 1 ; Check if the lock is closed
  155. goto lock
  156. movlw CHARGEPWM ; Start offering
  157. movwf CCPR1L
  158. ; Check if the diode is OK
  159. ; call read_cp_low ; Check the diode
  160. ; sublw 3
  161. ; btfss STATUS, Z ; The return value must be 3, otherwise go back to enter.
  162. ; goto enter
  163. ; Check the S2 state of the vehicle
  164. call read_cp_high
  165. movwf CPSTATE
  166. sublw 1 ; Cable still connected?
  167. btfsc STATUS, Z
  168. goto connected
  169. movf CPSTATE, w
  170. sublw 2 ; Charging requested?
  171. btfsc STATUS, Z
  172. goto charging
  173. ; Cable must be disconnected, unlock and go back to idle.
  174. goto enter
  175. charging
  176. bsf PORTB, 4 ; Power On
  177. call read_cp_high
  178. movwf CPSTATE
  179. sublw 2 ; Still charging?
  180. btfsc STATUS, Z
  181. goto charging
  182. bcf PORTB, 4 ; Power Off
  183. goto connected
  184. read_cp_high
  185. ; Lees CP uit en return met een waarde in W (0 = los, 1 = connected, 2 = S2 gesloten)
  186. ; PILOT_12 = 0F4
  187. ; PILOT_9 = 0D2
  188. ; PILOT_6 = 0B4
  189. ; TMR2 loopt mee gedurende de PWM. Als TMR2 < CCPR1L dan zitten we nog in de hoog periode.
  190. ; 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.
  191. ; We willen niet direct aan het begin van de periode zitten, maar na 5%.
  192. ; Dus 12 < TMR2 < (CCPR1L - 16)
  193. ; Check if TMR2 > 12d
  194. movlw d'12'
  195. subwf TMR2, W ; TMR2 - 12: moet > dan nul zijn, STATUS,C = 1
  196. btfss STATUS, C ; Continue to next step if STATUS,C = 1
  197. goto read_cp_high
  198. ; Check if TMR2 < (CCPR1L - 16)
  199. movlw d'16' ; 16 -> W
  200. subwf CCPR1L, W ; (CCPR1L - 16) -> W
  201. subwf TMR2, W ; TMR2 - (CCPR1L - 16): moet < dan nul zijn, STATUS,C = 0
  202. btfsc STATUS, C ; Continue to next step if STATUS,C = 0
  203. goto read_cp_high
  204. goto take_sample
  205. read_cp_low
  206. ; Read the CP at the low point, return 3 if OK, 0 if not.
  207. ; (CCPR1L + 12) < TMR2 < (PR2 - 16)
  208. ; Check if TMR2 > CCPR1L + 12
  209. movf CCPR1L, W ; CCPR1L -> W
  210. addlw d'12' ; (CCPR1L + 12) -> W
  211. subwf TMR2, W ; TMR2 - (CCPR1L + 12): moet > dan nul, STATUS,C = 1
  212. btfss STATUS, C
  213. goto read_cp_low
  214. ; Check if TMR2 < (PR2 - 16)
  215. movlw d'16' ; 16 -> W
  216. subwf PR2, W ; (PR2 - 16) -> W
  217. subwf TMR2, W ; TMR2 - (PR2 - 16): moet < dan nul zijn, STATUS,C = 0
  218. btfsc STATUS, C
  219. goto read_cp_low
  220. goto take_sample
  221. take_sample
  222. bsf ADCON0, GO ; Start the sample
  223. wait_for_ad_result
  224. btfsc ADCON0, DONE
  225. goto wait_for_ad_result
  226. ; Check if 12V
  227. movf ADRESH, w ; Voltage -> W
  228. sublw PILOT_12 ; PILOT_12 - Voltage: moet < nul zijn, dus STATUS,C = 0
  229. btfss STATUS, C
  230. retlw 0
  231. ; Check if 9V
  232. movf ADRESH, w
  233. sublw PILOT_9
  234. btfss STATUS, C
  235. retlw 1
  236. ; Check if 6V
  237. movf ADRESH, w
  238. sublw PILOT_6
  239. btfss STATUS, C
  240. retlw 2
  241. ; Check if diode OK
  242. movf ADRES, w ; Voltage -> W
  243. sublw DIODE_OK ; DIODE_OK - Voltage: moet > 0, STATUS,C = 1
  244. btfsc STATUS, C ; Skip over this of the STATUS,C = 0
  245. retlw 3
  246. ; Otherwise return "0", meaning just go to idle and turn everything off
  247. retlw 0
  248. lock
  249. bcf PORTA, 4
  250. bsf PORTA, 5
  251. call delay100 ; Blocking 100ms delay
  252. call delay100 ; Blocking 100ms delay
  253. bsf PORTA, 4 ; Stop the motor
  254. call delay2000 ; Wait 2 seconds to charge the capacitor back up..
  255. goto idle
  256. unlock
  257. movlw 0FA ; Turn off PWM
  258. movwf CCPR1L
  259. bsf PORTA, 4
  260. bcf PORTA, 5
  261. call delay100 ; Blocking 100ms delay
  262. call delay100 ; Blocking 100ms delay
  263. bsf PORTA, 5 ; Stop the motor
  264. call delay2000 ; Wait 2 seconds to charge the capacitor back up.
  265. goto enter
  266. delay100
  267. ; Preset timer 0 value at 15533
  268. movlw 3C
  269. movwf TMR0H
  270. movlw 0AD
  271. movwf TMR0L
  272. ; Set prescaler to 8x
  273. bcf T0CON, T0PS0
  274. bsf T0CON, T0PS1
  275. bcf T0CON, T0PS2
  276. ; Start the timer
  277. bcf INTCON, TMR0IF ; Clear the interrupt
  278. bsf T0CON, TMR0ON ; Start the timer
  279. goto delay_wait
  280. delay_wait
  281. btfss INTCON, TMR0IF ; Wait for the timer to overflow
  282. goto delay_wait
  283. bcf T0CON, TMR0ON ; Stop the timer
  284. return
  285. delay500
  286. movlw d'5'
  287. movwf DLYCOUNT
  288. goto delay_repeat
  289. delay1000
  290. movlw d'10'
  291. movwf DLYCOUNT
  292. goto delay_repeat
  293. delay2000
  294. movlw d'20'
  295. movwf DLYCOUNT
  296. goto delay_repeat
  297. delay_repeat
  298. call delay100
  299. decfsz DLYCOUNT, f
  300. goto delay_repeat
  301. return
  302. end