⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 lesson5_4.c

📁 51单片机矩阵键盘实验
💻 C
字号:
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
uchar code table[]={
0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,
0xc6,0xa1,0x86,0x8e,};
uchar code weit[]={0x00,0x01,0x02,0x00,0x08,0x10,0x20,};
sbit wei=P2^7;
sbit duan=P2^6;
uchar flag;
void delay(uint);
uint keyscan();
void display(uint,uint);
void main()
{
	while(1)
	{
		 display(keyscan(),keyscan()*keyscan());
	}
}

uint keyscan()//矩阵键盘检测
{
	uint num;
	uchar temp;
	P3=0xfe;
	temp=P3;
	temp=temp&0xf0;
	while(temp!=0xf0)
	{
		delay(5);
		temp=P3;
		temp=temp&0xf0;
		while(temp!=0xf0)
		{
			temp=P3;
			switch(temp)
			{
				case 0xee:num=0;break;
				case 0xde:num=1;break;
				case 0xbe:num=2;break;
				case 0x7e:num=3;break;
			}
			while(temp!=0xf0)
			{
				temp=P3;
				temp=temp&0xf0;
			}
		}
	}
	P3=0xfd;
	temp=P3;
	temp=temp&0xf0;
	while(temp!=0xf0)
	{
		delay(5);
		temp=P3;
		temp=temp&0xf0;
		while(temp!=0xf0)
		{
			temp=P3;
			switch(temp)
			{
				case 0xed:num=4;break;
				case 0xdd:num=5;break;
				case 0xbd:num=6;break;
				case 0x7d:num=7;break;
			}
			while(temp!=0xf0)
			{
				temp=P3;
				temp=temp&0xf0;
			}
		}
	}
	P3=0xfb;
	temp=P3;
	temp=temp&0xf0;
	while(temp!=0xf0)
	{
		delay(5);
		temp=P3;
		temp=temp&0xf0;
		while(temp!=0xf0)
		{
			temp=P3;
			switch(temp)
			{
				case 0xeb:num=8;break;
				case 0xdb:num=9;break;
				case 0xbb:num=10;break;
				case 0x7b:num=11;break;
			}
			while(temp!=0xf0)
			{
				temp=P3;
				temp=temp&0xf0;
			}
		}
	}
	P3=0xf7;
	temp=P3;
	temp=temp&0xf0;
	while(temp!=0xf0)
	{
		delay(5);
		temp=P3;
		temp=temp&0xf0;
		while(temp!=0xf0)
		{
			temp=P3;
			switch(temp)
			{
				case 0xe7:num=12;break;
				case 0xd7:num=13;break;
				case 0xb7:num=14;break;
				case 0x77:num=15;break;
			}
			while(temp!=0xf0)
			{
				temp=P3;
				temp=temp&0xf0;
			}
		}
	}
	return num;
}
void delay(uint x)
{
	uint i,j;
	for(i=0;i<x;i++)
		for(j=0;j<113;j++);
}

void display(uint num1,uint num)
{
	uchar a[6],i;
	a[0]=num1%100/10;
	a[1]=num1%10;
	a[3]=num/100;
	a[4]=num%100/10;
	a[5]=num%10;
	for(i=0;i<6;i++)
	{
		wei=1;
		P0=weit[i+1];
		wei=0;
		duan=1;
		P0=table[a[i]];
		duan=0;
		delay(1);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -