• AVR Freaks

Hot!Unexpected PWM Module Behavior

Author
Btipf
Starting Member
  • Total Posts : 74
  • Reward points : 0
  • Joined: 2013/09/07 14:13:26
  • Location: 0
  • Status: offline
2020/05/24 18:02:40 (permalink)
0

Unexpected PWM Module Behavior

Hi, I'm attempting to use a PIC12F1572 to make four strings of red and orange LED light strings flicker independently and in pseudo-random fashion to look like glowing embers. Please see my attached code.
 
The approach is one commonly found on the Internet in which an A.C. line is fed into a step down transformer. That low-voltage output is fed into a full-wave bridge rectifier and into an optoisolator to generate a 120 Hz sync pulse. This pulse interrupts the PIC to turn all light strings off and initiate timers with pseudo-random values. When each of those timers time out they interrupt the PIC to turn on their respective light string at the currently-specified time within each 120 Hz half cycle. The earlier in the cycle it's turned on, the brighter the lights appear. Four counters in the main program are used to keep track of how long each string has been on. As each counts down to zero the program calculates a new pseudorandom brightness count and interval time for its associated light string.
 
I'm using the Timer0 module to generate a periodic 100 mS interrupt for the main program, and I'm using Timer1 for the first light string. That much finally seems to be working fine.
 
However, I'm now attempting to set up PWM3 as the first of three PWM modules to function as 16-bit timers to control brighness for the other three light strings. I'm attempting to use PWM3 in Standard Mode to generate an interrupt when its Period Register is matched.
 
Not only am I having trouble figuring out how to get PWM3 to work as a simple timer, the opto-isolator signal (on RA4) for the first string's triac (controlled by Timer1) is inexplicably replicated on RA5 which is the source of the signal for the fourth string's triac driver(!). My first suspicion was a short, but I measure 4 Mohms between these two pins. Replacing the PIC with a new one yielded the same results.
 
So... I'd really appreciate some help understanding how to set up PWM3, PWM2, and PWM1 to function like Timer1 (e.g. do I need to use the LDA bit to load the PWM registers? What order should I change the PWM registers & control bits? Can I stop/start each timer by clearing/setting its enable bit?). For my sanity's sake, I'd also appreciate knowing how my RA4 signal is being replicated on RA5 - which seems to completely ignore changes I make to PWM3's timer load values... probably because I don't know what the heck I'm doing with these PWM modules.
 
Thanks!
 
 
#1

6 Replies Related Threads

    ric
    Super Member
    • Total Posts : 27595
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: Unexpected PWM Module Behavior 2020/05/24 19:41:01 (permalink)
    0
    Throw away the prototype for the ISR. It's not needed, and pointless.
    I'm curious why you use "while()" rather than "if()" for your tests inside the ISR.
    Why are you setting "PWM3TMR = PWM3PR;" in the ISR? Won't that cause an immediate match?
    I would have expected you to clear it to zero.
    (I've not used this PIC yet, so may have also misunderstood the datasheet...)
     

    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
    RISC
    Super Member
    • Total Posts : 5731
    • Reward points : 0
    • Status: online
    Re: Unexpected PWM Module Behavior 2020/05/25 03:27:08 (permalink)
    0
    Hi,
    There is a good example of PIC12F1572 PWM usage in this RGB badge reference design 
    Regards
    #3
    Btipf
    Starting Member
    • Total Posts : 74
    • Reward points : 0
    • Joined: 2013/09/07 14:13:26
    • Location: 0
    • Status: offline
    Re: Unexpected PWM Module Behavior 2020/05/25 10:57:24 (permalink)
    0
    ric
    Throw away the prototype for the ISR. It's not needed, and pointless.
    I'm curious why you use "while()" rather than "if()" for your tests inside the ISR.
    Why are you setting "PWM3TMR = PWM3PR;" in the ISR? Won't that cause an immediate match?
    I would have expected you to clear it to zero.
    (I've not used this PIC yet, so may have also misunderstood the datasheet...)
     


    Thanks for reviewing my code, Ric. I took your advice and eliminated the ISR prototype and changed "while()'s" to "if()'s"... though I don't think either of us expected these modifications to fix any of my problems.
     
    Good catch about setting PWM3TMR to 0 vs. PWM3PR. That probably comes from thinking about the PWMx timers as a regular timer in which one loads a value such that an interrupt is triggered when the timer overflows to zero.
     
    This did result in a slight change in my RA5 output. Now instead of matching the output of RA4 (for which I've no explanation), about 20% of the time it now generates a very brief pulse immediately following the A.C. line start-of-cycle sync pulse before returning to ground (for which I also have no explanation). Changing the PWM3 period register values still has no effect on the RA5 output.
     
    I really think I'd have this problem licked if I could just figure out - as the PIC12F1572's datasheet so tantalizingly suggests - how to configure PWMx modules to function like a conventional timer. I haven't found any examples of doing this on this forum or on Microchip's web site or via Google. Perhaps I'm not searching for the right key word(s).
     
    If it simply required setting PWMx for Standard Mode, enabling the right interrupts (e.g. PWMxPR), clearing PWMxTMR, setting PWMxPR to the timeout count and enabling PWMx, I don't think I'd be writing this. The added complication is setting the LDA bit to load the PWMx registers from the double buffers. Perhaps I'm doing this incorrectly.
     
    #4
    Btipf
    Starting Member
    • Total Posts : 74
    • Reward points : 0
    • Joined: 2013/09/07 14:13:26
    • Location: 0
    • Status: offline
    Re: Unexpected PWM Module Behavior 2020/05/25 11:05:16 (permalink)
    0
    RISC
    Hi,
    There is a good example of PIC12F1572 PWM usage in this RGB badge reference design 
    Regards



    Hi, RISC.
     
    Thanks for the helpful suggestion. The RGB example is at least one order of magnitude more sophisticated than my current programming skill, but based upon the PWM module code I found (e.g. RGBChroma.c) it seemed to use these modules in their conventional PWM role vs. as timers. If there's a particular file in the zipped suite you especially recommend I study, please let me know. I may have overlooked it.
     
    Thanks again!
    #5
    Btipf
    Starting Member
    • Total Posts : 74
    • Reward points : 0
    • Joined: 2013/09/07 14:13:26
    • Location: 0
    • Status: offline
    Re: Unexpected PWM Module Behavior 2020/05/26 22:00:14 (permalink)
    0
    Here's an unexpected update...
     
    Yesterday I (finally!) got my second light dimmer circuit (the first to use a PWM module as a timer) working as intended. I thought now the remaining challenge would be limited to correctly replicating the tested code with the remaining two PWM modules.
     
    But before I even programmed the code for the third dimmer circuit, tonight I discovered that the only instances in which the first PWM module works correctly are immediately after Real ICE has programmed the device. If I power down the circuit and then power it back up again, the dimmer function which uses the first PWM module as a timer fails to start... its output remains flat-lined at ground - indicating that the associated PWMx module has not generated an interrupt... presumably because the LDA bit hasn't been set correctly.
     
    As soon as I reprogram the device with the exact same code, the module starts working again as intended.
     
    I'd really appreciate any possible explanations for this behavior as well has how to set up these PWM modules as simple timers - setting the module's LDA bit to arm the load at the end of the current period.
     
    Thanks!
    #6
    Btipf
    Starting Member
    • Total Posts : 74
    • Reward points : 0
    • Joined: 2013/09/07 14:13:26
    • Location: 0
    • Status: offline
    Re: Unexpected PWM Module Behavior 2020/05/29 07:56:24 (permalink)
    0
    Hi. The attached file reflects my best effort to date to get several pseudo-randomly modulated light dimmer circuits working from a single PIC12F1572. The Timer1-driven light continues to work reliably, but while the PWMx-driven circuits work immediately after programming they fail to resume working after I've cycled circuit power.
     
    I've never seen this type of behavior before, and I have no explanation. I've connected a scope set up to capture a single trace when the PWM3-timed Light4 output at RA5 rises to turn on its light string. The scope doesn't even trigger once after cycling circuit power. I suppose it could be a POR problem, but I haven't seen this in a similar prior PIC12F1571 application which used the PWM3 module to control a servo.
     
    Interestingly, commenting out the "PWM3TMR = 0;" line at the end of the interrupt service routine allows the program to run after cycling power, but without the ability to reset PWM3's timer, the light string turns on and off erratically.
     
    I'd appreciate any thoughts on how to get the process running as expected after cycling power.
     
    Thanks!
    #7
    Jump to:
    © 2020 APG vNext Commercial Version 4.5