• AVR Freaks

Hot!How to read PWM Frequency and ON time

Author
dj_dan23
Starting Member
  • Total Posts : 52
  • Reward points : 0
  • Joined: 2014/12/03 17:25:22
  • Location: 0
  • Status: offline
2018/11/06 05:49:50 (permalink)
0

How to read PWM Frequency and ON time

I have to read a PWM coming from a car solenoid that may have a 1000hz or less in frequency and I'd like to be able to read this frequency and the ON time, with interrupt, cause I have other things to do like i2c for RTC Chronodot and EEPROM, and RS232 to print on the computer screen, already running, for now. I have a PIC32MX470F512L (USB Starter Kit3) running a SYSCLK at 96Mhz and the PBCLK at 12Mhz from 8Mhz primary Oscillator using XC32, connected to a i/o expansion DN320002. While reading on the net I haven't been able to find a simple way to do both at the same time, meaning find the frequency and the ON time of the PWM.  I'm aware of the 12Vdc to 3.3Vdc step down. So if anyone have an idea on how to do this, that may be helpful to others too.
Many thanks to all.
#1

15 Replies Related Threads

    Mysil
    Super Member
    • Total Posts : 3325
    • Reward points : 0
    • Joined: 2012/07/01 04:19:50
    • Location: Norway
    • Status: offline
    Re: How to read PWM Frequency and ON time 2018/11/06 06:40:50 (permalink)
    0
    Hi,
    PIC32 microcontroller have a Input Capture peripheral that should be suitable to capture the times of transition in a digital signal.
    Don't just read webposts of other projects, also study the datasheet of the MX470 you are using,
    and the Family Reference Manual section for the peripheral.
     
    Be aware of possible noise or transients in the input signal, some hardware and/or software filtering may make sense.
     
       Mysil
    post edited by Mysil - 2018/11/06 06:49:42
    #2
    dj_dan23
    Starting Member
    • Total Posts : 52
    • Reward points : 0
    • Joined: 2014/12/03 17:25:22
    • Location: 0
    • Status: offline
    Re: How to read PWM Frequency and ON time 2018/11/06 06:54:38 (permalink)
    0
    I' aware of the IC but how to use it to read the frequency AND the ON time in the same interrupt?
    That's my concern right now.grin: grin
     
     
    #3
    Mysil
    Super Member
    • Total Posts : 3325
    • Reward points : 0
    • Joined: 2012/07/01 04:19:50
    • Location: Norway
    • Status: offline
    Re: How to read PWM Frequency and ON time 2018/11/06 07:15:40 (permalink)
    0
    Hi again,
    You don't read the frequency and on-time in the same interrupt!
    You record the timestamp of the transition in a private static variable, and leave the interrupt service code.
    The next time there is a transition recorded, you Calculate Period time and/or On time, based on previous values, and the recent timestamp value. 
     
    There are many options, and FIFO buffer in the IC hardware.
    If the FIFO buffer is active, make sure to read until the buffer is empty.
     
    It may be easier to use two separate IC peripherals to capture the same signal, one to record rising edges,
    and another to capture falling edges. In this case, both IC peripherals should be clocked from the same timer.
     
       Mysil
    #4
    dj_dan23
    Starting Member
    • Total Posts : 52
    • Reward points : 0
    • Joined: 2014/12/03 17:25:22
    • Location: 0
    • Status: offline
    Re: How to read PWM Frequency and ON time 2018/11/06 07:38:57 (permalink)
    0
    I will look for the last one, "two separate IC". Seems easier to implement.
    If I may, can you elaborate more on this one, especially the "same timer" .
    Does it mean that it will have two interrupt ?
     
    Thanks for you help.
     
    #5
    Jim Nickerson
    User 452
    • Total Posts : 6022
    • Reward points : 0
    • Joined: 2003/11/07 12:35:10
    • Location: San Diego, CA
    • Status: offline
    Re: How to read PWM Frequency and ON time 2018/11/06 07:51:57 (permalink)
    0
    Maybe set ICM to Capture every edge ( rising and falling )
    And set FEDGE to capture rising edge first.
    The first and every other interrupt would be the rising edge.
    #6
    dj_dan23
    Starting Member
    • Total Posts : 52
    • Reward points : 0
    • Joined: 2014/12/03 17:25:22
    • Location: 0
    • Status: offline
    Re: How to read PWM Frequency and ON time 2018/11/06 09:04:25 (permalink)
    0
    I'll take a look at this option too, soon.
    I must first experiment with ICM first.
    Thanks.
    #7
    dj_dan23
    Starting Member
    • Total Posts : 52
    • Reward points : 0
    • Joined: 2014/12/03 17:25:22
    • Location: 0
    • Status: offline
    Re: How to read PWM Frequency and ON time 2018/11/07 09:50:59 (permalink)
    0
    After experimenting with IC I hookup a 1hz from an RTC, it seems to work properly except for the timing.
    The interrupt just load the FIFO value and print it on the computer screen. Here's what it tell for each "rising edge".
    03146-10362-17577-24792-32008-39223-46438-53654-60869-02548-09764 and so on. Around 7215 timer 2 count per second. So it appear that the clock for timer 2 seems to be 1/7215 = 7.246Khz or 138us The 12Mhz on PBCLK seems right using "SALEAE logic 16" that give 1.000009s using Prescaler = 256 & PR=46873.  Or I may not using the right math or setup. Anyone have an idea about that 7215 timer2 count for one second. Here what I'm using:
     
    void Initialize_PWM_Int_Tim2 (void)
      {
         ANSELBCLR = 0x0004;    // RB2 = DIGITAL
          TRISBSET = 0x0004;    // RB2 = INPUT
        
           ODCBCLR = 0x0004;    // Enable open drain on RA14 & RA15
          CNPUBCLR = 0x0004;    // Enable weak pullup
          CNPDBCLR = 0x0004;    // Enable weak pulldown
         CNCONBCLR = 0x0004;    // Enable Change Notice Control For Portx
          CNENBCLR = 0x0004;    // Enable change notification
          
          
       // ----------------------------------.---------------------------------------
       // ------------------------- INITIALIZE TIMER 2 -----------------------------
       // --------------------------------------------------------------------------
                                // Tpbclk = 12Mhz
       T2CON = 0x0000;          // Stop TIMER2 ON = 0 Bit 15
                                // and set Prescaler to 1:1 TCKPS = 0 Bit 6-5-4
                                // and set Clock from Tpblck TCS = 0 Bit 1
                                // TGATE = 0 Bit 7
                                // and set Mode to 16 bits T32 = 0 Bit 3

       TMR2 = 0;                // Clear timer 2
       PR2=0xffff;              // Set Preset to MAX
       T2CONbits.ON = 1;        // Start Timer 2
     
        
            
        
       // ----------------------------------.---------------------------------------
       // --------------------- INITIALIZE INPUT CAPTURE ---------------------------
       // --------------------------------------------------------------------------
        
        
       IC1CONbits.FEDGE = 1;    // Bit9 = 1= Capture rising edge first
       IC1CONbits.C32 = 0;      // Bit8 = 0= Timer in 16 bits mode
       IC1CONbits.ICTMR = 1;    // Bit7 = 0= Timer 3 is the counter source
                                // 1= Timer 2 is the counter source
       
       IC1CONbits.ICI = 0;      // Bit6-5 = 0= Int on every Capture Event
                                // 1= Int on every second capture event
                                // 2= Int on every thirs capture event
                                // 3= Int on every fourth capture event
       
       IC1CONbits.ICM = 3;      // Bit2-0 = 0= Disable
                                // 1= Every edge (Rising&Falling)
                                // 2= Every falling
                                // 3= Every rising
                                // 4= Prescale Every fouth rising
                                // 5= Prescale Every sixteen rising
                                // 6= Every edge specified by FEDGE
                                // 7= Intrrupet only in SLEEP MODE
      }



       // ----------------------------------.---------------------------------------
       // ----------------------- INITIALIZE INTERRUPT -----------------------------
       // --------------------------------------------------------------------------
    void Initialize_PWM_Int (void)
        {
         __builtin_disable_interrupts(); // Disable INT at CPU
        
         SYSTEM_RegUnLock();             // Unlock PPS
          CFGCONbits.IOLOCK = 0;
           IC1Rbits.IC1R = 7;            // Input Capture IC1 -> RB2
          CFGCONbits.IOLOCK = 1;
         SYSTEM_RegLock();               // Lock PPS
          

         mPWM_Timer2_IntFlagClr();       // #define mPWM_Timer2_IntFlagClr() (IFS0CLR = 1 << 6)

         IPC1bits.IC1IP = 5;             // IPC1SET = 5 << 12
         IPC1bits.IC1IS = 0;             // 12-11-10  9-8
                                         // 1  0  1   0 0
                                         // -------   ----
                                         //  Pri=5    Sub=0
        
         mPWM_Timer2_IntEna();           // #define mPWM_Timer2_IntEna() (IEC0SET = 1 << 6
        
         __builtin_enable_interrupts();  // Enable INT at CPU
      
       }
     
     

     void __ISR(5,IPL5AUTO) PWM_Int_Tim2(void)
       {
         memset(gPWMBuffer, 0, sizeof (gPWMBuffer));
         unsigned char pass=0;
         
         while (IC1CONbits.ICBNE)
          { gPWMBuffer [pass++] = IC1BUF;
            HexToBCD_16Bits(gPWMBuffer[pass-1],5);
            RS232_Write_Console_PWM_16Bits_Array(5);
          }
        

         mPWM_Timer2_IntFlagClr();     // (IFS0CLR = 1 << 6) IFS0bits.IC1IF = 0
     
        }
       
       

     
     
     
     
     
    #8
    pcbbc
    Super Member
    • Total Posts : 1099
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: online
    Re: How to read PWM Frequency and ON time 2018/11/07 10:32:54 (permalink)
    0
    Seems to me you don't know how many times timer 2 has rolled over in that 1 second.
    Trying to work back from the interval to the clock frequency is meaningless unless you know that hasn't happened.
     
    A 12MHz clock would give 12,000,000 = 0xB71B00 ticks in 1 second.  However you lose the 0xB7, as the timer 2 value is only 16 bits, and the 0x1B00 remaining is very close to the 0x1C30 you are measuring.
    #9
    crosland
    Super Member
    • Total Posts : 1582
    • Reward points : 0
    • Joined: 2005/05/10 10:55:05
    • Location: Bucks, UK
    • Status: offline
    Re: How to read PWM Frequency and ON time 2018/11/07 10:33:57 (permalink)
    0
    One of the Microchip "Tips and Tricks" booklets explains how to do this.
     
    #10
    dj_dan23
    Starting Member
    • Total Posts : 52
    • Reward points : 0
    • Joined: 2014/12/03 17:25:22
    • Location: 0
    • Status: offline
    Re: How to read PWM Frequency and ON time 2018/11/07 13:43:24 (permalink)
    0
    I didn't see it coming, the 32 bit register and the rollover.
    This for this  advice, I will study this one, right now. Smile: Smile
     
    #11
    dj_dan23
    Starting Member
    • Total Posts : 52
    • Reward points : 0
    • Joined: 2014/12/03 17:25:22
    • Location: 0
    • Status: offline
    Re: How to read PWM Frequency and ON time 2018/11/07 13:45:57 (permalink)
    0
    Ohhhhh!!! that should be useful, but do you have a narrower serach cause in the
    main page, there is "Results 1 - 10 of 27740 for "Tips and Tricks booklet"
    Thanks.
     
    #12
    crosland
    Super Member
    • Total Posts : 1582
    • Reward points : 0
    • Joined: 2005/05/10 10:55:05
    • Location: Bucks, UK
    • Status: offline
    Re: How to read PWM Frequency and ON time 2018/11/07 15:38:48 (permalink)
    0
    How about http://bfy.tw/Kkh3
     
    #13
    dj_dan23
    Starting Member
    • Total Posts : 52
    • Reward points : 0
    • Joined: 2014/12/03 17:25:22
    • Location: 0
    • Status: offline
    Re: How to read PWM Frequency and ON time 2018/11/07 15:58:18 (permalink)
    0
    Thanks for the tip. Is there a way to count the rollover times while using the IC without disturbing the official count.
    Meaning,a way of using only one timer instead of two and counting the rollover in the math for the end count.
     
    #14
    qhb
    Superb Member
    • Total Posts : 9998
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: How to read PWM Frequency and ON time 2018/11/07 19:01:51 (permalink)
    0
    Enable rollover interrupts for the timer that the IC is using.
     

    Nearly there...
    #15
    dj_dan23
    Starting Member
    • Total Posts : 52
    • Reward points : 0
    • Joined: 2014/12/03 17:25:22
    • Location: 0
    • Status: offline
    Re: How to read PWM Frequency and ON time 2018/11/08 05:52:18 (permalink)
    0
    I never had the idea of using the timer interrupt while using the IC.
    I was in the impression that IC lockout the timer interrupt.
     
    Thanks for that tip.
    Glad to have you all to help us. Smile: Smile
    #16
    Jump to:
    © 2019 APG vNext Commercial Version 4.5