你的浏览器版本过低,可能导致网站不能正常访问!
为了你能正常使用网站功能,请使用这些浏览器。

stm32数据类型

[复制链接]
攻城狮Melo 发布时间:2023-5-14 15:34

stm32编程过程经常定义变量类型,经常担心数据运算过程中 超过变量类型范围。因为在编程过程中,不同的CPU,其数据类型的意义各不相同,所以一定要注意相应变量数据类型的定义和转换,否则在计算中可能会出现不确定的错误。所以下面列出常见数据类型:


一、C语言数据类型

stm32使用的数据类型定义在 stm32f30x.h中

  1.   This file contains all the peripheral registers definitions, bits
  2.   *          definitions and memory mapping for STM32F30x devices.
复制代码


整型定义:

  1. #include "core_cm4.h" /* Cortex-M4 processor and core peripherals */

  2. #include "system_stm32f4xx.h"

  3. #include <stdint.h>


  4. /** @addtogroup Exported_types

  5. * @{

  6. */

  7. /*!< STM32F10x Standard Peripheral Library old types (maintained for legacy purpose) */

  8. typedef int32_t s32;

  9. typedef int16_t s16;

  10. typedef int8_t s8;


  11. typedef const int32_t sc32; /*!< Read Only */

  12. typedef const int16_t sc16; /*!< Read Only */

  13. typedef const int8_t sc8; /*!< Read Only */


  14. typedef __IO int32_t vs32;

  15. typedef __IO int16_t vs16;

  16. typedef __IO int8_t vs8;


  17. typedef __I int32_t vsc32; /*!< Read Only */

  18. typedef __I int16_t vsc16; /*!< Read Only */

  19. typedef __I int8_t vsc8; /*!< Read Only */


  20. <span style="color:#ff6666;">typedef uint32_t u32; /*常用类型*/

  21. typedef uint16_t u16;

  22. typedef uint8_t u8;</span>


  23. typedef const uint32_t uc32; /*!< Read Only */

  24. typedef const uint16_t uc16; /*!< Read Only */

  25. typedef const uint8_t uc8; /*!< Read Only */


  26. typedef __IO uint32_t vu32;

  27. typedef __IO uint16_t vu16;

  28. typedef __IO uint8_t vu8;


  29. typedef __I uint32_t vuc32; /*!< Read Only */

  30. typedef __I uint16_t vuc16; /*!< Read Only */

  31. typedef __I uint8_t vuc8; /*!< Read Only *
复制代码


浮点型:

  1. #if !defined(__STRICT_ANSI__) || defined(__USE_C99_MATH)

  2. /* C99 additions */

  3. typedef float float_t;

  4. typedef double double_t;
复制代码


注:还有float 浮点型 编译器中不能看到其定义(估计已编译了)。

而uint32_t 、uint16_t、uint8_t在哪里定义?在stdint.h文件中,详见下面:

  1. /* exact-width signed integer types */

  2. typedef signed char int8_t;

  3. typedef signed short int int16_t;

  4. typedef signed int int32_t;

  5. typedef signed __int64 int64_t;


  6. /* exact-width unsigned integer types */

  7. typedef unsigned char uint8_t;

  8. typedef unsigned short int uint16_t;

  9. typedef unsigned int uint32_t;

  10. typedef unsigned __int64 uint64_t;


  11. /* minimum values of exact-width signed integer types */

  12. #define INT8_MIN -128 /* s8 占用1个byte,数据范围 -2^7 到 (2^7-1) */

  13. #define INT16_MIN -32768 /* s16 占用2个byte,数据范围 -2^15 到 (2^15-1) */

  14. #define INT32_MIN (~0x7fffffff) /* -2147483648 is unsigned s32 占用 4个byte,数据范围 -2^31 到 (2^31-1) */

  15. #define INT64_MIN __ESCAPE__(~0x7fffffffffffffffll) /* -9223372036854775808 is unsigned int64_t占用8个byte,数据范围 -2^63 到 (2^63-1) */


  16. /* maximum values of exact-width signed integer types */

  17. #define INT8_MAX 127

  18. #define INT16_MAX 32767

  19. #define INT32_MAX 2147483647

  20. #define INT64_MAX __ESCAPE__(9223372036854775807ll)


  21. /* maximum values of exact-width unsigned integer types */

  22. #define UINT8_MAX 255 /* u8 占用1个byte, 数据范围 0 - 2^8*/

  23. #define UINT16_MAX 65535 /* u16 占用2个byte, 数据范围 0 - 2^16*/

  24. #define UINT32_MAX 4294967295u /* u32 占用4个byte, 数据范围 0 - 2^32*/

  25. #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 位。


转载自:路途…
如有侵权请联系删除



收藏 评论0 发布时间:2023-5-14 15:34

举报

0个回答

所属标签

相似分享

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
STM32N6 AI生态系统
STM32MCU,MPU高性能GUI
ST ACEPACK电源模块
意法半导体生物传感器
STM32Cube扩展软件包
关注我们
st-img 微信公众号
st-img 手机版