//数码管位 高位-----低位
//四个按键控制步进电机:正转,反转,加1,减1
//上电时电机启动,数码管上显示速度最小档1,加减档位均能通过数码管显示出来,电机采用单双八拍方式
//电机转速一共10档,通过按键调节转速
//电机正转时最高位数码管显示0,反转时显示1
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
#define led P0//数码管段选
//#define haha P2
sbit s1 = P3^4;sbit s2 = P3^5;sbit s3 = P3^6;sbit s4 = P3^7;//按键定义,s1正转,s2反转,s3加1,s4减1
sbit wei3 = P2^0;sbit wei2 = P2^2;sbit wei1 = P2^4;sbit wei0 = P2^6;//数码管位选定义
sbit a = P1^7;sbit b = P1^6;sbit c = P1^5;sbit d = P1^4;//脉冲信号输入端定义
uchar code tab[11]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff};//共阳数码管驱动信号0---9,不显示
uchar code time_counter[10][2]={{0xda,0x1c},{0xde,0xe4},{0xe1,0xec},{0xe5,0xd4},{0xe9,0xbc}, //9.7 ----1ms
{0xed,0xa4},{0xf1,0x8c},{0xf5,0x74},{0xf9,0x5c},{0xfc,0x18}};
uchar code qudong[8]={0x80,0xc0,0x40,0x60,0x20,0x30,0x10,0x90};
uchar num1 = 0;//控制取励磁信号变量
uchar num2 = 8;
uchar k=1;//加减档位控制,1为最小档
bit flag1 = 0;//初始正转,正反转标志
uchar buf[4]={0,10,0,1};//数码管显示缓存,正转,不显示,不显示,显示1档位,高----低
//================================定时器0/1初始化函数================================
void T0_T1_init()
{
TMOD = 0x11;//定时器0/1均工作于方式1,16位计时方式
TH1 = time_counter[k-1][0];
TL1 = time_counter[k-1][1];//定时器1,定时10ms用于步进电机转速控制
TR1 = 1;
ET1= 1;//开定时器中断
EA = 1;//开总中断
}
//================================ms级延时函数=======================================
void delay1m(uint x)
{
uint i,j;
for(i=0;i<x;i++) //连数x次,约 x ms
for(j=0;j<120;j++); //数120 次,约1 ms
}
void display()
{
wei3=1;wei2=1;wei1=1;wei0=0;led = tab[buf[3]];delay1m(1);led=0xff;
wei3=1;wei2=1;wei1=0;wei0=1;led = tab[buf[2]];delay1m(1);led=0xff;
wei3=1;wei2=0;wei1=1;wei0=1;led = tab[buf[1]];delay1m(1);led=0xff;
wei3=0;wei2=1;wei1=1;wei0=1;led = tab[buf[0]];delay1m(1);led=0xff;
}
//================================主函数=============================================
void main()
{
T0_T1_init();
buf[1] = 10;//不显示
while(1)
{
display();
if(s1 == 0)
{
delay1m(3);
if(s1 == 0)
{
flag1 = 0;//正转
buf[0] = 0;//最高位显示0
// haha = 0x00;//停止
}
while(!s1) display();
}
if(s2 == 0 )
{
delay1m(3);
if(s2 == 0)
{
flag1 = 1;//反转
buf[0] = 1;//最高位显示1
// haha = 0x00;//停止
}
while(!s2) display();
}
if(s3 == 0) //速度加1档
{
delay1m(3);
if(s3 == 0)
{
k++;
if(k > 10 )
{
k = 1;
}
buf[2]= k/10;
buf[3]= k%10;
}
while(!s3) display();
}
if(s4 == 0) //速度减1档
{
delay1m(3);
if(s4 == 0)
{
k--;
if(k == 0)
{
k = 10;
}
buf[2]= k/10;
buf[3]= k%10;
}
while(!s4) display();
}
}
}
//==================================定时器1中断函数,用于脉冲频率控制=====================================
void time1_interrupt()interrupt 3
{
static num1 = 0;
static num2 = 0;
TH1 = time_counter[k-1][0];
TL1 = time_counter[k-1][1];//定时器1,定时1 用于步进电机转速控制
if(flag1 == 0)//正转
{
switch(num1)
{
case 0:a = 1;b = 0;c = 0;d = 0;break;
case 1:a = 1;b = 1;c = 0;d = 0;break;
case 2:a = 0;b = 1;c = 0;d = 0;break;
case 3:a = 0;b = 1;c = 1;d = 0;break;
case 4:a = 0;b = 0;c = 1;d = 0;break;
case 5:a = 0;b = 0;c = 1;d = 1;break;
case 6:a = 0;b = 0;c = 0;d = 1;break;
case 7:a = 1;b = 0;c = 0;d = 1;break;
}
num1++;
if(num1 == 8)num1 = 0;
}
else //反转
{
switch(num2)
{
case 0:a = 1;b = 0;c = 0;d = 1;break;
case 1:a = 0;b = 0;c = 0;d = 1;break;
case 2:a = 0;b = 0;c = 1;d = 1;break;
case 3:a = 0;b = 0;c = 1;d = 0;break;
case 4:a = 0;b = 1;c = 1;d = 0;break;
case 5:a = 0;b = 1;c = 0;d = 0;break;
case 6:a = 1;b = 1;c = 0;d = 0;break;
case 7:a = 1;b = 0;c = 0;d = 0;break;
}
num2++;
if(num2 == 8)num2 = 0;
}
}
|