minimalmode3.asm 11 KB

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