stm32编程过程经常定义变量类型,经常担心数据运算过程中 超过变量类型范围。因为在编程过程中,不同的CPU,其数据类型的意义各不相同,所以一定要注意相应变量数据类型的定义和转换,否则在计算中可能会出现不确定的错误。所以下面列出常见数据类型:
一、C语言数据类型stm32使用的数据类型定义在 stm32f30x.h中 - This file contains all the peripheral registers definitions, bits
- * definitions and memory mapping for STM32F30x devices.
复制代码
整型定义: - #include "core_cm4.h" /* Cortex-M4 processor and core peripherals */
-
- #include "system_stm32f4xx.h"
-
- #include <stdint.h>
-
-
- /** @addtogroup Exported_types
-
- * @{
-
- */
-
- /*!< STM32F10x Standard Peripheral Library old types (maintained for legacy purpose) */
-
- typedef int32_t s32;
-
- typedef int16_t s16;
-
- typedef int8_t s8;
-
-
- typedef const int32_t sc32; /*!< Read Only */
-
- typedef const int16_t sc16; /*!< Read Only */
-
- typedef const int8_t sc8; /*!< Read Only */
-
-
- typedef __IO int32_t vs32;
-
- typedef __IO int16_t vs16;
-
- typedef __IO int8_t vs8;
-
-
- typedef __I int32_t vsc32; /*!< Read Only */
-
- typedef __I int16_t vsc16; /*!< Read Only */
-
- typedef __I int8_t vsc8; /*!< Read Only */
-
-
- <span style="color:#ff6666;">typedef uint32_t u32; /*常用类型*/
-
- typedef uint16_t u16;
-
- typedef uint8_t u8;</span>
-
-
- typedef const uint32_t uc32; /*!< Read Only */
-
- typedef const uint16_t uc16; /*!< Read Only */
-
- typedef const uint8_t uc8; /*!< Read Only */
-
-
- typedef __IO uint32_t vu32;
-
- typedef __IO uint16_t vu16;
-
- typedef __IO uint8_t vu8;
-
-
- typedef __I uint32_t vuc32; /*!< Read Only */
-
- typedef __I uint16_t vuc16; /*!< Read Only */
-
- typedef __I uint8_t vuc8; /*!< Read Only *
复制代码
浮点型: - #if !defined(__STRICT_ANSI__) || defined(__USE_C99_MATH)
-
- /* C99 additions */
-
- typedef float float_t;
-
- typedef double double_t;
复制代码
注:还有float 浮点型 编译器中不能看到其定义(估计已编译了)。 而uint32_t 、uint16_t、uint8_t在哪里定义?在stdint.h文件中,详见下面: - /* exact-width signed integer types */
-
- typedef signed char int8_t;
-
- typedef signed short int int16_t;
-
- typedef signed int int32_t;
-
- typedef signed __int64 int64_t;
-
-
- /* exact-width unsigned integer types */
-
- typedef unsigned char uint8_t;
-
- typedef unsigned short int uint16_t;
-
- typedef unsigned int uint32_t;
-
- typedef unsigned __int64 uint64_t;
-
-
- /* minimum values of exact-width signed integer types */
-
- #define INT8_MIN -128 /* s8 占用1个byte,数据范围 -2^7 到 (2^7-1) */
-
- #define INT16_MIN -32768 /* s16 占用2个byte,数据范围 -2^15 到 (2^15-1) */
-
- #define INT32_MIN (~0x7fffffff) /* -2147483648 is unsigned s32 占用 4个byte,数据范围 -2^31 到 (2^31-1) */
-
- #define INT64_MIN __ESCAPE__(~0x7fffffffffffffffll) /* -9223372036854775808 is unsigned int64_t占用8个byte,数据范围 -2^63 到 (2^63-1) */
-
-
- /* maximum values of exact-width signed integer types */
-
- #define INT8_MAX 127
-
- #define INT16_MAX 32767
-
- #define INT32_MAX 2147483647
-
- #define INT64_MAX __ESCAPE__(9223372036854775807ll)
-
-
- /* maximum values of exact-width unsigned integer types */
-
- #define UINT8_MAX 255 /* u8 占用1个byte, 数据范围 0 - 2^8*/
-
- #define UINT16_MAX 65535 /* u16 占用2个byte, 数据范围 0 - 2^16*/
-
- #define UINT32_MAX 4294967295u /* u32 占用4个byte, 数据范围 0 - 2^32*/
-
- #define UINT64_MAX __ESCAPE__(18446744073709551615ull)
复制代码
由上述可知: 1、有符号整型 - s8 占用1个byte,数据范围 -2^7 到 (2^7-1)
- s16 占用2个byte,数据范围 -2^15 到 (2^15-1)
- s32 占用 4个byte,数据范围 -2^31 到 (2^31-1)2^31 = 2147483647
- int64_t占用8个byte,数据范围 -2^63 到 (2^63-1) 2^63 = 9223372036854775807ll
2、无符号整型 - u8 占用1个byte, 数据范围 0 - 2^8
- u16 占用2个byte, 数据范围 0 - 2^16
- u32 占用4个byte, 数据范围 0 - 2^32 2^32 = 4294967295
- uint64_t 占用8个byte, 数据范围 0 - 2^64 2^64 = 18446744073709551615
3、浮点型 - float ——4个byte,有符号型,可以表达负数/小数; Float 类型至少要能精确表示到小数点后6位。
- double——8个byte,有符号型,可以表达负数/小数;Double 类型至少要能精确到小数点后 10 位。
转载自:路途…
如有侵权请联系删除
|