初级会员

- 积分
- 60
- 金钱
- 60
- 注册时间
- 2016-6-12
- 在线时间
- 15 小时
|
5金钱
这段代码是从例程里面复制到我自己新建的工程里面的,在求年份的时候发生错误。设置的时间是2016.9.25,结果求出来的年份却是224,其他时间都是对的,其他函数都没有错误。
如果用printf()函数一个一个打印的话,在while(temp>=365)这个循环中,temp1也等于2016,
但是循环结束,用printf("calendar.w_year=%d \r\n",calendar.w_year);打印的话,得到的却是calendar.w_year=224,
找了半天都没找出错误,请问有没有知道的是哪里出现错误了?求指教啊。。。
//得到当前的时间
//返回值:0,成功;其他:错误代码.
u8 RTC_Get(void)
{
static u16 daycnt=0;
u32 timecount=0;
u32 temp=0;
u16 temp1=0;
timecount=RTC_GetCounter();
printf("timecount=%d \r\n",timecount);
temp=timecount/86400; //得到天数(秒钟数对应的)
printf("day=%d \r\n",temp);
if(daycnt!=temp)//超过一天了
{
daycnt=temp;
temp1=1970; //从1970年开始
while(temp>=365) //while循环里面没有错误
{
if(Is_Leap_year(temp1))//是闰年
{
if(temp>=366)temp-=366;//闰年的秒钟数
else {temp1++;break;}
}
else temp-=365; //平年
temp1++;
printf("temp1=%d \r\n",temp1); //打印出来的temp1都是从1970一直加到2016的
}
calendar.w_year=temp1;//得到年份
printf("calendar.w_year=%d \r\n",calendar.w_year); //打印得出calendar.w_year=224,而不是2016
temp1=0;
while(temp>=28)//超过了一个月
{
if(Is_Leap_year(calendar.w_year)&&temp1==1)//当年是不是闰年/2月份
{
if(temp>=29)temp-=29;//闰年的秒钟数
else break;
}
else
{
if(temp>=mon_table[temp1])temp-=mon_table[temp1];//平年
else break;
}
temp1++;
}
calendar.w_month=temp1+1; //得到月份
printf("calendar.w_month=%d \r\n",calendar.w_month);
calendar.w_date=temp+1; //得到日期
printf("calendar.w_date=%d \r\n",calendar.w_date);
}
temp=timecount%86400; //得到秒钟数
printf("temp=%d \r\n",temp);
calendar.hour=temp/3600; //小时
printf("calendar.hour=%d \r\n",calendar.hour);
calendar.min=(temp%3600)/60; //分钟
printf("calendar.min=%d \r\n",calendar.min);
calendar.sec=(temp%3600)%60; //秒钟
printf("calendar.sec=%d \r\n",calendar.sec);
calendar.week=RTC_Get_Week(calendar.w_year,calendar.w_month,calendar.w_date);//获取星期
return 0;
}
|
最佳答案
查看完整内容[请看2#楼]
2016转成十六进制0x07E0
去掉高位转成一个字节0xE0刚好是你说的224.
所以你定义的年不是int型的。
溢出了。
|