kobre98
Full Member level 2
- Joined
- Sep 18, 2011
- Messages
- 145
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,296
- Location
- Gaza, Palestine
- Activity points
- 2,523
Hey
Now I'm using MicroCode studio Compiler
This is my code
Now I'm using MicroCode studio Compiler
This is my code
Code:
'*********************************************************************************************
' Sonar-ranger.bas
' Program to control the Devantech SRF04 ultrasonic module. Measure
' the distance between the the unit and an object. Convert the raw
' data to inches and centimeters and then send the results to an LCD
' display. PicBasic Pro Compiler by MicroEngineering Labs. The
' conversion factors listed in the SRF04 manual are for the PULSIN
' of a Basic Stamp II which measures in 2us increments. When running
' a PICmicro MCU at 4 MHz and using the PicBasic Pro compiler the
' PULSIN command measures in 10us increments and the PULSOUT command
' also generates pulses with 10us increments. The adjusted conversion
' factors for 10us increments are:
' inches = raw_data / 15
' centimeters = raw_data / 6
'in picbasic stamp " convert to inch ---> 1 / 73.746
' convert to cm ---> ' 1 / 29.034
'10us in picbasic pro but in basic stamp 2us for pulse so :
'The adjusted conversion factors for 10us increments are:
' inches = raw_data / 15 ' 73.746 / 5 =~ 15
' centimeters = raw_data / 6 ' 29.034 / 5 =~6
'*********************************************************************************************
'Set LCD Data port
DEFINE LCD_DREG PORTB
' Set starting Data bit (0 or 4) if 4-bit bus
DEFINE LCD_DBIT 4
' Set LCD Register Select port
DEFINE LCD_RSREG PORTB
' Set LCD Register Select bit
DEFINE LCD_RSBIT 1
' Set LCD Enable port
DEFINE LCD_EREG PORTB
' Set LCD Enable bit
DEFINE LCD_EBIT 0
' Set LCD bus size (4 or 8 bits)
DEFINE LCD_BITS 4
' Set number of lines on LCD
DEFINE LCD_LINES 2
' Set command delay time in us
DEFINE LCD_COMMANDUS 2000
' Set data delay time in us
DEFINE LCD_DATAUS 50
trisa = %00000001
TrisB=0
Buzzer var PORTA.1
'*********************************************************************************************
' variables adapted for PING)))(TM) Ultrasonic Range Finder (#28015) by Kenneth Wong (2006/06/07)
' trigger var PORTB.2
' echo var PORTB.3
sonar VAR PORTb.2
'*********************************************************************************************
dist_raw var word
dist_inch var word
dist_cm var word
conv_inch con 15
conv_cm con 6
Lcdout $fe,1 ' clear lcd screen
pause 1000 ' give time to initialize lcd
Lcdout $fe,$80,"- Sonar Ranger -" ' display program title on the LCD
sound Buzzer,[100,10,50,5,70,10,50,2] ' Make startup sound
pause 1000 ' wait for 1 second
Lcdout $fe,1
Lcdout $fe,$80,"inches:" ' set up the LCD display
Lcdout $fe,$C0,"cent:"
main:
gosub sr_sonar ' get sonar reading
Lcdout $fe,$89,#dist_inch," Inch " ' display the distance in inches
Lcdout $fe,$C7,#dist_cm," cm " ' display the distance in centimeters
Goto main
sr_sonar:
'*********************************************************************************************
' I/O assignments adapted for PING)))(TM) Ultrasonic Range Finder (#28015) by Kenneth Wong (2006/06/07)
output sonar ' set sonar pin as output
low sonar ' logic low for sonar pin
pulsout sonar,1 ' send a 10us trigger pulse to the SRF04
input sonar ' set sonar pin as input
pulsin sonar,1,dist_raw ' start timing the pulse width on echo pin
'*********************************************************************************************
dist_inch = (dist_raw/conv_inch) ' Convert raw data into inches
dist_cm = (dist_raw/conv_cm) ' Convert raw data into centimeters
pause 1 ' wait for 10us before returning to main
return
end