各位,调了半天了,怎么貌似都不得行啊,麻烦各位大神帮我看一看~,谢谢!
#include "msp430g2553.h"
unsigned char H[]={0xe0,0xd0,0xb0,0x70}; //行
unsigned char L[] ={BIT0,BIT1,BIT2,BIT3}; //列
unsigned int keyvalue;
unsigned int keyscan(void);
int main( void )
{
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer to prevent time out reset
P1SEL=0X00;
P1DIR =0Xf0;
P2DIR |= BIT0; // 设置输出
// P2OUT &=~BIT0;
for(;;)
{
keyscan();
if(keyvalue<16)
{
P2OUT &=~BIT0;
_delay_cycles(500);
}
}
}
unsigned int keyscan(void)
{
unsigned int i=0,lie=0,j=0,hang=0;
P1REN=0X0F; //上拉使能
// P1IN=0X0f;
P1OUT=0xf0;
if((P1IN & 0X0F)!=0X0F)
{
_delay_cycles(100);
if((P1IN & 0X0F)!=0X0F)
{
_delay_cycles(100);
for(i=0;i<4;i++) //行检测
{
P1OUT = H
;
for(j=0;j<4;j++)//列检测
{
if((P1IN & L[j])!=L[j])
{
// P1OUT=0X00;
hang=i;
lie=j;
keyvalue=lie+hang*4;
}
}
}
}
}
return keyvalue;
}
此帖出自小平头技术问答
[ 本帖最后由 carlnerv 于 2012-8-3 09:41 编辑 ]
- #include "msp430g2553.h" /*这里引号行吗?引号引用工程内文件,<>尖括号引用编译器的文件*/
- unsigned char H[]={0xe0,0xd0,0xb0,0x70}; //行 /*低电平扫描行的话,列的针脚最好加上拉电阻*/
- unsigned char L[] ={BIT0,BIT1,BIT2,BIT3}; //列 /*最好与行对应为低电平*/
- unsigned int keyvalue;
- unsigned int keyscan(void); /*这个函数里已经对keyvalue操作了,就不用返回keyvalue值了*/
- int main( void )
- {
- WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer to prevent time out reset
- P1SEL=0X00; /*这句一般不用写,PxSEL默认是00*/
- P1DIR =0Xf0;
- P2DIR |= BIT0; // 设置输出
- // P2OUT &=~BIT0;
- for(;;)
- {
- keyscan();
- if(keyvalue<16)
- {
- P2OUT &=~BIT0;
- _delay_cycles(500);
- }
- }
- }
- unsigned int keyscan(void)
- {
- unsigned int i=0,lie=0,j=0,hang=0;
- P1REN=0X0F; //上拉使能 /*这句可以放在main中初始化部分*/
- // P1IN=0X0f;
- P1OUT=0xf0;
- if((P1IN & 0X0F)!=0X0F) /*这部分是判断有键持续按下吧,但是不扫描怎么知道有键按下,这个可以放到扫描语句里*/
- {
- _delay_cycles(100);
- if((P1IN & 0X0F)!=0X0F)
- {
- _delay_cycles(100);
- for(i=0;i<4;i++) //行检测
- {
- P1OUT = H; /*笔误吧,或者是跟论坛的代码冲突了*/
- for(j=0;j<4;j++)//列检测
- {
- if((P1IN & L[j])!=L[j]) /*我不太会这种判断,我一般直接判断哪个脚等于什么值*/
- {
- // P1OUT=0X00;
- hang=i; /*这两句有点啰嗦吧*/
- lie=j;
- keyvalue=lie+hang*4; /*G2553能直接做乘法吗?我编译时总提示让我用硬件乘法器,而且硬乘还得等几个机器周期*/
- }
- }
- }
- }
- }
- return keyvalue; /*这句没什么用*/
- }
复制代码我写了一个,你可以参考一下- /* 4x4键盘,按键值为0-15,并以2进制数点亮Led
- *
- * P1.0-P1.7接键盘1-8脚,P2.0-P2.3接Led,共阴接法
- *
- */
- #include
- const unsigned char Row[]={0x01,0x02,0x04,0x08}; //行为高电平扫描
- const unsigned char Col[]={0x10,0x20,0x40,0x80}; //列检测高电平
- const unsigned char Led[]={0x00,0x01,0x02,0x03,
- 0x04,0x05,0x06,0x07,
- 0x08,0x09,0x0a,0x0b,
- 0x0c,0x0d,0x0e,0x0f}; //Led亮法
- volatile unsigned short KeyBuff = 0; //定义为char的话存储的不是数值,不知为何
- volatile unsigned int i,j,temp1; //全局定义会不会对内存有优化,而不是每次调用函数都重新定义
- void ScanKey()
- {
- //KeyBuff = 0;
- for(i=0;i<4;i++) //行扫描
- {
- P1OUT = Row[i]; //行依次为高电平
- __delay_cycles(1000); //防抖延时,不知道该插到哪里
- temp1 = P1IN & 0xf0; //只取列的数据,PxOUT会影响PxIN的数据
- for(j=0;j<4;j++) //扫描列
- {
- if(temp1 == Col[j]) //判断按键取值为那一列对应的值
- {
- KeyBuff = (i<<2) + j; //计算该按键对应的数值
- }
- }
-
- }
-
- }
- void main(void)
- {
- WDTCTL = WDTPW + WDTHOLD;
-
- P1DIR |= 0x0f; //P1方向,低位输出,高位输入
- P1REN |= 0xf0; //开启P1输入端电阻
- P2DIR |= 0x0f; //P2输出
-
- while(1)
- {
- ScanKey(); //扫描按键
- P2OUT = Led[KeyBuff]; //点亮Led
- }
- }
复制代码[ 本帖最后由 carlnerv 于 2012-8-3 15:04 编辑 ]
一周热门 更多>