Subversion Repositories Programming Utils

Rev

Rev 73 | Blame | Compare with Previous | Last modification | View Log | RSS feed

; This is the main file for the turn signal
; rm5248
;

#include "p16F690.inc"

; CONFIG
; __config 0xFFFC
 __CONFIG _FOSC_INTRCIO & _WDTE_ON & _PWRTE_OFF & _MCLRE_ON & _CP_OFF & _CPD_OFF & _BOREN_ON & _IESO_ON & _FCMEN_ON

    cblock 0x20
GPR0
GPR1
GPR2
GPR3
GPR4
GPR5
GPR6
GPR7
GPR8
GPR9
GPR10
LED_STATUS
STATUS_TEMP
W_TEMP
LEFT_TURN_REG
RIGHT_TURN_REG
    endc

#define RT_BRAKE    0
#define RT_TURN     2
#define LF_BRAKE    1
#define LF_TURN     0

; START

RESET CODE 0x00
        goto start

ISR CODE 0x04    
        movwf   W_TEMP          ;save Wreg
        swapf   STATUS, W       ;put status to be saved into W
        movwf   STATUS_TEMP     ;save status reg

        bcf     INTCON, T0IF    ;clear the interrupt(timer 0)
;------------------------------------------------------------------------
       
        movfw   LED_STATUS
        movwf   PORTB
        btfsc   LED_STATUS, 0
        movlw   0
        btfss   LED_STATUS, 0
        movlw   1
        movwf   LED_STATUS
;------------------------------------------------------------------------

        swapf   STATUS_TEMP,W   ;put status_temp back into W
        movwf   STATUS          ;move W into STATUS
        swapf   W_TEMP, F
        swapf   W_TEMP, W
        retfie                  ;return from interrupt

start:
        bsf     STATUS, RP1     ;bank 2
        clrf    ANSEL           ;digital i/o
        bcf     STATUS, RP1     ;register bank 0
        bsf     STATUS, RP0     ;select register bank 1
        movlw   b'11111000'     ;RA0-2 = output, RA3-5 = input
        movwf   TRISA
        movlw   b'11111110'     ;RC0 = output
        movwf   TRISC

        ; basic setup of TMR0 so we don't have to do it a lot
        movlw   b'11000111'     ; option reg: TMR0 on internal, 1:256 prescaler
        movwf   OPTION_REG

        bcf     STATUS, RP0
        bcf     STATUS, RP1     ;go back to register bank 0

;------------------------------------------------------------------------
        clrf    LED_STATUS
        call    enable_tmr0
        goto    $
;------------------------------------------------------------------------

        ; okay, now that we have our inputs/outputs done correctly,
        ; let's go and read the pins
mainloop:

        ; clear our data
        clrf    GPR0
        clrf    GPR1

        movf    PORTA, W    ; get input, put in wreg
        movwf   GPR0        ; put input in GPR0

        btfsc   GPR0, 5     ; check to see if we have a brake light(RA5)
        bsf     GPR1, 0     ; if we're here, the brake light is set, so set that flag
        btfsc   GPR0, 4     ; check to see if we have a left turn signal(RA4)
        bsf     GPR1, 1     ; if we're here, the left turn signal is on, so set that flag
        btfsc   GPR0, 3     ; check to see if we have a right turn signal(RA3)
        bsf     GPR1, 2     ; if we're here, the right turn signal is on, so set that flag


        ; if( brakes_on ){
        ;   if( no_left_turn_signal_in_past_1_second &&
        ;       no_right_turn_signal_in_past_1_second ){
        ;       both_brakes_on;
        ;   }
        ;   if( left_turn_in_past_1_second ){
        ;      right_brake_on;
        ;      left_signal_on;
        ;   }
        ;   if( right_turn_in_past_1_second ){
        ;      left_brake_on;
        ;      right_signal_on;
        ;    }
        ; } else{
        ;    turn_right_signal_on;
        ;    turn_left_signal_on;
        ; }
        btfsc   GPR0, 2     ; Check to see if we have our brakes on
        goto    brakes_on   ; yes, brakes are on.
        call turn_left_turn_on  ;brakes are off, turn the turn signals on
        call turn_right_turn_on
        goto mainloop

brakes_on:
        btfsc   GPR1, 1     ; do we have a left turn signal on?
        call    enable_tmr0 ; yes
        btfsc   GPR1, 2     ; do we have a right turn signal on?
        call    enable_tmr0 ; yes

        ; we need to test if our turn signals are on/if the interrupt is enabled
        ; if the interrupt for TMR0 is enabled, we know that at some point in
        ; the near past we turned our turn signals on.
        btfsc   INTCON, 5   ; is our interrupt for TMR0 on?
        goto no_signals     ; no
                            ; yes
        goto mainloop       ; continue on in main loop

enable_tmr0:
        ; enable the TMR0 interrupt and clear data, but only if not enabled
        ; already
        btfsc   INTCON, 5   ; is TMR0 interrupt set?
        return              ; yes it is, abort!!
        movlw   0
        movwf   TMR0        ; set TMR0 to 0
        movlw   b'10100000' ; clear all interrupts and enable TMR0 interrupt
        movwf   INTCON
        return

disable_tmr0:
        ; disable tmr0 interrupt
        movlw   0
        movwf INTCON
        return

; we have no turn signals on
no_signals:
        call turn_left_brake_on
        call turn_right_brake_on
        goto mainloop

turn_left_turn_on:
        bsf     PORTA, LF_TURN
        retlw   0

turn_right_turn_on:
        bsf     PORTA, RT_TURN
        retlw   0

turn_left_brake_on:
        bsf     PORTA, LF_BRAKE
        retlw   0

turn_right_brake_on:
        bsf     PORTC, RT_BRAKE
        retlw   0

        END