测试中错误部分的理解和学习
地址的定义部分错误
C语言中
*(volatile unsigned int *)0x500
的解释:
如下;
(unsigned int *)0x500
:将地址0x500
强制转化为int型指针
*(unsigned int *)0x500=0x10
:对地址为0x500
赋值为0x10
详见嵌入式中的 *(volatile unsigned int *)理解
修改测试代码如下:
#define DATA_Addr 0xFFFFC0000
#define DATA *(volatile int *) DATA_Addr
void SetHours(int hours)
{
unsigned short time;
time = (unsigned short)(DATA);
time &= 0x07FF;//将hous小时所在的比特位置0
DATA = ((unsigned short))(hours<<11)|time;//将要设置的hours变量通过移位变换到time变量中hours所在的比特位置,再与time以或运算合并
}
int getHours()
{
unsigned short time;
time = (unsigned short)(DATA);
return (int)((time>>11)&0x001f);//直接将time向右移动11位清除掉分钟和秒的比特位上的数据,再将hours之前的位清零;
}
测试:
对于秒部分的提取和置位的学习
这里直接给出代码因为seconds与上面同理只是位置不同
#define DATA_Addr 0xFFFFC0000
#define DATA *(volatile int *) DATA_Addr
void SetSeconds(int seconds)
{
unsigned short time;
time = (unsigned short)(DATA);
time &= 0xffe0;
DATA = ((unsigned short))(seconds)|time;
}
int getSeconds()
{
unsigned short time;
time = (unsigned short)(DATA);
return (int)(time&0x001f);
}