写这篇博文的目的就是为了清晰的缕一下自己的思路,把用到的记录下来以便将来方便使用。
基础类型
char:1个字节
short:2个字节
int:在51等8位单片机下为2字节,32位RAM单片机下为4字节(在考虑不同平台移植时最好用short和long)
long:4个字节
float:4个字节
double:8个字节
下面是stm32中常用到的类型:
u8 是 unsigned char
u16 是 unsigned short
u32 是 unsigned int(32位下,下面省略)
关于uint8_t/uint16_t/uint32_t/uint64_t 等数据类型
在inttypes.h下可以看到
#ifndef __INTTYPES_H_
#define __INTTYPES_H_
typedef signed char int8_t;
typedef unsigned char uint8_t;
typedef int int16_t;
typedef unsigned int uint16_t;
typedef long int32_t;
typedef unsigned long uint32_t;
typedef long long int64_t;
typedef unsigned long long uint64_t;
typedef int16_t intptr_t;
typedef uint16_t uintptr_t;
#endif
使用uint_t定义更加方便,由于使用了typedef(为C语言的关键字,作用是为一种数据类型定义一个新名字。)使得在不同的平台下更好的移植。
还有三种遇到的类型
typedef __IO int32_t vs32;
typedef __IO int16_t vs16;
typedef __IO int8_t vs8;
这里有__IO的定义
#define __IO volatile
volatile 影响编译器编译的结果,指出,volatile 变量是随时可能发生变化的,与volatile变量有关的运算,不要进行编译优化,以免出错。
它的作用使得编译器每次都会调用它本身,不会因为变量因为寄存器等变化出现错误,volatile可以保证对特殊地址的稳定访问,不会出错。