typedef unsigned char uchar;
typedef unsigned int uint;
//typedef也是一种宏定义类型,其书写方式和#define相反
/*******液晶端口初始化*******/
#define out P3DIR|=BIT0+BIT1+BIT2//把P3_012三个口设为输出
#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
#define lcd_rs0 P3OUT&=~BIT0
#define keyin (P1IN & 0x0f)
uchar temp,keynum=0;
int shi=23,fen=59,miao=55;//此处用char型会在按键调整减数到0时出错
/**********/
/*延时子程序*/
//调用8个时钟周期,返回6个,循环4个
//总周期个数为: 8+6+4*n=14+4*n 个时钟周期
//默认800k时钟,所以周期为1.25us 公式修正:17.5+5*n=定时时间
void delay(unsigned int x)//延时
{
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写数据**************/
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;
}
/*****写时分秒********/
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功能与方式控制********/
void lcd_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);
[page]
}
void Clock(void)
{
miao++;
if(miao==60)
{
miao=0;
fen++;
if(fen==60)
{
fen=0;
shi++;
if(shi==24)
{
shi=0;
}
write_sfm(2,shi);
}
write_sfm(5,fen);
}
write_sfm(8,miao);
}
void keyscan()
{
if(keyin!=0x0f)
{
delay(5); //延时消抖
if(keyin != 0x0f) //再次检测按键状态
{
temp=keyin;
while(keyin != 0x0f); //等待按键被放开
if(temp==0x0e)
{
keynum++;
switch(keynum)
{
case 1:
_DINT();
// CCTL0 &=~ CCIE; //两种停止时钟的方法效果一样
write_com(0x80+0x40+9);
write_com(0x0f);//光标开始闪
break;
case 2:
write_com(0x80+0x40+6);break;
case 3:
write_com(0x80+0x40+3);break;
case 4:
keynum=0;
write_com(0x0c);//开显示,光标不显示
_EINT();
// CCTL0|=CCIE; //使用相应打开时钟的方法
break;
}
}
if(temp==0x0d)
{
// while(keyin != 0x0d); //等待按键被放开
switch(keynum)
{
case 1:
miao++;
if(miao==60)miao=0;
write_sfm(8,miao);
write_com(0x80+0x40+9);
break;
case 2:
fen++;
if(fen==60)fen=0;
write_sfm(5,fen);
write_com(0x80+0x40+6);
break;
case 3:
shi++;
if(shi==24)shi=0;
write_sfm(2,shi);
write_com(0x80+0x40+3);
break;
}
}
if(temp==0x0b)
{
// while(keyin != 0x0d); //等待按键被放开
switch(keynum)
{
case 1:
miao--;
if(miao==-1)miao=59;
write_sfm(8,miao);
write_com(0x80+0x40+9);
break;
case 2:
fen--;
if(fen==-1)fen=59;
write_sfm(5,fen);
write_com(0x80+0x40+6);
break;
case 3:
shi--;
if(shi==-1)shi=23;
write_sfm(2,shi);
write_com(0x80+0x40+3);
break;
}
}
}
}
}
int main( void )
{ P6DIR|=BIT3;
P6OUT|=BIT3;
//上两句是打开1602的背光电源
WDTCTL = WDTPW + WDTHOLD;
lcd_init();
delay(500);
write_com(0x80+0x40+4);
write_data(':');
write_com(0x80+0x40+7);
write_data(':');
write_sfm(2,shi);
write_sfm(5,fen);
write_sfm(8,miao);
CCR0 = 32768 - 1;//设置定时器A的中断时间为1S
TACTL = TASSEL_1 + MC_1; //计数时钟ACLK, 增计数模式
CCTL0 |= CCIE; //使能CCR0比较中断
_EINT();//打开全局中断
P1DIR = 0xf0; //设置P1.0~P.3为输入状态
// P1DIR = BIT7; //设置P1.0~P.3为输入状态,P1.7为输出
// P1OUT = 0;
//以上两句是在没有独立按键情况下把P1_7拉低成为地线
while(1)
{
keyscan();
//本程序如果加低功耗模式会使按键功能失效
}
}
/*******************************************
函数名称:TimerA_ISR
功 能:定时器A的中断服务函数
********************************************/
#pragma vector=TIMERA0_VECTOR
__interrupt void TimerA_ISR(void)
{
Clock();
}
//到此时钟加按键完成
上一篇:基于IIC总线的AD转换模块PCF8591
下一篇:MSP430与DS18B20数码管显示(中断法)
推荐阅读最新更新时间:2024-03-16 13:59