紧急求助!单片机C语言 求标注!
#include <reg52.h>
#include<LCD1602.h>
#define uint unsigned int
#define uchar unsigned char
//本程序使用4相步进电机工作与8拍方式
//正转励磁序列为A->AB->B->BC->C->CD->D->DA
uchar FFW[]=
{
0x01,0x03,0x02,0x06,0x04,0x0c,0x08,0x09
};
//反转励磁序列为AD->D->CD->C->BC->B->AB->A
uchar REV[]=
{
0x09,0x08,0x0c,0x04,0x06,0x02,0x03,0x01
};
sbit K1 = P3^0; //正转
sbit K2 = P3^1; //反转
sbit K3 = P3^2; //停止
sbit K4 = P3^3; //加速
sbit K5 = P3^4; //减速
static bit K1_flag;
static bit K2_flag; // 按键按下标志
static double speed_count=30;//速度计数
uint count;
uchar t;
static double time;
static long t_time;
uchar time_temp[5];
//延时
void DelayMS(uint ms)
{
uchar i;
while(ms--)
{
for(i=0;i<120;i++);
}
}
void main()
{
EA=1;
ET0=1;
TMOD=0x01; //使用定时器T0的模式1
TH0=(65536-25000)/256; //定时器T0的高8位赋初值 //定时25ms
TL0=(65536-25000)%256; //定时器T0的高8位赋初值
TR0=1; //启动定时器T0
L1602_init(); //液晶初始化
L1602_string(1,1,"State: ");
L1602_string(2,1,"Speed: ");
while(1);
}
void time_0() interrupt 1
{
TH0=(65536-25000)/256; //定时器T0的高8位赋初值
TL0=(65536-25000)%256; //定时器T0的高8位赋初值
count++;
//速度计算 步进电机一圈8步 每一步是时间是speed_count 总8*speed_count ms
//速度每分钟1-100转的情况下:转1转的计算:60s*1000=60000ms,60000/转数*8步*25ms
//每分中的速度是 60000/8*speed_count
time=60000/(speed_count*8*25); // 正常是用1000/(speed_count*8) 如果这样定时器应该设成 TH0=(65536-25000)/256; 25ms
t_time=(long)(time*1000); //扩大百倍方便计算
time_temp[0]=t_time/100000;
time_temp[1]=t_time/10000%10; //十位
time_temp[2]=t_time/1000%10;
time_temp[3]=t_time/100%10;
time_temp[4]=t_time/10%10;
if(count==speed_count)
{
count=0;
t++;
if(t==8)
{
t=0;
}
if(K1_flag==1) //正转
{
P1 =~FFW[t];
}
if(K2_flag==1) //正转
{
P1 =~REV[t];
}
}
if(K1 == 0)
{
K1_flag=1;
K2_flag=0;
L1602_string(1,1,"State: Foreward!");
L1602_char(2,8,time_temp[0]+0x30);
L1602_char(2,9,time_temp[1]+0x30);
L1602_char(2,10,time_temp[2]+0x30);
L1602_char(2,11,'.');
L1602_char(2,12,time_temp[3]+0x30);
L1602_char(2,13,time_temp[4]+0x30);
L1602_char(2,14,'r');
L1602_char(2,15,'/');
L1602_char(2,16,'m');
}
if(K2 == 0) //反转
{
K2_flag=1;
K1_flag=0;
L1602_string(1,1,"State: Inversion!");
L1602_char(2,8,time_temp[0]+0x30);
L1602_char(2,9,time_temp[1]+0x30);
L1602_char(2,10,time_temp[2]+0x30);
L1602_char(2,11,'.');
L1602_char(2,12,time_temp[3]+0x30);
L1602_char(2,13,time_temp[4]+0x30);
L1602_char(2,14,'r');
L1602_char(2,15,'/');
L1602_char(2,16,'m');
}
if(K3 == 0) //停转
{
K1_flag=0;
K2_flag=0;
L1602_string(1,1,"State: Stop! ");
L1602_string(2,1,"Speed: ");
}
if(K4 == 0) //加速
{
DelayMS(5);
if(K4 ==0)
{
DelayMS(5);
while(!K4);
speed_count=speed_count-3;
if(speed_count<=3) //最短时间时间 75ms
{
speed_count=3;
}
L1602_char(2,8,time_temp[0]+0x30);
L1602_char(2,9,time_temp[1]+0x30);
L1602_char(2,10,time_temp[2]+0x30);
L1602_char(2,11,'.');
L1602_char(2,12,time_temp[3]+0x30);
L1602_char(2,13,time_temp[4]+0x30);
L1602_char(2,14,'r');
L1602_char(2,15,'/');
L1602_char(2,16,'m');
TR0=1; //启动定时器T0
}
}
if(K5 == 0) //减速
{
DelayMS(5);
if(K5==0)
{
DelayMS(5);
while(!K5);
speed_count=speed_count+3;
if(speed_count>=300) //最长时间20*5ms
{
speed_count=300;
}
L1602_char(2,8,time_temp[0]+0x30);
L1602_char(2,9,time_temp[1]+0x30);
L1602_char(2,10,time_temp[2]+0x30);
L1602_char(2,11,'.');
L1602_char(2,12,time_temp[3]+0x30);
L1602_char(2,13,time_temp[4]+0x30);
L1602_char(2,14,'r');
L1602_char(2,15,'/');
L1602_char(2,16,'m');
TR0=1; //启动定时器T0
}
}
}