Pulsein issue

Discussion specific to the 24-pin ZX microcontrollers, e.g. ZX-24r, ZX-24s and ZX-24t.
Post Reply
pjc30943
Posts: 220
Joined: 01 December 2005, 18:45 PM

Pulsein issue

Post by pjc30943 »

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?
eserdahl
Posts: 6
Joined: 14 December 2005, 16:06 PM
Location: Sunnyvale, CA
Contact:

Pulsein issue

Post by eserdahl »

Try replacing the line
 
     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 --------------------
pjc30943
Posts: 220
Joined: 01 December 2005, 18:45 PM

Re: Pulsein issue

Post by pjc30943 »

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
mikep
Posts: 796
Joined: 24 September 2005, 15:54 PM

Re: Pulsein issue

Post by mikep »

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
Mike Perks
pjc30943
Posts: 220
Joined: 01 December 2005, 18:45 PM

Re: Pulsein issue

Post by pjc30943 »

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... ]


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
pjc30943
Posts: 220
Joined: 01 December 2005, 18:45 PM

Re: Pulsein issue

Post by pjc30943 »

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.
mikep
Posts: 796
Joined: 24 September 2005, 15:54 PM

Re: Pulsein issue

Post by mikep »

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?
It's good you found the problem although I know it can be frustrating at times.

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.
pjc30943 wrote: Aside from moving to a zx1281e, of course.
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.
Mike Perks
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Re: Pulsein issue

Post by dkinzer »

pjc30943 wrote:There is no way to use both PWM and PulseIn then, correct?
Yes, both of those use Timer 1 and therefore are mutually exclusive along with all of the other routines that need Timer 1.

If you aren't using the software UART (which uses Timer 2) you can implement 8-bit PWM using Timer 2.
- Don Kinzer
pjc30943
Posts: 220
Joined: 01 December 2005, 18:45 PM

Re: Pulsein issue

Post by pjc30943 »

dkinzer wrote:
pjc30943 wrote:There is no way to use both PWM and PulseIn then, correct?
Yes, both of those use Timer 1 and therefore are mutually exclusive along with all of the other routines that need Timer 1.

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.
dkinzer
Site Admin
Posts: 3120
Joined: 03 September 2005, 13:53 PM
Location: Portland, OR

Re: Pulsein issue

Post by dkinzer »

pjc30943 wrote:I went with the 1281e, which should make this all much easier to implement.
Quite true. The mega1281-based devices offer more timers, more RAM and a second hardware UART, too.
- Don Kinzer
Post Reply