Count State Change
Bean,
··· I am tring to make this program, but I need it to be very simple (as it is my level) Here is what I am trying to do:
I need to count everytime a pin changes from 1 to 0, but it will be a pulse so it needs to count it quick and then store it in a variable for display.· So basically My device may pulse pin Ra.0 4 times in one second.· I need Var to return a count of 4.· Then if the device pulse another 4 1 second later I need the var to show 8.
Now I tried doing this two ways, but I can't get accurate way.
I have used Pulsin, but I sometimes get 10 pulses, 9 other times.· This is because its measuring the length or width·rather then counting·1 to·0 transition?
I also tried Count and that does not help either?
what is simple that can accomplish this?
Thanks so much!!!·
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Post Edited (Castrovinci) : 7/8/2008 5:17:37 PM GMT
··· I am tring to make this program, but I need it to be very simple (as it is my level) Here is what I am trying to do:
I need to count everytime a pin changes from 1 to 0, but it will be a pulse so it needs to count it quick and then store it in a variable for display.· So basically My device may pulse pin Ra.0 4 times in one second.· I need Var to return a count of 4.· Then if the device pulse another 4 1 second later I need the var to show 8.
Now I tried doing this two ways, but I can't get accurate way.
I have used Pulsin, but I sometimes get 10 pulses, 9 other times.· This is because its measuring the length or width·rather then counting·1 to·0 transition?
I also tried Count and that does not help either?
what is simple that can accomplish this?
Thanks so much!!!·

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Post Edited (Castrovinci) : 7/8/2008 5:17:37 PM GMT
Comments
The COUNT command should do it. What do you get with COUNT ?
Can you post your code that uses COUNT ?
If you are missing pulses when not in the COUNT command, you might want to use the RTCC input like I suggested in this thread http://forums.parallax.com/showthread.php?p=735697
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"A government big enough to give you everything you want, is big enough to take away everything you·have."·· Thomas Jefferson
"It is our choices, Harry, that show what we truly are, far more than our abilities."·Dumbledore from Harry Potter
www.iElectronicDesigns.com
·
Its a dollar bill acceptor and I can set the pulses for each bill entered.
·{{{{{{{{{{{{{{{{
If you use COUNT on slowly changing analog waveforms like sine waves, you may find that the value returned is higher than expected. This is because the waveform may pass through the SX’s 1.4-volt logic threshold slowly enough that noise causes false counts. You can fix this by enabling the SCHMITT trigger configuration on the pin used for COUNT.
{{{{{{{{{{{{{{{{
DEVICE SX48, OSC4MHZ, TURBO, STACKX, OPTIONX
FREQ 4_000_000
ID "COUNT"
'
' IO Pins
'
FreqIn PIN RA.0 ··INPUT ' frequency input
'
' Constants
'
Duration CON 1000 ' measure for one second
'
' Variables
'
hertz VAR Word ' measured cycles
WATCH hertz ' for Debug display
' =========================================================================
PROGRAM Start
' =========================================================================
'
' Program Code
'
Start:
··DO
····COUNT FreqIn, Duration, hertz ' measure frequency
····BREAK ' display in Watch window
··LOOP
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Post Edited (Castrovinci) : 7/8/2008 5:17:19 PM GMT
Use a pin on port RB or RC and it should work "FreqIn PIN RB.0 INPUT SCHMITT"
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"A government big enough to give you everything you want, is big enough to take away everything you·have."·· Thomas Jefferson
"It is our choices, Harry, that show what we truly are, far more than our abilities."·Dumbledore from Harry Potter
www.iElectronicDesigns.com
·
The program uses an interrupt to detect and count edges (count window is user-definable) and multiplex the display.
[noparse][[/noparse]Edit] Corrected for 1.51.03 compiler (sorry, I was using the beta 2.0 compiler when I wrote this).
Post Edited (JonnyMac) : 7/9/2008 5:34:07 AM GMT
One "advanced" technique that I did include is a specific sampling of the input that only occurs ONCE in the entire processing of the state. The importance of this is often much more academic than practical. This is especially true with very clean and slow moving signals. However, sometimes making multiple logic decisions that are each based upon the instantaneous state of an input can occasionally lead to unexpected results especially if you happen to be relying solely on the assumption that the signal will not change as you progress through the subroutine. This can be very difficult to find and track down later!
So my "advanced" tip is to sample your input at one instant in time and do the rest of your processing on the sampled state rather than the possibly still changing input state.
I hope this helps.
- Sparks
Hope this is helpful.
[noparse][[/noparse]Edit] Changed ISR rate to catch 20 uS pulses.
Post Edited (JonnyMac) : 7/9/2008 5:31:08 AM GMT
·· I figured out one of the problems.· It appears the pulse is only 20US and the count can only handle 1 MS or above.· Do we have any other idea's?· I need it to be with minimal parts for my idea though!· Thanks guys for helping!
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"A government big enough to give you everything you want, is big enough to take away everything you·have."·· Thomas Jefferson
"It is our choices, Harry, that show what we truly are, far more than our abilities."·Dumbledore from Harry Potter
www.iElectronicDesigns.com
·
Here is a couple of more things I came up with though.·
First Jonny Mac, I modified your program and am work so I figured I catch you guys now.· This is what I came up with, think it will work?· The only problem I have is I can not debug to view·this because I can't have an external osc, can this be done using the internal, and keeping the standard 4mhz freq.· How can I make this work for a watch as far as the setup?·
Also Bean you meantioned using the RTCC Pin, Can you take my example and show me what you mean, cause that sounds like I can use debug in that method.
Also what does BOR42 do?
DEVICE········· SX28, OSCXT2, TURBO, STACKX, OPTIONX, BOR42
FREQ··········· 50_000_000
ID············· "HZ_Meter"
'
' I/O Pins
'
Dispense·PIN·RC.1 INPUT
CoinIn··PIN·RC.0 INPUT· SCHMITT
'
' Constants
'
Pressed··CON·0···' for active-low inputs
NotPressed·CON·1
'
' Variables
'
flags··VAR·Byte
·isrFlag·VAR·flags.0···' isr marker
·lastIn··VAR·flags.1···' last input level
idx··VAR·Byte
money··VAR·Word
watch money
' =========================================================================
· INTERRUPT 100_000
' =========================================================================
Mark_ISR:
· isrFlag = 1
Count_Pulses:
· IF lastIn = 1 THEN
··· IF CoinIn = 0 THEN
····· money = money + 1
····· 'Can I add BREAK in here for the watch or do I do it in the main
··· ENDIF
· ENDIF
· lastIn = CoinIn
ISR_Exit:
· RETURNINT
' =========================================================================
· PROGRAM Start
' =========================================================================
'
' Subroutine / Function Declarations
'
'
' Program Code
'
Start:
· PLP_C = %00000011····' pull-up unused pins
· money = 0
Main:
goto main
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
NOTE that you cannot use a period interrupt when RTCC is counting from the external pin though.
Bean
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"A government big enough to give you everything you want, is big enough to take away everything you·have."·· Thomas Jefferson
"It is our choices, Harry, that show what we truly are, far more than our abilities."·Dumbledore from Harry Potter
www.iElectronicDesigns.com
·
··· Thanks for the reply, no I do not need a display in the final project, I just need to make sure it works in debug as it is easiest that way for me.· The end project will have no display
P.S. what is the BOR42 on the end of the device settings?
·Also if you still have an option like I submitted below with the internal osc let me know.· Thanks Jonny and Bean!· Bean I am going to try yours now.·
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
BOR42 sets the brown-out control (reset on brown-out) to 4.2 volts.
Correct me if I'm wrong but doesn't setting the option value disable roll over? OPTION = %1111_1000
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
The RTI setting just disables interrupts when it does roll-over.
NOTE your signal may be open-collector and needs a pull-up resistor on it.
Bean.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
"A government big enough to give you everything you want, is big enough to take away everything you·have."·· Thomas Jefferson
"It is our choices, Harry, that show what we truly are, far more than our abilities."·Dumbledore from Harry Potter
www.iElectronicDesigns.com
·
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔