如何取出整型的每一位
我们可以定义四个uint8_t的变量,然后将int类型变量每8位每8位的取出
1 int main() 2 { 3 OLED_Init(); 4 int num=261; 5 uint8_t num1; 6 uint8_t num2; 7 uint8_t num3; 8 uint8_t num4; 9 num1=(0xff000000&num)>>24; 10 num2=(0x00ff0000&num)>>16; 11 num3=(0x0000ff00&num)>>8; 12 num4=(0x000000ff&num); 13 14 OLED_Printf(0,0,OLED_6X8,"num %d",num); 15 OLED_Printf(0,8,OLED_6X8,"num1 %d",num1); 16 OLED_Printf(0,16,OLED_6X8,"num2 %d",num2); 17 OLED_Printf(0,24,OLED_6X8,"num3 %d",num3); 18 OLED_Printf(0,32,OLED_6X8,"num4 %d",num4); 19 OLED_Update(); 20 return 0; 21 }
如何取出浮点型每一位
浮点型在内存中按照IEEE754存储
比如19.625为01000001100111010000000000000000
浮点型不可以参与位运算,因此我们还要对它进行特殊的处理,方法是使用联合体让一个uint32_t的变量和它共用一块内存,这样这个uint32_t的变量在内存中的二进制形式和浮点型的一模一样,然后我们可以定义四个uint8_t的变量,然后将uint32_t类型变量每8位每8位的取出
1 union test 2 { 3 float value; 4 uint32_t num; 5 }Test; 6 int main() 7 { 8 Test.value=19.625; 9 OLED_Init(); 10 uint8_t num1; 11 uint8_t num2; 12 uint8_t num3; 13 uint8_t num4; 14 num1=(0xff000000&(Test.num))>>24; 15 num2=(0x00ff0000&(Test.num))>>16; 16 num3=(0x0000ff00&(Test.num))>>8; 17 num4=(0x000000ff&(Test.num)); 18 OLED_Printf(0,0,OLED_6X8,"num %f",Test.value); 19 OLED_Printf(0,8,OLED_6X8, "num1 %d",num1); 20 OLED_Printf(0,16,OLED_6X8,"num2 %d",num2); 21 OLED_Printf(0,24,OLED_6X8,"num3 %d",num3); 22 OLED_Printf(0,32,OLED_6X8,"num4 %d",num4); 23 OLED_Update(); 24 return 0; 25 }
项目问题1:
以EEPROM为例,它只能一个字节的一个字节的写入数据,那么我们如何将浮点型和整型写入EEPROM里面呢
标签:num4,Printf,数据类型,uint8,OLED,num,6X8,一些,相关 From: https://www.cnblogs.com/Sandals-little/p/17899025.html