想要看就干脆看个明白从非定时转定时代码基本一样。
#include "reg52.h"
unsigned char code smgduan[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0,64}; //共阴0~F消隐减号
sbit k1=P3^4;
sbit k2=P3^5;
sbit k3=P3^6;
sbit k4=P3^7;
bit k=0,kk=0;
void CKF(unsigned char C)
{//向串口发送数据
SBUF=C;
while(!TI);
TI=0;
}
void CKS() interrupt 4
{//串口接收数据
if(RI)
{
RI=0;
if(SBUF=='S'){k=1;kk=0;}
if(SBUF=='X'){k=0;kk=1;}
if(SBUF=='L'){k=kk=0;}
}
}
void CKSZ()
{
SCON=0X50;//串口模式1允许接收
TMOD=0X20;//T1工作模式2
PCON=0X00;//波特率不倍增
TH1=TL1=0XFD;//波特率9600
TI=RI=0;
TR1=1;
IE=0X90;//允许串口中断
}
void main()
{//缘由https://ask.csdn.net/questions/8117223
unsigned char Xd=0,ls=0,ys=0,sy=0,sm=0;
CKSZ();
while(1)
{
if(++ys==0)
{
if(++sy==0)
{
if(k)
{
if(ls)ls*=2;else ls=1;
if(sm<9)++sm;else sm=0;
}
if(kk)
{
if(ls)ls/=2;else ls=128;
if(sm)--sm;else sm=9;
}
if(k==kk)ls=k;
}
P1=ls;//流水灯
P2=smgduan[sm];//静态数码管
}
if(!k1&&!++Xd)
{
CKF('S');
while(!k1);
}
if(!k2&&!++Xd)
{
CKF('X');
while(!k2);
}
if(!k3&&!++Xd)
{
CKF('L');
while(!k3);
}
}
}
https://blog.csdn.net/xianfajushi/article/details/139620581 |