|
用單片機(jī)做一個(gè)從矩陣鍵盤輸入四個(gè)數(shù)并在數(shù)碼管上顯示的實(shí)驗(yàn),檢測(cè)按鍵松手的程序不知道出什么問題了,檢測(cè)不了,一直在循環(huán)里面跳不出來(lái)。用proteus仿真沒有顯示,程序在下面。
#include<reg51.h>
#include <intrins.h>
//--定義使用的IO口--//
#define show P0
#define GPIO_KEY P1
//--定義全局變量--//
unsigned char code number[10]={
0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x3f};
//0、1、2、3、4、5、6、7、8、9、的顯示碼
unsigned char KeyValue;
unsigned char data1[4];
//用來(lái)存放要顯示的4位數(shù)的值
void Delay10ms(unsigned int c); //誤差 0us
void KeyDown(); //檢測(cè)按鍵函數(shù)
void DigDisplay(); //動(dòng)態(tài)顯示函數(shù)
void main(void)
{ int m=0;
while(1)
{
KeyDown();
DigDisplay();
}
}
void DigDisplay()
{
unsigned char i;
unsigned int j;
unsigned char choice ;
choice=0xEF;
for(i=0;i<4;i++)
{
choice=choice >> 1 ;
P2=choice;
show=data1[i];//發(fā)送段碼
j=10; //掃描間隔時(shí)間設(shè)定
while(j--);
show=0x00;//消隱
}
}
void KeyDown(void)
{
unsigned char m=0;
while(m!=4)
{
GPIO_KEY=0x0f;
if(GPIO_KEY!=0x0f)
{
//Delay10ms(1);
if(GPIO_KEY!=0x0f)
{
//測(cè)試列
GPIO_KEY=0X0F;
// Delay10ms(1);
switch(GPIO_KEY)
{ case(0X0e): KeyValue=0;break;
case(0X0d): KeyValue=1;break;
case(0X0b): KeyValue=2;break;
case(0X07): KeyValue=3;break; }
//測(cè)試行
GPIO_KEY=0XF0;
// Delay10ms(1);
switch(GPIO_KEY)
{ case(0Xe0): KeyValue=KeyValue;break;
case(0Xd0): KeyValue=KeyValue+4;break;
case(0Xb0): KeyValue=KeyValue+8;break; }
while(GPIO_KEY!=0XF0) //按鍵松手檢測(cè)
{
Delay10ms(1);
}
data1[m]=number[KeyValue];
m=m+1;
}
}
}
}
void Delay10ms(unsigned int c) //誤差 0us
{
unsigned char a, b;
for (;c>0;c--)
{
for (b=38;b>0;b--)
{
for (a=130;a>0;a--);
}
}
}
|
|