• AVR Freaks

Hot!PIC12F1572 Timer 1 Problem

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

PIC12F1572 Timer 1 Problem

Hi. I can't seem to correctly configure Timer1 as a simple timer in a PIC12F1571 project intended to randomly modulate the brightness of some red Christmas tree lights to make them look like glowing embers.
 
A low voltage transformer drives an optocoupler to generate a sync pulse (external interrupt) at the zero crossing of each rectified A.C. half cycle (120 Hz). This interrupt simply turns the lights on via a triac optocoupler.
 
Timer0 is set up as a 100 mS interrupt which decrements a counter. When that counter reaches 0, one new pseudo random number is generated to determine the count of 100 mS interrupts until the next brightness change, and a second pseudo random number is generated to set the brightness level until the next change. Toggling RA5 at each occurrence of this interrupt yields the expected 5 Hz frequency.
 
Timer1 uses that brightness count to determine how long the lights stay on during the half cycle after the A.C. sync pulse... the greater the time, the brighter.
 
Using the T1CON register I've attempted to set the clock at Fosc/4 and a 1:8 prescale (500 KHz/4)/8 = 15,625 Hz. The Timer1 interrupt should be occurring at 120 Hz. However, setting the same RA5 toggle at the Timer1 interrupt service routine reveals that it gets there about once every 5 seconds!
 
I don't understand what I'm doing wrong. At 15,625 Hz my maximum possible interval between interrupts would be 4.2 seconds. I've tried to use the MPLABX simultator, but I can't seem to fully trust it.
 
I've uploaded my most recent .c file. Any advice would be appreciated.
#1

4 Replies Related Threads

    ric
    Super Member
    • Total Posts : 27627
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PIC12F1572 Timer 1 Problem 2020/05/16 19:37:03 (permalink)
    +1 (1)
    Btipf
    Timer0 is set up as a 100 mS interrupt which decrements a counter. When that counter reaches 0, one new pseudo random number is generated to determine the count of 100 mS interrupts until the next brightness change, and a second pseudo random number is generated to set the brightness level until the next change. Toggling RA5 at each occurrence of this interrupt yields the expected 5 Hz frequency.

    Why not use TMR2 for this?
    It is more suited for regular interrupts, with the built-in reload register.
     

    Using the T1CON register I've attempted to set the clock at Fosc/4 and a 1:8 prescale (500 KHz/4)/8 = 15,625 Hz.

    That all looks ok?

    The Timer1 interrupt should be occurring at 120 Hz.

    That is assuming the T1 count rolls over before the next external interrupt.
    Otherwise you are setting a new count before it rolls over.
    It looks like you're trying to debug too many variables at once.
    Get it going with just a short count first, then add the variable element.
     

    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
    Btipf
    Starting Member
    • Total Posts : 74
    • Reward points : 0
    • Joined: 2013/09/07 14:13:26
    • Location: 0
    • Status: offline
    Re: PIC12F1572 Timer 1 Problem 2020/05/17 00:05:13 (permalink)
    0
    Ric, thanks for your suggestions. I’ll look int Timer2, and I’ll see if I can figure out a way to determine if Timer1’s value is staying within expected limits without relying on the simulator. I like your idea to force Timer1 to be artificially set up with known values near overflow (close to 2^16 - 1) to make sure that works before attempting to use pseudo random values.
    #3
    Btipf
    Starting Member
    • Total Posts : 74
    • Reward points : 0
    • Joined: 2013/09/07 14:13:26
    • Location: 0
    • Status: offline
    Re: PIC12F1572 Timer 1 Problem 2020/05/17 08:15:18 (permalink)
    0
    So, I artificiality forced Timer1 to reload with 65,481 which should cause the interrupt to occur at about the middle of the next half sine wave cycle. No change. Instead of an expected 120 Hz Timer1 interrupt rate, I get one interrupt once every 5 seconds or so.

    It’s as if I’ve inadvertently selected the wrong clock source (a super slow one) for Timer1.
    #4
    Btipf
    Starting Member
    • Total Posts : 74
    • Reward points : 0
    • Joined: 2013/09/07 14:13:26
    • Location: 0
    • Status: offline
    Re: PIC12F1572 Timer 1 Problem 2020/05/17 12:02:13 (permalink)
    0
    I still cannot get Timer1 to run:

    T1CONbits.TMR1CS = 0b00;
    T1CONbits.T1CKPS = 0b11;
    T1CONbits.nT1SYNC = 1;
    T1CONbits.TMR1ON = 1;
    T1GCONbits.TMR1GE = 0;
    INTCONbits.PEIE = 1;
    PIE1bits.TMR1IE = 1;
    INTCONbits.GIE = 1;

    If anyone sees anything I’ve missed to get Timer1 to run and generate an interrupt on a PIC12F1572, please let me know.
    Thanks!
    #5
    Jump to:
    © 2020 APG vNext Commercial Version 4.5