单片机的4*4按钮是根据PIN口的高四位和低四位实现的。(其中第几位返回0,显示第几行/列)
我们可以根据btn去存贮其高四位和低四位实现知道第几排第几列。
即可得到一下代码。
//需要自行学习中断相关知识才可以理解interrupt的内容(这里不再赘述)
#include <reg51.h>
sbit SOUNDER = P3^7;
unsigned int SoundFeqInit[]={64620,64400,64524,64580,64684,64777,64820,64898,64920,64980,65030,65058,65110,65157,65178,65218,11451};
unsigned char Musical_note;
unsigned char Mynums[]={
~0x3F, //0
~0x06, //1
~0x5B, //2
~0x4F, //3
~0x66, //4
~0x6D, //5
~0x7D, //6
~0x07, //7
~0x7F, //8
~0x6F, //9
~0x77, //A
~0x7C, //B
~0x39, //C
~0x5E, //D
~0x79, //E
~0x71, //F
~0x00};
void delayms(unsigned char t);
void main(void)
{
unsigned char btn;
P2 = 0xFF;
TMOD = 0x01;
IE = 0x82;
TR0 = 0;
while(1)
{
P2 = 0xf0;
if(0xf0 != (P2&0xf0))
{
delayms(10);
if(0xf0 != (btn=P2&0xf0))
{
P2 = 0x0f;
btn |= P2;
TR0 = 1;
switch(btn)
{
case 0xee:
Musical_note = 0;
P0=Mynums[0];
break;
case 0xde:
Musical_note = 1;
P0=Mynums[1];
break;
case 0xbe:
Musical_note = 2;
P0=Mynums[2];
break;
case 0x7e:
Musical_note = 3;
P0=Mynums[3];
break;
case 0xed:
Musical_note = 4;
P0=Mynums[4];
break;
case 0xdd:
Musical_note = 5;
P0=Mynums[5];
break;
case 0xbd:
Musical_note = 6;
P0=Mynums[6];
break;
case 0x7d:
Musical_note = 7;
P0=Mynums[7];
break;
case 0xeb:
Musical_note = 8;
P0=Mynums[8];
break;
case 0xdb:
Musical_note = 9;
P0=Mynums[9];
break;
case 0xbb:
Musical_note = 10;
P0=Mynums[10];
break;
case 0x7b:
Musical_note = 11;
P0=Mynums[11];
break;
case 0xe7:
Musical_note = 12;
P0=Mynums[12];
break;
case 0xd7:
Musical_note = 13;
P0=Mynums[13];
break;
case 0xb7:
Musical_note = 14;
P0=Mynums[14];
break;
case 0x77:
Musical_note = 15;
P0=Mynums[15];
break;
default:TR0 = 0;break;
}
}
}
if(0xf0 == P2)
{
TR0 = 0;
SOUNDER = 1;
}
delayms(10);
}
}
void T0Sev(void) interrupt 1
{
TH0 = SoundFeqInit[Musical_note]/256;
TL0 = SoundFeqInit[Musical_note]%256;
SOUNDER = ~SOUNDER;
}
void delayms(unsigned char t)
{
unsigned int i,k;
for(k=0;k<t;k++)
{
for(i=0;i<500;i++){;} // 500*2us= 1ms
}
}
标签:case,break,P0,12,16,显示字符,note,Mynums,Musical
From: https://www.cnblogs.com/AnnaStore/p/18597233