minimalmode3.asm 8.7 KB

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