DC Motor
#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
sbit IN1=P1^0; // IR sensor PIN connected P1.0
sbit IN2=P1^1;
sbit IN3=P1^2;
sbit IN4=P1^3;
unsigned char SCI_ReceiveByte( void );
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();
void main()
{
unsigned char CMD[]={0x38,0x01,0x0f,0x06,0x80},TEMP1,i,RX;
for(i=0;i<5;i++)
{
TEMP1=CMD[i]; //write the commands to the LCD
CMD_WRT(TEMP1);
}
TMOD=0X20;
TH1=0XFD;
SCON=0X50;
TR1=1;
P1=0X00;
while(1)
{
RX=SCI_ReceiveByte();
DELAY();
if(RX=='F')
{
IN1=1;
IN2=1;
IN3=0;
IN4=0;
CMD_WRT(0X01); // LCD clear cmd
CMD_WRT(0X80); // LCD 1st line cmd
LCD_WRT("MOTOR FORWARD"); // Display the data on LCD
DELAY();
}
else if(RX=='B')
{
IN3=1;
IN4=1;
IN1=0;
IN2=0;
CMD_WRT(0X01); // LCD clear cmd
CMD_WRT(0X80); // LCD 1st line cmd
LCD_WRT("MOTOR BACKWARD"); // Display the data on LCD
DELAY();
}
else if(RX=='L')
{
IN1=1;
IN3=1;
IN2=0;
IN4=0;
CMD_WRT(0X01); // LCD clear cmd
CMD_WRT(0X80); // LCD 1st line cmd
LCD_WRT("MOTOR LEFT"); // Display the data on LCD
DELAY();
}
else if(RX=='R')
{
IN2=1;
IN4=1;
IN1=0;
IN3=0;
CMD_WRT(0X01); // LCD clear cmd
CMD_WRT(0X80); // LCD 1st line cmd
LCD_WRT("MOTOR RIGHT"); // Display the data on LCD
DELAY();
}
else if(RX=='S')
{
IN1=0;
IN2=0;
IN3=0;
IN4=0;
CMD_WRT(0X01); // LCD clear cmd
CMD_WRT(0X80); // LCD 1st line cmd
LCD_WRT("MOTOR STOP"); // Display the data on LCD
DELAY();
}
}
}
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 )
{
unsigned char byte;
while(RI!=1);
byte = SBUF;
RI=0;
return byte;
}
void DELAY()
{ //delay of 3ms
unsigned int X=800000;
while(X--);
}