#include <reg51.h>
#include<intrins.h>
sbit K1 = P3^2;//加速
sbit K2 = P3^3;//减速
sbit K3 = P3^4;//正传
sbit K4 = P3^5;//反转
sbit K5 = P3^6;//启动
sbit K6 = P3^7;//停止
sbit RS=P2^6;
sbit RW=P2^7;
void main( )
{
bit fangxiang = 1;//0是逆时针
bit kaiqi=0;//0是停止
int sudu= 4;//每次步进间隔时间为 1ms * sudu,也就是最快每秒25转
char str_buff[] = { "00.00" };
int speed = 0;
init_lcd();
lcd( 0, 0, "LCD_INIT" );
delay( 100 );
lcd( 0, 0, " " );
Init_Motor();
Set_Motor(kaiqi, fangxiang, sudu);
lcd( 1, 0, " reversal " );
lcd( 0, 0, "Speed: " );
lcd( 0, 12, "r/s" );
speed = 1000 / ( 1.0 * 8.0 * sudu ) * 1000;
str_buff[0] = speed / 10000 % 10 + '0';
str_buff[1] = speed / 1000 % 10 + '0';
str_buff[3] = speed / 100 % 10 + '0';
str_buff[4] = speed % 10 + '0';
lcd( 0, 6, str_buff );
while(1)
{
//加速
if( K1 == 0 )
{
delay( 50 );
if( K1 == 0 )
{
sudu--;
if( sudu <= 3 )
{
sudu = 4;
}
speed = 1000 / ( 1.0 * 8.0 * sudu ) * 1000;
str_buff[0] = speed / 10000 % 10 + '0';
str_buff[1] = speed / 1000 % 10 + '0';
str_buff[3] = speed / 100 % 10 + '0';
str_buff[4] = speed % 10 + '0';
lcd( 0, 6, str_buff );
Set_Motor(kaiqi, fangxiang, sudu);
// while( K1 == 0 );
}
}
//减速
if( K2 == 0 )
{
delay( 50 );
if( K2 == 0 )
{
sudu++;
if( sudu >= 60000 )
{
sudu = 60000;
}
speed = 1000 / ( 1.0 * 8.0 * sudu ) * 1000;
str_buff[0] = speed / 10000 % 10 + '0';
str_buff[1] = speed / 1000 % 10 + '0';
str_buff[3] = speed / 100 % 10 + '0';
str_buff[4] = speed % 10 + '0';
lcd( 0, 6, str_buff );
Set_Motor(kaiqi, fangxiang, sudu);
// while( K1 == 0 );
}
}
//正
if( K3 == 0 )
{
delay( 50 );
if( K3 == 0 )
{
fangxiang = 1;
lcd( 1, 0, "Forward rotation" );
Set_Motor(kaiqi, fangxiang, sudu);
while( K3 == 0 );
}
}
//反
if( K4 == 0 )
{
delay( 50 );
if( K4 == 0 )
{
fangxiang = 0;
lcd( 1, 0, " reversal " );
Set_Motor(kaiqi, fangxiang, sudu);
while( K4 == 0 );
}
}
//启
if( K5 == 0 )
{
delay( 50 );
if( K5 == 0 )
{
kaiqi = 1;
Set_Motor(kaiqi, fangxiang, sudu);
while( K6 == 0 );
}
}
//停
if( K6 == 0 )
{
delay( 50 );
if( K6 == 0 )
{
kaiqi = 0;
Set_Motor(kaiqi, fangxiang, sudu);
while( K6 == 0 );
}
}
}
}
求一个可以控制步数的程序另外有大佬可以看一下程序哪里有问题吗
|