You are on page 1of 4

8051 program

#include<reg51.h>
#include<stdio.h>
#include<serial.h>
#include<lcd.h>
#include<delay.h>
sbit relay1 = P2^0;
sbit relay2 = P2^1;
sbit sensor = P3^2;
void recive_count();
xdata unsigned char rotatebuf[20],unitbuf[20],unit=0;
int rotate=0;
unsigned char temp;
void count(void);
void main()
{
intSerial();
intLcd();
putCLcdC(0x80);
putSLcd("Energy Power");
putCLcdC(0xc0);
putSLcd("Management");
relay1 = 0;
relay2 = 0;
delay_ms(1000);
putCLcdC(0x01);
IE = 0x92;
while(1)
{
putCLcdC(0x01);
sprintf(rotatebuf, "Rotation:-%d",rotate);
putCLcdC(0x80);
putSLcd(rotatebuf);
sprintf(unitbuf, "UNIT:-%bu",unit);
putCLcdC(0xc0);
putSLcd(unitbuf);
delay_ms(250);
count();
}
}
void count(void)
{
if(sensor == 0)
{
while(sensor == 0);

rotate++;
if(rotate >= 25)
{
rotate=0;
unit--;
if(unit <=10 && unit>0)
{
relay1 = 1;
relay2 = 0;
}
if(unit == 0)
{
relay1 = 0;
relay2 = 0;
putSSerial("Power Is Over");
}
}
delay_ms(500);
}
}
void serial0() interrupt 4
{
if(RI == 1)
{
temp=SBUF;
if(temp =='A')
{
unit = 50;
relay1 = 1;
relay2 = 1;
}
else if(temp == 'B')
{
unit = 10;
relay1 = 1;
relay2 = 0;
}
RI=0;
}
}
#include"reg51.h"
#include "serial.h"
void intSerial(void)
{
SCON=0X50;
//----Using Timer 1-----------TMOD=0X20;

TH1=-3;
//9600 Baud Rate
TR1=1;
//IE = 0x92;
//------------------------------}
void putSSerial(unsigned char *mess)
{
while(*mess)
putCSerial(*mess++);
}
void putCSerial(unsigned char ch)
{
SBUF=ch;
while(!TI);
TI=0;
}
unsigned char getCSerial(void)
{
char ch;
while(!RI);
RI=0;
ch=SBUF;
return ch;
}
void flushSerialPort( void )
{
if( RI ) getCSerial();
}
#include<reg51.h>// header file for 89cv51rd2bn_ic
//-----External PINS--------------#define lcd_data P0//define p2_port as a lcd_bus
sbit RS=P1^0;
sbit RW=P1^1;
sbit E=P1^2;

//define p1.7 as rs pin of display


//define P1.5 as read/write pin of display
//define P1.6 as enable pin of display

sbit BUSY=P0^7;
//define P2.7 as busy flag pin of display
//*******************lcd busy_flag_cheack subroutene**************
void lcd_wait(void)
{
E=0;
lcd_data=0XFF;
RW=1;
RS=0;
for(E=0,E=1;BUSY;E=0,E=1);
E=0;
RW=0;
}

//*********LLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLLL**********LC
D command subroutine***********************
void putCLcdC(unsigned char com)
{
lcd_wait();
RS=0;
lcd_data=com;
E=1;
E=0;
}
//*********************LCD data subroutine*************************
void putCLcdD(unsigned char dat){
lcd_wait();
RS=1;
lcd_data=dat;
E=1;
E=0;
}
//**********************lcd_inti subroutene*************************
void intLcd(void){
putCLcdC(0X38);
putCLcdC(0X0c);
putCLcdC(0X01);
}
//*********************string_display subroutene********************
void putSLcd(unsigned char *st) {
for( ;*st ;putCLcdD(*st++) );
}
//-----------------------------end---------------------------------void delay_ms(unsigned int msec)
{
volatile int i, j;
for(j=0; j<=msec; j++)
for(i=0; i<167; i++);
}
void delay_us( volatile unsigned int usec )
{
while( --usec );
}

You might also like