电路原理图
也就是管脚图
数码管管脚图
只用其中一个,不做选择,默认第一个
代码
main.c
#include<reg52.h>typedef unsigned int u16;
typedef unsigned char u8;u8 gsmg_code[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};//共阴0-F#define PRESS P1
#define SMG P0void delay_10us(u16 n)
{while(n--);
}press_scan(u8 mode)
{
int KEY=0;PRESS=0XF7; //第一列扫描if(PRESS!=0XF7){switch(PRESS){case 0X77: KEY=1;break;case 0XB7: KEY=5;break;case 0XD7: KEY=9;break;case 0XE7: KEY=13;break;}}while(PRESS!=0XF7); //等待第一列按键松开PRESS=0XFB; //第二列扫描if(PRESS!=0XFB){switch(PRESS){case 0X7B: KEY=2;break;case 0XBB: KEY=6;break;case 0XDB: KEY=10;break;case 0XEB: KEY=14;break;}}while(PRESS!=0XFB); //等待第二列按键松开PRESS=0XFD; //第三列扫描if(PRESS!=0XFD){switch(PRESS){case 0X7D: KEY=3;break;case 0XBD: KEY=7;break;case 0XDD: KEY=11;break;case 0XED: KEY=15;break;}}while(PRESS!=0XFD); //等待第三列按键松开PRESS=0XFE; //第四列扫描if(PRESS!=0XFE){switch(PRESS){case 0X7E: KEY=4;break;case 0XBE: KEY=8;break;case 0XDE: KEY=12;break;case 0XEE: KEY=16;break;}}while(PRESS!=0XFE); //等待第四列按键松开return(KEY);
}void main()
{int key=0;SMG=0;while(1){key=press_scan(0);if(key!=0){SMG=gsmg_code[key-1];}}
}
实验结果;
51开发板矩阵按键实验