program pgrm2
'include "P12F629def.pbas"
Symbol min_frequency = 6
Symbol max_frequency = 248
symbol bobine = GP5
symbol lampe = GP4
symbol inter0 = GP0
symbol inter1 = GP1
symbol inter2 = GP2
symbol inter3 = GP3
Dim savtmr As Byte
dim calclampe as byte
dim etatlampe as byte
sub procedure Interrupt
if INTCON.T0IF = 1 then
' interruption zero
GPIO.bobine = not GPIO.bobine ' inverse letat de la bobine
INTCON.T0IF=0 ' set t0ie clear t0if
TMR0 = savtmr
end if
if PIR1.TMR1IF = 1 then
' interruption un, change la valeur de timer0 entre 6 et 156 (prescaler off)
' toutes les 0.5 seconde environ. donne une frequence entre 1 et 10 KHz
savtmr = savtmr+1
if TMR0>max_frequency then
savtmr=min_frequency
end if
TMR0 = savtmr
PIR1.TMR1IF=0
end if
end sub
sub procedure lectureinter
' lecture de letat des interrupteurs et allumage eventuel de la lampe
calclampe = testbit(GPIO, inter0)+testbit(GPIO, inter1)+testbit(GPIO, inter2)+testbit(GPIO, inter3)
if calclampe<>etatlampe then
GPIO.lampe = not GPIO.lampe ' inverse letat de la lampe
etatlampe = calclampe ' stocke letat des inters pour la suite
end if
end sub
main:
' calibrage de l'oscillateur
OSCCAL = 0x74 ' for the linker to know osccal !!!
' asm
' bsf STATUS,RP0
' call 0x03ff
' movwf OSCCAL
' bcf STATUS, RP0
' end asm
' disable pullups
setbit(option_reg,NOT_GPPU)
clearbit(WPU,0)
clearbit(WPU,1)
clearbit(WPU,2)
clearbit(WPU,3)
' disable comparator
setbit(CMCON,CM0)
setbit(CMCON,CM1)
setbit(CMCON,CM2)
' initialisation des entrees sorties
setbit ( TRISIO, inter0) ' inter en entree
setbit ( TRISIO, inter1)
setbit ( TRISIO, inter2)
setbit ( TRISIO, inter3)
clearbit ( TRISIO, lampe) ' lampe et bobine en sortie
clearbit ( TRISIO, bobine)
clearBit(GPIO, lampe) ' lampe eteinte au debut
etatlampe = testbit(GPIO, inter0)+testbit(GPIO, inter1)+testbit(GPIO, inter2)+testbit(GPIO, inter3)
INTCON.GIE = 0 ' Disable global interrupts
While INTCON.GIE = 1 ' Make sure they are off
INTCON.GIE = 0 ' Continue to clear GIE
Wend ' Exit when GIE is clear
' initialisation des timers pour interruptions
' Timer0 Registers:
' Prescaler=1:4; TMR0 Preset=6; Freq=1 000,00Hz; Period=1 000,00 µs
OPTION_REG.T0CS = 0 ' bit 5 TMR0 Clock Source Select bit:0=Internal Clock (CLKO) / 1=Transition on T0CKI pin
OPTION_REG.T0SE = 0 ' bit 4 TMR0 Source Edge Select bit: 0=low/high / 1=high/low
OPTION_REG.PSA = 0 ' bit 3 Prescaler Assignment bit: 0=Prescaler is assigned to the Timer0
OPTION_REG.PS2 = 0 ' bits 2-0 PS2:PS0: Prescaler Rate Select bits
OPTION_REG.PS1 = 0
OPTION_REG.PS0 = 0
OPTION_REG.NOT_GPPU = 1
TMR0 = min_frequency
savtmr = min_frequency
INTCON.T0IE = 1 ' Enable TMRO interrupt
' Timer1 Registers:
' Prescaler=1:8; TMR1 Preset=3036; Freq=2,00Hz; Period=500,00 ms
T1CON.T1CKPS1 = 1 ' bits 5-4 Prescaler Rate Select bits
T1CON.T1CKPS0 = 1
T1CON.T1OSCEN = 1 ' bit 3 Timer1 Oscillator Enable Control: bit 1=on
T1CON.NOT_T1SYNC = 1 ' bit 2 Timer1 External Clock Input Synchronization Control bit: 1=Do not synchronize external clock input
T1CON.TMR1CS = 0 ' bit 1 Timer1 Clock Source Select bit: 0=Internal clock (FOSC/4) / 1 = External clock from pin T1CKI (on the rising edge)
T1CON.TMR1ON = 1 ' bit 0 enables timer
TMR1H = $0B ' preset for timer1 MSB register
TMR1L = $DC ' preset for timer1 LSB register
PIE1.TMR1IE = 1 ' enable interrupt on timer 1
INTCON.GIE = 1 ' autorise GIE les interrupts sur le pic
_loop:
lectureinter
goto _loop
end.