Browse Source

Implemented diode check

Stan Janssen 7 years ago
parent
commit
17bbd4577a
1 changed files with 60 additions and 27 deletions
  1. 60 27
      minimalmode3.asm

+ 60 - 27
minimalmode3.asm

@@ -51,15 +51,16 @@
     config EBTRB = OFF
 
 
-CONTMR	equ 000
-CPSTATE equ 001
-DLYCOUNT equ 002
+CONTMR	    equ 000
+CPSTATE     equ 001
+DLYCOUNT    equ 002
 
-PILOT_12 equ 0F4
-PILOT_9 equ 0D2
-PILOT_6 equ 0B4
+PILOT_12    equ 0F4
+PILOT_9     equ 0D2
+PILOT_6     equ 0B4
+DIODE_OK    equ 0B
 
-CHARGEPWM equ 3C
+CHARGEPWM   equ 3C
 
 start
     ; Initialize Ports                          TRISA
@@ -81,6 +82,8 @@ start
     bsf     ADCON0, ADON        ; Turn on ADC module
     movlw   b'00100101'         ; Settings for ADC: left justified, 8 cycles, Fosc/16
     movwf   ADCON2
+    movlw   b'00000001'
+    movwf   ADCON0
     
     
     ; PORTB, 0 = display 1 = digital out
@@ -154,7 +157,7 @@ enter
     
     bcf     PORTB, 4    ; Power Off
     
-    btfss   PORTC, 1    ; Make sure the lock is off
+    btfss   PORTC, 1    ; Make sure the socket is unlocked
     goto    unlock
     
     movlw   5           ; Reset CONTMR
@@ -162,12 +165,10 @@ enter
     
     movlw   0FA         ; Turn off PWM
     movwf   CCPR1L
-    call    delay100
     
 
 idle
-    call    read_cp     ; Check if there is a car
-    
+    call    read_cp_high     ; Check if there is a car
     sublw   0
     btfsc   STATUS, Z
     goto    enter
@@ -188,7 +189,14 @@ connected
     movlw   CHARGEPWM       ; Start offering
     movwf   CCPR1L
     
-    call    read_cp
+    ; Check if the diode is OK
+    call    read_cp_low      ; Check the diode
+    sublw   3
+    btfsc   STATUS, Z        ; The return value must be 3, otherwise go back to enter.
+    goto    enter
+    
+    ; Check the S2 state of the vehicle
+    call    read_cp_high
     movwf   CPSTATE
     sublw   1           ; Cable still connected?
     btfsc   STATUS, Z
@@ -200,12 +208,12 @@ connected
     goto    charging
     
     ; Cable must be disconnected, unlock and go back to idle.
-    goto   unlock
+    goto    enter
 
 charging
     bsf     PORTB, 4           ; Power On
     
-    call    read_cp
+    call    read_cp_high
     sublw   2                   ; Still charging?
     movwf   CPSTATE
     btfsc   STATUS, Z
@@ -215,34 +223,53 @@ charging
     goto    connected
     
 
-read_cp
-    ; Lees CP uit en return met een waarde in W (0 = los, 1 = connected, 2 = S2 gesloten, 3 = error)
+
+read_cp_high
+    ; Lees CP uit en return met een waarde in W (0 = los, 1 = connected, 2 = S2 gesloten)
     ;    PILOT_12  = 0F4
     ;    PILOT_9   = 0D2
     ;    PILOT_6   = 0B4
-    ; Selecteer CP poort voor analoge conversie
-    movlw   b'00000001'
-    movwf   ADCON0
 
-wait_for_pwm_period
     ; TMR2 loopt mee gedurende de PWM. Als TMR2 < CCPR1L dan zitten we nog in de hoog periode.
     ; 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.
     ; We willen niet direct aan het begin van de periode zitten, maar na 5%.
-    ; Dus CCPR1L/2 < TMR2 < (CCPR1L - 16) 
+    ; Dus 12 < TMR2 < (CCPR1L - 16) 
     
     ; Check if TMR2 > 12d
     movlw   d'12'
     subwf   TMR2, W             ; TMR2 - 12: moet > dan nul zijn, STATUS,C = 1
-    btfsc   STATUS, C
-    goto    wait_for_pwm_period
+    btfss   STATUS, C           ; Continue to next step if STATUS,C = 1
+    goto    read_cp_high
     
     ; Check if TMR2 < (CCPR1L - 16)
     movlw   d'16'               ; 16 -> W
     subwf   CCPR1L, W           ; (CCPR1L - 16) -> W
     subwf   TMR2, W             ; TMR2 - (CCPR1L - 16): moet < dan nul zijn, STATUS,C = 0
-    btfsc   STATUS, C
-    goto    wait_for_pwm_period
+    btfsc   STATUS, C           ; Continue to next step if STATUS,C = 0
+    goto    read_cp_high
+    goto    take_sample
+ 
+
+read_cp_low
+    ; Read the CP at the low point, return 3 if OK, 0 if not.
+    ; (CCPR1L + 12) < TMR2 < (PR2 - 16)
+    
+    ; Check if TMR2 > CCPR1L + 12
+    movf    CCPR1L, W           ; CCPR1L -> W
+    addlw   d'12'               ; (CCPR1L + 12) -> W
+    subwf   TMR2, W             ; TMR2 - (CCPR1L + 12): moet > dan nul, STATUS,C = 1
+    btfss   STATUS, C
+    goto    read_cp_low
     
+    ; Check if TMR2 < (PR2 - 16)
+    movlw   d'16'               ; 16 -> W
+    subwf   PR2, W              ; (PR2 - 16) -> W
+    subwf   TMR2, W             ; TMR2 - (PR2 - 16): moet < dan nul zijn, STATUS,C = 0
+    btfsc   STATUS, C
+    goto    read_cp_low
+    goto    take_sample
+
+take_sample
     bsf     ADCON0, GO          ; Start the sample
     
 wait_for_ad_result
@@ -250,8 +277,8 @@ wait_for_ad_result
     goto    wait_for_ad_result
     
     ; Check if 12V
-    movf    ADRESH, w
-    sublw   PILOT_12
+    movf    ADRESH, w       ; Voltage -> W
+    sublw   PILOT_12        ; PILOT_12 - Voltage: moet < nul zijn, dus STATUS,C = 0
     btfss   STATUS, C
     retlw   0
     
@@ -267,6 +294,12 @@ wait_for_ad_result
     btfss   STATUS, C
     retlw   2
     
+    ; Check if diode OK
+    movf    ADRES, w            ; Voltage -> W
+    sublw   DIODE_OK            ; DIODE_OK - Voltage: moet > 0, STATUS,C = 1
+    btfsc   STATUS, C           ; Skip over this of the STATUS,C = 0
+    retlw   3
+    
     ; Otherwise return "0", meaning just go to idle and turn everything off
     retlw   0