功能概述:
實現一個4x4按鍵矩陣,通過按鍵掃描,當有按鍵按下時,識別按鍵值,并通過UART串口發送,同時在lcd1602顯示。
功能實現:
1. 按鍵掃描 - void keyboardScan(){
- P1OUT = 0xef;//P1.4 = 0
- if((P1IN & 0x0f) != 0x0f){
- delay_ms8M(5);//消抖
- if((P1IN & 0x0f) != 0x0f){
- if((P1IN & 0x01) == 0){ keyValue = '3';while((P1IN & 0x01) == 0);};
- if((P1IN & 0x02) == 0){ keyValue = '7';while((P1IN & 0x02) == 0);};
- if((P1IN & 0x04) == 0){ keyValue = 'B';while((P1IN & 0x04) == 0);};
- if((P1IN & 0x08) == 0){ keyValue = 'F';while((P1IN & 0x08) == 0);};
- keyboard_envent = ~keyboard_envent;
- }
- }
- P1OUT = 0xdf;//P1.5 = 0
- if((P1IN & 0x0f) != 0x0f){
- delay_ms8M(5);//消抖
- if((P1IN & 0x0f) != 0x0f){
- if((P1IN & 0x01) == 0){ keyValue = '2';while((P1IN & 0x01) == 0);};
- if((P1IN & 0x02) == 0){ keyValue = '6';while((P1IN & 0x02) == 0);};
- if((P1IN & 0x04) == 0){ keyValue = 'A';while((P1IN & 0x04) == 0);};
- if((P1IN & 0x08) == 0){ keyValue = 'E';while((P1IN & 0x08) == 0);};
- keyboard_envent = ~keyboard_envent;
- }
- }
- P1OUT = 0xbf;//P1.6 = 0
- if((P1IN & 0x0f) != 0x0f){
- delay_ms8M(5);//消抖
- if((P1IN & 0x0f) != 0x0f){
- if((P1IN & 0x01) == 0){ keyValue = '1';while((P1IN & 0x01) == 0);};
- if((P1IN & 0x02) == 0){ keyValue = '5';while((P1IN & 0x02) == 0);};
- if((P1IN & 0x04) == 0){ keyValue = '9';while((P1IN & 0x04) == 0);};
- if((P1IN & 0x08) == 0){ keyValue = 'D';while((P1IN & 0x08) == 0);};
- keyboard_envent = ~keyboard_envent;
- }
- }
- P1OUT = 0x7f;//P1.7 = 0
- if((P1IN & 0x0f) != 0x0f){
- delay_ms8M(5);//消抖
- if((P1IN & 0x0f) != 0x0f){
- if((P1IN & 0x01) == 0){ keyValue = '0';while((P1IN & 0x01) == 0);};
- if((P1IN & 0x02) == 0){ keyValue = '4';while((P1IN & 0x02) == 0);};
- if((P1IN & 0x04) == 0){ keyValue = '8';while((P1IN & 0x04) == 0);};
- if((P1IN & 0x08) == 0){ keyValue = 'C';while((P1IN & 0x08) == 0);};
- keyboard_envent = ~keyboard_envent;
- }
- <div> }</div><div>}</div>
復制代碼
2. UART初始化 - void uartInit(){
- DCOCTL = 0;
- BCSCTL1 = CALBC1_1MHZ; // 選擇1MHZ作為波特率發生器輸入時鐘頻率
- DCOCTL = CALDCO_1MHZ;
-
- P3SEL = 0X30; // P3.4 TX P3.5 RX
- UCA0CTL1 |= UCSSEL_2;//clk = SMCLK 輔助時鐘 select clock source
- UCA0BR0 = 104; // 1MHZ / 9600 = 104.17
- UCA0BR1 = 0; // baud rate 9600
- UCA0MCTL = UCBRS0;//波特率調整 0.17 * 8 = 1.36 取整 1
-
- UCA0CTL1 &= ~UCSWRST; // 初始化USCI,復位釋放,結束初始化
- }
復制代碼
3. LCD1602初始化 - void lcd1602Init(){
- P3DIR |= BIT0 + BIT1 + BIT2;
- dataout;
- rst_en;
- P1DIR = 0X70;
-
- write_com(0x38);//8 bit data, doule line, 5x7 point matrix
- write_com(0x0c);//open display, close cusor,close blink
- write_com(0x06);//auto move to next char after write or read
- write_com(0x01);//clear screen
- }
復制代碼
實現過程中遇到的問題:
1. 按鍵按下后會連續不斷的發送,就算以很快的速度按下還是會發送兩次。 解決辦法:等待按鍵放開。 2. Proteus虛擬終端關閉后再也打不開。 解決辦法:感覺這是一個bug,只能重新建一個項目。
|