Input capture don't function correctly

General discussion on mikroBasic for dsPIC30/33 and PIC24.
Post Reply
Author
Message
vins72
Posts: 2
Joined: 26 Jun 2009 14:56

Input capture don't function correctly

#1 Post by vins72 » 26 Jun 2009 15:02

Dear Sir, sorry for my bad english, i have purchased a dsPicPro4 with dsPic30F6014A MCU.
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.
Thank at all for suggestion.

p.erasmus
Posts: 3391
Joined: 05 Mar 2009 10:28

#2 Post by p.erasmus » 27 Jun 2009 21:26

i don't understand why the interrupt fire every capture when in the IC1CON the bit are set for every 2 capture
You can not capture every second capture the device datasheet
specifies
(1) every rising and every falling edge - thus capture pulse width
(2) every Rising or every falling - Period or Frequency
(3) every fourth Rising or Falling -period/4
(4) every 16th Rising oo Falling -period /16
see the data sheet no second edge capturing
P.Erasmus
Saratov,Russia
--------------------------------------------------------------

vins72
Posts: 2
Joined: 26 Jun 2009 14:56

#3 Post by vins72 » 29 Jun 2009 07:40

Dear p.erasmus,

i don'w want capture on every second rising edge, i want an interrupt on every second capture (ICI Parameter)

ICI <1:0> - Select number of captures per interrupt bits
00 – interrupt on every capture event
01 – interrupt on every second capture event
10 – interrupt on every third capture event
11 – interrupt on every fourth capture event

Thank's again.

Vins

Skyline
Posts: 267
Joined: 10 Jan 2006 09:35

#4 Post by Skyline » 29 Jun 2009 10:14

Hi,

Your code looks ok for interrupt on every second rising edge capture, I would like to test it, but is it possible for you to explain how you know that the interrupt is on every rising edge capture instead of every second rising edge? Is it because Periodo = 0?

Also, is the original Input1CaptureInt routine longer than what you shown in the code above?

p.erasmus
Posts: 3391
Joined: 05 Mar 2009 10:28

#5 Post by p.erasmus » 29 Jun 2009 14:15

Yes you are correct looking at the Datasheet it is not shown every second capture however looking at the IC referance manual it is shown you
IC1CON value also seems to be correct ,
as skyline I also would like to know how do you know that it fires the interrupt every edge and not on the second edge detected
P.Erasmus
Saratov,Russia
--------------------------------------------------------------

Post Reply

Return to “mikroBasic for dsPIC30/33 and PIC24 General”