我想讀,並根據下表寫從/到unsigned char類型一個unsigned char型的特定位: 如何讀取/寫入
例如我有以下變數:
unsigned char hsi_div = 0x01; /* HSI/2 */
unsigned char cpu_div = 0x05; /* Fmaster/32 */
我想寫hsi_div
對位4,3和cpu_div要位2,1,0(想象整個焦炭名爲CLK_DIVR):
CLK_DIVR |= hsi_div << 4; //not correct!
CLK_DIVR |= cpu_div << 2; //not correct!
並讓說我想讀回寄存器,以確保我做到了正確:
if(((CLK_DIVR << 4) - 1) & hsi_div)) { /* SET OK */ }
if(((CLK_DIVR << 2) - 1) & cpu_div)) { /* SET OK */ }
我的按位操作有什麼問題!?我沒有得到正確的行爲。
你想要使用8位的uint8_t。無符號字符*至少爲8位*,但可能更多。 –
使用固定寬度類型(請參閱'stdiont.h'),而不是標準整數類型。並選擇你使用的語言。 C和C++是不同的語言! – Olaf
@ThomasMatthews對於我使用的編譯器和平臺無關緊要 –