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); }