vibration uart code
/*
* Project name:
Vibration sensor
* Copyright
(c) Researchdesignlab.com
* Description:
* Test configuration:
MCU: AT89S52
Dev.Board: 8051
Oscillator: 11.0592 MHz
Software: Keil uVision3
*/
#include<reg51.h>
#define LCD_PORT P2 // LCD D0-D7 PINS connected P2
sbit rs=P3^5; // LCD RS PIN connected P3.5
sbit en=P3^7; // LCD EN PIN connected P3.7
sbit D7=P2^7;
sbit rw=P3^6; // LCD RW PIN connected P3.6
void busy(); //LCD busy
void CMD_WRT(unsigned char); //To write the commands to the LCD
void DATA_WRT(unsigned char); //To write the data to the LCD
void LCD_WRT(unsigned char *);
void DELAY();
unsigned char SCI_ReceiveByte( void );
void main()
{
unsigned char CMD[]={0x38,0x01,0x0f,0x06,0x80},TEMP1,i,REC;
for(i=0;i<5;i++)
{
TEMP1=CMD[i]; //write the commands to the LCD
CMD_WRT(TEMP1);
}
TMOD=0X20; //TIMER 1 , MODE 2
TH1=0XFD; //9600 BAURD RATE //SERAIL INIT
SCON=0X50;
TR1=1; //TIMER1 ON
DELAY();
DELAY();
DELAY(); //DELAY
DELAY();
CMD_WRT(0x01);
CMD_WRT(0X80);
LCD_WRT("Vibration sensor");
while(1)
{
REC=0;
CMD_WRT(0XC0);
while(REC!=0X0D)
{
REC=SCI_ReceiveByte();
DATA_WRT(REC);
}
}
}
void DELAY()
{ //delay of 3ms
unsigned int X=800000,y=800000;
while(X--);
while(y--);
}
void busy()
{
D7=1;
rs=0; //cmd mode
rw=1; //read
while(D7!=0) //wait till LCD is ready
{
en=0;
en=1; // low to high latch
}
}
void CMD_WRT(unsigned char val)
{
busy();
LCD_PORT=val; //write cmd to P2
rs=0; //cmd mode
rw=0; //write
en=1;
en=0; //high to low latch
}
void DATA_WRT(unsigned char ch)
{
busy();
LCD_PORT = ch; //write cmd to P2
rs=1; //data mode
rw=0; //write
en=1;
en=0; //high to low latch
}
void LCD_WRT(unsigned char *string)
{
while(*string)
DATA_WRT(*string++);
}
unsigned char SCI_ReceiveByte( void )
{ // RECIVING SERIAL DATA
unsigned char byte;
while(RI!=1); // WAIT TILL RI IS HIGH
byte = SBUF; //RECIVE DATA FROM SBUF TO BYTE
RI=0;
return byte; // RETURN THE DATA
}