Subversion Repositories Programming Utils

Rev

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

Rev Author Line No. Line
73 rm5248 1
; This is the main file for the turn signal
2
; rm5248
3
;
4
 
5
#include "p16F690.inc"
6
 
7
; CONFIG
8
; __config 0xFFFC
9
 __CONFIG _FOSC_INTRCIO & _WDTE_ON & _PWRTE_OFF & _MCLRE_ON & _CP_OFF & _CPD_OFF & _BOREN_ON & _IESO_ON & _FCMEN_ON
10
 
11
    cblock 0x20
12
GPR0
13
GPR1
14
GPR2
15
GPR3
16
GPR4
17
GPR5
18
GPR6
19
GPR7
20
GPR8
21
GPR9
22
GPR10
74 rm5248 23
LED_STATUS
24
STATUS_TEMP
25
W_TEMP
26
LEFT_TURN_REG
27
RIGHT_TURN_REG
73 rm5248 28
    endc
29
 
74 rm5248 30
#define RT_BRAKE    0
31
#define RT_TURN     2
32
#define LF_BRAKE    1
33
#define LF_TURN     0
73 rm5248 34
 
74 rm5248 35
; START
73 rm5248 36
 
74 rm5248 37
RESET CODE 0x00
38
        goto start
73 rm5248 39
 
74 rm5248 40
ISR CODE 0x04    
41
        movwf   W_TEMP          ;save Wreg
42
        swapf   STATUS, W       ;put status to be saved into W
43
        movwf   STATUS_TEMP     ;save status reg
73 rm5248 44
 
74 rm5248 45
        bcf     INTCON, T0IF    ;clear the interrupt(timer 0)
46
;------------------------------------------------------------------------
47
 
48
        movfw   LED_STATUS
49
        movwf   PORTB
50
        btfsc   LED_STATUS, 0
51
        movlw   0
52
        btfss   LED_STATUS, 0
53
        movlw   1
54
        movwf   LED_STATUS
55
;------------------------------------------------------------------------
73 rm5248 56
 
74 rm5248 57
        swapf   STATUS_TEMP,W   ;put status_temp back into W
58
        movwf   STATUS          ;move W into STATUS
59
        swapf   W_TEMP, F
60
        swapf   W_TEMP, W
61
        retfie                  ;return from interrupt
73 rm5248 62
 
74 rm5248 63
start:
64
        bsf     STATUS, RP1     ;bank 2
65
        clrf    ANSEL           ;digital i/o
66
        bcf     STATUS, RP1     ;register bank 0
67
        bsf     STATUS, RP0     ;select register bank 1
68
        movlw   b'11111000'     ;RA0-2 = output, RA3-5 = input
69
        movwf   TRISA
70
        movlw   b'11111110'     ;RC0 = output
71
        movwf   TRISC
73 rm5248 72
 
74 rm5248 73
        ; basic setup of TMR0 so we don't have to do it a lot
74
        movlw   b'11000111'     ; option reg: TMR0 on internal, 1:256 prescaler
75
        movwf   OPTION_REG
76
 
77
        bcf     STATUS, RP0
78
        bcf     STATUS, RP1     ;go back to register bank 0
79
 
80
;------------------------------------------------------------------------
81
        clrf    LED_STATUS
82
        call    enable_tmr0
83
        goto    $
84
;------------------------------------------------------------------------
85
 
73 rm5248 86
        ; okay, now that we have our inputs/outputs done correctly,
87
        ; let's go and read the pins
88
mainloop:
89
 
90
        ; clear our data
74 rm5248 91
        clrf    GPR0
92
        clrf    GPR1
73 rm5248 93
 
74 rm5248 94
        movf    PORTA, W    ; get input, put in wreg
95
        movwf   GPR0        ; put input in GPR0
73 rm5248 96
 
74 rm5248 97
        btfsc   GPR0, 5     ; check to see if we have a brake light(RA5)
98
        bsf     GPR1, 0     ; if we're here, the brake light is set, so set that flag
99
        btfsc   GPR0, 4     ; check to see if we have a left turn signal(RA4)
100
        bsf     GPR1, 1     ; if we're here, the left turn signal is on, so set that flag
101
        btfsc   GPR0, 3     ; check to see if we have a right turn signal(RA3)
102
        bsf     GPR1, 2     ; if we're here, the right turn signal is on, so set that flag
73 rm5248 103
 
104
 
74 rm5248 105
        ; if( brakes_on ){
106
        ;   if( no_left_turn_signal_in_past_1_second &&
107
        ;       no_right_turn_signal_in_past_1_second ){
108
        ;       both_brakes_on;
109
        ;   }
110
        ;   if( left_turn_in_past_1_second ){
111
        ;      right_brake_on;
112
        ;      left_signal_on;
113
        ;   }
114
        ;   if( right_turn_in_past_1_second ){
115
        ;      left_brake_on;
116
        ;      right_signal_on;
73 rm5248 117
        ;    }
74 rm5248 118
        ; } else{
119
        ;    turn_right_signal_on;
120
        ;    turn_left_signal_on;
73 rm5248 121
        ; }
74 rm5248 122
        btfsc   GPR0, 2     ; Check to see if we have our brakes on
123
        goto    brakes_on   ; yes, brakes are on.
124
        call turn_left_turn_on  ;brakes are off, turn the turn signals on
125
        call turn_right_turn_on
126
        goto mainloop
73 rm5248 127
 
74 rm5248 128
brakes_on:
129
        btfsc   GPR1, 1     ; do we have a left turn signal on?
130
        call    enable_tmr0 ; yes
131
        btfsc   GPR1, 2     ; do we have a right turn signal on?
132
        call    enable_tmr0 ; yes
73 rm5248 133
 
74 rm5248 134
        ; we need to test if our turn signals are on/if the interrupt is enabled
135
        ; if the interrupt for TMR0 is enabled, we know that at some point in
136
        ; the near past we turned our turn signals on.
137
        btfsc   INTCON, 5   ; is our interrupt for TMR0 on?
138
        goto no_signals     ; no
139
                            ; yes
140
        goto mainloop       ; continue on in main loop
73 rm5248 141
 
74 rm5248 142
enable_tmr0:
143
        ; enable the TMR0 interrupt and clear data, but only if not enabled
144
        ; already
145
        btfsc   INTCON, 5   ; is TMR0 interrupt set?
146
        return              ; yes it is, abort!!
147
        movlw   0
148
        movwf   TMR0        ; set TMR0 to 0
149
        movlw   b'10100000' ; clear all interrupts and enable TMR0 interrupt
150
        movwf   INTCON
151
        return
73 rm5248 152
 
74 rm5248 153
disable_tmr0:
154
        ; disable tmr0 interrupt
155
        movlw   0
156
        movwf INTCON
157
        return
73 rm5248 158
 
74 rm5248 159
; we have no turn signals on
160
no_signals:
161
        call turn_left_brake_on
162
        call turn_right_brake_on
163
        goto mainloop
73 rm5248 164
 
74 rm5248 165
turn_left_turn_on:
166
        bsf     PORTA, LF_TURN
167
        retlw   0
73 rm5248 168
 
74 rm5248 169
turn_right_turn_on:
170
        bsf     PORTA, RT_TURN
171
        retlw   0
73 rm5248 172
 
74 rm5248 173
turn_left_brake_on:
174
        bsf     PORTA, LF_BRAKE
175
        retlw   0
73 rm5248 176
 
74 rm5248 177
turn_right_brake_on:
178
        bsf     PORTC, RT_BRAKE
179
        retlw   0
73 rm5248 180
 
181
        END