Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Need a bidirectional command

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    www.mcselec.com Forum Index -> BASCOM-AVR Archive
View previous topic :: View next topic  
Author Message
acatano

Bascom Member



Joined: 24 Apr 2004
Posts: 44
Location: Sunrise, USA

usa.gif
PostPosted: Sat Mar 19, 2005 4:03 am    Post subject: Need a bidirectional command Reply with quote

Sad Hello everybody:
I want to send a pulse and some time later receive a response using the same pin.
Please somebody help!!.
Back to top
View user's profile Yahoo Messenger
Luciano

Bascom Member



Joined: 29 Nov 2004
Posts: 3149
Location: Italy

blank.gif
PostPosted: Sat Mar 19, 2005 11:51 am    Post subject: Reply with quote

Hello! Very Happy

Please describe your project first. Idea

"I want to send a pulse..." = Electrical characteristic Question and timing Question of the pulse.
Is the sender master and the receiver slave? Question Describe the receiver please. Question

"...some time later " = 1 ms, 1 sec, 1 min, timing Question please.

"...receive a response" = Electrical characteristic Question and timing Question of the response.

Luciano
Back to top
View user's profile
ATIU

Bascom Member



Joined: 15 Oct 2004
Posts: 534

philippines.gif
PostPosted: Sat Mar 19, 2005 12:48 pm    Post subject: Reply with quote

If its serial data then check "serialin" and "serialout" command from Bascom Help. If its just simple pulse signal, then use "DDRx.n" (x is the port name and n is the pin number.. DDRB.0 for port B pin 0), DDRB.0 or DDRB=1 to change pin direction, from input to output or vise versa. You can then just SET or RESET the pin to ge the desired output to the pin. To create delays, in between SET and RESET you can use WAITMS or WAITUS, or use timer interrupts if timing is important to generate the desired pulse width.
Back to top
View user's profile
acatano

Bascom Member



Joined: 24 Apr 2004
Posts: 44
Location: Sunrise, USA

usa.gif
PostPosted: Sat Mar 19, 2005 3:36 pm    Post subject: Need a bidirectional command Reply with quote

Rolling Eyes Thanks Atiu for your response.
Actually the project is to run a Ping]]] a sonar ranger sold by Parallax.
Using one single pin sends a 5 usec pulse, and waits for a response.
Works similary like SRF04 and SRF08 but uses just one signal pin.
Back to top
View user's profile Yahoo Messenger
Luciano

Bascom Member



Joined: 29 Nov 2004
Posts: 3149
Location: Italy

blank.gif
PostPosted: Sat Mar 19, 2005 7:16 pm    Post subject: Reply with quote

Hi!

Try the Bascom AVR commands Pulseout and Pulsein.

The Basic Stamp 2 has similar commands.

See these two PDF files for BS2 code examples:
http://www.parallax.com/dl/docs/prod/acc/PingDocs.pdf
http://www.parallax.com/dl/docs/prod/acc/PingISeeYou.pdf

Best regards,

Luciano
Back to top
View user's profile
acatano

Bascom Member



Joined: 24 Apr 2004
Posts: 44
Location: Sunrise, USA

usa.gif
PostPosted: Sun Mar 20, 2005 3:37 am    Post subject: Need a bidirectional command Reply with quote

Embarassed I have some experience with Pbasic.
Pulseout/Pulsenin Pbasic ARE NOT even close to Pulseout/Pulsein in Bascom.
Back to top
View user's profile Yahoo Messenger
Luciano

Bascom Member



Joined: 29 Nov 2004
Posts: 3149
Location: Italy

blank.gif
PostPosted: Sun Mar 20, 2005 11:03 am    Post subject: Reply with quote

Hi!

The echo pulse of the Parallax PING))) ultrasonic range finder is a positive TTL pulse.
By measuring the echo pulse width, the distance to target can easily be calculated.
(The echo pulse of ultrasonic range finder varies from 115 uS to 18.5 mS, range
1.2 in to 3.3 yrds or 3 cm to 3 m).

With the Bascom AVR PULSEIN statement you can easily measure the pulse width of your
ultrasonic range finder.

From the Bascom AVR Help file, PULSEIN statement :
No hardware timer is used. A 16 bit counter is used. It will increase in 10 uS units.
But this depends on the XTAL. You can change the library routine to adjust the units.

* * *

If you change the PULSEIN statement counter units to 1us, its timeout will occur after 65ms.
The 65ms will be still enough because the pulse width of the ultrasonic range finder is max. 18.5ms.
(To change the library routine you will need the full version of Bascom AVR).

There are many ways you can measure the pulse width of your ultrasonic range finder.
The Bascom AVR PULSEIN statement is just one of these.

To trigger the PING))) ultrasonic range finder, the Bascom AVR PULSEOUT statement will work.
There are many ways you can generate 5us TTL pulse that will trigger your ultrasonic range finder.
The Bascom AVR PULSEOUT statement is just one of these.


Luciano
Back to top
View user's profile
acatano

Bascom Member



Joined: 24 Apr 2004
Posts: 44
Location: Sunrise, USA

usa.gif
PostPosted: Sun Mar 20, 2005 3:08 pm    Post subject: Need a bidirectional command Reply with quote

We are getting off tangent.
The problem is not how to calculate the distance.

The problem as I see it is that if you use one single pin and use Pulseout, you have to declare it an output. But after 5 usec the same pin has to be changed to an input. That means you have to change from an output to an input in the fly.
My question is how to change the port registers from output to input and back on the fly.

Atiu sugestion may be the right one.
Back to top
View user's profile Yahoo Messenger
Luciano

Bascom Member



Joined: 29 Nov 2004
Posts: 3149
Location: Italy

blank.gif
PostPosted: Sun Mar 20, 2005 7:02 pm    Post subject: Reply with quote

Hi!

Before you use PULSEOUT you will have to set the pin as output.
PULSEOUT does not that automatically because it can be dangerous
for the pin.

When you use PULSEIN you don't have to set the pin as input because
PULSEIN will do that automatically for you.

After you have sent the 5us trigger pulse, the Parallax PING))) ultrasonic range finder will
wait about 200 microseconds before transmitting the ultrasonic burst in the air.
This will allows PULSEIN to switch the pin as an input and be ready to receive the echo
pulse from the ultrasonic range finder.

See code example below.

Best regards,

Luciano

Code:

$crystal = 4000000

Dim w As word

Do

Config PINB.2 = Output 'PORTB Bit 2 as output
Pulseout PORTB , 2 , 5 '5uS  pulse on Port B, Bit 2
pulsein w , PINB , 2 , 0 'detect time from 0 to 1 on Port B, Bit 2
print w ' variable w will contain the pulse time in 10 uS units

Loop 'loop for ever
 
Back to top
View user's profile
ATIU

Bascom Member



Joined: 15 Oct 2004
Posts: 534

philippines.gif
PostPosted: Mon Mar 21, 2005 12:53 am    Post subject: Reply with quote

Changing output to input or vise versa is easy, just use the "ddrx.n" where x is the port name and n is the pin number register, as follows;

Dim X as Word
DDRB.0 = 1 'Pinb.0 as output or you can use Set DDRB.0
Set Portb.0 'Portb.0 high
Waitus 5 ' Wait 5 us
Reset Portb.0 'Portb.0 low
DDRB.0 = 0 'Pinb.0 is now input or you can use Reset DDRB.0
'Listen to Portb and do something
X = 0
Do
Loop Until Pinb.0 = 1 'Loops forever waiting for the pulse back
Do
Inc X
Waitus 5
Loop Until Pintb.0 = 0 'Loops and count the pulse width
'Distance = the X variable in 5 us interval multiplied by something...

You can also use interrupt pins with the same principle above, just enable and disable interrupts when needed/necessary or while pin is set as output.
Back to top
View user's profile
Luciano

Bascom Member



Joined: 29 Nov 2004
Posts: 3149
Location: Italy

blank.gif
PostPosted: Mon Mar 21, 2005 2:30 pm    Post subject: Reply with quote

Hi!

Config PINB.0 = Output
DDRB.0 = 1
Set DDRB.0

The above three lines of Bascom AVR code are equivalent.
The compiler will translate that in the assembly instruction:

SBI 0x17,0

SBI sets a specified bit in an I/O Register. This instruction operates on
the lower 32 I/O Registers – addresses 0-31. Takes two cycles to execute.

* * * * * *

Config PINB.0 = Input
DDRB.0 = 0
Reset DDRB.0

The above three lines of Bascom AVR code are equivalent.
The compiler will translate that in the assembly instruction:

CBI 0x17,0

CBI clears a specified bit in an I/O Register. This instruction operates on
the lower 32 I/O Registers – addresses 0-31. Takes two cycles to execute.

* * * * * *

If while your code is in execution you need to change a pin data
direction according to the value of a variable or another pin state,
then use DDRB.0 = x or Set DDRB.0 or Reset DDRB.0.

Example 1:

The pin data direction is stored in the EEPROM.
At power up you read the EEPROM and with the
returned data you set the data direction of the pin.

Example 2:

Dim my_bit as Bit
my_bit = 1
DDRB.0 = my_bit '(This line will take 12 CPU cycles to execute).

* * * * * *

In the code for the ultrasonic range finder the pin
data direction is set at compile time. You can use
any of the three statements below the result is the same,
the code will execute in 2 CPU cycles. It does not
matter if your code changes the pin data direction
every x ms. The data direction is stored in the
program memory at compile time.

Config PINB.0 = Output
DDRB.0 = 1
Set DDRB.0

The only question is which one of the three statements
is more readable when you search a bug in the code.

Best regards,

Luciano
Back to top
View user's profile
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    www.mcselec.com Forum Index -> BASCOM-AVR Archive All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum