资源预览内容
第1页 / 共11页
第2页 / 共11页
第3页 / 共11页
第4页 / 共11页
第5页 / 共11页
第6页 / 共11页
第7页 / 共11页
第8页 / 共11页
第9页 / 共11页
第10页 / 共11页
亲,该文档总共11页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
基于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) / / 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 translation/* 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 / / 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 ()* 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 output, 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, observe 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 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); / / 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 & 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)! = 0x0F); / / 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, 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 ()*
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号