#include<regx51.h>
#define uchar unsigned char
#define uint unsigned int
#define duan P2
#define wei P3
//#define frequncy (1000/100)
uchar timer0_tick,ZKB=1;//timer0_tick计数,ZKB占空比
uchar i=0,n=0,flag=0;
uchar code seven_seg[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};// 共阳:{0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//1,2,3, 4, 5, 6, 7, 8, 9
uchar code scan[2]={0x01,0x00};
uchar counter[2]={0,0};
sbit AN1=P1^0;//调整十位
sbit AN2=P1^1;//调整个位
sbit AN3=P1^2;//启动按键
sbit AN4=P1^3;//确认按键
sbit out=P1^7;
void delay(uint z)//软件延时函数
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
static void timer0_isr(void) interrupt 1 using 0 //中断函数
{
TR0=0;
TL0=(65535-10)%256;
TH0=(65535-10)/256;
TR0=1;
if (ZKB>99) ZKB=1;
if (ZKB<1) ZKB=99;
counter[0]=ZKB%10;
counter[1]=ZKB/10;
n++;
if(n==100)
{
n=0;
i++;
if(i==2) i=0;
wei=scan[i];
duan=seven_seg[counter[i]];
}
if(AN2==0)
{
delay(20);
if(AN2==0)
{
flag=1;
counter[0]++;
if(counter[0]==10)
{
counter[0]=0;
}
}
}
if(AN1==0)
{
delay(20);
if(AN1==0)
{
flag=1;
counter[1]++;
if(counter[1]==10)
{
counter[1]=0;
}
}
}
ZKB=counter[0]+counter[1]*10;
if(AN4==0)
{
delay(5);
if(AN4==0)
flag=0;
}
if(timer0_tick++==100)
{
timer0_tick=0;
}
if(flag==1)
out=0;// P3_7为脉冲输出引脚
else
{
if (timer0_tick<=ZKB) /*当小于占空比值时输出低电平,高于时是高电平,从而实现占空比的调整*/
{
out=1;
}
else
{
out=0;
}
}
}
static void timer0_initialize(void)//中断初始化
{
EA=0;
timer0_tick=0;
TR0=0;
TMOD=0x01;
TL0=0xf6;
TH0=0xff;
PT0=0;
ET0=1;
TR0=1;
EA=1;
}
void main(void)
{
STAR: delay(100);
if(AN3!=0) goto STAR;//按键3启动脉冲
timer0_initialize();
while(1);
}
评论0