/*AT89S52
晶振:11.0592MHZ
引脚连接方式见下面定义,该程序经过我仔细调试,只要连接上即可直接运行,*/
#include<reg52.H>
#include <intrins.h>
#define seg_data P0 //数码管显示数据输出端口
sbit K1 = P1^4; //按键引脚定义
sbit K2 = P1^5;
sbit K3 = P1^6;
sbit bit1 = P2^0; //数码管位选通端口定义
sbit bit2 = P2^1;
sbit bit3 = P2^2;
/*共阳数码管0-9 */
const unsigned char seg[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
unsigned char seg_data1=0,seg_data2=0,seg_data3=0,delaytime=0;
struct boolean_variable //定义布尔型变量,每个变量只占用一个bit RAM
{
char a :1;
char b :1;
char c :1;
char d :1;
char e :1;
char f :1;
}variable1;
char code reserve[3]_at_ 0x3b; //保留0x3b开始的3个字节
/******************************************************************************
定时器初始化
******************************************************************************/
void time0_time1_initialize()
{
TMOD=0X11; //T0模式1,T1模式1 相当于00010001B
ET0=1; //T0开中断
ET1=1; //T1开中断
PT0=1; //T0为高优先级
PT1=0; //T1为低优先级
EA=1; //中断总开关打开
TH1=0XF8; ////T1装入初值,实现2ms定时
TL1=0XCC;
TH0=0X4C; //T0装入初值,实现50ms定时
TL0=0X00;
TR1=1; //开启T1
}
#define bit1_shanshuo variable1.c
#define bit2_shanshuo variable1.d
#define bit3_shanshuo variable1.e
#define jishu_key variable1.f
/******************************************************************************
动态扫描显示程序
******************************************************************************/
unsigned int judge_segdata=1;
void seg_display()
{
if (jishu_key == 1)
{
if (judge_segdata > 3)
judge_segdata =1;
switch (judge_segdata)
{
case 1: seg_data=seg[seg_data1];bit1 = 0;bit2 = 1;bit3 = 1;break;
case 2: seg_data=seg[seg_data2];bit1= 1;bit2 = 0;bit3 = 1;break;
case 3: seg_data=seg[seg_data3];bit1 = 1;bit2 = 1;bit3 = 0;break;
}
goto S;
}
if (jishu_key == 0)
{
if (judge_segdata >200)
judge_segdata =1;
if (bit1_shanshuo == 1)
{
if(judge_segdata<100)
{
switch (judge_segdata%4)
{
case 1: seg_data=seg[seg_data1];bit1 =1;bit2 = 1;bit3 = 1;break;
case 2: seg_data=seg[seg_data2];bit1= 1;bit2 = 0;bit3 = 1;break;
case 3: seg_data=seg[seg_data3];bit1 = 1;bit2 = 1;bit3 = 0;break;
}
goto S;
}
else
{
switch (judge_segdata%4)
{
case 1: seg_data=seg[seg_data1];bit1 = 0;bit2 = 1;bit3 = 1;break;
case 2: seg_data=seg[seg_data2];bit1= 1;bit2 = 0;bit3 = 1;break;
case 3: seg_data=seg[seg_data3];bit1 = 1;bit2 = 1;bit3 = 0;break;
}
goto S;
}
}
if (bit2_shanshuo == 1)
{
if(judge_segdata<100)
{
switch (judge_segdata%4)
{
case 1: seg_data=seg[seg_data1];bit1 =0;bit2 = 1;bit3 = 1;break;
case 2: seg_data=seg[seg_data2];bit1= 1;bit2 = 1;bit3 = 1;break;
case 3: seg_data=seg[seg_data3];bit1 = 1;bit2 = 1;bit3 = 0;break;
}
goto S;
}
else
{
switch (judge_segdata%4)
{
case 1: seg_data=seg[seg_data1];bit1 = 0;bit2 = 1;bit3 = 1;break;
case 2: seg_data=seg[seg_data2];bit1= 1;bit2 = 0;bit3 = 1;break;
case 3: seg_data=seg[seg_data3];bit1 = 1;bit2 = 1;bit3 = 0;break;
}
goto S;
}
}
if (bit3_shanshuo == 1)
{
if(judge_segdata<100)
{
switch (judge_segdata%4)
{
case 1: seg_data=seg[seg_data1];bit1 =0;bit2 = 1;bit3 = 1;break;
case 2: seg_data=seg[seg_data2];bit1= 1;bit2 = 0;bit3 = 1;break;
case 3: seg_data=seg[seg_data3];bit1 = 1;bit2 = 1;bit3 = 1;break;
}
goto S;
}
else
{
switch (judge_segdata%4)
{
case 1: seg_data=seg[seg_data1];bit1 = 0;bit2 = 1;bit3 = 1;break;
case 2: seg_data=seg[seg_data2];bit1= 1;bit2 = 0;bit3 = 1;break;
case 3: seg_data=seg[seg_data3];bit1 = 1;bit2 = 1;bit3 = 0;break;
}
goto S;
}
}
}
S: judge_segdata++;
}
/******************************************************************************
定时计数器1的中断服务子程序, 用于定时扫描数码管
******************************************************************************/
unsigned char dc1=1;
void time1_interrupt() interrupt 3 using 2
{
TH1=0XF8; //T1重新装入初值
TL1=0XCC;
dc1++;
if (dc1 == 40) //200ms到了?数码管计数加1
{
dc1 = 0;
if (jishu_key == 1)
{
seg_data1++;
if (seg_data1 == 10)
{
seg_data1 = 0;
seg_data2++;
}
if (seg_data2 == 10)
{
seg_data2 = 0;
seg_data3++;
}
if (seg_data3 == 10)
seg_data3 = 0;
}
}
seg_display();
}
#define second variable1.a
static unsigned char dc2=0;
/******************************************************************************
定时计数器0的中断服务子程序, 用于定时扫描数码管
******************************************************************************/
void time0_interrupt() interrupt 1 using 3
{
TH0=0X4C; //T0装入初值,继续50ms定时
TL0=0X00;
dc2++; //50ms计数变量加1
if (dc2 == delaytime) //2s到了?
{
second = 1; //布尔型变量second置位
dc2=0;
}
}
/******************************************************************************
延时程序
******************************************************************************/
void delay(int time0)
{
unsigned char i;
for(;time0 >= 0;time0--)
{
for(i=0;i<120;i++);
}
}
/******************************************************************************
按键执行程序
******************************************************************************/
#define timedelay1 50
void key_function()
{
unsigned char j=1;
bit1_shanshuo = 1; //数码管第一位闪烁
delay(500); //延时500ms
delaytime = 100 ; //定时器0 定时100*50ms = 5000ms=5s
TH0=0X4C;
TL0=0X00;
TR0 = 1; //开启T0,定时5s
while(!second) //如果5秒内没有按键,则按键设置程序返回
{
if (K1 == 0)
{
dc2 = 0; //清零T0十秒计数变量
j++;
}
if (j > 3)
j=1;
switch (j)
{
case 1:bit1_shanshuo = 1;bit2_shanshuo = 0;bit3_shanshuo = 0;break;
case 2:bit1_shanshuo = 0;bit2_shanshuo = 1;bit3_shanshuo = 0;break;
case 3:bit1_shanshuo = 0;bit2_shanshuo = 0;bit3_shanshuo = 1;break;
default: break;
}
if (K2 == 0)
{
dc2 = 0; //清零T0十秒计数变量
switch (j)
{
case 1: seg_data1++;if (seg_data1>9) seg_data1=0;break;
case 2: seg_data2++;if (seg_data2>9) seg_data2=0;break;
case 3: seg_data3++;if (seg_data3>9) seg_data3=0;break;
}
}
if (K3 == 0)
{
dc2 = 0; //清零T0十秒计数变量
switch (j)
{
case 1: seg_data1--;if (seg_data1>9) seg_data1=0;break;
case 2: seg_data2--;if (seg_data2>9) seg_data2=0;break;
case 3: seg_data3--;if (seg_data3>9) seg_data3=0;break;
}
}
delay(400); //延时400ms
}
jishu_key = 1; //数码管继续计数
TR0 = 0;
dc2=0;
second=0;
}
/******************************************************************************
K1键扫描程序
******************************************************************************/
void key_press()
{
if (K1 == 0) //key1按下?
{
jishu_key = 0; //数码管停止计数
delaytime = 40; //定时器1定时40*50ms = 2s
TH0=0X4C;
TL0=0X00;
TR0 = 1; //T0开始定时
delay(10); //消除键盘抖动
while (!K1) //等待
{
bit1 = 1;bit2 = 1;bit3 = 1;
if (second == 1)//两秒计数变量为1?
{
TR0 = 0; //关闭T0
dc2=0;
second = 0;
key_function();
return;
}
}
TR0 = 0;
dc2=0;
jishu_key=1;
}
}
void main()
{
K1 = 1;
K2 = 1;
K3 = 1;
second = 0;
jishu_key = 1;
bit1_shanshuo =0;
bit2_shanshuo =0;
bit3_shanshuo =0;
time0_time