2017-2018-1 20155315 embedded C language test of the basic design of information security system

Source: Internet
Author: User
Tags volatile

Hours requirements

Pseudo code
    • Extract hours
    1. 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)
    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
    3. 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
    • Set hours
    1. Place the original hours 0

      newtime = oldtime & ~(0x1F<<11);
    2. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.