20175327 嵌入式基础

20175327 嵌入式基础

要求:
在作业本上完成附图作业,要认真看题目要求。
提交作业截图
作弊本学期成绩清零(有雷同的,不管是给别人传答案,还是找别人要答案都清零)

每个寄存器都有自己固有的地址,我们需要通过C语言访问这些地址。

#define Time_Addr 0xFFFFC0000;
#define Time        *(volatile  int *)(Time_Addr+2)

该寄存器是16位的,首先(volatile int)是一个指针,它存储的地址就是后面的(Time_Addr+2),然后取这个地址的值,接下来我们就能直接赋值给Time来改变地址上(Time_Addr+2)存储的值了。
在seconds中不需要移位,但我们看到“seconds➗2”的标志,这是因为hour=24,minute=60,seconds=60,各需要5位,6位,6位;但是寄存器只有16位,需要seconds需要➗2

提取小时

因为hours所占的位置为11—15,所以需要先右移11位,又因为hours占5位,所以需要与上0x1F,在十六位中把除了后五位都变成0,00000000000011111=0x1F

#define Time_Addr 0xFFFFC0000 //实时钟芯片的IO映像基址是OxFFFFC0000
#define TIME *(volatile int *)(Time_Addr+2) //时间存放在基址+2的寄存器中
int getHours() 
{
int time=TIME;
return (time>>11)&0x1F;
}

同理:

提取分钟:

int getMimutes
{
int time = TIME;
return (time>>5)&0x3F;
}

提取秒

int getSeconds();
{
int time = TIME;
return time&0x1F;
}

设置小时,分钟,秒

void SetHours(int hours) //插入Hours
{
    int oldtime=TIME;
    int newtime;
    newtime=oldtime&~(0x1F<<11);//将小时清零,保留分钟与秒钟
    newtime|=(hours&0x1F)<<11;//设置小时时间
    TIME=newtime;
 }
void SetMinutes(int minuets) //插入Minutes
{
    int oldtime=TIME;
    int newtime=oldtime&~(0x3F<<5);
    newtime|=(new mins&0x3F)<<5;
    TIME=newtime;
 }
void SetSeconds(int seconds)
{
    int oldtime=TIME;
    int newtime = oldtime & ~0x1F;
    newtime|=seconds & 0x1F;
    TIME=newtime;
 }
原文地址:https://www.cnblogs.com/hollfull/p/12027957.html