in this project you are gonna learn to control both speed and direction of unipolar stepper motor. The speed and direction of motor can be adjusted with help of 4X4 keypad while displaying data simultaneously on 16*2 lcd screen.first enter the direction via direction control keys(righ and left) on keypad followed by speed to which the motor to be rotated and finally press the Enter key to start the operation. A reset pin is provided which is used at the end of operation to execute the code repetitively.
PROTEUS DESIGN
CODE
/*stepper motor speed control(1-1000rpm) via keypad*/
#include<reg51.h>
#define Error 20
#define stepper P3
sbit rs = P2^0;
sbit en = P2^1;
sbit sw = P3^7;
void lcdint();
void lcdcmd(char);
void lcddata(char);
void lcdstring(char *p);
void delay(int);
void monitor_motor(unsigned int,unsigned char);
void togle_e_pin();
char swich();
char scankey();
sbit r1 = P1^0; //RowA
sbit r2 = P1^1; //RowB
sbit r3 = P1^2; //RowC
sbit r4 = P1^3; //RowD
sbit c1 = P1^4; //Column1
sbit c2 = P1^5; //Column2
sbit c3 = P1^6; //Column3
sbit c4 = P1^7; //Column4
char key;
unsigned int s;
char direc;
unsigned char num[10];
void main()
{
unsigned int u=0;
P2=0xf0;
P3=0xff;
sw=0;
rs=0;
en=0;
lcdint();
while(1)
{
lcdcmd(0x80);
lcdstring("direction");
direc=swich();
if(direc=='R'||direc=='L')
{
if(direc=='R')
{
lcdcmd(0x8b);
lcdstring("RIGHT");
}
if(direc=='L')
{
lcdcmd(0x8b);
lcdstring("LEFT");
}
lcdcmd(0xc0);
lcdstring("speed");
key=swich();
lcdcmd(0xcd);
while(key!=Error)
{
if(key!='R'&&key!='L')
{
num[u]=key;
lcddata(key);
u++;
key=swich();
delay(20000);
}
key=swich();
}
s=num[2]*100+num[1]*10+num[0]*1;
monitor_motor(s,direc);
}
}
}
void delay(int k)
{
int i;
for(i=0;i<k;i++);
}
void togle_e_pin()
{
en= 1;
delay(100);
en= 0;
delay(100);
}
void lcddata(char value)
{
rs= 1;
P2 &= 0x0F;
P2 |= (value&0xF0);
togle_e_pin();
P2 &= 0x0F;
P2 |= ((value<<4)&0xF0);
togle_e_pin();
}
void lcdcmd(char value)
{
rs= 0;
P2 &= 0x0F;
P2 |= (value&0xF0);
togle_e_pin();
P2 &= 0x0F;
P2 |= ((value<<4)&0xF0);
togle_e_pin();
}
void lcdint()
{
P2 &= 0x0F;
P2 |= (0x30&0xF0);
togle_e_pin();
delay(500);
P2 &= 0x0F;
P2 |= (0x20&0xF0);
togle_e_pin();
delay(500);
lcdcmd(0x28);
lcdcmd(0x01);
lcdcmd(0x0c);
}
void lcdstring(char *p)
{
while(*p!='\0')
{
lcddata(*p);
delay(2000);
p++;
}
}
char swich()
{
char key='a';
while(key=='a')
{
key=scankey();
}
return key;
}
char scankey()
{
r1=0;r2=r3=r4=1;
if (c1==0) { delay(100);return '7'; }
if (c2==0) { delay(100);return '8'; }
if (c3==0) { delay(100);return '9'; }
if (c4==0) { delay(100);return 'L'; } //left
r2=0;r1=r4=r3=1;
if (c1 == 0) { delay(100);return '4'; }
if (c2 == 0) { delay(100);return '5'; }
if (c3 == 0) { delay(100);return '6'; }
if (c4 == 0) { delay(100);return 'R'; } //right
r3=0;r1=r2=r4=1;
if (c1 == 0) { delay(100);return '1'; }
if (c2 == 0) { delay(100);return '2'; }
if (c3 == 0) { delay(100);return '3'; }
r4=0;r1=r2=r3=1;
if (c2 == 0) { delay(100);return '0'; }
if (c3 == 0) { delay(100);return Error; } //enter
return 'a';
}
void monitor_motor(unsigned int l,unsigned char rotation)
{
unsigned int speed;
speed=(5000000*(l^2));
while(rotation=='R'||rotation=='L')
{
if(rotation=='R')
{
stepper=0x01;
delay(speed);
stepper=0x02;
delay(speed);
stepper=0x04;
delay(speed);
stepper=0x08;
delay(speed);
}
if(rotation=='L')
{
stepper=0x08;
delay(speed);
stepper=0x04;
delay(speed);
stepper=0x02;
delay(speed);
stepper=0x01;
delay(speed);
}
if(sw==1)
break;
}
lcdcmd(0x01);
}
VIDEO
Category:

Comments
This is a fully complete code