我以前倒是做过,不过程序还有点问题,调时间的时候容易过界,但正常走时候就正常了。开发环境用的IAR,单片机用f149,显示用的1602的四线模式。
你自己看着改程序吧,其实我也是51上移植过去的。
悲剧了,帖不下了.我帖在我博客里了啊,自己去找吧.
主程序
__________________________分隔线____________________________________
#include <msp430x14x.h>
#include "ds1302.h"
#include "LCD1602x4_mps.h"
#define DS1302_SECOND 0x81 //时钟芯片的寄存器位置,存放时间
#define DS1302_MINUTE 0x83
#define DS1302_HOUR 0x85
#define DS1302_WEEK 0x8b
#define DS1302_DAY 0x87
#define DS1302_MONTH 0x89
#define DS1302_YEAR 0x8d
unsigned char DateString[11],TimeString[9],week_value[2],TempBuffer[7]; //
char hide_sec,hide_min,hide_hour,hide_day,hide_week,hide_month,hide_year;
char done,count,temp,flag,up_flag,down_flag;
//unsigned int temp_value=0,temp_max=0;temp_min=0; //温度值
void DateToStr(void) //将时间年,月,日,星期数据转换成液晶显示字符串,放到数组里DateString[]
{ unsigned char Year,Month,Day,Week;
Year=rtc_getyear();
Month=rtc_getmon();
Day=rtc_getdate();
Week=rtc_getday();
if(hide_year<2) //这里的if,else语句都是判断位闪烁,<2显示数据,>2就不显示,输出字符串为 2007/07/22
{
DateString[0] = '2';
DateString[1] = '0';
DateString[2] = Year/10 + '0';
DateString[3] = Year%10 + '0';
}
else
{
DateString[0] = ' ';
DateString[1] = ' ';
DateString[2] = ' ';
DateString[3] = ' ';
}
DateString[4] = '/';
if(hide_month<2)
{
DateString[5] = Month/10 + '0';
DateString[6] = Month%10 + '0';
}
else
{
DateString[5] = ' ';
DateString[6] = ' ';
}
DateString[7] = '/';
if(hide_day<2)
{
DateString[8] = Day/10 + '0';
DateString[9] = Day%10 + '0';
}
else
{
DateString[8] = ' ';
DateString[9] = ' ';
}
if(hide_week<2)
{
week_value[0] = Week%10 + '0'; //星期的数据另外放到 week_value[]数组里,跟年,月,日的分开存放,因为等一下要在最后显示
}
else
{
week_value[0] = ' ';
}
week_value[1] = '\0';
DateString[10] = '\0'; //字符串末尾加 '\0' ,判断结束字符
}
void TimeToStr(void) //将时,分,秒数据转换成液晶显示字符放到数组 TimeString[];
{ unsigned char Hour,Minute,Second;
Hour=rtc_gethour();
Minute=rtc_getmin();
Second=rtc_getsec();
if(hide_hour<2)
{
TimeString[0] = Hour/10 + '0';
TimeString[1] = Hour%10 + '0';
}
else
{
TimeString[0] = ' ';
TimeString[1] = ' ';
}
TimeString[2] = ':';
if(hide_min<2)
{
TimeString[3] = Minute/10 + '0';
TimeString[4] = Minute%10 + '0';
}
else
{
TimeString[3] = ' ';
TimeString[4] = ' ';
}
TimeString[5] = ':';
if(hide_sec<2)
{
TimeString[6] = Second/10 + '0';
TimeString[7] = Second%10 + '0';
}
else
{
TimeString[6] = ' ';
TimeString[7] = ' ';
}
DateString[8] = '\0';
}
void show_time() //液晶显示程序
{
TimeToStr(); //时间数据转换液晶字符
DateToStr(); //日期数据转换液晶字符
// ReadTemp(); //开启温度采集程序
// temp_to_str(); //温度数据转换成液晶字符
LCD_PutStr(TempBuffer,25); //显示温度
LCD_PutStr(DateString,0); //显示日期
LCD_PutStr(week_value,15); //显示星期
LCD_PutStr(" Week",10); //在液晶上显示 字母 week
LCD_PutStr(TimeString,16); //显示时间
}
////////////////////////////////////////////////////////////////////////////
void outkey() //跳出调整模式,返回默认显示
{ unsigned char Second;
if (!(P1IN&BIT0))
{
count=0;
hide_sec=0,hide_min=0,hide_hour=0,hide_day=0,hide_week=0,hide_month=0,hide_year=0;
Second=dataread(DS1302_SECOND);
datawrite(0x8e,0x00); //写入允许
datawrite(0x80,Second&0x7f);
datawrite(0x8E,0x80); //禁止写入
done=0;//temp_max=0;sund=1;
while(!(P1IN&BIT0));
delay_nms(2);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Upkey()//升序按键
{
if(!(P1IN&BIT1))
{
switch(count)
{case 1:
temp=dataread(DS1302_SECOND); //读取秒数
temp=temp+1; //秒数加1
up_flag=1; //数据调整后更新标志
if((temp&0x7f)>0x59) //超过59秒,清零
temp=0;
break;
case 2:
temp=dataread(DS1302_MINUTE); //读取分数
temp=temp+1; //分数加1
up_flag=1;
if(temp>0x59) //超过59分,清零
temp=0;
break;
case 3:
temp=dataread(DS1302_HOUR); //读取小时数
temp=temp+1; //小时数加1
up_flag=1;
if(temp>0x23) //超过23小时,清零
temp=0;
break;
case 4:
temp=dataread(DS1302_WEEK); //读取星期数
temp=temp+1; //星期数加1
up_flag=1;
if(temp>0x7)
temp=1;
break;
case 5:
temp=dataread(DS1302_DAY); //读取日数
temp=temp+1; //日数加1
up_flag=1;
if(temp>0x31)
temp=1;
break;
case 6:
temp=dataread(DS1302_MONTH); //读取月数
temp=temp+1; //月数加1
up_flag=1;
if(temp>0x12)
temp=1;
break;
case 7:
temp=dataread(DS1302_YEAR); //读取年数
temp=temp+1; //年数加1
up_flag=1;
if(temp>0x99)
temp=0;
break;
default:break;
}
while(!(P1IN&BIT1));
delay_nms(2);
}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////
void Downkey()//降序按键
{
if(!(P1IN&BIT2))
{
switch(count)
{case 1:
temp=dataread(DS1302_SECOND); //读取秒数
temp=temp-1; //秒数减1
down_flag=1; //数据调整后更新标志
if((temp&0x7f)>0x59) //小于0秒,返回59秒
temp=0x59;
break;
case 2:
temp=dataread(DS1302_MINUTE); //读取分数
temp=temp-1; //分数减1
down_flag=1;
if(temp>0x59)
temp=0x59; //小于0秒,返回59秒
break;
case 3:
temp=dataread(DS1302_HOUR); //读取小时数
temp=temp-1; //小时数减1
down_flag=1;
if(temp==0x00)
temp=0x23;
break;
case 4:
temp=dataread(DS1302_WEEK); //读取星期数
temp=temp-1; //星期数减1
down_flag=1;
if(temp==0x00)
temp=0x07;
break;
case 5:
temp=dataread(DS1302_DAY); //读取日数
temp=temp-1; //日数减1
down_flag=1;
if(temp==0x00)
temp=0x31;
break;
case 6:
temp=dataread(DS1302_MONTH); //读取月数
temp=temp-1; //月数减1
down_flag=1;