A little more help on smartpins
CRST1
Posts: 103
With your help I finally got the P2 counting on a smartpin.
The part of code that counts is:
The pulses generated are 16hz which reads out as 10000032 which is 16hz at 160mhz clock.
All is good until I unplug the jumper and the count stays at 10000032.
How would I reset Z to 0 when the pulses stop?
I tried using dirl #7 and dirh #7 but then I get a fluctuating count.
Thanks again for any help.
The part of code that counts is:
PUB Main | tmr1, count1 clkset(oscmode, freq) 'start the SmartSerial object serial.Start(rx, tx, %0000, baud) coginit(7, RPMs, @RPMStack) asm dirl #7 'disable pin endasm wrpin_(%0000_0000_000_0000000000000_00_10011_0,7) 'times rise to rise in clocks wxpin_(1,7) 'times one cycle wypin_(%0,7) 'rise to rise asm dirh #7 'enable pin endasm repeat asm rdpin tmr1, #7 {wc} endasm serial.str(string(" Tmr ")) serial.dec(tmr1) serial.str(string(" ")) PUB RPMs dirb[24] :=1 repeat outb[24] :=1 waitcnt(cnt + 5_000_000) outb[24] :=0 waitcnt(cnt + 5_000_000)
The pulses generated are 16hz which reads out as 10000032 which is 16hz at 160mhz clock.
All is good until I unplug the jumper and the count stays at 10000032.
How would I reset Z to 0 when the pulses stop?
I tried using dirl #7 and dirh #7 but then I get a fluctuating count.
Thanks again for any help.
Comments
I think you'll find that with no edges to time, the Z register (which holds the result) will still give you the result of the last completed measurement, but the IN bit will be low.
If you tested INA[7] before RDPIN you'd know whether the Z register contents was current or stale. Simply waiting for a new reading won't achieve what you want so you need to keep looping and detect valid new readings and act accordingly.
As your loop could easily cycle more than once per pulse you need to keep track of this in a variable.
It could be something as crude as
Finding a value for toolong might need some trial and error, or some cycle counting based on the final code.
EDIT: Of course, the whole status testing, stale count tracking, and pin reading could be done in asm, but the serial object timing is the timing wildcard.
I take it it's set to 1 when it reads a complete pulse from rise to rise. then what sets it low again?
The reason for this is to read a speed and tach pulse train for a vehicle.
And for mode %10011:
So, yep, your hunches are all correct.
Yes that will work. I had tried similar before but was using clock values for the counter loop. It was way too long of loop and never reset to 0. Brought the number way down and now it works with the too long counter.
It's been a long three days of studying but its getting there.
Thank you
Glad to be of help. Living is learning, or at least it should be.