Toll-Free Customer Support 24/7

Stepper 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

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 DELAY1();

void main()
{
unsigned char CMD[]={0x38,0x01,0x0f,0x06,0x80},TEMP1,i,RX,j;

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)
{
P1=0X00;
RX=SCI_ReceiveByte();
DELAY();

if(RX=='F')
{
for(j=0;j<425;j++) //FORWARD
{
P1=0x11;
DELAY1();
P1=0x22;
DELAY1();
P1=0x44;
DELAY1();
P1=0x88;
DELAY1();
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')
{
for(j=0;j<425;j++) //BACKWARD
{
P1=0x88;
DELAY1();
P1=0x44;
DELAY1();
P1=0x22;
DELAY1();
P1=0x11;
DELAY1();
CMD_WRT(0X01); // LCD clear cmd
CMD_WRT(0X80); // LCD 1st line cmd
LCD_WRT("MOTOR BACKWARD"); // 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--);
}
void DELAY1()
{
unsigned int z=500;
while(z--);
}

Back to top