minimalmode3.asm 8.7 KB

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