PIC17F877A Accelometer Interface

By Kunal Kapoor
12-05-2018
470

Intro

In this project we will be interfacing the 3 axis accelometer sensor and taking the readings of all 3 axis outputs. We will measure the response as a voltage output.

 

The digital output of data can then be used to perform any application specific task. Eg this can be used in making stabilization system for a UAV, Quadcopter, or control a motion sensitive robot.

We will be using an AD accelometer with following pin configuration

 

Pin X,Y,Z are the three main output pins giving the output voltage ranging from 0 to 3,3V

Each of these pins represents an axis. The output of these three pins will be received in ADC input of PIC microcontroller and will be decoded to display on a LCD screen

In this project we will be simply interfacing the accelometer in recording inputs and displaying it as a real time voltmeter. However as per your project needs, you can configure the data and manipulate as per your own needs.

 

S. no

Item Name

Qty

1

PIC16F877A MCU

1

2

16X2 LCD

1

3

Accelometer module

1

 

 

CIRCUIT:



IDE : MPLAB from Microchip


Code:

snippet:


main.h:

#include

__CONFIG(0x2f0a);

#include"delay.h"

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--;
	}
}

main2.c:
#include"main.h"
#include

/////serial somm
void lcdcmd(unsigned char y);
void lcdat(unsigned char x);


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

unsigned char tr[]={"000000"};
unsigned int n=0,i=0,j;
unsigned int voltage1,voltage2;
unsigned int x,y,z;
	

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

//	TRISB=0x00;
	//TRISC=0xff;


	unsigned int count,adresll=0,adreslh=0,total=0,lower,higher;

	ADCON0=0x01;		//channel 0 selected initially
	ADCON1=0xc3;		//initialize adc
	
	//////takng humidity readings and converting it into decimal

	while(1)
	{
	
	//	calculating voltage of x
		voltage1=0;
		for(j=0;j<10;j++)
		{
		ADCON0=0x05;			///channe1 1 
		while(ADIF!=1);
		//ADCON0=0x00;
		ADIF=0;
		lower+=ADRESL;
		higher+=ADRESH;
		}
		lower=lower/j;
		higher=higher/j;

		tr[0]=higher;
		tr[1]=lower;
//		adreslh=lower/(0x10);
//		adresll=lower-(adreslh*0x10);

				
//		total=(ADRESH*256)+(adreslh*16)+(adresll);		//output from adc in decimal
//		voltage1=total*(4.88)/(10);
		


//	calculating voltage of y
	
		lower=0x00;
		higher=0x00;
		total=0;
		voltage2=0;
	
		for(j=0;j<5;j++)
		{
		ADCON0=0x19;
		while(ADIF!=1);
		//ADCON0=0x00;
		ADIF=0;
		lower+=ADRESL;
		higher+=ADRESH;
		}
		lower=lower/5;
		higher=higher/5;

		tr[2]=higher;
		tr[3]=lower;

	

	//	calculating voltage of z
		voltage1=0;
		for(j=0;j<10;j++)
		{
		ADCON0=0x11;			///channe1 3 
		while(ADIF!=1);
		//ADCON0=0x00;
		ADIF=0;
		lower+=ADRESL;
		higher+=ADRESH;
		}
		lower=lower/j;
		higher=higher/j;
		
		tr[5]=higher;
		tr[4]=lower;
	
TXSTA=0x20;
RCSTA=0x90;
	SPBRG=0x19;		//baud rate=9600
//while(TXIF==0);		//until tr is empty
		RB1=1;
		for(i=6;i>0;i--)
		{
			
			i--;
			TXREG=tr[i];
			while(TXIF==0);		//until tr is empty
			TXIF=0;
			
		}
	RB1=0;
}	
}





Related Tutorials