Jump to content
Electronics-Lab.com Community

Ultrasonic Sensor using PIC for my FYP...


Steven5103

Recommended Posts

i need help...
the ultrasonic sensor could not work using the software i'm having...
there are current going to PIC but the PIC has no output for the ultrasonic.
i think the problem should be on the software...
but i cant figure out which part of the software has mistakes...
i need help please...
thank you..
dsc01112.jpg


The software :
#include <stdio.h>
#include <htc.h>
#include "delay.h"
#include "lcd.h"

#define ULTRASONIC_SENSOR RC2
#define MOTOR1_SIGNAL     RB7
#define MOTOR1_DIRECTION  RB6
#define MOTOR2_SIGNAL     RB5
#define MOTOR2_DIRECTION  RB4

__CONFIG(0x3f71);

void main ( void )
{
   unsigned int sensorPulseRisingEdge, sensorPulseFallingEdge;
   unsigned int sensorPulseWidth;
   char LCDString [ 17 ];
   
   // Initializations
   TRISB = 0x00;
   TRISC = 0x00;

   lcd_init();

   lcd_goto ( 0x00 );
   lcd_puts ( "Pulse width:" );

   MOTOR1_DIRECTION = MOTOR2_DIRECTION = 0;

   T1CON = 0x01;

   while ( 1 )
   {
       // Body
TRISC2 = 0;
RC2 = 1;
DelayUs ( 5 );
RC2 = 0;
TRISC2 = 1;

       // Measure pulse width of ultrasonic sensor
       while ( RC2 == 0 ) // wait for RC2 to go high
           continue;

       sensorPulseRisingEdge = ( TMR1H << 8 ) + TMR1L;

       while ( RC2 == 1 ) // wait for RC2 to go low
           continue;

       sensorPulseFallingEdge = ( TMR1H << 8 ) + TMR1L;

       sensorPulseWidth = sensorPulseFallingEdge - sensorPulseRisingEdge;

       lcd_goto ( 0x40 );
       sprintf ( LCDString, "%d us    ", sensorPulseWidth );
       lcd_puts ( LCDString );

       DelayMs ( 250 );
       DelayMs ( 250 );
   }
}

Link to comment
Share on other sites


Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
  • Create New...