一个矩阵键盘的程序,单步调试出来的结果是对的。但全速运行就不对了
#include <msp430.h>
#define CPU_F (25000000)
#define delay_us(x) __delay_cycles((long)(CPU_F*(double)x/1000000.0))//延时x微秒
#define delay_ms(x) __delay_cycles((long)(CPU_F*(double)x/1000.0)) //延时x毫秒
unsigned char key(void)
{
static unsigned char column,cc,line,ll,key;
column = 0;
cc=0;
line = 0;
key = 30;
P6DIR |= (BIT0 + BIT1 + BIT2 + BIT3);
P6OUT |= (BIT0 + BIT1 + BIT2 + BIT3);
P2DIR &= ~(BIT3 + BIT4 + BIT5 + BIT6);
column = P2IN & (BIT3 + BIT4 + BIT5 + BIT6);
column = column>>3;
if(column)
{
// delay_ms(5);
// cc = P2IN & (BIT3 + BIT4 + BIT5 + BIT6);
// cc = cc>>3;
// if(cc==column)
// {
switch(column)
{
case 1:column = 0;break;
case 2:column = 1;break;
case 4:column = 2;break;
case 8:column = 3;break;
}
P2DIR |= (BIT3 + BIT4 + BIT5 + BIT6);
P2OUT |= (BIT3 + BIT4 + BIT5 + BIT6);
P6DIR &= ~(BIT0 + BIT1 + BIT2 + BIT3);
line = P6IN & (BIT0 + BIT1 + BIT2 + BIT3);
// delay_ms(5);
// ll = P6IN & (BIT0 + BIT1 + BIT2 + BIT3);
// if(ll==line)
// {
switch(line)
{
case 1:line = 0;break;
case 2:line = 1;break;
case 4:line = 2;break;
case 8:line = 3;break;
}
key = column * 4 + line + 1;
while(P6IN & (BIT0 + BIT1 + BIT2 + BIT3));
// }
// }
}
return key;
}
此帖出自
小平头技术问答
一周热门 更多>