Subversion Repositories Programming Utils

Compare Revisions

Ignore whitespace Rev 73 → Rev 74

/trunk/TurnSignal/main.asm
2,8 → 2,6
; rm5248
;
 
 
#define GPIO 0
#include "p16F690.inc"
 
; CONFIG
22,125 → 20,162
GPR8
GPR9
GPR10
GPR11
GPR12
GPR13
GPR14
GPR15
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
 
; GPR10-GPR15 act as a poor man's counter, if they all overflow X number of seconds have passed,
; so turn on the turn signal.
RESET CODE 0x00
goto start
 
; 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
 
ORG 0x00
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
;------------------------------------------------------------------------
 
movlw b'11111100' ; GP3/GP2 = input, GP1/GP0 = output
//tris GPIO ; load GPIO TRIS register with the above values
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
 
; clear our counters
clrf GPR15
clrf GPR14
clrf GPR13
clrf GPR12
clrf GPR11
clrf GPR10
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
clrf GPR0
clrf GPR1
 
movf GPIO, 1 ; get input, put in wreg
movwf GPR0 ; put input in GPR0
movf PORTA, W ; get input, put in wreg
movwf GPR0 ; put input in GPR0
 
btfsc GPR0, 3 ; check to see if we have a brake light(GPIO3)
bsf GPR1, 0 ; if we're here, the brake light is set, so set that flag
btfsc GPR0, 2 ; check to see if we have a turn signal
bsf GPR1, 1 ; if we're here, the turn signal is running, so set that flag
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( turnSignal == on ){
; turn_signal_output = on;
; } else if( turnSignal == off ){
; if( no_turn_signal_in_past_2_seconds ){
; bake_output = on;
; }else{
; turn_signal_output = on;
; 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 a turn signal
goto turn_signal ; nope, turn the turn 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
 
; if we're here, the turn signal must be off
call increment_counter
; wreg is 1 if we should turn the brake input on,
; else 0
movwf GPR2
btfsc GPR2, 0 ; test to see if we should turn the turn signal on
goto turn_signal ; yes, we should turn the turn signal on
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
 
call brakes_on ; turn the brakes on
goto mainloop ; all done with the loop, start again. should only get here if brakes on
; 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
 
turn_signal:
call turn_signal_on
goto mainloop ;continue on the 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
 
turn_signal_on:
bsf GPIO, 0 ; GPIO0 = turn signals
retlw 0
disable_tmr0:
; disable tmr0 interrupt
movlw 0
movwf INTCON
return
 
brakes_on:
bsf GPIO, 1 ; GPIO1 = brake
retlw 0
; we have no turn signals on
no_signals:
call turn_left_brake_on
call turn_right_brake_on
goto mainloop
 
increment_counter:
movlw 1 ; wreg = 1
addwf GPR15, GPR15 ; add 1 to GPR15
btfsc STATUS, 0 ; check the status register, did we overflow?
goto add_to_GPR14 ; if we're here, we overflowed
retlw 0 ; we have not timed out
turn_left_turn_on:
bsf PORTA, LF_TURN
retlw 0
 
add_to_GPR14:
movlw 1
addwf GPR14, GPR14
btfsc STATUS, 0
goto add_to_GPR13
retlw 0
turn_right_turn_on:
bsf PORTA, RT_TURN
retlw 0
 
add_to_GPR13:
movlw 1
addwf GPR13, GPR13
btfsc STATUS, 0
goto add_to_GPR12
retlw 0
turn_left_brake_on:
bsf PORTA, LF_BRAKE
retlw 0
 
add_to_GPR12:
movlw 1
addwf GPR12, GPR12
btfsc STATUS, 0
goto add_to_GPR11
retlw 0
turn_right_brake_on:
bsf PORTC, RT_BRAKE
retlw 0
 
add_to_GPR11:
movlw 1
addwf GPR11, GPR11
btfsc STATUS, 0
goto add_to_GPR10
retlw 0
 
add_to_GPR10:
movlw 1
addwf GPR10, GPR10
btfsc STATUS, 0
nop
retlw 1
 
END