?? keyanddisplay.c
字號:
#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 + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -