Hours

要求

2017-2018-1 20155315 《信息安全系统设计基础》嵌入式C语言测试-LMLPHP

伪代码

  • 提取Hours
  1. 提取时间地址

    时间存放在(基址+2)的16位寄存器中,定义一个时间宏存放地址。
#define Time_Addr 0xFFFFC0000
#define TIME *(volatile int *) (Time_Addr+2)
  1. 根据结构图,Hours占5位,其地址与时间地址的偏移量为11,因此提取数值的时候将原数值右移11位。
time>>11
  1. 右移后将数值与0x1F(00011111),将数值的低五位提取出来,也就是Hours
(time>>11)&0x1F
  • 设置Hours
  1. 将原Hours置0
newtime = oldtime & ~(0x1F<<11);
  1. 将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;
}

总结

05-11 15:39