keyanddisplay.c
来自「基于MCS—51系列单片机I/O口的按键和显示程序.」· C语言 代码 · 共 569 行
C
569 行
#include <reg51.h>
#include <intrins.h>
#include <stdio.h>
#define uchar unsigned char
#define uint unsigned int
sbit dq = P3^7; //18B20单总线位变量
sbit P3_3 = P3^3; //数码管个十百位的位选端位变量
sbit P3_4 = P3^4;
sbit P3_5 = P3^5;
sbit P1_0 = P1^0; //4055译码器的输入接口
sbit P1_1 = P1^1;
sbit P1_2 = P1^2;
sbit P1_3 = P1^3;
sbit P1_4 = P1^4; //LED控制端
sbit P3_2 = P3^2; //测试端口
sbit up = P1^7; //上翻键
sbit set = P3^0; //设定键
sbit down = P3^1; //下翻键
bit flag;
static uint Temperature; //温度终值
static uchar temp_buff[9]; //存储读取的字节,read scratchpad为9字节,read rom ID为8字节
static uchar id_buff[8];
static uchar *p;
static uchar crc_data;
static uchar code CrcTable [256]={
0, 94, 188, 226, 97, 63, 221, 131, 194, 156, 126, 32, 163, 253, 31, 65,
157, 195, 33, 127, 252, 162, 64, 30, 95, 1, 227, 189, 62, 96, 130, 220,
35, 125, 159, 193, 66, 28, 254, 160, 225, 191, 93, 3, 128, 222, 60, 98,
190, 224, 2, 92, 223, 129, 99, 61, 124, 34, 192, 158, 29, 67, 161, 255,
70, 24, 250, 164, 39, 121, 155, 197, 132, 218, 56, 102, 229, 187, 89, 7,
219, 133, 103, 57, 186, 228, 6, 88, 25, 71, 165, 251, 120, 38, 196, 154,
101, 59, 217, 135, 4, 90, 184, 230, 167, 249, 27, 69, 198, 152, 122, 36,
248, 166, 68, 26, 153, 199, 37, 123, 58, 100, 134, 216, 91, 5, 231, 185,
140, 210, 48, 110, 237, 179, 81, 15, 78, 16, 242, 172, 47, 113, 147, 205,
17, 79, 173, 243, 112, 46, 204, 146, 211, 141, 111, 49, 178, 236, 14, 80,
175, 241, 19, 77, 206, 144, 114, 44, 109, 51, 209, 143, 12, 82, 176, 238,
50, 108, 142, 208, 83, 13, 239, 177, 240, 174, 76, 18, 145, 207, 45, 115,
202, 148, 118, 40, 171, 245, 23, 73, 8, 86, 180, 234, 105, 55, 213, 139,
87, 9, 235, 181, 54, 104, 138, 212, 149, 203, 41, 119, 244, 170, 72, 22,
233, 183, 85, 11, 136, 214, 52, 106, 43, 117, 151, 201, 74, 20, 246, 168,
116, 42, 200, 150, 21, 75, 169, 247, 182, 232, 10, 84, 215, 137, 107, 53};
static uint LED_one,LED_two,LED_three; //百位,十位,个位的显示数变量
static uchar opera_flag;
static uint sum;
static uint num;
static uint tempera[5] = {0}; //温度实时采样值
static uint Temperature_final; //显示温度
static uint Page_flag; //显示页面标志
static uint Scantime_flag; //扫键时间标志
static uint Key_value; //键值
static uint timing_num; //计数标志
static uint bdata Scan_flag;
sbit Keyscan_flag = Scan_flag^1; //按键确认标志
sbit Up_flag = Scan_flag^2; //上翻键确认标志
sbit Set_flag = Scan_flag^3; //设定键确认标志
sbit Down_flag = Scan_flag^4; //下翻键确认标志
void Display_key_1(void);
void Display_key_2(void);
void Key_scan(void);
/************************************************************
*function:在数码管显示
*parameter:
*Return:
*Modify:
*************************************************************/
void Dis_play_temp(uint x,uint y,uint z,uint k)
{
if(1 == k)
{
switch(x)
{
case 0:
{
P1_3 = 0;
P1_2 = 0;
P1_1 = 0;
P1_0 = 0;
P3_3 = 0;
P3_4 = 1;
P3_5 = 1;
};break;
case 1:
{
P1_3 = 0;
P1_2 = 0;
P1_1 = 0;
P1_0 = 1;
P3_3 = 0;
P3_4 = 1;
P3_5 = 1;
};break;
case 2:
{
P1_3 = 0;
P1_2 = 0;
P1_1 = 1;
P1_0 = 0;
P3_3 = 0;
P3_4 = 1;
P3_5 = 1;
};break;
case 3:
{
P1_3 = 0;
P1_2 = 0;
P1_1 = 1;
P1_0 = 1;
P3_3 = 0;
P3_4 = 1;
P3_5 = 1;
};break;
case 4:
{
P1_3 = 0;
P1_2 = 1;
P1_1 = 0;
P1_0 = 0;
P3_3 = 0;
P3_4 = 1;
P3_5 = 1;
};break;
case 5:
{
P1_3 = 0;
P1_2 = 1;
P1_1 = 0;
P1_0 = 1;
P3_3 = 0;
P3_4 = 1;
P3_5 = 1;
};break;
case 6:
{
P1_3 = 0;
P1_2 = 1;
P1_1 = 1;
P1_0 = 0;
P3_3 = 0;
P3_4 = 1;
P3_5 = 1;
};break;
case 7:
{
P1_3 = 0;
P1_2 = 1;
P1_1 = 1;
P1_0 = 1;
P3_3 = 0;
P3_4 = 1;
P3_5 = 1;
};break;
case 8:
{
P1_3 = 1;
P1_2 = 0;
P1_1 = 0;
P1_0 = 0;
P3_3 = 0;
P3_4 = 1;
P3_5 = 1;
};break;
case 9:
{
P1_3 = 1;
P1_2 = 0;
P1_1 = 0;
P1_0 = 1;
P3_3 = 0;
P3_4 = 1;
P3_5 = 1;
};break;
}
}
else if(2 == k)
{
switch(y)
{
case 0:
{
P1_3 = 0;
P1_2 = 0;
P1_1 = 0;
P1_0 = 0;
P3_3 = 1;
P3_4 = 0;
P3_5 = 1;
};break;
case 1:
{
P1_3 = 0;
P1_2 = 0;
P1_1 = 0;
P1_0 = 1;
P3_3 = 1;
P3_4 = 0;
P3_5 = 1;
};break;
case 2:
{
P1_3 = 0;
P1_2 = 0;
P1_1 = 1;
P1_0 = 0;
P3_3 = 1;
P3_4 = 0;
P3_5 = 1;
};break;
case 3:
{
P1_3 = 0;
P1_2 = 0;
P1_1 = 1;
P1_0 = 1;
P3_3 = 1;
P3_4 = 0;
P3_5 = 1;
};break;
case 4:
{
P1_3 = 0;
P1_2 = 1;
P1_1 = 0;
P1_0 = 0;
P3_3 = 1;
P3_4 = 0;
P3_5 = 1;
};break;
case 5:
{
P1_3 = 0;
P1_2 = 1;
P1_1 = 0;
P1_0 = 1;
P3_3 = 1;
P3_4 = 0;
P3_5 = 1;
};break;
case 6:
{
P1_3 = 0;
P1_2 = 1;
P1_1 = 1;
P1_0 = 0;
P3_3 = 1;
P3_4 = 0;
P3_5 = 1;
};break;
case 7:
{
P1_3 = 0;
P1_2 = 1;
P1_1 = 1;
P1_0 = 1;
P3_3 = 1;
P3_4 = 0;
P3_5 = 1;
};break;
case 8:
{
P1_3 = 1;
P1_2 = 0;
P1_1 = 0;
P1_0 = 0;
P3_3 = 1;
P3_4 = 0;
P3_5 = 1;
};break;
case 9:
{
P1_3 = 1;
P1_2 = 0;
P1_1 = 0;
P1_0 = 1;
P3_3 = 1;
P3_4 = 0;
P3_5 = 1;
};break;
}
}
else
{
switch(z)
{
case 0:
{
P1_3 = 0;
P1_2 = 0;
P1_1 = 0;
P1_0 = 0;
P3_3 = 1;
P3_4 = 1;
P3_5 = 0;
};break;
case 1:
{
P1_3 = 0;
P1_2 = 0;
P1_1 = 0;
P1_0 = 1;
P3_3 = 1;
P3_4 = 1;
P3_5 = 0;
};break;
case 2:
{
P1_3 = 0;
P1_2 = 0;
P1_1 = 1;
P1_0 = 0;
P3_3 = 1;
P3_4 = 1;
P3_5 = 0;
};break;
case 3:
{
P1_3 = 0;
P1_2 = 0;
P1_1 = 1;
P1_0 = 1;
P3_3 = 1;
P3_4 = 1;
P3_5 = 0;
};break;
case 4:
{
P1_3 = 0;
P1_2 = 1;
P1_1 = 0;
P1_0 = 0;
P3_3 = 1;
P3_4 = 1;
P3_5 = 0;
};break;
case 5:
{
P1_3 = 0;
P1_2 = 1;
P1_1 = 0;
P1_0 = 1;
P3_3 = 1;
P3_4 = 1;
P3_5 = 0;
};break;
case 6:
{
P1_3 = 0;
P1_2 = 1;
P1_1 = 1;
P1_0 = 0;
P3_3 = 1;
P3_4 = 1;
P3_5 = 0;
};break;
case 7:
{
P1_3 = 0;
P1_2 = 1;
P1_1 = 1;
P1_0 = 1;
P3_3 = 1;
P3_4 = 1;
P3_5 = 0;
};break;
case 8:
{
P1_3 = 1;
P1_2 = 0;
P1_1 = 0;
P1_0 = 0;
P3_3 = 1;
P3_4 = 1;
P3_5 = 0;
};break;
case 9:
{
P1_3 = 1;
P1_2 = 0;
P1_1 = 0;
P1_0 = 1;
P3_3 = 1;
P3_4 = 1;
P3_5 = 0;
};break;
}
}
}
/************************************************************
*Function:延时处理
*parameter:
*Return:
*Modify:
*************************************************************/
void TempDelay (uchar us)
{
while(us--);
}
/************************************************************
*function:解析要显示的各个位值
*parameter:
*Return:
*Modify:
*************************************************************/
void Trans_temp(uint x)
{
LED_one = x / 100; //转换百位值
LED_two = (x - LED_one * 100)/10;
LED_three = x - LED_two * 100 - LED_two * 10;
}
/************************************************************
*Function:定时5ms中断
*parameter:
*Return:
*Modify:
*************************************************************/
void KeyAndDis_Timer0() interrupt 1 using 2
{
TR0 = 0;
TH0 = 0xf6;
TL0 = 0x3c;
TR0 = 1;
Scantime_flag--;
timing_num++;
if(Scantime_flag == 0)
{
Scantime_flag = 1;
Key_scan();
}
}
/************************************************************
*Function:每30ms扫键一次,去抖30ms,调显示,扫到键后100ms不再扫键
*parameter:
*Return:
*Modify:
*************************************************************/
void Key_scan(void)
{
if(6 <= timing_num) //扫描时间到
{
if((0 == up)||(0 == set)||(0 == down)) //有键按下
{
if(12 == timing_num) //去抖30ms
{
if((0 == up)||(0 == set)||(0 == down)) //确认有键按下
{
Keyscan_flag = 1;
if(0 == up) //若上翻键被按下
Up_flag = 1; //上翻标志置1
else if(0 == set)
Set_flag = 1;
else Down_flag = 1;
Scantime_flag = 20; //后100ms不再扫键
timing_num = 0;
Display_key_1();
}
}
}
else timing_num = 0;
}
}
/************************************************************
*Function:扫描到键值后显示相应内容,界面0和界面1
*parameter:
*Return:
*Modify:
*************************************************************/
void Display_key_1(void)
{
if(1 == Keyscan_flag)
{
if((1 == Set_flag)&&(0 == Page_flag)) //在缺省界面下按设定键,则进入功能选择界面
{
Page_flag = 1; //置相应的标志位和显示值
Key_value = 1;
Keyscan_flag = 0;
Set_flag = 0;
}
if((1 == Up_flag)&&(1 == Page_flag)) //在功能选择菜单按上翻键
{
Up_flag = 0;
Page_flag = 1;
Keyscan_flag = 0;
switch(Key_value)
{
case 0: Key_value = 3;break;
case 1: Key_value = 0;break;
case 2: Key_value = 1;break;
case 3: Key_value = 2;break;
}
}
if((1 == Down_flag)&&(1 == Page_flag))
{
Down_flag = 0;
Page_flag = 1;
Keyscan_flag = 0;
switch(Key_value)
{
case 0: Key_value = 1;break;
case 1: Key_value = 2;break;
case 2: Key_value = 3;break;
case 3: Key_value = 0;break;
}
}
Display_key_2();
}
}
/************************************************************
*Function:扫描到键值后显示相应内容,界面2
*parameter:
*Return:
*Modify:
*************************************************************/
void Display_key_2(void)
{
if(1 == Keyscan_flag)
{
if((1 == Set_flag)&&(1 == Page_flag))
{
Keyscan_flag = 0;
Page_flag = 2;
Set_flag = 0;
}
}
}
void main()
{
uint i;
P3_3 = 1;
P3_4 = 1;
P3_5 = 1;
P1_4 = 0;
opera_flag = 0;
EA = 1; //允许CPU中断
ET0 = 1; //定时器0中断打开
TMOD = 0x1; //设定时器为模式1,16位模式
TH0=0xf6;
TL0=0x3c; //设定时值为5000us(5ms)
TR0 = 1; //定时器0装入定时值
num = 1;
sum = 1;
Page_flag = 0; //缺省界面标志为0
LED_one = 0;
LED_two = 0;
LED_three = 0;
timing_num = 0; //计数标志置0
Scantime_flag = 1;
Key_value = 0; //初始键值置0
Keyscan_flag = 0;
Up_flag = 0;
Set_flag = 0;
Down_flag = 0;
while(1)
{
for(i=1;i<4;i++)
{
TempDelay(2500);
Trans_temp(Key_value);
Dis_play_temp(LED_one,LED_two,LED_three,i);
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?