步进电机控制
电路设计:
代码设计:
启动仿真,当两个按钮均未被按下时,LM016L液晶显示屏屏幕显示“stop”字样
4.当上面的按钮(与P0^0相连)被按下时,发现步进电机开始运转,同时LM016L液晶显示屏屏幕显示“fast”字样
5.当下面的按钮(与P0^2相连)被按下时,发现步进电机减速运转直至停止,同时LM016L液晶显示屏屏幕显示“slow”字样
C语言代码:
#include<reg51.h>
sbit F = P0^0; // Button for Fast Speed
sbit S = P0^2; // Button for Slow Speed
sbit RS = P3^1;// Register Select Pin of LCD
sbit EN = P3^2;// Enable Pin of LCD
#define LCD P2 // LCD Data pins
sbit Motor = P3^0;
void MSdelay(int n)
{
int i,j;
for(i=0;i<1275;i++)
for(j=0;j<n;j++);
}
void lcd_cmd(char a)
{
LCD = a;
RS = 0;
EN = 1;
MSdelay(1);
EN = 0;
}
void lcd_data(char b)
{
LCD = b;
RS = 1;
EN = 1;
MSdelay(1);
EN = 0;
}
void lcd_display(char *lcd)
{
while(*lcd!='\0')
{
lcd_data(*lcd);
lcd++;
}
}
void main()
{
lcd_cmd(0x01);
lcd_cmd(0x0E);
lcd_cmd(0x38);
lcd_display("DC Motor Speed");
while(1)
{
if(F==0)
{
lcd_cmd(0xC0);
lcd_display("Fast");
while(F==0)
{
Motor = 0;
MSdelay(100);
Motor = 1;
MSdelay(0);
}
}
if(S==0)
{
lcd_cmd(0xC0);
lcd_display("Slow");
while(S==0)
{
Motor = 0;
MSdelay(50);
Motor = 1;
MSdelay(50);
}
}
else
{
lcd_cmd(0xC0);
lcd_display("Stop");
Motor = 1;
}
}
}
C语言代码:
#include<reg51.h>
sbit F = P0^0; // Button for Fast Speed
sbit S = P0^2; // Button for Slow Speed
sbit RS = P3^1;// Register Select Pin of LCD
sbit EN = P3^2;// Enable Pin of LCD
#define LCD P2 // LCD Data pins
sbit Motor = P3^0;
void MSdelay(int n)
{
int i,j;
for(i=0;i<1275;i++)
for(j=0;j<n;j++);
}
void lcd_cmd(char a)
{
LCD = a;
RS = 0;
EN = 1;
MSdelay(1);
EN = 0;
}
void lcd_data(char b)
{
LCD = b;
RS = 1;
EN = 1;
MSdelay(1);
EN = 0;
}
void lcd_display(char *lcd)
{
while(*lcd!='\0')
{
lcd_data(*lcd);
lcd++;
}
}
void main()
{
lcd_cmd(0x01);
lcd_cmd(0x0E);
lcd_cmd(0x38);
lcd_display("DC Motor Speed");
while(1)
{
if(F==0)
{
lcd_cmd(0xC0);
lcd_display("Fast");
while(F==0)
{
Motor = 0;
MSdelay(100);
Motor = 1;
MSdelay(0);
}
}
if(S==0)
{
lcd_cmd(0xC0);
lcd_display("Slow");
while(S==0)
{
Motor = 0;
MSdelay(50);
Motor = 1;
MSdelay(50);
}
}
else
{
lcd_cmd(0xC0);
lcd_display("Stop");
Motor = 1;
}
}
}