I2C counter OR counter + parallel/serial I2C
Fellow SXers;
Do you know if there is an I2C counter OR counter + parallel/serial I2C combination that would do the trick. For arguements sake, lets say it would be 8 bit (0-256), running off 5V. I would like to count the number of pulses from a sensor but I don't want to tie down the Stamp or SX processor.. rather just read the counts in every so often and then reset the counter. Any ideas would be appreciated. John
Do you know if there is an I2C counter OR counter + parallel/serial I2C combination that would do the trick. For arguements sake, lets say it would be 8 bit (0-256), running off 5V. I would like to count the number of pulses from a sensor but I don't want to tie down the Stamp or SX processor.. rather just read the counts in every so often and then reset the counter. Any ideas would be appreciated. John
Comments
Isn't that the convept of Virtual Periferals. The (maybe) small cost difference is easily offset in stocking only a few part types - not having to go look for another part type -
and in tayloring it to just exactly what you want instead of "making something work"
Your best bet would be to use a binary counter chip and an I2C I/O Expander to read it and reset it.
On the other hand, adding a second SX might be simpler as Doug suggests.
Just ask if you would like to see some example code and I'll whip some up.
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
www.iElectronicDesigns.com
·
Unfortunately, I can't read my LED display when I go outside in bright sunshine.. so I will have to switch tot LCD displaysfor this project.
Nah the INTERRUPT will use RTCC. But depending on how fast the input pulse is that are counting you could code a counter as part of the interrupt routine.
SX to SX communications can be anything from "just connecting 8 pins from each device" to "serial (SERIN/SEROUT)".
If you use the SX48 you get two 16-bit hardware counters. That's what I would use.
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
www.iElectronicDesigns.com
Post Edited (Bean (Hitt Consulting)) : 11/13/2007 7:45:16 PM GMT
In general, the more specific people are in describing their requirements the more specific the resulting answers seem to be. In this case, several people have offered different approaches that all seem likely to accomplish your goal. If any of them are not clear enough, just keep asking questions.
- Sparks
I had also thought of just interupting on every pulse and then calculating the time between pulses...but I needed the timer code for that and apparently RTCC is already in use.
The extra (independant?) counters on the SX48 sound promising but I don't have any of those chips and they don't seem to come in DIP form so how would I test stuff out on my PDB/breadboard?
Is there an 7 seg LED that is bright enough to be seen in daylight?
Post Edited (JonnyMac) : 11/14/2007 12:43:30 PM GMT
It really hard to suggest things when we don't know what the sensor signal looks like.
What is the sensor ? What does it's signal look like.
Like Jon, I don't understand why you are so intent on adding extra hardware. I'd bet you a doughnut that the SX28 can do it, but we need to know exactly what the signal looks like to help you.
The whole idea of the SX is that it can most things without adding extra hardware.
If you think you will miss the pulse because it is very quick, you can use the edge latches on the portB pins. The port B pins can be setup to capture rising or falling edges. So if you get a quick rise and fall while the program is doing other things, the latch will still be set so you KNOW a pulse happened and it won't be missed.
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
www.iElectronicDesigns.com
·
The sensor is Melexis 90217 from Parallax (magnet detection). It is mounted on a 27"
bicycle wheel near the central bearing. There are 2 magnets 180degrees apart provide
2 pulses per revolution. I don't know what the signal looks like but I speculate that
it is like a square wave with a 20% duty cyle. (as long as the magnet is in range the
pulse stays high?).
I wouldn't say that the pulses are quick, just that if they come at
the wrong time ( when interupt is running, and it runs alot ) the SX28 could miss them.
I was thinking I would miss pulses because the SX/28 could not detect pulses while it
was busy on the interupt routine. But looking at your code again, it looks like the SX
can latch a pulse, while other code is running. Then, as long as some code 'counts' the
pulse and resets the latch before the next pulse arrives, it should work ok. That code
is DELAY_MS.
If I understand Jon's extra code, it looks like the MAIN program should loop, updating
display LEDs values while the interupt routine multiplexes the display cathodes.
The MAIN calls DELAY_MS at some unknown interval ( when interupt is not running and other
code in MAIN is not running). During that time 'JNB isrFlag'
would check if say RB.0 is high and then branch somewhere where a counter could be
incremented. That counter could be inspected every so often in the main program, used
in a calculation and then reset. This seems to require input RB.0 be set to act like
a latch? I think I just need some help mixing some assembler code in SX/B to get the
job done.
respectfully, John
ps. Bean, I like the idea of doing as much as possible on one SX/28 chip (for this project)
to keep the parts count and cost as low as possible. Just show me how..
ps. Jon, If the pulse stayed high long enough wouldn't it retrigger the latch and give more than
1 count per pulse?
Actually, the Port B pins conditionally trigger and latch on pulse EDGES where the signal RISES or FALLS. Once the signal is high, resetting the latch will not cause it to trigger again until the signal falls and then begins to rise again.
- Sparks
You won't get more than one count per pulse with the code I gave you. Note that there is a bit variable called "lastScan" that is updated each time through. The only time the counter gets incremented is when the last scan was zero and the new scan is one; the code is looking for a low-to-high change on the sensor input line. And they can't come at the "wrong" time if you use a polled interrupt versus using edge triggered. The only thing you need to do is make sure your periodic interrupt is running fast enough to catch the pulses. Running it faster tha 1 ms won't hurt the display code at all.
How fast does your bike go?
What is the distance from the axle to the magnet placement?
What is the diameter of the magnets?
With these numbers and a calculator you should be able to estimate -- pretty reasonably -- your pulse rate and pulse width.
Somehow I did not see you there. I was anxious to post! Sorry about that.
- Sparks
I have added some code to the timer program to count melexis pulses on RB.7 but it doesn't seem to work. Could anyone review the code and tell me what is wrong?
Thanks, John
·To This:
Bean
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
www.iElectronicDesigns.com
·
I was seeing numbers flying by... or constant... but not 1 digit increment with each pass of magnet... so I hooked same signal to external counter circuit with its own display (74LS90) and then both the external counter circuit and the sx seemed to respond to the inputs better.
The external count increment exactly by one count for each magnet pass.
However, the SX rb.7 tends to give multiple counts for each magnet 'pass'. I figure sx interupt is so fast that during the time that I pass the magnet it must be resetting and setting several times till the magnet is out of the sphere of influence. Any ideas on how to fix that?
Thanks, John
Basically, I think it would help to first establish that your software code works as it should. Then tackle the possibility that the hardware is not detecting the signal accurately.
- Sparks