C语言中
*(volatile unsigned int *)0x500
的解释:
以下;学习
(unsigned int *)0x500
:将地址0x500
强制转化为int型指针测试
*(unsigned int *)0x500=0x10
:对地址为0x500
赋值为0x10
.net
修改测试代码以下:指针
#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以前的位清零; }
测试:code
这里直接给出代码由于seconds与上面同理只是位置不一样blog
#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); }