2 Attachment(s)
Ultrasonic sensor, serial communication.
Hi Folks,
I need a little help with this task. I want to read data from Ultrasonic distance meter which put out every 100ms serial data.
The protocol is here:
Attachment 8945
The captured data looks like:
Attachment 8946
My program trying to capture this data looks like:
Code:
#CONFIG
__config _CONFIG1, _FOSC_INTOSC & _WDTE_OFF & _PWRTE_ON & _MCLRE_OFF & _CP_OFF & _CPD_OFF & _BOREN_ON & _CLKOUTEN_OFF & _IESO_ON & _FCMEN_ON
__config _CONFIG2, _WRT_OFF & _PLLEN_ON & _STVREN_ON & _BORV_19 & _LVP_OFF
#ENDCONFIG
;MCU PIC 12F1840
DEFINE OSC 4 ; Use internal clock 4MHz
OSCCON = %01101010 ; 4 MHz internal
OSCTUNE = %00000000 ; Internal osc tunning
TRISA = %100000 ; RA.5 input rest output
ANSELA = %00000
OPTION_REG.7=1 ; disable internal pull-ups
APFCON = %10001100
string var byte[16]
' Set receive register to receiver enabled
DEFINE HSER_RCSTA 90h
' Set transmit register to transmitter enabled
DEFINE HSER_TXSTA 24h
DEFINE HSER_BAUD 9600
clear
start:
hSerin 100,start,[wait ($ff),str string\16]
pause 100
HSEROUT [hex string,13,10]
pause 500
goto start
obviously it doesn't work because my poor programming skills.
I searched the forum to find something to point me how to capture these data but no success.
Can someone point me how to do that?
Thanks,
Louis
Re: Ultrasonic sensor, serial communication.
Try something like:
Code:
Distance VAR WORD
Sum VAR BYTE
Start:
HSERIN 1000, NoData, [wait ($FF), Distance.BYTE1, Distance.BYTE0, SUM]
' Here you could evalute SUM to verify packet is correct but lets not do that now.
HSEROUT["Distance: ", DEC Distance, "mm", 13]
Goto Start
NoData:
HSEROUT["No data from sensor", 13]
Goto Start
/Henrik.
Re: Ultrasonic sensor, serial communication.
Thanks Henrik,
It works.
How simple and elegant solution.