#include<reg51.h>
//---重定义关键词---//
#define uchar unsigned char
#define uint unsigned int
sbit motor=P1^0;
sbit IN2=P1^1;
sbit ENA=P1^2;
sbit K1=P3^1;
sbit sensor=P3^3;
int count,sec=0;
char jishi;
char Time=0,state;
unsigned char ZKB1=30; //占空比
unsigned char ZKB2=60; //占空比
unsigned char ZKB3=90; //占空比
//--定义全局变量--//
unsigned char code DIG_PLACE[8] = {
0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};//位选控制 查表的方法控制
unsigned char code DIG_CODE[17] = {
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
//0、1、2、3、4、5、6、7、8、9、A、b、C、d、E、F的显示码
//--声明全局函数--//
void Delay10ms(unsigned int c) //误差 0us
{
unsigned char a,b;
for(;c>0;c--)
for(b=38;b>0;b--)
for(a=130;a>0;a--);
}
void main()
{
IN2=0;
ENA=P1;
motor=0;
TMOD=0X11;
TH0=0x3c;
TL0=0xb0;
TH1=(65536-100)/256;
TL1=(65536-100)%256;
EA=1;
ET0=1;TR0=1;
ET1=0;TR1=0;
motor=0;
P0 = DIG_CODE[0];
while(1)
{
while(sensor==0)
{
}
state=1;
while(sensor==1)
{
jishi=0;
ET1=1;TR1=1;
count=0;sec=0;
P0 = DIG_CODE[state];
if(K1==0)
{
Delay10ms(1);
if(K1==0)
{
state++;
if(state>3)
state=1;
P0 = DIG_CODE[state];
while(K1==0);
}
}
}
jishi=1;
}
}
void Timer0() interrupt 1
{
TH0=0x3c;
TL0=0xb0;
if(jishi==1)
{
count++;
if(count>=20)
{
count=0;
sec++;
if(sec>=5)
{
sec=0;
jishi=0;
ET1=0;TR1=0;
motor=0;
P0 = DIG_CODE[0];
}
}
}
}
void DSY_Refresh2() interrupt 3
{
TH1=(65536-100)/256;
TL1=(65536-100)%256;
if(state==1)
{
Time++;
if(Time<ZKB1)
{
motor=1;
}
else if(Time<100)
{
motor=0;
}
else
{
Time=0;
}
}
if(state==2)
{
Time++;
if(Time<ZKB2)
{
motor=1;
}
else if(Time<100)
{
motor=0;
}
else
{
Time=0;
}
}
if(state==3)
{
Time++;
if(Time<ZKB3)
{
motor=1;
}
else if(Time<100)
{
motor=0;
}
else
{
Time=0;
}
}
}
#include<reg51.h>
//---重定义关键词---//
#define uchar unsigned char
#define uint unsigned int
sbit motor=P1^0;
sbit IN2=P1^1;
sbit ENA=P1^2;
sbit K1=P3^1;
sbit sensor=P3^3;
int count,sec=0;
char jishi;
char Time=0,state;
unsigned char ZKB1=30; //占空比
unsigned char ZKB2=60; //占空比
unsigned char ZKB3=90; //占空比
//--定义全局变量--//
unsigned char code DIG_PLACE[8] = {
0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};//位选控制 查表的方法控制
unsigned char code DIG_CODE[17] = {
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
//0、1、2、3、4、5、6、7、8、9、A、b、C、d、E、F的显示码
//--声明全局函数--//
void Delay10ms(unsigned int c) //误差 0us
{
unsigned char a,b;
for(;c>0;c--)
for(b=38;b>0;b--)
for(a=130;a>0;a--);
}
void main()
{
IN2=0;
ENA=P1;
motor=0;
TMOD=0X11;
TH0=0x3c;
TL0=0xb0;
TH1=(65536-100)/256;
TL1=(65536-100)%256;
EA=1;
ET0=1;TR0=1;
ET1=0;TR1=0;
motor=0;
P0 = DIG_CODE[0];
while(1)
{
while(sensor==0)
{
}
state=1;
while(sensor==1)
{
jishi=0;
ET1=1;TR1=1;
count=0;sec=0;
P0 = DIG_CODE[state];
if(K1==0)
{
Delay10ms(1);
if(K1==0)
{
state++;
if(state>3)
state=1;
P0 = DIG_CODE[state];
while(K1==0);
}
}
}
jishi=1;
}
}
void Timer0() interrupt 1
{
TH0=0x3c;
TL0=0xb0;
if(jishi==1)
{
count++;
if(count>=20)
{
count=0;
sec++;
if(sec>=5)
{
sec=0;
jishi=0;
ET1=0;TR1=0;
motor=0;
P0 = DIG_CODE[0];
}
}
}
}
void DSY_Refresh2() interrupt 3
{
TH1=(65536-100)/256;
TL1=(65536-100)%256;
if(state==1)
{
Time++;
if(Time<ZKB1)
{
motor=1;
}
else if(Time<100)
{
motor=0;
}
else
{
Time=0;
}
}
if(state==2)
{
Time++;
if(Time<ZKB2)
{
motor=1;
}
else if(Time<100)
{
motor=0;
}
else
{
Time=0;
}
}
if(state==3)
{
Time++;
if(Time<ZKB3)
{
motor=1;
}
else if(Time<100)
{
motor=0;
}
else
{
Time=0;
}
}
} |