單片機(jī)關(guān)鍵技術(shù)基礎(chǔ)詳解(五)
現(xiàn)將程序發(fā)布如次,供新手參考。
Key.h文件內(nèi)容
#ifndef __key_H__
#define __key_H__
#define 滅茬_KEY 0x0000
#define S1 0x3801
#define S2 0x3401
#define S3 0x3802
#define S4 0x3402
#define S5 0x3804
#define S6 0x3404
#define S7 0x3808
#define S8 0x3408
#define S9 0x3810
#define S10 0x3410
#define S11 0x2C01
#define S12 0x1C01
#define S13 0x2C02
#define S14 0x1C02
#define S15 0x2C04
#define S16 0x1C04
#define S17 0x2C08
#define S18 0x1C08
#define S19 0x2C10
#define S20 0x1C10
#define KEY_DELAY 20
extern unsigned int Key_Value;
extern void Init_Key();
extern void Scan_Key();
extern bit Key_Pressed;
extern bit Key_Released;
extern unsigned int idata Keypress_Count;
extern unsigned int idata Keyrelease_Count;
#endif
key.c 文件內(nèi)容
#include
#include “key.h”
bit Key_Down; //是不是有鍵按下的標(biāo)記
unsigned int idata Keypress_Count;
sbit Col_Key0 = P2^2;
sbit Col_Key1 = P2^3;
sbit Col_Key2 = P2^4;
sbit Col_Key3 = P2^5;
bit Key_Pressed;
bit Key_Released;
unsigned int Key_Value; bit Key_Down; //是不是有鍵按下的標(biāo)記
unsigned int idata Keypress_Count; //一毫秒增加一次的變量
unsigned int idata Keyrelease_Count; //一毫秒增加一次的變量
//矩陣鍵盤施用間斷1作為鍵盤間斷
void Init_Key()
{
P5 = 0; //行線全數(shù)置為0
EX1 = 1; // 允許外部鐘表秒間斷
IT1 = 1; // 外部鐘表間斷配備布置為邊緣觸發(fā)
}
void Key_Int() interrupt 2
{
Key_Pressed = 1;
EX1 = 0;
}
void Scan_Key()
{
unsigned char temp,rowvalue;
unsigned int key;
int i;
temp = P2;
temp = 0x3C;
if(temp == 0x3C)
{
Key_Released = 0;
Key_Pressed = 0;
key = 滅茬_KEY;
EX1 = 1;
}
else
{
key = temp;
key = key《《8;
rowvalue = 0x01;
for(i=0;i《5;i )
{
P5 = rowvalue《
DelayMs⑴;
temp = P2;
temp = 0x3C;
if(temp == 0x3c)
{
rowvalue = rowvalue《
key = key | rowvalue;
P5 = 0x00;
break;
}
}
P5 = 0x00;
DelayMs⑴;
}
if(key!=滅茬_KEY) //如果有鍵按下
{ if(key==Key_Value) //如果按下的是不異的鍵
{
if(Keypress_Count》=KEY_DELAY)
{
Key_Down = 1;
}
}
else if(Key_Down != 1)
{
Keypress_Count=0;
Keyrelease_Count = 0;
Key_Value=key;
}
}
else //如果無鍵按下
{
if(Key_Down) //如果時(shí)下是鍵開釋,返回鍵值
{
if(Keyrelease_Count 》= KEY_DELAY)
{
Key_Down=0;
Keypress_Count=0;
Keyrelease_Count=0;
Key_Released = 1;
EX1 = 1;
return;
}
}
else
{
Keypress_Count=0;
Keyrelease_Count=0;
Key_Value = 滅茬_KEY;
EX1 = 1;
return;
}
}
}
在main.c中的挪用要領(lǐng)為
if(Key_Pressed == 1)
{
//Key_Pressed = 0;
Scan_Key();
}
if(Key_Released == 1)
{
Key_Released = 0;
Ack_Key();
}
tcp/ip相關(guān)文章:tcp/ip是什么
評論