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

Reading the SX48 Timers ?

BeanBean Posts: 8,129
edited 2006-02-14 08:06 in General Discussion
This is my first program using the on-board 16 bit timers on the SX48.

I can read RTCC at any time, but it seems the timers must have some external event to "capture" the count in the timer.

Am I missing something or is it just not possible to read the timers on the SX48 ?

[noparse][[/noparse]edit]

Here is what I want to do:

· I have a SX48 running at 10Mhz. I have a 30Khz signal that I want to measure.
· I want to count "how many 10MHz clocks occur during 1000 cycles of the 30KHz clock" of course it will be very close to 333333 and I know the 16 bit counter will overflow, I don't care I just want the count that's left. Which would be 5653 if all the clocks were "perfect".

· My idea was to have one counter count the 30KHz pulses and generate an interrupt when the count reaches 1000. In the interrupt I would just "read" the 2nd counter (which is counting from the internal clock 1:1) and store it's value in variables.

Bean.


▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
"SX-Video·Module" Now available from Parallax for only $28.95

http://www.parallax.com/detail.asp?product_id=30012

"SX-Video OSD module" Now available from Parallax for only·$49.95
http://www.parallax.com/detail.asp?product_id=30015

Product web site: www.sxvm.com

"Ability may get you to the top, but it takes character to keep you there."


Post Edited (Bean (Hitt Consulting)) : 2/9/2006 3:47:51 PM GMT

Comments

  • Paul BakerPaul Baker Posts: 6,351
    edited 2006-02-09 16:24
    It appears you are correct that the timer count cannot be directly accessed. How about combining the 16 bit timer to clock the external 30 kHz clock (using the compare mode), and extending the RTCC to 17 bits (16 bits plus overflow bit). That way you have access to the number of processor cycles that have occured during the 1000 external cycles. Or if you are·confident enough that the count wont be off by more than 127 cycles, you could initialize·the RTCC so that 333,333 cycles end at a value of 127,·keep the RTCC to 8 bits and just capture the RTCC upon the 16 bit timer's compare·interuppt

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·1+1=10

    Post Edited (Paul Baker) : 2/9/2006 4:29:41 PM GMT
  • pjvpjv Posts: 1,903
    edited 2006-02-09 16:51
    Hi Bean;

    Yeah, that's one of the "not so nice" things abaout the SX48/52.

    Also the (forced) requirement of alternating a timer's two "compare" registers is another "feature", and is not well described in the Ubicom docs. It can really throw you for a loop.

    Cheers,

    Peter (pjv)
  • BeanBean Posts: 8,129
    edited 2006-02-09 17:59
    Okay thanks guys, I thought I was missing something there.
    I found that in Capture/Compare Mode I can just toggle the "capture" pin then read the capture register. No big deal.
    But that doesn't work in External Event Mode. How do I get the count of external events ? Pulsing the capture pin doesn't work.
    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "SX-Video·Module" Now available from Parallax for only $28.95

    http://www.parallax.com/detail.asp?product_id=30012

    "SX-Video OSD module" Now available from Parallax for only·$49.95
    http://www.parallax.com/detail.asp?product_id=30015

    Product web site: www.sxvm.com

    "Ability may get you to the top, but it takes character to keep you there."
    ·
  • Paul BakerPaul Baker Posts: 6,351
    edited 2006-02-09 18:42
    The capture mode is meant to capture the time duration of a single event hence the need to provide access to the timer count, whereas the external event is meant to capture the counts of multiple events (though Im not sure why they didnt provide a similar capture count type mechanism).

    An alternative to the method I described above is using both 16 bit timers, the first is set to external event mode, setting up the the span of r1 and r2 to be 1000 cycles. The output of the external event timer is used as the input to the second timer which is configured in the capture/compare mode, in which you retrieve the capture register containing the number of processor cycles it took for the other timer to count 1000 external pulses. I may not have to precise methodology correct, but the combination of the two timers configured as such should accomplish what you are after.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·1+1=10

    Post Edited (Paul Baker) : 2/9/2006 6:45:33 PM GMT
  • BeanBean Posts: 8,129
    edited 2006-02-09 19:26
    Paul,
    Thanks, I got it working. In case your wondering it basically a reciprocal frequency counter.

    Setup counter 1 for capture/compare mode.

    Setup counter 2 for external event mode. Set R1 of counter 2 to 1000 (or whatever value). Set counter 2 to generate an interrupt when it reaches R1.

    In the interrupt routine pulse counter 1 capture pin. Read the count captured from counter 1 capture register.

    Works like a charm.

    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "SX-Video·Module" Now available from Parallax for only $28.95

    http://www.parallax.com/detail.asp?product_id=30012

    "SX-Video OSD module" Now available from Parallax for only·$49.95
    http://www.parallax.com/detail.asp?product_id=30015

    Product web site: www.sxvm.com

    "Ability may get you to the top, but it takes character to keep you there."
    ·
  • Paul BakerPaul Baker Posts: 6,351
    edited 2006-02-09 19:40
    Glad to hear it worked, at lunch I remembered using cascaded multi-purpose timers in college for IR packet transmission, but we used the two timers on the MC68HC11 in reverse, the first in capture mode to get the IR pulse widths, and the second in external event mode to count the number of bits in the packet.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·1+1=10
  • BeanBean Posts: 8,129
    edited 2006-02-10 02:31
    I've not played around with the timers until now. So they have been somewhat of a mystery. But man they really made this task much easier.
    I've something simalar with the SX28 and it was ugly and required 100% of the CPU time while measuring.
    Now I'll have to try to think of some other uses for them so I can learn more about them.
    Bean.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "SX-Video·Module" Now available from Parallax for only $28.95

    http://www.parallax.com/detail.asp?product_id=30012

    "SX-Video OSD module" Now available from Parallax for only·$49.95
    http://www.parallax.com/detail.asp?product_id=30015

    Product web site: www.sxvm.com

    "Ability may get you to the top, but it takes character to keep you there."
    ·
  • Paul BakerPaul Baker Posts: 6,351
    edited 2006-02-10 03:12
    The PWM mode is great for a set-it-and-forget-it DAC, and since its 16 bit you can achieve pretty good resolution.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·1+1=10
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2006-02-12 15:34
    Can anyone suggest some reading material for a novice to try to get up to speed on this. Something that is relatively introductory and overview.

    I just cannot seem to grasp the 'Capture and Compare' concept.

    It wouldn't hurt to actually have examples [noparse][[/noparse]Guenther's book stays with the SX-28 for examples - so this is really uncharted].

    Thanks....

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "When all think alike, no one is thinking very much.' - Walter Lippmann (1889-1974)

    ······································································ Warm regards,····· G. Herzog [noparse][[/noparse]·黃鶴 ]·in Taiwan
  • Paul BakerPaul Baker Posts: 6,351
    edited 2006-02-13 18:27
    The new parallax documents do a better job explaining them: http://www.parallax.com/dl/docs/prod/datast/SX48BD-Data-v1.01.pdf#page=32

    the Capture and Compare are actually two different functions in the same mode and are abbreviated for Input Capture and Output Compare.

    The input compare function upon detecting a transition on the input compare pin for that timer, copies the current·value of the free running timer into a register, you can think of this as a timestamp. This is much like the function of the Port B interrupts but with the timer value thrown in. Obtaining two time stamps you can determine the time between two different events (if the transistion edge the input compare activates on is the same) or the duration of a single event (if the transition edge·the input compare activates on is changed).

    The output compare mode is much like the PWM mode except the timer does not reset when the value R2 is reached. This is equivilent to a fixed period PWM (base time is fixed according to 216 timer cycles).·I am a little unclear of the real world behavior of output compare mode for the SX, whether the contents of R2 dictate the second toggling of the output pin, or if the timer overflow causes the toggling of the bit (or if you have to manually toggle the bit during a timer overflow interrupt). But this behaviour could be easily verified by using a value of $8000 for R1 and a value of say $8080 for R2 and using a large prescaler for the timer and an LED·connected to the output compare pin for the timer. If the LED blinks briefly,·R2 dictactes the second toggling (I dont think this is what happens), if the LED blinks at a rate of 215 timer clock cycles the timer overflow toggles the pin, if the LED blinks at a rate of 216 timer cycles then no second toggling occurs (I think this is what happens), but in your ISR you can toggle the output pin manually upon timer overflow.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·1+1=10

    Post Edited (Paul Baker) : 2/13/2006 6:45:15 PM GMT
  • LoopyBytelooseLoopyByteloose Posts: 12,537
    edited 2006-02-14 08:06
    Thanks Paul. I suspected that, but every manufacturer [noparse][[/noparse]except for intuitive Parallax] out there seems to call it a Capture/Compare Unit.

    I read Guenther's presentation and it seems that Capture is dependent on one pin per timer. One cannot assign other pins.

    I really have to work around this limitation [noparse][[/noparse]in software] as I wanted to attach a 4 channel R/C reciever to an SX and decode the pulse width of each channel.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    "When all think alike, no one is thinking very much.' - Walter Lippmann (1889-1974)

    ······································································ Warm regards,····· G. Herzog [noparse][[/noparse]·黃鶴 ]·in Taiwan
Sign In or Register to comment.