#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(200);
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;
}
}
////////////////////////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);
}
///////////////////////////keyscan
void keyscan(void)
{
uchar temp;
if(pkey!=0X0F)
{
delay(200);
if(pkey!=0x0f)
{
temp=pkey;
switch(temp)
{
case 0x0e: sec++;break;
case 0x0d: min++;break;
case 0x0b: hor++;break;
// case 0x07: hor++;break;
default:break;
}
}
}
}
///////////////////////////main program
int main(void)
{
WDTCTL=WDTPW+WDTHOLD;//CLOSE DOG
P2DIR=0XFF;
P6DIR=0XFF;
LCD_init();
CCR0=32768-1;
TACTL=TASSEL_1+MC_1;
CCTL0|=CCIE;
_EINT();
while(1)
{
LPM3;
display();
keyscan();
}
}
#pragma vector = TIMERA0_VECTOR
__interrupt void Timer_A (void)
{
clock();
LPM3_EXIT;
}
此帖出自
小平头技术问答
一周热门 更多>