Pulsein issue
Pulsein issue
I'm having trouble with pulsein on the 24ae.
Pared down to its barest level, I have
dim pulseInWidth as single
putpin 5, zxInputTristate
Do
pulseIn 5, 1, pulseInWidth
debug.print cstr(pulseInWidth)
sleep 0.3
Loop
which always returns 0.0, indicating a timeout or a pulse that's too long. The function form returns 0 as well.
The pulses are about 500us to 1ms; they come from an absolute encoder, and have a 0..5V swing. I can see the pulses at the pin on a scope.
If I replace the pulsein line with
WaitForInterrupt zx1000_0000, WaitPinChangeC 'pin5 is C.7
with a manual pushbutton at the pin to verify that the pins I tried this on all function, and that I'm not halucinating that the connection is good, it indeed continues in the loop at every button state change.
This is the first time I've tried pulsein on a zx device, so I must be missing something basic...
Any help on why this doesn't work?
Pared down to its barest level, I have
dim pulseInWidth as single
putpin 5, zxInputTristate
Do
pulseIn 5, 1, pulseInWidth
debug.print cstr(pulseInWidth)
sleep 0.3
Loop
which always returns 0.0, indicating a timeout or a pulse that's too long. The function form returns 0 as well.
The pulses are about 500us to 1ms; they come from an absolute encoder, and have a 0..5V swing. I can see the pulses at the pin on a scope.
If I replace the pulsein line with
WaitForInterrupt zx1000_0000, WaitPinChangeC 'pin5 is C.7
with a manual pushbutton at the pin to verify that the pins I tried this on all function, and that I'm not halucinating that the connection is good, it indeed continues in the loop at every button state change.
This is the first time I've tried pulsein on a zx device, so I must be missing something basic...
Any help on why this doesn't work?
Pulsein issue
Try replacing the line
putpin 5, zxInputTristate
with
putpin 5, zxInputPullup
This will make the pin 5 an input pin.
HTH
Eric
putpin 5, zxInputTristate
with
putpin 5, zxInputPullup
This will make the pin 5 an input pin.
HTH
Eric
----- Original Message -----
From: ZX-24 (zx24.forum@zbasic.net)
To: zx24.forum@zbasic.net (zx24.forum@zbasic.net)
Sent: Sunday, August 12, 2007 6:14 PM
Subject: Pulsein issue
I'm having trouble with pulsein on the 24ae.
Pared down to its barest level, I have
dim pulseInWidth as single
putpin 5, zxInputTristate
Do
pulseIn 5, 1, pulseInWidth
debug.print cstr(pulseInWidth)
sleep 0.3
Loop
which always returns 0.0, indicating a timeout or a pulse that's too long. The function form returns 0 as well.
The pulses are about 500us to 1ms; they come from an absolute encoder, and have a 0..5V swing. I can see the pulses at the pin on a scope.
If I replace the pulsein line with
WaitForInterrupt zx1000_0000, WaitPinChangeC 'pin5 is C.7
with a manual pushbutton at the pin to verify that the pins I tried this on all function, and that I'm not halucinating that the connection is good, it indeed continues in the loop at every button state change.
This is the first time I've tried pulsein on a zx device, so I must be missing something basic...
Any help on why this doesn't work?
-------------------- m2f --------------------
Read this topic online here:
http://www.zbasic.net/forum/viewtopic.php?p=3907#3907
-------------------- m2f --------------------
Re: Pulsein issue
Thanks, but tristate already means its an input.
eserdahl wrote:Try replacing the line
putpin 5, zxInputTristate
with
putpin 5, zxInputPullup
This will make the pin 5 an input pin.
HTH
Eric
Re: Pulsein issue
I tried your code on a ZX-24ae and a ZX-24. It worked on both. I'm sure by now you have done something similar.
Setting the pin to zxInputTristate or zxInputPullUp doesn't make any difference as the PulseIn function sets the pin to an input. You can eliminate this line of code.
Rather than using 5, it might be better to code the pin number as C.7 to be more readable.
I don't have many other suggestions. You could try adding this line before the loop but don't forget to multiply any result you get by 8.
Setting the pin to zxInputTristate or zxInputPullUp doesn't make any difference as the PulseIn function sets the pin to an input. You can eliminate this line of code.
Rather than using 5, it might be better to code the pin number as C.7 to be more readable.
I don't have many other suggestions. You could try adding this line before the loop but don't forget to multiply any result you get by 8.
Code: Select all
Register.TimerSpeed2 = 3
Mike Perks
Re: Pulsein issue
Thanks Mike.
I will try it on a zx24 and see if there's any difference, as well as adjust the TMR2 prescalar and see what happens.
[ The pin is coded as a constant C.7 in the header file, but I simplified as much as possible for the post in order to be transparent... ]
I will try it on a zx24 and see if there's any difference, as well as adjust the TMR2 prescalar and see what happens.
[ The pin is coded as a constant C.7 in the header file, but I simplified as much as possible for the post in order to be transparent... ]
mikep wrote:I tried your code on a ZX-24ae and a ZX-24. It worked on both. I'm sure by now you have done something similar.
Setting the pin to zxInputTristate or zxInputPullUp doesn't make any difference as the PulseIn function sets the pin to an input. You can eliminate this line of code.
Rather than using 5, it might be better to code the pin number as C.7 to be more readable.
I don't have many other suggestions. You could try adding this line before the loop but don't forget to multiply any result you get by 8.Code: Select all
Register.TimerSpeed2 = 3
Re: Pulsein issue
Okay, so I know what the issue is. Unfortunately.
In another module I had initialized PWM in hardware.
So since pulseIn uses TMR2 as well, that would explain why, after I disabled PWM initialization, PulseIn works properly.
There is no way to use both PWM and PulseIn then, correct?
Aside from moving to a zx1281e, of course.
In another module I had initialized PWM in hardware.
So since pulseIn uses TMR2 as well, that would explain why, after I disabled PWM initialization, PulseIn works properly.
There is no way to use both PWM and PulseIn then, correct?
Aside from moving to a zx1281e, of course.
Re: Pulsein issue
It's good you found the problem although I know it can be frustrating at times.pjc30943 wrote:Okay, so I know what the issue is. Unfortunately.
In another module I had initialized PWM in hardware.
So since pulseIn uses TMR2 as well, that would explain why, after I disabled PWM initialization, PulseIn works properly.
There is no way to use both PWM and PulseIn then, correct?
I think you mean Timer 1 for PulseIn and PWM. You could use Timer 2 for hardware 8-bit PWM but you will need to set up your own registers and you will lose the software-based serial ports.
Of course I would recommend the ZX-1281e or ZX-128e (cheaper) as they provide that many more facilities in a package just 0.2" wider than a ZX-24ae.pjc30943 wrote: Aside from moving to a zx1281e, of course.
Mike Perks
Re: Pulsein issue
Yes, both of those use Timer 1 and therefore are mutually exclusive along with all of the other routines that need Timer 1.pjc30943 wrote:There is no way to use both PWM and PulseIn then, correct?
If you aren't using the software UART (which uses Timer 2) you can implement 8-bit PWM using Timer 2.
- Don Kinzer
Re: Pulsein issue
dkinzer wrote:Yes, both of those use Timer 1 and therefore are mutually exclusive along with all of the other routines that need Timer 1.pjc30943 wrote:There is no way to use both PWM and PulseIn then, correct?
If you aren't using the software UART (which uses Timer 2) you can implement 8-bit PWM using Timer 2.
Unfortunately I'm using a software UART. I went with the 1281e, which should make this all much easier to implement.
Re: Pulsein issue
Quite true. The mega1281-based devices offer more timers, more RAM and a second hardware UART, too.pjc30943 wrote:I went with the 1281e, which should make this all much easier to implement.
- Don Kinzer