• AVR Freaks

Hot!Issue with Interrupt using PWM

Author
mpittman
Starting Member
  • Total Posts : 22
  • Reward points : 0
  • Joined: 2016/04/05 08:17:25
  • Location: 0
  • Status: offline
2020/09/23 13:46:02 (permalink)
0

Issue with Interrupt using PWM

Hello,
 
I'm using a PIC24FJ128GC010 with XC16 (v1.51) and MPLABX 5.4.
 
I've configured the Output Compare lines to run in PWM mode to drive a stepper motor controller.  I have the PWM output configured so that it is outputting a square wave at the frequency that I require to drive my stepper motor.
PWM is configured as edge aligned and appears to be working fine.  I can vary the PWM period and speed up or slow down the output / motor.
OC1CON1bits.OCTSEL = 0; // TMR2 as clock source
OC1CON2bits.SYNCSEL = 0x1F; // self
 
The issue I'm having is with the OC/PWM interrupt.  I'm counting interrupts to track the number of steps taken so I can determine when to stop.  However, the interrupts do not seem to be generated at the same freq as the PWM output?  For instance, if I set the PWM output freq to be 200 Hz (5ms per pulse), I get one full rotation per second of my motor which is correct since it is a 200 step per rotation motor.  In the interrupt I am counting pulses to determine when it makes 3 full rotations = 600 steps.  However, the motor make many more than 3 rotations, but when my motor stops, the interrupt reports that is only counted 600 pulses?  The 600 pulses would be correct, however the motor made many more than 3 rotations so it should have been more than 600 steps/pulses.
 
Am I wrong in thinking the PWM interrupt should be generated on each pulse output?  
Of course my first thoughts are that I have something configured incorrectly, but the motor speed control appears to be working just fine, it's just the counting of steps/pulses in the interrupt that appears to be not working.
 
Any suggestions?
#1

8 Replies Related Threads

    ric
    Super Member
    • Total Posts : 28660
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Issue with Interrupt using PWM 2020/09/23 15:37:12 (permalink)
    5 (1)
    I predict there is a bug somewhere in the code you have not shown.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #2
    mpittman
    Starting Member
    • Total Posts : 22
    • Reward points : 0
    • Joined: 2016/04/05 08:17:25
    • Location: 0
    • Status: offline
    Re: Issue with Interrupt using PWM 2020/09/24 05:33:03 (permalink)
    0
    That's quite possible, but I'm unable to find it.
     
    I guess what really has me stumped is the PWM output/motor speed control works just fine.  It's only the step counting in the interrupt that isn't working as expected.  I have a pin toggle set in the interrupt routine and when I scope it, it seems to be all over the place.  One interrupt period is 5ms and the next is 25ms?
     
    I guess I could post some of my code but that's not easily done in its current state.  I may have to create a new, simplified project to do so.
    #3
    ric
    Super Member
    • Total Posts : 28660
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Issue with Interrupt using PWM 2020/09/24 05:46:28 (permalink)
    4.5 (2)
    merlepittman
    ...
     I may have to create a new, simplified project to do so.

    That's always a good idea.
    Often just the process of creating it points you towards the problem.

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #4
    rpg7
    Super Member
    • Total Posts : 1416
    • Reward points : 0
    • Joined: 2003/11/07 12:47:35
    • Status: offline
    Re: Issue with Interrupt using PWM 2020/09/24 06:14:59 (permalink)
    0
    It may be that the postscaling is not set to 1.
    #5
    mpittman
    Starting Member
    • Total Posts : 22
    • Reward points : 0
    • Joined: 2016/04/05 08:17:25
    • Location: 0
    • Status: offline
    Re: Issue with Interrupt using PWM 2020/09/24 06:53:39 (permalink)
    0
    Postscaling?  Sorry I don't see anything about postscaling in the datasheet for the OC/PWM.  Can you clarify?
    #6
    rpg7
    Super Member
    • Total Posts : 1416
    • Reward points : 0
    • Joined: 2003/11/07 12:47:35
    • Status: offline
    Re: Issue with Interrupt using PWM 2020/09/24 07:46:09 (permalink)
    4 (1)
    merlepittman
    Postscaling?  Sorry I don't see anything about postscaling in the datasheet for the OC/PWM.  Can you clarify?



    Sorry - just a shot in the dark. on the pic16 & pic18 the timers for PWM have pre and post scaling.
    #7
    mpittman
    Starting Member
    • Total Posts : 22
    • Reward points : 0
    • Joined: 2016/04/05 08:17:25
    • Location: 0
    • Status: offline
    Re: Issue with Interrupt using PWM 2020/09/24 08:00:40 (permalink)
    0
    Just in case anyone is interested: 
    It appears that the OC/PWM interrupt was lagging and getting behind due to other interrupts.  I increased the priority of the OC/PWM interrupt and now it works fine.
     
    Thanks for the comments.
    #8
    Aussie Susan
    Super Member
    • Total Posts : 3769
    • Reward points : 0
    • Joined: 2008/08/18 22:20:40
    • Location: Melbourne, Australia
    • Status: offline
    Re: Issue with Interrupt using PWM 2020/09/24 19:18:37 (permalink)
    4.67 (3)
    While that is quite possible, my experience is that playing wth the priorities of any interrupt is unnecessary except in very unusual circumstances.
    What is far more likely is that you have an ISR that is spending more time than it should. If there is anything more than setting a few registers etc. in the ISR then you probably need to set a flag and do the processing in the main loop.
     You may well be nesting interrupts now and that can lead to its own set of problems if you are not careful and code accordingly.
    Susan
    #9
    Jump to:
    © 2020 APG vNext Commercial Version 4.5