TIMER (SX/B) for Counting
· OK, the TIMER command was mentioned in another Subject.· So, I read what's available in SX/B Help and... I need more.
· Can these TIMER Configurations set up an SX48 with an edge-triggered input/s?· I want to make a counter, but COUNT and PULSIN have time constraints.· I want to set it running and whenever an event occurs an associated variable should INC.· The events could be a long time in coming, then several in succession, or maybe it'll be·really busy.
· Can these TIMER Configurations set up an SX48 with an edge-triggered input/s?· I want to make a counter, but COUNT and PULSIN have time constraints.· I want to set it running and whenever an event occurs an associated variable should INC.· The events could be a long time in coming, then several in succession, or maybe it'll be·really busy.
Comments
It would be easier to make an edge-triggered interrupt from one of the RB pins and just INC a word variable in the interrupt routine. Don't forget to clear the pending register before ending the interrupt. Plus this method will work on the SX28 too.
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Teacher: What is the difference between ignorance and apathy ?
Student: I don't know and I don't care
Teacher: Correct !
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
www.hittconsulting.com
Post Edited (Bean (Hitt Consulting)) : 8/13/2007 1:37:00 PM GMT
I'm a black and white photography amateur, and I collect and (try to) use old cameras. Many of them have sticky shutters that need to be cleaned because of years of sitting on shelves or stuffed in boxes. After cleaning them, I need to figure out exactly what their shutter speed is when I process the negatives.
I was thinking of building a simple circuit with a phototransistor placed inside the camera and a light in front of the lens: a SX (48/52?) with a LCD attached would then display the duration the light was caugth by the phototransistor. Do you know if this component have the sufficient resolution/speed for this?
When the transistor is lit, this could be detected by the SX, which would then count the duration while transistor is getting the light, then display the value on the LCD once it's dark again.
I would assume that using an interrupt would be much more precise than polling a pin.
What would be the best way of measuring that interval? The duration would be in the range of milliseconds, and to get any real precision, I would assume microseconds.
I was thinking of using a sx52 protoboard I have. Would something like this work?
I would assume that it's relatively easy to start the TIMER with the interrupt, but how would you stop it?
Would you poll the pin, then once the phototransistor status returns to unlit, stop the timer?
I've attached a very crude drawing of what I thought might work. Sorry for the child-like artistry, that's pretty much all I can do
Thanks!
Benoit
Post Edited By Moderator (Bean (Hitt Consulting)) : 8/13/2007 5:20:14 PM GMT
Actual just using PULSIN would be the easiest way to do that. It has 10uSec (0.01mSec) resolution.
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Teacher: What is the difference between ignorance and apathy ?
Student: I don't know and I don't care
Teacher: Correct !
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
www.hittconsulting.com
·
Thanks!
-Benoit
I have also looked at the TIMER command in the SX/B help file for the SX48. Actually printed out that whole .pdf
and had it bound for a manual - much easier to reference.
I'm a bit confused on how it exactly how it it works too, especially how it applies to what I'm trying to do
See my "Interrupts for Dummies" post.
I did order two SX48 boards today and hope to have them by the end of the week. Once I get to actually writing code
and programing the chips·I hope this will make some more sense.
Thanks,
Alex
The best information about the timers in in Gunther book
http://www.parallax.com/detail.asp?product_id=70002
For your application you will be using the timer to generate pulses. So you will use the PWM mode.
Let's say the SX clock is 4MHz and you want a 1KHz output you would use
TIMER1 PWM, 2000, 4000
This means the output pulses will be high for 2000 clocks, and will repeat every 4000 clocks (high for 2000, low for 2000).
Of course you can use WORD variables instead of the constants to create whatever pulse rate you want.
If the value go above 65535, then you need to use the prescaler.
Let's say you wanted a 1 Hz pulse (still with the SX clock at 4MHz). You would use the timer prescaler of 64 and use "TIMER1 PWM 31250, 62500"
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Teacher: What is the difference between ignorance and apathy ?
Student: I don't know and I don't care
Teacher: Correct !
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
www.hittconsulting.com
·
Thanks for that info to get me started. I agree with you - that appears much easier to use that the interrupts - at least
for a beginner like me.
My main confusion came from what the prescaler exactly did, but you explained that for me.
I do have Gunther's book and will read up on the TIMER info in it.
Thanks,
Alex
With a 10 uS interrupt period this should theoretically work up to 50 MHz -- I've tested with the PDB and the highest freq from the pulse generator is 1000 Hz.
I think you meant to say it will work up to 50 KHz.
Something that might be useful for that program is that the WKPND_B register gets set even if interrupts are not enabled. So you can use WKPND_B as a latch to tell if a pin changed state while you weren't looking at it. This only works on port RB pins of course.
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Teacher: What is the difference between ignorance and apathy ?
Student: I don't know and I don't care
Teacher: Correct !
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
www.hittconsulting.com
·
See section 11 Multi-function Timers
http://www.parallax.com/dl/docs/prod/datast/SX48BD-Data-v1.5.pdf
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Teacher: What is the difference between ignorance and apathy ?
Student: I don't know and I don't care
Teacher: Correct !
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
www.hittconsulting.com
·
The way to get the timer count is to switch the timer to capture mode, then toggle the capture pin. But there is a problem. As soon as you switch to capture mode, the timers starts counting SX clock pulses until the capture pin is toggled.
It takes a bit of trickery to get the real count from the counter. The bit I found out myself·is that the timer prescaler does NOT effect the external count mode. When using external counter you always get the number of pulses on the pin regardless of the prescaler.
So you can set the prescaler to 128, put the counter in EXTERNAL mode, clear the counter (which clears the prescaler too), count your pulses (the prescaler will not be advanced), switch to capture mode (now you have 128 SX clocks to toggle the capture pin before the timer gets advanced 1 count).
Here is a demo program...
Bean.
·
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Teacher: What is the difference between ignorance and apathy ?
Student: I don't know and I don't care
Teacher: Correct !
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
www.hittconsulting.com
Post Edited (Bean (Hitt Consulting)) : 8/15/2007 5:51:29 PM GMT
Update: I've attached the "Practical SX/B" version of the program; perhaps PJ and others will find this useful. I expanded the TIMER1 EXTERNAL instruction to assembly so that I can set the edge level for the external event; perhaps this can be added as an option to SX/B.
Post Edited (JonnyMac) : 8/15/2007 6:32:06 PM GMT
[noparse][[/noparse]EDIT] Okay I was wrong, I believe it is 1/2 the SX clock frequency. In my application I'm reading 20MHz clocking the SX at 50MHz.
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Teacher: What is the difference between ignorance and apathy ?
Student: I don't know and I don't care
Teacher: Correct !
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
www.hittconsulting.com
Post Edited (Bean (Hitt Consulting)) : 8/16/2007 11:16:11 AM GMT
I'm going from memory here..... a project 2 years ago. As best as I can recall, you can count up to the full clock speed, but not beyond.
Cheers,
Peter (pjv)
I edited my post. From my notes it was a maximum of 1/2 the SX clock frequency. But it seemed I had to be slightly below 1/2 or it would start screwing up (probably a duty cycle problem).
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Teacher: What is the difference between ignorance and apathy ?
Student: I don't know and I don't care
Teacher: Correct !
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
www.hittconsulting.com
·
The theorem states that
“ Exact reconstruction of a continuous-time baseband signal from its samples is possible if the signal is bandlimited and the sampling frequency is greater than *twice* the signal bandwidth.
In other words... you sample twice to gain precision and therefore the most you can count is 1/2 your clock rate since after that you lose precision (IE; the signal changed and you missed it).
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
There's nothing like a new idea and a warm soldering iron.
Post Edited (pwillard) : 9/26/2007 6:40:23 PM GMT