|
8x8矩陣按鍵顯示,單片機1通過串口通信把矩陣按鍵的值發單片機2,在單片機2上的數碼管上進行顯示。
0.png (27.16 KB, 下載次數: 160)
下載附件
2016-1-2 00:10 上傳
下面是仿真原理圖在附件中可下載Proteus仿真工程文件,里面可以看清晰的原理圖
1.png (37.69 KB, 下載次數: 142)
下載附件
2016-1-2 00:10 上傳
單片機雙機通信第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);
- }
復制代碼
|
-
-
8x8矩陣鍵盤-兩機串口通信.zip
2015-12-30 00:39 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
80.25 KB, 下載次數: 191, 下載積分: 黑幣 -5
單片機與單片機通信
評分
-
查看全部評分
|