Menu

Count PWM pulses

Help
2017-07-31
2017-08-02
  • David Stephenson

    How do I count PWM pulses. Then switch on another pin after x pulses on (or near to) the rising edge.
    Essentially what I am looking for is an internal register that is "following" the PWM pulses.
    The chip is a 16F1788.

     
  • David Stephenson

    The timer2 to PR2 interrupt flag (PIR1.1) seemed promising, but for some reason is going at a different frequency (I don't know how this is possible).
    I've got the PWM at 400 kHz (period 2.5 us) and the interrupt flag is going at a rate of 500 kHz (2.0 us)

     
  • David Stephenson

    Just my poor coding being interpreted by the assembler in an unexpected way. PIR1.1 may well be producing an interrupt but it clears too fast to capture.
    Maybe the easiest way is to feed the PWM back into an input pin (not an elegant solution).

     
  • stan cartwright

    stan cartwright - 2017-08-02

    I can't think how except connecting to an input pin. Using software pwm in help it says- The PWM is only operational for the number of cycles stated in the calling method. So why try counting pwm?..out of interest.

     
  • William Roth

    William Roth - 2017-08-02

    There is no internal register that is holding the number of PWM pulses. The simplest method to count PWM pulses would be to connect the PWM out signal to either the T0CK1 or T1CK1 Pin and set up either Timer0 or Timer1 as a counter (External Clock Source)

    I would use a 200R to 1k series resister to connect the PWM OUT pin to the tmer CK input pin.

     

    Last edit: William Roth 2017-08-02
  • David Stephenson

    This has turned out more difficult than anticipated. The device I am sending the pulses specifies a minimum frequency of 400 kHz. This did not seem to be a problem at first after all I can go up to 32 MHz on the 16F1788, but when I finally did the maths 400 kHz rate only allows 20 instructions between pulses which is a bit tight when incrementing a counter testing the number of increments and maybe doing something else. I thought at one stage of using the clockout instead of the PWM, but then I would be limited to the chip clock divided by a power of 2.
    If anyone wants to make suggestions the device is an optical array Toshiba TCD1103. At the moment I am just trying to get the necessay pulses working, but I will have to think about an external ADC and maybe a FIFO at some stage.
    It may be an idea to move up to a 64MHz chip.

     
    • Anobium

      Anobium - 2017-08-02

      Investigate CLC and NCO. These are hardware and with very low latency. You could config CLC to count independent of your program activities.

       
  • David Stephenson

    Thanks for that it may be the way to go.

     
  • Anobium

    Anobium - 2017-08-02

    If you want. I can ask Microchip to provide us some insights on how to do this.

     
  • David Stephenson

    I think I am getting the timing, but only just mainly by writing ASM code (well maybe).
    Problem now is the device I am connecting to is pulling the logic level from the 16F1688 way down (is this normal?).Also the datasheet for the doide array (TCD1103) in the "suggested circuit" has logic inverters and I'm wondering if the logic is all inverted.

     
  • David Stephenson

    The low logic was a rogue piece of solder on the PCB. It now works and surprisingly can be slowed down well below what is in the datasheet so I might get away with using the internal ADC.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.