用MSP430的32768Hz晶振寫的時(shí)鐘
typedef unsigned char uchar;
typedef unsigned int uint;
//typedef也是一種宏定義類型,其書寫方式和#define相反
/*******液晶端口初始化*******/
#define out P3DIR|=BIT0+BIT1+BIT2//把P3_012三個(gè)口設(shè)為輸出
#define lcd_enset P3OUT|=BIT2
#define lcd_enclear P3OUT&=~BIT2
#define lcd_rw1 P3OUT|=BIT1
#define lcd_rw0 P3OUT&=~BIT1
#define lcd_rs1 P3OUT|=BIT0
#definelcd_rs0 P3OUT&=~BIT0
#define keyin (P1IN & 0x0f)
uchar temp,keynum=0;
int shi=23,fen=59,miao=55;//此處用char型會(huì)在按鍵調(diào)整減數(shù)到0時(shí)出錯(cuò)
/**********/
/*延時(shí)子程序*/
//調(diào)用8個(gè)時(shí)鐘周期,返回6個(gè),循環(huán)4個(gè)
//總周期個(gè)數(shù)為: 8+6+4*n=14+4*n 個(gè)時(shí)鐘周期
//默認(rèn)800k時(shí)鐘,所以周期為1.25us 公式修正:17.5+5*n=定時(shí)時(shí)間
void delay(unsigned int x)//延時(shí)
{
unsigned int i;
i=x;
for(;i>0;i--) ;
}
/******LCD寫指令程序***********/
void write_com(unsigned char com)
{ out;
P4DIR=0XFF;
P4OUT=com;
lcd_rw0;
lcd_rs0;
_NOP();
lcd_enclear;
delay(10);
lcd_enset;
delay(10);
lcd_enclear;
}
/***********LCD寫數(shù)據(jù)**************/
void write_data(unsigned char dat)
{ out;
P4DIR=0XFF;
P4OUT=dat;
lcd_rw0;
lcd_rs1;
_NOP();
_NOP();
lcd_enclear;
delay(10);
lcd_enset;
delay(10);
lcd_enclear;
}
/*****寫時(shí)分秒********/
void write_sfm(uchar add,uchar dat)
{
uchar a,b;
a=dat/10;
b=dat%10;
write_com(0x80+0x40+add);
write_data(a+0x30);
write_data(b+0x30);
}
/*********lcd初始化,寫lcd功能與方式控制********/
voidlcd_init()
{ out;
write_com(0x38);
delay(20);
write_com(0x0f);
delay(20);
write_com(0x06);
delay(20);
write_com(0x0c);
delay(20);
write_com(0x01);
delay(20);
評(píng)論