PIC16F877A Ultrasound Interface

By Kunal Kapoor
12-05-2018
307

In this ultrasound project we will be interfacing the standard issue ultrasound module available in markets and will display its data on LCD screen

Ultrasound module which we will be using is a TTL one and not PWM. TTL offers easy accessibility of data at 500ms data interval. Data can be fetched using serial port of the MCU.

 

S. no

Item Name

Qty

1

PIC16F877A MCU

1

2

16X2 LCD display

1

3

Ultrasound module TTL

1 module

4

Miscellaneous

-

               

Project


 Circuit Diagram:



IDE: MPLAB by Microchip


Code Snippet:


main.h:

#include
#include"delay.h"
#define _XTAL_FREQ 16000000
__CONFIG(0x2F0A);
void main(void);


delay.h:

#ifndef DELAY_H
#define DELAY_H

//FUNCTION PROTOTYPE
void delay(unsigned int time);

//DEFINE USER DEFINED DATATYPE
typedef unsigned char ubyte;

#endif


delay.c:

#include "delay.h"

void delay(unsigned int time)
{
	unsigned int pause;
	while( time > 0)
	{
		pause = 255;
		while(pause>0)
		{pause--;}
		time--;
	}
}

sandeepultrasound.c:
#include"main.h"
#include
//void lcd_initialize(void);

void lcd_data(unsigned char r);
void lcd_command(unsigned char y);

#define rs RB7
#define rw RB6
#define en RB5
#define ldata PORTD

unsigned char name[]={"distance: "};
unsigned int i,sum=0;
unsigned char a[]={"00000"};

void main(void)
{
TXSTA=0x20;
RCSTA=0x90;
	SPBRG=0x19;		//baud rate=9600

	TRISB=0;
	TRISD=0;
	TRISC0=0;
	TRISC1=0;
	TRISC2=0;

	PORTB=0x00;
	delay(10);
	lcd_command(0x38);
	delay(10);
	lcd_command(0x0c);
//	delay(10);
	lcd_command(0x01);
	delay(10);
	//lcd_command(0x06);
	//delay(10);
	
	while(name[i]!=NULL)
	{
	lcd_data(name[i]);
	i++;
	}
	unsigned char temp;

	
	while(1)
	{
		//lcd_command(0x89);
		if(temp==0x0d)
		{
		sum=((a[0]-48)*(100))+((a[1]-48)*(10))+((a[2]-48))+((a[4]-48)/(10));
		if(sum<20)
		{PORTC=0xff;}
			
		if(sum>20)
		{PORTC=0x00;}
			
		i=0;
		sum=0;
		}

		RCIF=0;
		RB1=0;
		while(RCIF==0);	/* set when register is not empty */	
		temp=RCREG;

		if(temp!=0x0d)
		{
		a[i]=temp;
		lcd_data(temp);
		i++;
		}
		//delay(100);
		RB1=1;
	
	}
}

void lcd_data(unsigned char r)
{
	rs=1;
	rw=0;
	ldata=r;
	en=1;
	delay(1);
	en=0;
//	RB0=1;
	
}

void lcd_command(unsigned char y)
{
	rs=0;
	rw=0;
	ldata=y;
	en=1;
	delay(1);
	en=0;
} 

Download the code from here




Related Tutorials