/********************************************************************
1602液晶顯示
********************************************************************/
#include
#define CPU_F ((double)8000000)
#define delay_us(x) __delay_cycles((long)(CPU_F*(double)x/1000000.0))
#define delay_ms(x) __delay_cycles((long)(CPU_F*(double)x/1000.0))
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
#define P10 3
#define P11 6
#define P12 7
#define RS_CLR P3OUT &= ~(1 << P10) //RS置低
#define RS_SET P3OUT |= (1 << P10) //RS置高
#define RW_CLR P3OUT &= ~(1 << P11) //RW置低
#define RW_SET P3OUT |= (1 << P11) //RW置高
#define EN_CLR P3OUT &= ~(1 << P12) //E置低
#define EN_SET P3OUT |= (1 << P12) //E置高
#define DataPort P4OUT //P4口為數(shù)據(jù)口
#define keyin (P1IN & 0xf0)
uchar temp,keynum = 0;
int shi=23,fen=59,miao=58;
//*************************************************************************
// 系統(tǒng)時鐘初始化
//*************************************************************************
void Clock_Init()
{
uchar i;
BCSCTL1&=~XT2OFF; //打開XT振蕩器
BCSCTL2|=SELM1+SELS; //MCLK為8MHZ,SMCLK為8MHZ
do{
IFG1&=~OFIFG; //清除震蕩標(biāo)志
for(i=0;i<100;i++)
_NOP(); //延時等待
}
while((IFG1&OFIFG)!=0); //如果標(biāo)志為1,則繼續(xù)循環(huán)等待
IFG1&=~OFIFG;
}
//*************************************************************************
// MSP430內(nèi)部看門狗初始化
//*************************************************************************
void WDT_Init()
{
WDTCTL = WDTPW + WDTHOLD; //關(guān)閉看門狗
}
//*************************************************************************
// 初始化IO口子程序
//*************************************************************************
void Port_init()
{
P1SEL = 0x00; //P2普通IO功能
P1DIR = 0x0F; //P24~P27輸入模式,外部電路已接上拉電阻
P4SEL = 0x00;
P4DIR = 0xFF;
P3SEL = 0x00;
P3DIR|= BIT3 + BIT6 + BIT7; //控制口設(shè)置為輸出模式
}
//***********************************************************************
// 顯示屏命令寫入函數(shù)
//***********************************************************************
void LCD_write_com(unsigned char com)
{
RS_CLR;
RW_CLR;
EN_SET;
DataPort = com; //命令寫入端口
delay_ms(5);
EN_CLR;
}
//***********************************************************************
// 顯示屏數(shù)據(jù)寫入函數(shù)
//***********************************************************************
void LCD_write_data(unsigned char data)
{
RS_SET;
RW_CLR;
EN_SET;
DataPort = data; //數(shù)據(jù)寫入端口
delay_ms(5);
EN_CLR;
}
//***********************************************************************
// 顯示屏清空顯示
//***********************************************************************
void LCD_clear(void)
{
LCD_write_com(0x01); //清屏幕顯示
delay_ms(5);
}
//***********************************************************************
// 寫入時分秒
//***********************************************************************
void LCD_write_sfm(uchar add,uchar dat)
{
uchar a,b;
a = dat/10;
b = dat%10;
LCD_write_com(0xc0 + add);
LCD_write_data(a+0x30);
LCD_write_data(b+0x30);
}
//***********************************************************************
// 顯示屏字符串寫入函數(shù)
//***********************************************************************
void LCD_write_str(unsigned char x,unsigned char y,unsigned char *s)
{
if (y == 0)
{
LCD_write_com(0x80 + x); //第一行顯示
}
else
{
LCD_write_com(0xC0 + x); //第二行顯示
}
while (*s)
{
LCD_write_data( *s);
s ++;
}
}
//***********************************************************************
// 顯示屏單字符寫入函數(shù)
//***********************************************************************
void LCD_write_char(unsigned char x,unsigned char y,unsigned char data)
{
if (y == 0)
{
LCD_write_com(0x80 + x); //第一行顯示
}
else
{
LCD_write_com(0xC0 + x); //第二行顯示
}
LCD_write_data( data);
}
//***********************************************************************
// 顯示屏初始化函數(shù)
//***********************************************************************
void LCD_init(void)
{
LCD_write_com(0x38); //顯示模式設(shè)置
delay_ms(5);
LCD_write_com(0x08); //顯示關(guān)閉
delay_ms(5);
LCD_write_com(0x01); //顯示清屏
delay_ms(5);
LCD_write_com(0x06); //顯示光標(biāo)移動設(shè)置
delay_ms(5);
LCD_write_com(0x0C); //顯示開及光標(biāo)設(shè)置
delay_ms(5);
}
//***********************************************************************
// 時鐘顯示
//***********************************************************************
void clock(void)
{
miao++;
if(miao==60)
{
miao=0;
fen++;
if(fen==60)
{
fen=0;
shi++;
if(shi==24)
{
shi=0;
}
LCD_write_sfm(2,shi);
}
LCD_write_sfm(5,fen);
}
LCD_write_sfm(8,miao);
}
//***********************************************************************
// 按鍵檢測
//***********************************************************************
void keyscan()
{
if(keyin != 0xf0)
{
delay_ms(50);
if(keyin != 0xf0)
{
temp = keyin;
while(keyin != 0xf0);
if(temp==0xe0)
{
P2OUT |= BIT0;
keynum++;
switch(keynum)
{
case 1:
_DINT();
LCD_write_com(0xc0+9);
LCD_write_com(0x0f);
case 2:
LCD_write_com(0xc0+6);break;
case 3:
LCD_write_com(0xc0+3);break;
case 4:
keynum = 0;
LCD_write_com(0x0c);
}
}
if(temp==0xd0)
{
switch(keynum)
{
case 1:
miao++;
if(miao==60)miao=0;
LCD_write_sfm(8,miao);
LCD_write_com(0xc0+9);
break;
case 2:
fen++;
if(fen==60)fen=0;
LCD_write_sfm(5,fen);
LCD_write_com(0xc0+6);
case 3:
shi++;
if(shi==24)shi=0;
LCD_write_sfm(3,shi);
LCD_write_com(0xc0+4);
}
}
if(temp==0xb0)
{
switch(keynum)
{
case 1:
miao--;
if(miao==-1)miao=59;
LCD_write_sfm(8,miao);
LCD_write_com(0xc0+9);
break;
case 2:
fen--;
if(fen==-1)fen=59;
LCD_write_sfm(5,fen);
LCD_write_com(0xc0+6);
case 3:
shi--;
if(shi==-1)shi=23;
LCD_write_sfm(3,shi);
LCD_write_com(0xc0+4);
}
}
}
}
}
//***********************************************************************
// 主程序
//***********************************************************************
void main(void)
{
P2DIR |= BIT0 + BIT1 + BIT2;
P2OUT &= ~(BIT0 + BIT1 + BIT2);
WDT_Init(); //看門狗設(shè)置
// Clock_Init(); //系統(tǒng)時鐘設(shè)置
Port_init(); //系統(tǒng)初始化,設(shè)置IO口屬性
delay_ms(100); //延時100ms
LCD_init(); //液晶參數(shù)初始化設(shè)置
LCD_clear(); //清屏
LCD_write_com(0xc0+4);
LCD_write_data(':');
LCD_write_com(0xc0+7);
LCD_write_data(':');
LCD_write_sfm(2,shi);
LCD_write_sfm(5,fen);
LCD_write_sfm(8,miao);
CCR0 = 1023;
TACTL = TASSEL_2 + MC_1;
CCTL0 |= CCIE;
_EINT();
while(1)
{
keyscan();
}
}
#pragma vector=TIMERA0_VECTOR
__interrupt void TimerA_ISR(void)
{
clock();
}