为什么有时候按下按键的时候时钟是卡住不跑,还有就是为什么会按一下跳跃好几个数值?
#include<msp430x16x.h>
#define uint unsigned int
#define uchar unsigned char
#define pkey (P1IN&0X0F)
uchar sec=0,min=0,hor=0;
/*延时子程序*/
void delay(uint z)
{
while(z--);
}
/*write LCD command code*/
void LCDcommand(uchar wdata)
{
P6OUT&=~0X08;//clear RS
P6OUT&=~0X10;//clear RW
P6OUT&=~0X20;//clear E
delay(100);
P6OUT|=0X20;//SET UP E
P2OUT=wdata;//sent command or data
delay(100);
P6OUT&=~0X20;//clear E
}
/*write data*/
void LCDdata(uint wdata)
{
P6OUT|=0X08;//SET RS
P6OUT&=~0X10;//clear RW
P6OUT&=~0X20;//clear E
delay(100);
P6OUT|=0X20;//SET UP E
P2OUT=wdata;//sent command or data
delay(100);//delay instead of check busy
P6OUT&=~0X20;//clear E
}
/*init LCD*/
void LCD_init(void)
{
LCDcommand(0x38);//all secrean
delay(5);
LCDcommand(0x01);//clear display
LCDcommand(0x0c);
LCDcommand(0x06);
}
////////////////////////设置光标显示,x行号y列号
void display_xy(uchar x,uchar y)
{
if(y)
{
x+=0x80+0x40;
}
else
{
x+=0x80;
}
LCDcommand(x);
}
////////////////////////在光标位置显示字符串
void display_1(uchar m,uchar n,uchar data)
{
display_xy(m,n);
LCDdata(data);
}
////////////////////////clock program
void clock(void)
{
sec++;
if(sec==60)
{
sec=0;
min++;
}
if(min==60)
{
min=0;
hor++;
}
if(hor==24)
{
hor=0;
min=0;
sec=0;
}
}
////////////////////////display
void display(void)
{
uchar sec0,sec1,min0,min1,hor0,hor1;
sec1=sec/10;
sec0=sec-sec1*10;
min1=min/10;
min0=min-min1*10;
hor1=hor/10;
hor0=hor-hor1*10;
sec1+=48;
sec0+=48;
min1+=48;
min0+=48;
hor1+=48;
hor0+=48;
display_1(11,1,sec0);
display_1(10,1,sec1);
display_1(9,1,':');
display_1(8,1,min0);
display_1(7,1,min1);
display_1(6,1,':');
display_1(5,1,hor0);
display_1(4,1,hor1);
}
///////////////////////////main program
int main(void)
{
WDTCTL=WDTPW+WDTHOLD;//CLOSE DOG
P2DIR=0XFF;
P6DIR=0XFF;
P1IES = 0x0f; // P1.0~P1.3选择下降沿中断
P1IE = 0x0f; // 打开中断使能
P1DIR = 0XF0; //设置P1.0~P.3为输入状态
P1OUT = 0;
// P1IFG=0;
_EINT();
LCD_init();
CCR0=32768-1;
TACTL=TASSEL_1+MC_1;
CCTL0|=CCIE;
_EINT();
while(1)
{
LPM3;
display();
}
}
#pragma vector=PORT1_VECTOR
__interrupt void key_interrupt(void)
{
if(P1IFG&0X0F)
switch(P1IFG)
{
case 0x01:
if(pkey==0x0e)
{
delay(30);
if(pkey==0x0e)
{
while(pkey!=0x0f)
{
min++;
P1IFG=0;
return;
}
}
} break;
case 0x02:
if(pkey==0x0d)
{
delay(30);
if(pkey==0x0d)
{
while(pkey!=0x0f)
{
hor++;
P1IFG=0;
return;
}
}
} break;
case 0x04:
if(pkey==0x0b)
{
delay(30);
if(pkey==0x0b)
{
while(pkey!=0x0f)
{
min--;
P1IFG=0;
return;
}
}
} break;
case 0x08:
if(pkey==0x07)
{
delay(30);
if(pkey==0x07)
{
while(pkey!=0x0f)
{
hor--;
P1IFG=0;
return;
}
}
} break;
default:break;
}
}
#pragma vector = TIMERA0_VECTOR
__interrupt void Timer_A (void)
{
clock();
LPM3_EXIT;
}
此帖出自
小平头技术问答
一周热门 更多>