|
8x8矩阵按键显示,单片机1通过串口通信把矩阵按键的值发单片机2,在单片机2上的数码管上进行显示。
下面是仿真原理图在附件中可下载Proteus仿真工程文件,里面可以看清晰的原理图
单片机双机通信第1个单片机的程序:
- /*--------------------------------------------------------------------------------------------------------------------------------
- 说明: 发送端单片机1程序:
- 8*8矩阵键盘读取数码管显示,再由串口与单片机2进行通信;
- 把单片机1读取的按键值发送给单片机2进行显示。
- MCU: AT89S52
- 晶振: 11.0592MHZ
- 2015-12-30
- ---------------------------------------------------------------------------------------------------------------------------------*/
- #include<reg52.h>
- #include<intrins.h>
- #define uint unsigned int
- #define uchar unsigned char
- sbit D1=P3^6; //数码管十位com1
- sbit D2=P3^7; //数码管个位com2
- uchar shi,ge,num;
- uchar temp;
- uchar code table[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
- void delay(uint z);//延时函数
- void display(); //显示函数
- void keyscan(); //8*8_64矩阵键盘
- void InitUART (void); //串口初始化
- /*----------------------------------------------------------------------------------
- main()_程序入口
- ----------------------------------------------------------------------------------*/
- void main()
- {
- InitUART();//串口初始化
- SBUF=0;
- while(1)
- {
- display();
- keyscan();
- }
- }
- /*------------------------------------------------
- 串口初始化
- ------------------------------------------------*/
- void InitUART (void)
- {
- SCON = 0x50; // SCON: 模式 1, 8-bit UART, 使能接收
- TMOD |= 0x20; // TMOD: timer 1, mode 2, 8-bit 重装
- TH1 = 0xFD; // TH1: 重装值 9600 波特率 晶振 11.0592MHz
- TR1 = 1; // TR1: timer 1 打开
- EA = 1; //打开总中断
- ES = 1; //打开串口中断
- }
- /*------------------------------------------------
- 串口中断程序
- ------------------------------------------------*/
- void UART_SER (void) interrupt 4 //串行中断服务程序
- {
- // unsigned char Temp; //定义临时变量
-
- if(RI) //判断是接收中断产生
- {
- RI=0; //标志位清零
- num=SBUF; //读入缓冲区的值
- // SBUF=num; //把接收到的值再发回单片机2
- }
- if(TI) //如果是发送标志位,清零
- TI=0;
-
- }
-
- /*----------------------------------------------------------------------------------
- 延时
- ----------------------------------------------------------------------------------*/
- void delay(uint z)
- {
- uint i,j;
- for(i=z;i>0;i--)
- for(j=112;j>0;j--);
- }
- /*----------------------------------------------------------------------------------
- 显示
- ----------------------------------------------------------------------------------*/
- void display()
- { //num=23;
- shi=num/10;
- ge=num%10;
- P2=table[shi];//显示十位
- D1=0;
- D2=1;
- delay(5);
- D1=1;
- P2=table[ge];//显示个位
- D2=0;
- delay(5);
- D2=1;
- }
- /*----------------------------------------------------------------------------------
- 矩阵键盘
- ----------------------------------------------------------------------------------*/
- void keyscan()
- {
- uchar temp,n,k;
- n=0x7f;
- for(k=0;k<8;k++)
- {
- P0=n;
- temp=P1&0xff;
- if(temp!=0xff)
- {
- delay(3);
- temp=P1&0xff;
- if(temp!=0xff)
- {
- temp=P1;
- if(n==0x7f)
- {
- switch(temp)
- {
- case 0xfe:num=1;SBUF=1;break;
- case 0xfd:num=9;SBUF=9;break;
- case 0xfb:num=17;SBUF=17;break;
- case 0xf7:num=25;SBUF=25;break;
- case 0xef:num=33;SBUF=33;break;
- case 0xdf:num=41;SBUF=41;break;
- case 0xbf:num=49;SBUF=49;break;
- case 0x7f:num=57;SBUF=57;break;
- }
- }
-
- if(n==0xbf)
- {
- switch(temp)
- {
- case 0xfe:num=2; SBUF=2;break;
- case 0xfd:num=10;SBUF=10;break;
- case 0xfb:num=18;SBUF=18;break;
- case 0xf7:num=26;SBUF=26;break;
- case 0xef:num=34;SBUF=34;break;
- case 0xdf:num=42;SBUF=42;break;
- case 0xbf:num=50;SBUF=50;break;
- case 0x7f:num=58;SBUF=58;break;
- }
- }
-
- if(n==0xdf)
- {
- switch(temp)
- {
- case 0xfe:num=3; SBUF=3;break;
- case 0xfd:num=11;SBUF=11;break;
- case 0xfb:num=19;SBUF=19;break;
- case 0xf7:num=27;SBUF=27;break;
- case 0xef:num=35;SBUF=35;break;
- case 0xdf:num=43;SBUF=43;break;
- case 0xbf:num=51;SBUF=51;break;
- case 0x7f:num=59;SBUF=59;break;
- }
- }
-
- if(n==0xef)
- {
- switch(temp)
- {
- case 0xfe:num=4; SBUF=4;break;
- case 0xfd:num=12;SBUF=12;break;
- case 0xfb:num=20;SBUF=20;break;
- case 0xf7:num=28;SBUF=28;break;
- case 0xef:num=36;SBUF=36;break;
- case 0xdf:num=44;SBUF=44;break;
- case 0xbf:num=52;SBUF=52;break;
- case 0x7f:num=60;SBUF=60;break;
- }
- }
-
- if(n==0xf7)
- {
- switch(temp)
- {
- case 0xfe:num=5;SBUF=5;break;
- case 0xfd:num=13;SBUF=13;break;
- case 0xfb:num=21;SBUF=21;break;
- case 0xf7:num=29;SBUF=29;break;
- case 0xef:num=37;SBUF=37;break;
- case 0xdf:num=45;SBUF=45;break;
- case 0xbf:num=53;SBUF=53;break;
- case 0x7f:num=61;SBUF=61;break;
- }
- }
-
- if(n==0xfb)
- {
- switch(temp)
- {
- case 0xfe:num=6;SBUF=6;break;
- case 0xfd:num=14;SBUF=14;break;
- case 0xfb:num=22;SBUF=22;break;
- case 0xf7:num=30;SBUF=30;break;
- case 0xef:num=38;SBUF=38;break;
- case 0xdf:num=46;SBUF=46;break;
- case 0xbf:num=54;SBUF=54;break;
- case 0x7f:num=62;SBUF=62;break;
- }
- }
- if(n==0xfd)
- {
- switch(temp)
- {
- case 0xfe:num=7;SBUF=7;break;
- case 0xfd:num=15;SBUF=15;break;
- case 0xfb:num=23;SBUF=23;break;
- case 0xf7:num=31;SBUF=31;break;
- case 0xef:num=39;SBUF=39;break;
- case 0xdf:num=47;SBUF=47;break;
- case 0xbf:num=55;SBUF=55;break;
- case 0x7f:num=63;SBUF=64;break;
- }
- }
- if(n==0xfe)
- {
- switch(temp)
- {
- case 0xfe:num=8; SBUF=8;break;
- case 0xfd:num=16;SBUF=16;break;
- case 0xfb:num=24;SBUF=24;break;
- case 0xf7:num=32;SBUF=32;break;
- case 0xef:num=40;SBUF=40;break;
- case 0xdf:num=48;SBUF=48;break;
- case 0xbf:num=56;SBUF=56;break;
- case 0x7f:num=64;SBUF=64;break;
- }
- }
- while(temp!=0xff)
- {
- temp=P1&0xff;
- }
- }
- }
- n=_cror_(n,1);
- }
- }
复制代码
双机通信单片机2程序:
- /*--------------------------------------------------------------------------------------------------------------------------------
- 说明: 接收端单片机2程序:
- 读取数码管显示,由串口与单片机1进行通信;
- 接收单片机1读取的8*8矩阵键盘按键值并在数码管上进行显示。
- MCU: AT89S52
- 晶振: 11.0592MHZ
- 2015-12-30
- ---------------------------------------------------------------------------------------------------------------------------------*/
- #include<reg52.h>
- #include<intrins.h>
- #define uint unsigned int
- #define uchar unsigned char
- uchar shi,ge,num;
- uchar code table[]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
- void delay(uint z);//延时函数
- void display(); //显示函数
- void keyscan(); //8*8_64矩阵键盘
- void InitUART (void); //串口初始化
- /*----------------------------------------------------------------------------------
- main()_程序入口
- ----------------------------------------------------------------------------------*/
- void main()
- {
- InitUART();
- SBUF=0;
- while(1)
- {
- display();
- }
- }
- /*------------------------------------------------
- 串口初始化
- ------------------------------------------------*/
- void InitUART (void)
- {
- SCON = 0x50; // SCON: 模式 1, 8-bit UART, 使能接收
- TMOD |= 0x20; // TMOD: timer 1, mode 2, 8-bit 重装
- TH1 = 0xFD; // TH1: 重装值 9600 波特率 晶振 11.0592MHz
- TR1 = 1; // TR1: timer 1 打开
- EA = 1; //打开总中断
- ES = 1; //打开串口中断
- }
- /*------------------------------------------------
- 串口中断程序
- ------------------------------------------------*/
- void UART_SER (void) interrupt 4 //串行中断服务程序
- {
-
- if(RI) //判断是接收中断产生
- {
- RI=0; //标志位清零
- num=SBUF; //读入缓冲区的值
- // SBUF=0; //把接收到的值再发回单片机1
- }
- if(TI) //如果是发送标志位,清零
- TI=0;
-
- }
-
- /*----------------------------------------------------------------------------------
- 延时
- ----------------------------------------------------------------------------------*/
- void delay(uint z)
- {
- uint i,j;
- for(i=z;i>0;i--)
- for(j=112;j>0;j--);
- }
- /*----------------------------------------------------------------------------------
- 显示
- ----------------------------------------------------------------------------------*/
- void display()
- {// num=23;
- shi=num/10;
- ge=num%10;
- P2=table[shi];//显示十位
- delay(5);
- P1=table[ge];//显示个位
- delay(5);
- }
复制代码
|
评分
-
查看全部评分
|