Hours requirements
Pseudo code
Extract Time Address
Time is stored in the 16-bit register (base +2), defining a time macro to store the address.
#define Time_Addr 0xFFFFC0000#define TIME *(volatile int *) (Time_Addr+2)
According to the structure chart, the hours occupies 5 bits, and its address and time address are offset by 11, so when extracting the value, the original value is shifted to the right 11 bits.
time>>11
When you move right, the value is 0x1f (00011111), and the lower five bits of the value are extracted, which is hours
(time>>11)&0x1F
Place the original hours 0
newtime = oldtime & ~(0x1F<<11);
Move the hours to the left by 11 bits, the other bit to 0, so that the hours is set to a new time after the left hours or the time of the previous step
newtime =newtime | ((hours&0x1F)<<11);
Code implementation```
Define TIME_ADDR 0xffffc0000define 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 0xffffc0000define 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 0xffffc0000define 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;
}
```
Summarize
2017-2018-1 20155315 embedded C language test of the basic design of information security system