嵌入式C语言——提取时分秒
题目:
![](https://images2017.cnblogs.com/blog/1071583/201801/1071583-20180104185844456-611107225.png)
相关知识点
提取位:
![](https://images2017.cnblogs.com/blog/1071583/201801/1071583-20180104190103378-1660300436.png)
插入位
![](https://images2017.cnblogs.com/blog/1071583/201801/1071583-20180104190153628-2070023868.png)
用宏定义定义地址
#define TIME_Addr 0xFFFFC0000
#define TIME *(volatile int *)(TIME_Addr+2)
分析
- 时分秒分别占5位、6位、5位!
- 0x3F写成二进制111111六位进行&操作(适用于分)
- 0x1F写成二进制11111五位进行&操作(适用于时、秒)
提取小时
#define Time_Addr 0xFFFFC0000 //实时钟芯片的IO映像基址是OxFFFFC0000
#define TIME *(volatile int *)(Time_Addr+2) //时间存放在基址+2的寄存器中
int getHours()
{
int time=TIME;
return (time>>11)&0x1F;
}
插入小时
#define Time_Addr 0xFFFFC0000 //实时钟芯片的IO映像基址是OxFFFFC0000
#define TIME *(volatile int *)(Time_Addr+2) //时间存放在基址+2的寄存器中
void SetHours(int hours)
{
int oldtime=TIME;
int newtime;
newtime=oldtime&~(0x1F<<11);
newtime|=(hours&0x1F)<<11;
TIME=newtime;
}
提取分钟
#define Time_Addr 0xFFFFC0000 //实时钟芯片的IO映像基址是OxFFFFC0000
#define TIME *(volatile int *)(Time_Addr+2) //时间存放在基址+2的寄存器中
int getMinutes()
{
int time=TIME;
return (time>>5)&0x3F;
}
插入分钟
#define Time_Addr 0xFFFFC0000 //实时钟芯片的IO映像基址是OxFFFFC0000
#define TIME *(volatile int *)(Time_Addr+2) //时间存放在基址+2的寄存器中
void SetMinutes(int minutes)
{
int oldtime=TIME;
int newtime;
newtime=oldtime&~(0x3F<<5);
newtime|=(minutes&0x3F)<<5;
TIME=newtime;
}
提取秒
#define Time_Addr 0xFFFFC0000 //实时钟芯片的IO映像基址是OxFFFFC0000
#define TIME *(volatile int *)(Time_Addr+2) //时间存放在基址+2的寄存器中
int getSeconds()
{
int time=TIME;
return (time&0x1F)*2; //此处不考虑Seconds除2带来的误差,因此直接乘2
}
插入秒
#define Time_Addr 0xFFFFC0000 //实时钟芯片的IO映像基址是OxFFFFC0000
#define TIME *(volatile int *)(Time_Addr+2) //时间存放在基址+2的寄存器中
void SetSeconds(int seconds)
{
int oldtime=TIME;
int newtime;
newtime=oldtime&~0x1F;
newtime|=(seconds/2)&0x1F;
TIME=newtime;
}