Hours
要求
伪代码
- 提取时间地址
时间存放在(基址+2)的16位寄存器中,定义一个时间宏存放地址。
#define Time_Addr 0xFFFFC0000
#define TIME *(volatile int *) (Time_Addr+2)
- 根据结构图,Hours占5位,其地址与时间地址的偏移量为11,因此提取数值的时候将原数值右移11位。
time>>11
- 右移后将数值与0x1F(00011111),将数值的低五位提取出来,也就是Hours
(time>>11)&0x1F
- 将原Hours置0
newtime = oldtime & ~(0x1F<<11);
- 将Hours左移11位,其他位为0,这样将左移后的Hours或上一步的时间后,就将Hours设置到新的时间中
newtime =newtime | ((hours&0x1F)<<11);
代码实现
#define Time_Addr 0xFFFFC0000
#define TIME *(volatile int *) (Time_Addr+2)
int gethours()
{
int time = TIME;
return (time>>11)&0x1F;
}
void sethours(int hours)
{
int oldtime = TIME;
int newtime = oldtime & ~(0x1F<<11);
newtime =newtime | ((hours&0x1F)<<11);
TIME=newtime;
}
minutes
#define Time_Addr 0xFFFFC0000
#define TIME *(volatile int *) (Time_Addr+2)
int getminutes()
{
int time = TIME;
return (time>>5)&0x3F;
}
void setminutes(int minutes)
{
int oldtime = TIME;
int newtime = oldtime & ~(0x3F<<5);
newtime =newtime | ((minutes&0x3F)<<5);
TIME=newtime;
}
seconds
#define Time_Addr 0xFFFFC0000
#define TIME *(volatile int *) (Time_Addr+2)
int getseconds()
{
int time = TIME;
return time&0x1F;
}
void setseconds(int hours)
{
int oldtime = TIME;
int newtime = oldtime & ~0x1F;
newtime =newtime | (seconds&0x1F);
TIME=newtime;
}
总结