I wrote the attached souce file and i don't understand why the interrupt fire every capture when in the IC1CON the bit are set for every 2 capture.
The code :
Code: Select all
program PWM_Encoder
' Variabili controllo PWM
dim FrequenzaPwm as word
dim PassoDuty as byte
dim pwm_period as word
dim pwmStep as word
dim pwmDuty as word
dim Duty as word
dim strDuty as string[5]
' Variabili Lettura RPM
Dim T1 as word
Dim T2 as word
dim Periodo as word
Dim strPeriodo as string[5]
sub procedure Input1CaptureInt org $16
T2 = IC1BUF
T1 = IC1BUF
IFS0.1 = 0 ' Cancella il bit di interrupt flag IC1IF (IFS<1>)
If T2 > T1 then
Periodo = T2 - T1
else
Periodo = (PR2 - T1) + T2
end if
end sub
Sub Procedure InitDisplay
Lcd_Init_dsPICPRO3() ' Inizializzazione display
Lcd_Cmd(LCD_CURSOR_OFF) ' Cursore off
Lcd_Out(1, 1, "PWM %")
Lcd_Out(2, 1, "Rpm")
end sub
Sub Procedure AggiornaDuty
if Duty = 100 then
pwmDuty = pwm_period
else
pwmDuty = Duty * pwmStep
end if
Pwm_Set_Duty(pwmDuty, 1)
' Aggiorna display
wordtostr(Duty,strDuty)
Lcd_Out(1, 7, strDuty)
End Sub
main:
' Impostazione porte
ADPCFG = $FFFF
TRISA.6 = 1 ' Pulsante Avvio
TRISA.7 = 1 ' Pulsante incremento PWM
TRISA.9 = 1 ' Pulsante decremento PWM
TRISA.10 = 1 ' Pulsante Stop
TRISD.8 = 1 ' Ingresso Lettura esensore giri
PR2 = $FFFF ' PR2 Registro del periodo di Timer2
T2CON = $0000
TMR2 = $0000
T2CON = $8010
IC1CON = $00A0
IC1CON = $00A3
IPC0 = IPC0 or $0010 ' Imposta la priorità dell'interrupt
IFS0.1 = 0 ' Cancella il bit di interrupt flag IC1IF
IEC0.1 = 1 ' Abilita l'interrupt per IC1
InitDisplay ' Inizilizza il display
'Uart1_Init(19200) ' Inizilizza la seriale
FrequenzaPwm = 1000
PassoDuty = 5
T2 = 0
T1 = 0
Periodo = 0
' Impostazioni PWM
pwm_period = Pwm_Init(FrequenzaPwm, 1, 1, 3)
Duty = PassoDuty
pwmStep = pwm_period div 100
Pwm_Init(FrequenzaPwm, 1, 1, 3)
AggiornaDuty
while(true)
wordtostr(periodo, strPeriodo)
Lcd_Out(2, 7, strPeriodo)
if Button(PORTA, 6, 100, 1) = 1 then
Pwm_Start(1)
end if
if Button(PORTA, 7, 100, 1) = 1 then
Duty = Duty + PassoDuty
if Duty >= 100 then
Duty = 100
end if
AggiornaDuty
end if
if Button(PORTA, 9, 100, 1) = 1 then
Duty = Duty - PassoDuty
if Duty <= PassoDuty then
Duty = PassoDuty
end if
AggiornaDuty
end if
if Button(PORTA, 10, 100, 1) = 1 then
Pwm_Stop(1)
end if
wend
end.