MSP4301602显示Ⅰ
扫描二维码
随时随地手机看文章
/********************************************************************
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口为数据口
#define keyin (P1IN & 0xf0)
uchar temp,keynum = 0;
int shi=23,fen=59,miao=58;
//*************************************************************************
// 系统时钟初始化
//*************************************************************************
void Clock_Init()
{
uchar i;
BCSCTL1&=~XT2OFF; //打开XT振荡器
BCSCTL2|=SELM1+SELS; //MCLK为8MHZ,SMCLK为8MHZ
do{
IFG1&=~OFIFG; //清除震荡标志
for(i=0;i<100;i++)
_NOP(); //延时等待
}
while((IFG1&OFIFG)!=0); //如果标志为1,则继续循环等待
IFG1&=~OFIFG;
}
//*************************************************************************
// MSP430内部看门狗初始化
//*************************************************************************
void WDT_Init()
{
WDTCTL = WDTPW + WDTHOLD; //关闭看门狗
}
//*************************************************************************
// 初始化IO口子程序
//*************************************************************************
void Port_init()
{
P1SEL = 0x00; //P2普通IO功能
P1DIR = 0x0F; //P24~P27输入模式,外部电路已接上拉电阻
P4SEL = 0x00;
P4DIR = 0xFF;
P3SEL = 0x00;
P3DIR|= BIT3 + BIT6 + BIT7; //控制口设置为输出模式
}
//***********************************************************************
// 显示屏命令写入函数
//***********************************************************************
void LCD_write_com(unsigned char com)
{
RS_CLR;
RW_CLR;
EN_SET;
DataPort = com; //命令写入端口
delay_ms(5);
EN_CLR;
}
//***********************************************************************
// 显示屏数据写入函数
//***********************************************************************
void LCD_write_data(unsigned char data)
{
RS_SET;
RW_CLR;
EN_SET;
DataPort = data; //数据写入端口
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);
}
//***********************************************************************
// 显示屏字符串写入函数
//***********************************************************************
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 ++;
}
}
//***********************************************************************
// 显示屏单字符写入函数
//***********************************************************************
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);
}
//***********************************************************************
// 显示屏初始化函数
//***********************************************************************
void LCD_init(void)
{
LCD_write_com(0x38); //显示模式设置
delay_ms(5);
LCD_write_com(0x08); //显示关闭
delay_ms(5);
LCD_write_com(0x01); //显示清屏
delay_ms(5);
LCD_write_com(0x06); //显示光标移动设置
delay_ms(5);
LCD_write_com(0x0C); //显示开及光标设置
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(); //看门狗设置
// Clock_Init(); //系统时钟设置
Port_init(); //系统初始化,设置IO口属性
delay_ms(100); //延时100ms
LCD_init(); //液晶参数初始化设置
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();
}