list p=12F683 ; list directive to define processor
#include <p12F683.inc> ; processor specific variable definitions
errorlevel -302 ; suppress message 302 from list file
__CONFIG _FCMEN_OFF & _IESO_OFF & _CP_ON & _CPD_ON & _BOD_OFF & _MCLRE_OFF & _WDT_OFF & _PWRTE_ON & _INTOSCIO
;***** VARIABLE DEFINITIONS******************
w_temp EQU 0x70 ; variable used for context saving
status_temp EQU 0x71 ; variable used for context saving
FLAGS EQU 0x20 ;software state register
PULSER EQU 0x21 ;pulse on counter
DWELLTIMER EQU 0x22 ;pulse off counter
STATE EQU 0x23 ;programmed state
BUTTONHOLD EQU 0x24
FREQR EQU 0x25 ;frequency value
TEMP1 EQU 0x26
TEMP2 EQU 0x27
PULSEL EQU 0x28
DWELLTIMEL EQU 0x29
TEMP5 EQU 0x2A
LEDBLINK EQU 0x2B
TEMP3 EQU 0x2C
BURSTRATE EQU 0x63 ;3E
BURSTRight EQU 0x64 ;3F
BURSTLeft EQU 0x65
FLAGS2 equ 0x66 ;41
SHADOWP equ 0x67 ;42
LEDActivado equ 0x68 ;43
DATOSEE equ 0x69 ;44
DIRECCIONEE equ 0x6A ;45
DESACTIVADO equ 0x6B ;46
TEMPFREE equ 0x6C ;47
TEMPLED3EE equ 0x6D ;48
TEMPLED3EEconta equ 0x6E ;49
TEMPLED4EE equ 0x6F ;4A
TEMPLED4EEconta equ 0x72 ;4B
HAYCAMBIO equ 0x73 ;4C
TEMP equ 0x74 ;4D
TEMPFastForward equ 0x75
TEMPFastRewind equ 0x76
TEMPFast equ 0x77
;*******bits in FLAGS register***********
PULSEONR equ 0
PULSEONL equ 1
TRIGOUT equ 2
LEDHI equ 3
STATECHANGE equ 4
BURSTACTIVE equ 6
COD5MODE equ 7
;******bits in FLAGS2 register
BDWELLRT equ 0
BDWELLLT equ 1
BURSTONR equ 2
BURSTONL equ 3
ActLEDIzq equ 1
ActLEDDer equ 0
;*******bits in STATE register***********
RIGHTACTIVE equ 0
LEFTACTIVE equ 1
;*********GPIO pin names*****************
LEDL equ 4
LEDR equ 5
BUTTONR equ 2
BUTTONL equ 3
TRIGGERR equ 0
TRIGGERL equ 1
;**********************************************************************
ORG 0x000 ; processor reset vector
goto main ; go to beginning of program
ORG 0x004 ; interrupt vector location
movwf w_temp ; save off current W register contents
movf STATUS,w ; move status register into W register
movwf status_temp ; save off contents of STATUS register
;Take a good note here. OSC is left at default 4MHz.
; We are running at 1MHz instruction cycle, or 1 micro second
;Timer 2 is prescaled 1:4 and compared (PR2) to 250.
; we now have hardware interupt that falls every 1ms exactly
;within limitation of osctune. Main code is inconsequential.
;PULSE and DWELL values control the signals precisely.
;All pulse logic is switched within the few microseconds to
;service the interupt routine. DO NOT modify the ISR
;with any kind of 'wait' or 'loop'. If you want to trigger
; timed events do so by setting an open FLAG bit and have the
;event done in the main loop.
btfss PIR1, TMR2IF ;timer 2 int happened
retfie
;**************** Interruptions *********************************************
;Our main routine will turn the flag bit
;LEDON on or off. The 10ms pulse is a duty cycle
;for the code to work with both CG and
;matrix controllers.
;**************** Tactiles push logic **************************************
BlinkLed
btfss FLAGS, LEDHI
goto LedLow
decfsz LEDBLINK, f ;if pulse is active, count down 10ms
goto RightLED
movlw 0x0A
movwf LEDBLINK
bcf FLAGS, LEDHI ;10ms done, turn off LEDHI bit
goto RightLED
LedLow
decfsz LEDBLINK, f ;LEDLO time, count down 10ms
goto RightLED
movlw 0x0A
movwf LEDBLINK
bsf FLAGS, LEDHI ;10ms done, turn LEDHI bit on
RightLED
btfsc STATE, RIGHTACTIVE ;is RF on?
goto OutputRightLED
bsf STATUS, RP0 ;bank 1
bsf TRISIO, LEDR ;tristate LED
bcf STATUS, RP0 ;bank 0
goto LeftLED
OutputRightLED
btfsc FLAGS, LEDHI ;if ledhi true,
bsf GPIO, LEDR ;turn output hi
btfss FLAGS, LEDHI ;if ledhi flase,
bcf GPIO, LEDR ;turn led output lo
bsf STATUS, RP0 ;bank 1
bcf TRISIO, LEDR ;turn output on
bcf STATUS, RP0 ;bank 0
LeftLED
btfsc STATE, LEFTACTIVE ;is RF on?
goto OutputleftLED
bsf STATUS, RP0 ;bank 1
bsf TRISIO, LEDL ;tristate LED
bcf STATUS, RP0 ;bank 0
goto RightTrigger
OutputleftLED
btfsc FLAGS, LEDHI ;if ledhi true,
bsf GPIO, LEDL ;turn output hi
btfss FLAGS, LEDHI ;if ledhi flase,
bcf GPIO, LEDL ;turn led output lo
bsf STATUS, RP0 ;bank 1
bcf TRISIO, LEDL ;turn output on
bcf STATUS, RP0 ;bank 0
;**************** Right trigger logic***************************************
RightTrigger
;**************** Disparar en modo programacion
btfsc DESACTIVADO, 1
goto ActiveR
;**********************************************
btfsc STATE, RIGHTACTIVE ;is RF on?
goto ActiveR
bsf STATUS, RP0 ;bank 1
bsf TRISIO, TRIGGERR ;output off
bcf STATUS, RP0 ;bank 0
goto EndInterupt
ActiveR
btfss FLAGS, TRIGOUT ;controllertype
bcf GPIO, TRIGGERR ;determines
btfsc FLAGS, TRIGOUT ;output state
bsf GPIO, TRIGGERR
btfsc FLAGS, BURSTACTIVE
goto BurstfireR
btfss FLAGS, PULSEONR ;is pulse active?
goto OffstateR
decfsz PULSER, f ;if pulse is active, count down 10ms
goto EndInterupt
bcf FLAGS, PULSEONR ;10ms is over, clear flags and
bsf STATUS, RP0 ;bank 1
bsf TRISIO, TRIGGERR ;tristate output
bcf STATUS, RP0 ;bank 0
movf FREQR, w ;and reset pulse time.
movwf PULSER
btfss FLAGS, COD5MODE ;test COD5 mode
goto EndInterupt
bcf STATUS, C
rrf PULSER, f ;25% DUTYCYCLE for COD5 release
goto EndInterupt
OffstateR
decfsz DWELLTIMER, f ;count down dwell time to next pulse
goto EndInterupt
bsf FLAGS, PULSEONR ;if dwell is done turn on outputs
bsf STATUS, RP0 ;bank 1
bcf TRISIO, TRIGGERR ;and reset pulse time
bcf STATUS, RP0 ;bank 0
movf FREQR, w ;from frequency value
movwf DWELLTIMER
btfss FLAGS, COD5MODE ;test COD5 mode
goto EndInterupt
bcf STATUS, C
rrf DWELLTIMER, f
addwf DWELLTIMER, f ;75% dutycycle for pull
BurstfireR
;**************** Disparar en modo programacion
btfsc DESACTIVADO, 1
goto $+3
;**********************************************
btfss FLAGS2, BURSTONR
goto EndInterupt
btfsc FLAGS2, BDWELLRT ;burst fire
goto BurstDwellR
btfss FLAGS, PULSEONR ;is pulse active?
goto OffstateR2
decfsz PULSER, f ;if pulse is active, count down 10ms
goto EndInterupt
bcf FLAGS, PULSEONR ;10ms is over, clear flags and output
bsf STATUS, RP0 ;bank 1
bsf TRISIO, TRIGGERR ;and reset pulse time.
bcf STATUS, RP0 ;bank 0
movf FREQR, w
movwf PULSER
btfsc FLAGS, COD5MODE ;test COD5 mode
rrf PULSER, f
decfsz BURSTRight, f ;count down burst shots
goto EndInterupt
movf BURSTRATE, w ;reset counter
movwf BURSTRight
bsf FLAGS2, BDWELLRT ;signal dwell
movf FREQR, w
movwf DWELLTIMER ;reset dwell counter
btfss FLAGS, COD5MODE ;test COD5 mode
goto EndInterupt
bcf STATUS, C
rrf DWELLTIMER, f
addwf DWELLTIMER, f
goto EndInterupt
OffstateR2
decfsz DWELLTIMER, f ;count down dwell time to next pulse
goto EndInterupt
bsf FLAGS, PULSEONR ;if dwell is done turn on outputs
bsf STATUS, RP0 ;bank 1
bcf TRISIO, TRIGGERR ;and reset pulse time
bcf STATUS, RP0 ;bank 0
movf FREQR, w ;from frequency value
movwf DWELLTIMER
btfss FLAGS, COD5MODE ;test COD5 mode
goto EndInterupt
bcf STATUS, C
rrf DWELLTIMER, f
addwf DWELLTIMER, f
goto EndInterupt
BurstDwellR
btfss FLAGS, TRIGOUT
goto CGBurstR
btfss GPIO, TRIGGERR
goto EndInterupt
goto BurstResetR
CGBurstR
btfsc GPIO, TRIGGERR
goto EndInterupt
BurstResetR
bcf FLAGS, PULSEONR ;if dwell is done turn on outputs
bcf FLAGS2, BDWELLRT
bcf FLAGS2, BURSTONR
EndInterupt
bcf PIR1, TMR2IF
movf status_temp,w ; retrieve copy of STATUS register
movwf STATUS ; restore pre-isr STATUS register contents
swapf w_temp,f
swapf w_temp,w ; restore pre-isr W register contents
retfie ; return from interrupt
;
; HAY MUCHAS RUTINAS MAS AQUI PERO NO LAS PONGO PORQUE EL PROGRAMA ES LARGO Y NO LO VEO NECESARIO
;
main
clrf LEDActivado
clrf DATOSEE
clrf DIRECCIONEE
clrf DESACTIVADO
clrf TEMPFREE
clrf TEMPLED3EE
clrf TEMPLED3EEconta
clrf TEMPLED4EE
clrf TEMPLED4EEconta
clrf HAYCAMBIO
clrf TEMP
clrf TEMPFastForward
clrf TEMPFastRewind
clrf TEMPFast
; --------------------------
clrf GPIO
clrf FLAGS ;clear outputs and code status register
clrf FLAGS2
movlw 0x07
movwf CMCON0 ;comparators off
bsf STATUS, RP0 ;bank 1
movlw 0x0F ;start with 0-3 inputs (ADC read)
movwf TRISIO ;pin 0 & 1 & 5 out, all other inputs
movlw 0x51 ;Fosc/16, pin 0 analog
movwf ANSEL ;Right trigger is analog input
movlw 0x04
movwf WPU ;enable week pullups
bcf OPTION_REG, 7 ;enable all week pullups
bcf STATUS, RP0 ;bank 0
movlw 0x01 ;Left just, Vdd, channel 0
movwf ADCON0 ;module on.
movlw 0x10
movwf TEMP1
Wait4ADC
decfsz TEMP1, f
goto Wait4ADC ;acquisition time
bsf ADCON0, 1 ;start ADC
btfsc ADCON0, 1
goto $-1 ;wait for conversion to be done
movlw 0x40 ;about 0.5V from 2V ref
subwf ADRESH, w ;if f > W, C=1
btfsc STATUS, C ;ie- if trigger > 0.5V, then matrix
bsf FLAGS, TRIGOUT ;matrix config sources (output high)
bsf STATUS, RP0 ;bank 1
movlw 0x0C ;restore pin IO
movwf TRISIO
clrf ANSEL ;all digital now
bcf STATUS, RP0 ;bank 0
movlw 0x0A ;start with 10ms pulse width
movwf LEDBLINK ;for the LEDs
; movlw FIRERATE1 ;this will get overwritten,
movwf PULSER ; but start with a default value
movwf PULSEL
movwf DWELLTIMER
movwf DWELLTIMEL
movlw 0x05 ;init burst rate
movwf BURSTRATE
movwf BURSTRight
movwf BURSTLeft
; movf EESTATE, w
movlw 0x00
movwf DIRECCIONEE
call ReadEE ;get state value
movwf STATE
movlw 0x01
movwf T2CON ;timer 2, prescale 1:4, no postscale
bsf STATUS, RP0 ;bank 1
movlw 0xFA
movwf PR2 ;setup 1ms hardware interupt
bsf PIE1, TMR2IE ;enable timer2 interupt
bcf STATUS, RP0 ;bank 0
bsf T2CON, TMR2ON ;turn on timer2
movlw 0xC0
movwf INTCON ;turn on interupts globally
goto InitState ;get first fireratSTATE4e value
;******************** Y AHORA EL CODIGO DE LO QUE QUIERO HACER, QUE SE QUE LO ESTOY HACIENDO MAL, PERO NO SE QUE ***********************************************
;********************************* NO SE SI EL PROBLEMA ESTE EN ALGO RELACIONADO CON LAS INTERRUPCIONES *******************************************************
btfss STATE, LEFTACTIVE ;is RF on?
goto TestLeft
ActiveReloaded
btfss FLAGS, TRIGOUT ;controllertype
bcf GPIO, TRIGGERL ;determines
btfsc FLAGS, TRIGOUT ;output state
bsf GPIO, TRIGGERL
bcf FLAGS, PULSEONL ;10ms is over, clear flags and
bsf STATUS, RP0 ;bank 1
bsf GPIO, TRIGGERL ;tristate output
bcf STATUS, RP0 ;bank 0
; call Wait10ms
; bsf STATUS, RP0 ;bank 1
; bcf GPIO, TRIGGERL ;and reset pulse time
; bcf STATUS, RP0 ;bank 0
DemoraActiveReloaded
movlw 0x64
movwf BUTTONHOLD ;setup for 2 second hold
call Wait10ms
decfsz BUTTONHOLD, f
goto DemoraActiveReloaded
bcf FLAGS, PULSEONL ;10ms is over, clear flags and
bsf STATUS, RP0 ;bank 1
bsf GPIO, TRIGGERL ;tristate output
bcf STATUS, RP0 ;bank 0
call Wait10ms
bsf FLAGS, PULSEONL ;if dwell is done turn on outputs
bsf STATUS, RP0 ;bank 1
bcf GPIO, TRIGGERL ;and reset pulse time
bcf STATUS, RP0 ;bank 0