基于mspf的矩阵按键扫描程序.doc

上传人:marr****208 文档编号:133553563 上传时间:2020-05-28 格式:DOC 页数:11 大小:18.01KB
返回 下载 相关 举报
基于mspf的矩阵按键扫描程序.doc_第1页
第1页 / 共11页
基于mspf的矩阵按键扫描程序.doc_第2页
第2页 / 共11页
基于mspf的矩阵按键扫描程序.doc_第3页
第3页 / 共11页
基于mspf的矩阵按键扫描程序.doc_第4页
第4页 / 共11页
基于mspf的矩阵按键扫描程序.doc_第5页
第5页 / 共11页
点击查看更多>>
资源描述

《基于mspf的矩阵按键扫描程序.doc》由会员分享,可在线阅读,更多相关《基于mspf的矩阵按键扫描程序.doc(11页珍藏版)》请在金锄头文库上搜索。

1、基于msp430f5438的矩阵按键扫描程序* * function: matrix key scan program*/#include msp430x54x.h/* macro definition*/#define uint unsigned int / / definition data type abbreviation#define uchar unsigned char#define KEY_IN P4IN / / define port register#define KEY_OUT P4OUT#define KEY_DIR P4DIR#define B0 (0x01) / /

2、 a definition#define B1 (0x02)#define B2 (0x04)#define B3 (0x08)#define B4 (0x10)#define B5 (0x20)#define B6 (0x40)#define B7 (0x80)/* function declaration*/Uchar Scan (void); / / matrix keyboard scanningVoid Delay (uint delay_num); / / delay functionUint Translate (uchar code); / / scan code transl

3、ation/* global variable definition*/Uchar key_code; / / return to the keyboard scan codeUint key_value; / / the corresponding digital keyboardConst uchar / / key_table17=scan decoding table0xFF / / no key press / / four high scanning line, for the low four column scanning0x7E / / K10x7D / / K20x7B /

4、 / K30x77 / / K40xBE / / K50xBD / / K60xBB / / K70xB7 / / K80xDE / / K90xDD / / K100xDB / / K110xD7 / / K120xEE / / K130xED / / K140xEB / / K150xE7 / / K16;/* function (module) Name: Main ()* * function: call the keyboard scanner, initialize the I/O port* call the function list: scan (), Translate (

5、)* call this function list: None* input parameters: None* output parameters: None* * function return value: no* Resource watchdog I/O port module used by * * other instructions: None*/Void main (void)WDTCTL = WDTPW + WDTHOLD; / / stop watchdogKEY_DIR = (B7 + B6 + B5 + B4); / / set IO high 4 bit outp

6、ut, low input for 4KEY_OUT = 0x00; / / IO high 4 bit output low levelWhile (1)Key_code = Scan ();Call / return scanner, scan codeKey_value = Translate (key_code); / / call scan code translation function returns the digital keyboardIf (key_value! = 0) / / if a key(_NOP); / / set a breakpoint here, ob

7、serve the scan code and digital keyboard/* function (module) Name: Scan ()* function: query the key status, return the key value* call the function list: Delay ();* call this function list: None* input parameters: None* output parameters: keyboard scan code* * function return value: no* resource I/O

8、 port used by * * other instructions:*/Uchar Scan (void)Uint i;Uchar col_scan = 0; / / scan variableUchar key_code = 0xFF; / / scan codeKEY_OUT = 0x00; / / all 4 lines to zero, to determine whether a key is pressedIf (KEY_IN & 0x0F)! = / / 0x0F) to determine whether a key is pressedDelay (5000); / /

9、 keyboard debounce delay 20 msIf (KEY_IN & 0x0F)! = 0x0F) / / again determine the key is validKey_code = KEY_IN & 0x0F; / / save column scan codeCol_scan = B7; / / the first scan line 1For (i=4; i0; i-) / / scan lineKEY_OUT = 0xFF & (col_scan); / / the corresponding line scan set low levelIf (KEY_IN

10、 & 0x0F)! = 0x0F) / / if theres a buttonBreak;Col_scan = col_scan 1; / / scan lineCol_scan = (col_scan & 0xF0); / / get the line scan codeKey_code |= col_scan; / / get the full keyboard scan codeKEY_OUT = 0x00; / / all 4 lines to zero, to determine whether a key is pressedWhile (KEY_IN & 0x0F)! = 0x

11、0F); / / wait for the press releaseReturn (key_code); / / return key/* function (module) Name: Translate* * function: scan code translation* call the function list: None* call this function list: None* input parameters: None* output parameter: keyboard corresponds to the number, if the output is 0,

12、indicating no key press* * function return value: no* * resources are not available* other instructions:*/Uint Translate (uchar code)Uchar key_cd = code; / / scan codeUint key_val; / / digital keyboardFor (key_val=0; key_val=16; key_val+) / / find scanning table by digital keyboardIf (key_tablekey_val = = key_cd) / / scan code complianceBreak;Return (key_val);/* function (module) Name: Delay ()*

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 高等教育 > 其它相关文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号