PDA

View Full Version : How to generate 40MHz pulse with PWM command for my ultrasonic transmitter..please



chenko
- 21st September 2008, 06:40
cORRECTION....i want to use command PWM to generate 40KHz (NOT 40mHZ) pulse for my ultrasonic transmitter but i cannot find the way..I use Port E for the ultrasonic transmitter..I hope anyone can help me with code examples ... maybe suggest another way to generate that pulse... Thank you..

skimask
- 21st September 2008, 06:43
i want to use command PWM to generate 40MHz pulse for my ultrasonic transmitter bit i cannot find the way..I use Port E for the ultrasonic transmitter..I hope anyone can help me with code examples that may help me... maybe suggest another way to generate that pulse... Thank you..

40Mhz eh?
Probably 40Khz...
What have you tried so far? Can you blink an LED? If you can blink an LED, all you have to do is make it blink at 40khz and you're set.
What kind of hardware do you have?
What kind of software are you using?
What kind of programming have you done?
You don't give a lot of information to work with ya know.
There's a bunch of different ways to generate a 40khz pulse stream...Not a lot of ways to generate a 40Mhz pulse stream using a PIC.

chenko
- 21st September 2008, 07:21
I used PIC 16F877A in my project..My project is Smart System of Ultrasonic Car parking...The purpose of the project is try to detect the distance between car and obstacle then display the range on the LCD..I used PIC Basic Pro for the coding..I want to generate 40Khz (not 40Mhz like I state before) because that is my frequency of ultrasonic transmitter

skimask
- 21st September 2008, 07:40
I used PIC 16F877A in my project..My project is Smart System of Ultrasonic Car parking...The purpose of the project is try to detect the distance between car and obstacle then display the range on the LCD..I used PIC Basic Pro for the coding..I want to generate 40Mhz because that is my frequency of ultrasonic transmitter

Are you sure about that 40Mhz?
Are you sure 40Mhz falls in the range of an ultrasonic transmitter?

chenko
- 21st September 2008, 07:54
yup..you're correct...only 40kHz..i was mistaken by look to the other datasheet..thank you.. can you help me with the coding to generate that 40kHz..??

mackrackit
- 21st September 2008, 08:10
http://www.rentron.com/Infrared_Communication.htm

http://www.rentron.com/PicBasic/infrared_object_detection.htm

skimask
- 21st September 2008, 08:24
http://www.rentron.com/Infrared_Communication.htm

http://www.rentron.com/PicBasic/infrared_object_detection.htm

Let the weeding begin...:D

mackrackit
- 21st September 2008, 08:29
Let the weeding begin...:D

It will be great when Bruce finishes his book. I will be doing some weeding then too :)

skimask
- 22nd September 2008, 04:16
It will be great when Bruce finishes his book. I will be doing some weeding then too :)

$20 says that even when Bruce does finish the book, we'll still be answering the same questions over and over and over...and not because of a lack of information from the book.
Maybe Bruce can include a couple of hands with each book.. ya know, for the user to hold while messing around with the PICs.

Picstar
- 22nd September 2008, 06:33
I am working with the following code using the lcd to display distance with the Ping sensor.
I am using the QL200 development board, so my DEFINE statements may be different or un-needed for you. My problem is that the ping sensor works,( I can see it on my oscilloscope),
but I cannot get a reading on my lcd. Only 0.
Any help would be appreciated!.

' File......sonar1.pbp
' Microcontroller used: Microchip Technology 16F887
'--------Program Desciption--------
' Using the ping ultrasonic range finder,
' the distance from the range finder to an object
' is displayed on an LCD screen in inches.
' rb0 is signal in/out
define osc 4
adcon1 = %00000000
define lcd_dreg portd
define lcd_dbit 4
define lcd_ereg porta
define lcd_ebit 3
dist var byte
range var word
inches CON 15 'Assigns the value 15 conversion
pause 2000

sonar 'Pulse Width
pulsout 0,20
pauseus 100
pulsin 0,10,range
dist = range/inches 'Converts raw sonar reading to inches
LCDOUT $FE,1,"Distance"
LCDOUT $FE,$14,#range
PAUSE 2000
GOTO sonar
END

Ioannis
- 22nd September 2008, 06:42
Try the DEIFINE's in capital.

Ioannis

skimask
- 22nd September 2008, 14:13
The define, in this case, won't make any difference since it's define osc 4, which is PBP default...



sonar 'Pulse Width

How does that work? Is it a label? Is it a command? Is it a comment?



pulsout 0,20
pauseus 100
pulsin 0,10,range

You wait for 100us instead of going straight to looking for a return pulse? How does THAT work? Shouldn't you jump directly to looking for the return pulse?
Which version of PBP are you using?

Picstar
- 22nd September 2008, 14:16
Tried capitalizing Define's but to no avail, you're right though they are supposed to be in capitol. Any other ideas??

Picstar
- 22nd September 2008, 14:24
You're right missing : in sonar label. According to the specs on the ping unit, there is a 750us pause called a "holdoff?". At any rate, I have tried it without a pause and have changed timing in the pulsin and pulsout commands, but still no luck. Using PBP 2.50b. heres the latest code:


' File......sonar1.pbp
' Microcontroller used: Microchip Technology 16F887
'--------Program Desciption--------
' Using the ping ultrasonic range finder,
' the distance from the range finder to an object
' is displayed on an LCD screen in inches.
' rb0 is signal in/out
adcon1 = %00000000
define LCD_DREG PORTD
define LCD_DBIT 4
define LCD_EREG PORTA
define LCD_EBIT 3
dist var byte
range var byte
inches CON 15 'Assigns the value 15 conversion
pause 2000

sonar: 'Pulse Width
pulsout portb.0,20
pulsin portb.0,100,range
'dist = range/inches 'Converts raw sonar reading to inches
LCDOUT $FE,1,"Distance"
LCDOUT $FE,$14,#range
PAUSE 2000
GOTO sonar
END

skimask
- 22nd September 2008, 14:30
Re-read the manual on the use of the pulsin statement.

You say the unit has a 'holdoff' of 750us, and yet you originally used a pauseus of 100. What's up with that?

Bruce
- 22nd September 2008, 17:18
Pulsin pin,state,var. State can only be a 1 or 0.

Also you need to disable A/D. Several pins you're using have A/D enabled by default.

skimask
- 22nd September 2008, 19:17
http://www.picbasic.co.uk/forum/showthread.php?t=561

Picstar
- 23rd September 2008, 00:13
Thanks to you all for your responses. The following code works but only after I hit the reset
button on my dev. board, then it works all the time. Even after shutting off power and restarting...strange. I'm a newbie so I'm puzzled by all the cmcon, adcon1, trisb etc. Read the datasheet on my pic? Yeah, right looks like Chinese to me.

' File......sonar1.pbp
' Microcontroller used: Microchip Technology 16F887
'--------Program Desciption--------
' Using the ping ultrasonic range finder,
' the distance from the range finder to an object
' is displayed on an LCD screen in inches.
' rb0 is signal in/out
anselh = 0
define LCD_DREG PORTD
define LCD_DBIT 4
define LCD_EREG PORTA
define LCD_EBIT 3
dist var word
range var word
inches CON 13 'Assigns the value 13 conversion
pause 2000 'wait for lcd to initialize
sonar:
pulsout portb.0,20
pulsin portb.0,1,range
dist = (range/inches) 'Converts raw sonar reading to inches
LCDOUT $FE,1,"Distance"
LCDOUT $FE,$14,#dist
PAUSE 2000
GOTO sonar
END

Bruce
- 23rd September 2008, 00:26
See if this helps. Add this to the init section of your code;

ANSEL = 0 ' disable A/D on porta
LOW 0 ' make portb.0 low so pulsout = a high going pulse on RB0

PULSOUT toggles a pin so the initial state of the pin should be set to the opposite of the
pulse logic you want.

ANSEL = 0 disables A/D for porta & porte.

Picstar
- 23rd September 2008, 10:21
Well, getting back to Chenko's original question, here is my latest code that includes an alarm when an object is closer than a preset limit. Thank's Bruce, but still no luck with reset problem. However, the code DOES work! Chenko you may need to change portb.0 to porte.0 and set the define statements for your board.

' File......sonar1.pbp
' Microcontroller used: Microchip Technology 16F887
'--------Program Desciption--------
' Using the ping ultrasonic range finder,
' the distance from the range finder to an object
' is displayed on an LCD screen in inches.
' rb0 is signal in/out
' alarm is sent when object is too close.
' reset before use
anselh = 0
define LCD_DREG PORTD
define LCD_DBIT 4
define LCD_EREG PORTA
define LCD_EBIT 3
dist var word
range var word
w1 con 35 ' alarm range value,change for your own use
inches CON 13 'Assigns the value 13 conversion
pause 2000 'wait for lcd to initialize

sonar:
pulsout portb.0,20
pulsin portb.0,1,range
dist = (range/inches) 'Converts raw sonar reading to inches
LCDOUT $FE,1,"Distance"
LCDOUT $FE,$14,#dist
if dist < w1 then alarm 'alarm distance
PAUSE 1000
GOTO sonar
alarm:
sound portc.0,[100,50] 'buzzer connected to RC0
goto sonar
END

Archangel
- 23rd September 2008, 10:27
You got PWRTE whatever flavor of powerup timer enable in your config statement ?

Acetronics2
- 23rd September 2008, 10:38
Hi,

As this card looks like an EasyPic 5 ...

Which power source do you use ???

and ... Disabling the Brownout feature ( default enabled ...) could be an idea.

BTW ... What about the Master Clear pin ???

Alain

Ioannis
- 23rd September 2008, 10:55
Yeah, Alain got me on the turn...

Have you connected somewhere the MCLR pin?

Ioannis

Picstar
- 23rd September 2008, 18:20
O.K., I disabled the mclr in my fuse configuration and that did the trick. Brownout voltage could not be turned off, only changed from 4.0 v to 2.1 v. I also tried pwrte but,no luck. Anyhow thanks to all, I could find nowhere else that dealt with the Ping module and specific code to interface with a PIC mcu.
Now anyone that wants to incorporate this sensor into a robotics or alarm project has access to some code that will work, or give a starting point for writing their own. Now, I wonder if I can spin the Ping module with a motor and have a quasi radar system ........


Those who never dream, never will.