[SIZE=3][/SIZE][P][FACE=宋体][SIZE=9pt]程序已经做到最简,[/FACE][/SIZE][FACE=宋体][SIZE=9pt]P1.0~[/FACE][/SIZE]P1.7口接矩阵键盘8个引脚(4×4键盘),实现扫描键盘,存入str中
不知为何,在CCS V4中模拟时,watch str,得不到按键的数据。[/P][SIZE=3][/SIZE][P][FACE=宋体][SIZE=9pt]比如,当我运行的时候每间断时间,按[/FACE][/SIZE][FACE=宋体][SIZE=9pt]2,[/FACE][/SIZE]str中没有任何显示2。连长得像个2的都没有。。。。[/P][SIZE=3][/SIZE][P][FACE=宋体][SIZE=9pt]时钟是[/FACE][/SIZE][FACE=宋体][SIZE=9pt]32.768khz[/FACE][/SIZE][/P][SIZE=3][/SIZE][P][FACE=宋体][SIZE=9pt]//后为注释。[/FACE][/SIZE]
如需更多信息,可以加QQ:329974601 共同讨论。[/P][SIZE=3][/SIZE][P][FACE=宋体][SIZE=9pt]
问题[/FACE][/SIZE]1:是否要加入TIMER或中断?
问题2:程序逻辑是否有问题?
问题3:如何修改?[/P][SIZE=3][/SIZE][P][FACE=宋体][SIZE=9pt]问题[/FACE][/SIZE][FACE=宋体][SIZE=9pt]4:因为我要做一个计算器,[/FACE][/SIZE]key_map能否改为: key_Map[16]={ [/P][SIZE=3][/SIZE][P][FACE=宋体][SIZE=9pt]'1','2','3','+',
'4','5','6','-',
'7','8','9','*',
'^','0','=','/'[/FACE][/SIZE][/P][SIZE=3][/SIZE][P][FACE=宋体][SIZE=9pt]};
多谢多谢~~[/FACE][/SIZE]
程序如下:
/**********************************************
**********************************************/
#include <msp430g2231.h>
unsigned char key_Pressed, // if there is key pressed
key_val, // save the keyvalue
key_Flag; // 按键是否释放,即取消脉冲的干扰
// in otherwords, the program can scan the next value
unsigned char tmp;
unsigned char key_Map[16] ={ //keypad decode table
0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,
0xc6,0xa1,0x86,0x8e
};
unsigned char str[30]={0};
int counter=0;
//------------------------------------------------------------------------------
// Function prototypes
//------------------------------------------------------------------------------
void init_Keyboard();
void check_Key();
void delay();
void key_Event();
/*********************************************
* main()
*********************************************/
void main(void)
{
unsigned char str[30]={0};
int counter=0;
WDTCTL = WDTHOLD + WDTPW ; // stop watchdog
init_Keyboard(); // 初始化 键盘[/P][SIZE=3][/SIZE][P][FACE=宋体][SIZE=9pt]while(1)
{
key_Event(); //检测是否有按键按下[/FACE][/SIZE][/P][SIZE=3][/SIZE][P][FACE=宋体][SIZE=9pt]if (key_Flag == 1) // if there is value
{
key_val = 0xFF;
key_Flag = 0; // clear flag
}
}
}
void init_Keyboard(void)
{
P1DIR |= 0x0F; // P1.0~P1.3 设为输入[/FACE][/SIZE]
P1DIR &= 0x0F; // set P1.4~P1.7 设为输出
P1OUT |= 0xF0; // P1.4~P1.7 输出 0
key_Flag = 0;// initialize key_Flag
key_Pressed = 0;// initialize key_Pressed
key_val = 0xFF;
}
/*********************************************
* Check_Key(),get key value
*********************************************/
void check_Key(void){
unsigned char row ,col,tmp1,tmp2;
// 设置逐行扫描,单次,只选择一位输出0,其余位输出1,检测输入值,是否为0,若为0,则定位了行列
tmp1 = 0x80;
for(row=0;row<4;row++){
P1OUT |= 0xF0; // P1.4~P1.7=1
P1OUT &= ~tmp1; //onepin of P1.4~P1.7 is 0
tmp1 >>= 1; // tmp1 右移[/P][SIZE=3][/SIZE][P][FACE=宋体][SIZE=9pt]P1DIR |= 0x0F;
delay();
if ((P1IN & 0x0F) < 0x0f){ // if P1IN P1.0~P1.3has one 0
tmp2 = 0x01; // tmp2is used to check which col is 0
for(col =0;col<0x04;col++){ // check col
if((P1IN & tmp2)!=0x00){ // judge this col
key_val =key_Map[ row*4 +col] ; 查表得到键值[/FACE][/SIZE]
str[counter]=key_val; 赋值给str
counter++;
return; // out of loop
}
tmp2 <<= 1; // tmp2 shuffer left
}
}
if(key_val==0xFF)
{
key_Pressed = 0;
}
}
}
/***********************************************************
* delay,cancel the glitch
************************************************************/
void delay(){
unsigned char tmpa;
for(tmpa=0x0f;tmpa>0;tmpa--);
}
/************************************************************
* key_Event(),check if there is key being pressed,if soget the value
*************************************************************/
void key_Event(void)
{
P1OUT &= 0x0F; // set P1.0 to 1.3 output 1
P1OUT = 0xFF;
P1DIR &= 0xF0; // P1.0~P1.3 set as input
P1DIR |= 0xF0; // set P1.4~P1.7 set as output
P1OUT &= 0x0F; // set P1OUT
tmp = P1IN; // get P1IN
if ((key_Pressed ==0x00)&&((tmp & 0x0F) < 0x0f))
{ //if there is key pressed
key_Pressed = 1; // if so,set key_Pressed
delay(); //cancel glitch
check_Key(); // call check_Key(),get key value
}else if ((key_Pressed ==1)&&((tmp & 0x0F) == 0x0F))
{ //if key is relessed
key_Pressed = 0; // clear key_Pressed
key_Flag = 1; // set key_Flag
}
}[/P][SIZE=3][/SIZE][P][FACE=Times New Roman][SIZE=3] [/FACE][/SIZE][/P][SIZE=3][/SIZE] |