Shop OBEX P1 Docs P2 Docs Learn Events
SX48 Timers — Parallax Forums

SX48 Timers

JDOhioJDOhio Posts: 72
edited 2006-08-08 01:46 in General Discussion
I was reviewing the thread 'Reading the SX48 Timers" from Feb 2006 (http://forums.parallax.com/showthread.php?p=570291). I was curious about the concept of triggering one timer with the output of the other timer. In the thread, it says I could pulse the second timer during the interrupt handler of the first timer.

I was wondering if I could have the timers do this pulsing themselves by connecting one's Compare Output to the other's External Event Clock Source. What I was unsure about was what actually happens to the Compare Output. The documentation says it "toggles" the Compare Output. Is this a low-high-low transition?

Thanks,

Joe

Comments

  • BeanBean Posts: 8,129
    edited 2006-08-01 00:30
    Joe,
    What I was referring to in that post was just using an regular output pin to trigger the counter.
    What are you wanting to do ?
    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Cheap 4-digit LED display with driver IC·www.hc4led.com

    Low power SD Data Logger www.sddatalogger.com

    "You're braver than you believe, stronger than you seem, and smarter than you think" Christopher Robin to Pooh
    ·
  • JDOhioJDOhio Posts: 72
    edited 2006-08-01 00:41
    Thanks for the reply!

    I was thinking that I could set up both timers in Capture/Compare. I want to connect Timer 1's Compare Output to Timer 2's External Event Clock Source. Both of these are output pins (RB.6 and RC.3, respectively). When Timer 1 reaches my compare value, it should "toggle" RB.6, and Timer 2 should see that as a transition and increment its count. In this manner, I wouldn't have to have a counter in RAM that I maintain - I could set a compare value on Timer 2 and receive the interrupt when there is a match (but I digress).

    I wasn't sure if the transition on RB.6 is low to high, or low to high to low again. I wasn't sure what the documentation meant by "toggle".

    Joe

    1 Aug 2006
    Some corrections:
    RC.3 would have to be an input and Timer 2 would have to be set up in External Event Mode.

    Post Edited (JDOhio) : 8/1/2006 9:47:27 AM GMT
  • JDOhioJDOhio Posts: 72
    edited 2006-08-04 20:45
    I experimented with this idea.

    I set Timer 1 up in Capture/Compare mode. The Compare register is set to $ffff. When Timer 1 reaches $ffff, it toggles RB.6 (i.e., low-high-low). This pulse should happen approximately every 1.3 ms (20nS/pulse * 65536 pulses = 1.3 ms).
    I set Timer 2 up in External Event Mode. In this mode, Timer 2 counts pulses on RC.3. I physically connect RB.6 (from Timer 1) to RC.3 (the external input for Timer 2) and Timer 2 counts the number of times Timer 1 reaches $ffff. I set Timer 2's Compare register to $0bf0 (2288 decimal) and set up for an interrupt when the Compare register and Timer 2 match. The interrupt should occur approximately every three seconds (2288 * 1.3 ms=2.99 s).
    After the SX48 is programmed, there is a two minute time period in which nothing happens. I set a break point as indicated in the code and there is no interrupt. After two minutes, the interrupt begins to occur regularly but not at the designed time of three seconds. It happens about every six seconds.

    1. Why might there be a two minute delay before the code begins to operate as expected?
    2. Why does the interrupt occur every six seconds rather than every three seconds?

    Thanks,

    Joe

    DEVICE    SX48,OSCHS1
    FREQ    50000000
    
    INTERRUPT
        GOSUB DoSomething
    T2R1CMH=$08
    T2R1CML=$f0
    T1R1CMH=$ff
    T1R1CML=$ff
    
    RETURNINT
    
    AFlag VAR Byte
    
    PROGRAM Start
    DoSomething:
        IF AFlag=1 THEN    'break point set here
            AFlag=0
        ELSE
            AFlag=1
        ENDIF
    
    RETURN
    
    WATCH AFlag,8,UDEC
    
    Start:
    LVL_B=%00000000
    LVL_C=%00000000
    PLP_B=%01000000
    PLP_C=%00000111
    TRIS_B=%10111111    'RB.6 is an output
    TRIS_C=%11111000    
    
    RC=%00000000
    
    AFlag=1
    
    T2CNTA=%00000100    'interrupt on compare
    T2CNTB=%00100011    'timer unrelated,edge detection,divide by one,external clock
    T2R1CMH=$08
    T2R1CML=$f0
    '$08,$F0  This is every three seconds
    
    T1CNTA=%00000000    'disable interrupts on timer 1
    T1CNTB=%00000010    'timer unrelated,edge detection,divide by one,capture/compare
    T1R1CMH=$ff
    T1R1CML=$ff
    'RB.6 toggles
    
    Main:
        GOTO Main
    
    END
    
    
  • BeanBean Posts: 8,129
    edited 2006-08-06 14:08
    Joe,
    I'm away for the weekend. I'll try your code when I get back.

    I would just use Timer1 in PWM mode, and you'll need to clear Timer2 (I think that is why the long initial delay). You can use the new TIMER commands too.

    OUTPUT RB.6
    TIMER1 PWM, 32768, 65535
    T2CNTA = %00000100 ' Enable compare interrupts for Timer2
    TIMER2 EXTERNAL
    TIMER2 R1, $08F0
    TIMER2 CLEAR

    And all your should have to do in the interrupt is maybe "TIMER2 CLEAR". You don't need to reset R1.

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Cheap 4-digit LED display with driver IC·www.hc4led.com

    Low power SD Data Logger www.sddatalogger.com

    "You're braver than you believe, stronger than you seem, and smarter than you think" Christopher Robin to Pooh
    ·
  • BeanBean Posts: 8,129
    edited 2006-08-07 00:31
    Joe,
    · Here is a program that does what you were trying...

    DEVICE SX48,OSCHS1
    FREQ 50_000_000
    ' Connect RB.6 to RC.3 to connect the output of Timer1 to the clock of Timer2
    ' Should generate an interrupt every 3 seconds.
     
    AFlag VAR Bit
    
     
    WATCH AFlag,8,UDEC
    
    INTERRUPT
      GOSUB DoSomething
      TIMER2 CLEAR
      RETURNINT
    
    PROGRAM Start
     
    DoSomething:
    BREAK
      AFlag = ~AFlag             ' Toggle AFlag value
    RETURN
    
    Start:
      LVL_B=%00000000
      LVL_C=%00000000
      PLP_B=%01000000
      PLP_C=%00000111
      TRIS_B=%10111111           'RB.6 is an output
      TRIS_C=%11111000    
      RC=%00000000
      AFlag=1                    ' Set flag initially to 1
     
      ' Setup TIMER 2
      T2CNTA=%00000100           'Interrupt on compare
      TIMER2 EXTERNAL            'Timer 2 external exvent mode 
      TIMER2 R1, $08F0           'Interrupt when count is $08F0 or 2288
      TIMER2 CLEAR               'Clear timer 2
     
      ' Setup TIMER 1
      TIMER1 PWM, $8000, $FFFF   'Timer1 PWM mode ON for $8000 counts, period = $FFFF counts
     
    Main:
      GOTO Main
    END
    

    Bean.


    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Cheap 4-digit LED display with driver IC·www.hc4led.com

    Low power SD Data Logger www.sddatalogger.com

    "You're braver than you believe, stronger than you seem, and smarter than you think" Christopher Robin to Pooh
    ·
  • JDOhioJDOhio Posts: 72
    edited 2006-08-07 21:15
    Thanks!

    Resetting R1 is my misunderstanding. The documentation gave me the impression that when writing ANY of the timer registers, the timer is cleared. I tried this code and performs as you say. Wicked cool.

    How might I have cleared a timer in SX/B using SX-Key v3.10?


    Joe
  • BeanBean Posts: 8,129
    edited 2006-08-08 01:46
    Joe,
    There were "CLEART1" and "CLEART2" commands. These have been superceeded (sp?) by the "TIMER1 CLEAR" and "TIMER2 CLEAR" commands.

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Cheap 4-digit LED display with driver IC·www.hc4led.com

    Low power SD Data Logger www.sddatalogger.com

    "You're braver than you believe, stronger than you seem, and smarter than you think" Christopher Robin to Pooh
    ·
Sign In or Register to comment.