|
|
View previous topic :: View next topic |
Author |
Message |
amjad_alahdal
Joined: 19 Feb 2013 Posts: 50
|
The reading is not right |
Posted: Wed Sep 04, 2013 4:40 am |
|
|
Hi,
I used PIC18F2455 Crystal is 16MHz,
I used an ultrasonic range finder such as this one :
https://www.sparkfun.com/products/9491
it's very obvious that I can use 3 ways to get the reading,
I could read the voltage using the analog, but, it wasn't very accurate. So, I tried to use the RS232
I used the following Code :
Code: |
#include<18F2455.h>
#device PASS_STRINGS = IN_RAM
#FUSES HS,NOPROTECT, NOLVP,NOWDT
#USE DELAY(CLOCK = 16M)
#define Indicator_LED PIN_A5
#define To_LCD PIN_B7
#define FROM_URF PIN_B6
#USE RS232(BAUD = 9600,bits = 1,XMIT = TO_LCD, RCV = FROM_URF,STREAM = COM_LCD,ERRORS)
#include<lcd.h>
#include<math.h>
void main()
{
unsigned int32 distance = 0 ;
delay_ms(1000);
fprintf(COM_LCD ,"The Distance:");
lcd_cursor_move(2,5);
fprintf(COM_LCD,"MM");
while(true)
{
delay_ms(250);
distance = getc();
lcd_cursor_move(2,1);
fprintf(COM_LCD,"%ld",distance);
}
}
|
but the reading is totally wrong
I will be happy if you help me |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9244 Location: Greensville,Ontario
|
|
Posted: Wed Sep 04, 2013 6:39 am |
|
|
You've got to read the datasheet again....page two
When using 'RS-232..serial' the data from the sensor is sent as a 'datastream' consisting of ASCII 'R', then 3 numbers, then a Carriage return.
Knowing this you can either use the GETS() CCS function or use the 'ex_sisr.c' example and make a buffered serial ISR.
How you proceed is up to you..
Personally , I'd modify the ex_sisr.c program as it allows capturing the sonar data 'in the bakground' while your 'main' program does 'other things'.
hth
jay |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19545
|
|
Posted: Wed Sep 04, 2013 7:45 am |
|
|
Provided you have good smoothing, and are not doing anything silly, the analog output from these is very good. It is ratiometric to the supply, so isn't affected by supply variations, and can give accuracies nearly as good as the sensor gets....
Remember the sensor _will_ give instantaneous spurious readings as 'noises off' are picked up. Were you selecting the ADC clock correctly?. Were you allowing for the acquire time?. Was the line from the sensor properly shielded?.
For reading the serial, search here for threads about 'state machines'. A very simple one in the serial ISR, is the best way to do this.
Best Wishes |
|
|
|
|
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
|
Powered by phpBB © 2001, 2005 phpBB Group
|