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

stm32数据类型

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

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


; k. A& \0 x7 f1 y5 H- [! a! G+ h

一、C语言数据类型

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

  1.   This file contains all the peripheral registers definitions, bits ! Y1 N# L4 O2 J) T
  2.   *          definitions and memory mapping for STM32F30x devices.
复制代码


: i  R/ n8 c( S- W2 h% w9 }' n

整型定义:

  1. #include "core_cm4.h" /* Cortex-M4 processor and core peripherals */
    / H8 m- [' |2 R

  2. # {  A; O5 z! |( g; \
  3. #include "system_stm32f4xx.h"
    % P! t# C8 P0 V% D, G- S" J
  4. 4 _& i* D' C5 H7 D2 }; K6 I
  5. #include <stdint.h>5 J2 B9 l& \0 Q+ M6 j
  6. 0 O: x( A& A& P, d6 q2 m" e2 K5 V" G

  7. 7 h% ]  I! y* t
  8. /** @addtogroup Exported_types
      J7 \3 f  X( I1 J6 B7 }+ `
  9. ( V1 m9 f# U: j, `3 a
  10. * @{3 K2 _0 r9 N8 ?; J( `1 U1 u
  11. 0 `5 g7 h  W$ P3 M8 q$ M- }
  12. */; i. @: k6 G4 W: J! X0 q* w8 j6 I: {

  13. ) q  J% R7 O+ V. j% f, O
  14. /*!< STM32F10x Standard Peripheral Library old types (maintained for legacy purpose) */
    ; Y9 u% t+ E. a2 j2 O7 D
  15. " H+ D& v0 G; K8 `
  16. typedef int32_t s32;
    8 e4 E+ {, N) L* z+ C. a' ]
  17. 7 {- \2 f& @9 M4 M9 i6 I% R5 N
  18. typedef int16_t s16;
    : Z) q6 M9 u$ ]  H- o" S
  19. 6 m5 o( u( _- K) Q- _+ u  Q5 y7 {: R
  20. typedef int8_t s8;* [" c% Z) L2 L6 U- P
  21.   g: ^9 ~% p3 ]9 s" A& |
  22. 8 g- ~3 L$ y6 q
  23. typedef const int32_t sc32; /*!< Read Only */
    : Z# G  A; s# x
  24. # ?6 a3 _7 L) v+ p3 _
  25. typedef const int16_t sc16; /*!< Read Only */) h4 y9 s1 n" |
  26. 3 {8 s0 F* w* j
  27. typedef const int8_t sc8; /*!< Read Only */( R$ q/ U5 ^9 R. m1 x

  28. " [% `) ^2 Z  L" B

  29. 7 x1 u2 d& z8 o: L9 _1 W2 N; o: y. J
  30. typedef __IO int32_t vs32;, Z2 V$ v2 V% u/ V

  31. 7 U8 h1 ~# j& V+ _
  32. typedef __IO int16_t vs16;
    ) X9 h! L& n- @# h: U0 C* m
  33. 4 l& u0 M$ T! s4 x6 J
  34. typedef __IO int8_t vs8;/ N- J0 H4 R8 W' {& a- U9 _9 c
  35. 9 R! U% \! K: P$ z" ]3 y/ o, J

  36. $ U$ g& K4 D1 o8 C0 M
  37. typedef __I int32_t vsc32; /*!< Read Only */
    * }7 ~6 H- }- l: Z- X5 S
  38. $ u% l# H! J6 w: C' {" e9 |8 ]
  39. typedef __I int16_t vsc16; /*!< Read Only */
      Z; |' l# z8 m1 _/ g, e
  40. + {' q. O% S/ Q2 ]! v7 B: u8 |+ l( f
  41. typedef __I int8_t vsc8; /*!< Read Only */
    : E1 M1 I7 o- u$ ^' b  d6 J

  42. 1 c. V1 y$ @+ h( ~* _

  43. , `" r% k3 [  @
  44. <span style="color:#ff6666;">typedef uint32_t u32; /*常用类型*/* Q/ q# q# L4 K' @
  45. 4 g6 b6 B* R+ f( c, P
  46. typedef uint16_t u16;( a+ t& t0 s9 \  |. R  x5 H

  47. : w6 Z1 W% j1 X' @1 W
  48. typedef uint8_t u8;</span>3 s2 Q/ t# |" O2 w

  49. 2 F! k% i' X1 ]1 m: N# X" m- g

  50. ( F, S  k0 Q+ u* J+ A2 @4 W
  51. typedef const uint32_t uc32; /*!< Read Only */3 u6 R6 b: w- l1 X, `

  52. ! ^: v' y8 I% A; ^  L# D: w5 P
  53. typedef const uint16_t uc16; /*!< Read Only */
    % t- z( U: ^+ n* x
  54. % ^5 s; r: j6 S7 p7 P* T. D
  55. typedef const uint8_t uc8; /*!< Read Only */
    . t2 d# D7 [9 X

  56. * m% y4 B. j  R0 I; f" k

  57. 0 B6 S3 w2 H- S7 l1 r& ~: }. x
  58. typedef __IO uint32_t vu32;
    " ?' h' u" E' e6 X9 @  N6 O$ a

  59. * u$ V: Q9 e; M: x! k/ ~, ^
  60. typedef __IO uint16_t vu16;( m& b' c$ V8 S7 b

  61. 1 {  N2 i, a( }+ M
  62. typedef __IO uint8_t vu8;1 B! s% U4 w; c; h/ y$ t

  63. " a5 ?( B' C3 ?; t
  64. / S; I$ Q, v: w
  65. typedef __I uint32_t vuc32; /*!< Read Only */
    # p* h+ ?; W0 b) y/ [/ I

  66. % |# _$ p+ P3 b
  67. typedef __I uint16_t vuc16; /*!< Read Only */5 L9 c) K; ?' U* m

  68. ! ^5 K+ o7 K2 t) [' ?2 J8 H* ?
  69. typedef __I uint8_t vuc8; /*!< Read Only *
复制代码

3 y! s# ?( n8 f- h5 t+ z/ N1 F1 L$ a

: Y9 m+ C& `* z8 b+ I- ~0 Z

浮点型:

  1. #if !defined(__STRICT_ANSI__) || defined(__USE_C99_MATH)- q: r; i! E3 C7 R. K$ J

  2. / }; v9 p# v7 {$ ~7 R. b3 \
  3. /* C99 additions */
    ' e, Y; o8 M/ k/ C) M& v: `, ~4 n' l

  4. & U- A9 W( Z. |: Q
  5. typedef float float_t;
    ( ]0 t+ Z" j" I  B6 ]8 K! g
  6. 6 p  l/ I4 S# ?0 m+ P6 @
  7. typedef double double_t;
复制代码


& i3 @) _7 p  D: P2 `

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

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

  1. /* exact-width signed integer types */
    8 b  w' \; _* K/ F+ G
  2. & H/ E9 }# z: h! V% n4 y
  3. typedef signed char int8_t;
    % k# w6 G+ h  Y- t
  4. 9 ^! ], u1 `) S* j" i) c
  5. typedef signed short int int16_t;* b* D8 _) z! y4 ]5 U/ B5 A

  6. 0 F9 r# J8 O" I+ T
  7. typedef signed int int32_t;
    $ z( H$ T+ o; N8 I% C
  8. 8 A+ K! w* E0 w. S- v8 C
  9. typedef signed __int64 int64_t;# N  r2 u  v& x/ _
  10. # ~' x. t8 H8 a9 }+ X

  11.   S/ i+ }' |! j5 g! B
  12. /* exact-width unsigned integer types */
    4 p! h4 s- Z+ V% i  q, l( ]
  13. 2 l0 ?4 _. R" \3 W. _4 Z
  14. typedef unsigned char uint8_t;5 H+ H& ]- H) `6 J2 a5 x! Z1 [

  15. $ n1 p( t! h) v. I$ L' V. W5 R7 p% @, P
  16. typedef unsigned short int uint16_t;( Y1 z7 s/ W. e

  17. ! U; S7 V  A( }% i5 I$ P" G
  18. typedef unsigned int uint32_t;' {& q* @$ r/ D7 S3 X6 N

  19. 7 z' G& e* j. i, @% k+ N
  20. typedef unsigned __int64 uint64_t;, q1 I/ h3 S. A( P$ {& [- ^' }' M

  21. 7 L9 j) |% E; q2 u
  22. ! O" W8 A5 k! u6 H0 |' v/ r
  23. /* minimum values of exact-width signed integer types */5 N8 a4 v7 g/ p2 _

  24. / y+ N, l- Y4 ]) B% [
  25. #define INT8_MIN -128 /* s8 占用1个byte,数据范围 -2^7 到 (2^7-1) */0 v) p4 W7 E7 Z: Q8 ?9 j" P
  26. : I+ k' ?' |- r
  27. #define INT16_MIN -32768 /* s16 占用2个byte,数据范围 -2^15 到 (2^15-1) */0 d; b  l9 B) t# S& P9 k
  28. + F6 M( n. |9 N
  29. #define INT32_MIN (~0x7fffffff) /* -2147483648 is unsigned s32 占用 4个byte,数据范围 -2^31 到 (2^31-1) */9 Z0 U% j7 f% Z1 N5 ?

  30. : O) K! P% ?4 k% O% i
  31. #define INT64_MIN __ESCAPE__(~0x7fffffffffffffffll) /* -9223372036854775808 is unsigned int64_t占用8个byte,数据范围 -2^63 到 (2^63-1) */
    ( N  {- {& R6 e" ?6 F9 X
  32. + `! S# S: v$ X' N5 C! S
  33. 6 D& i, {2 z6 A# Q" R
  34. /* maximum values of exact-width signed integer types */
    % s* p3 J1 F3 v4 r8 z1 H

  35. - m# u( i3 M  ?
  36. #define INT8_MAX 127
    9 m$ \) j2 d! z- T3 w3 X) U
  37. & \3 i' V; [! {: {/ F/ R' ^5 \
  38. #define INT16_MAX 327679 Q4 k7 B9 v4 ~+ y4 x0 ?( H7 _
  39. 7 y/ j" |/ u# B& Y
  40. #define INT32_MAX 2147483647
    ; S4 f4 [- e! N

  41. # M& ?! \7 g. C( t4 _  H! ?
  42. #define INT64_MAX __ESCAPE__(9223372036854775807ll)
    , E5 h- X) E) }0 C, V: P' |

  43. ' K# _1 s  d0 K: ]2 P& @" e

  44. : _  J* ~% e* ?+ h/ j% h
  45. /* maximum values of exact-width unsigned integer types */
    $ Y+ g5 C( q8 `, b. S, h
  46. - {4 w% n( [& n; P6 Q) m( O
  47. #define UINT8_MAX 255 /* u8 占用1个byte, 数据范围 0 - 2^8*/1 t: G2 {! y( C) }% l

  48. 8 E. n& ]/ t2 I8 k6 G" h* Z" ~
  49. #define UINT16_MAX 65535 /* u16 占用2个byte, 数据范围 0 - 2^16*/
    - F. b1 n; f) q% x6 k) G
  50. ! x( ?: `; x/ [0 F5 j- E* t
  51. #define UINT32_MAX 4294967295u /* u32 占用4个byte, 数据范围 0 - 2^32*/
    & I3 Z: w/ t7 t  p7 I7 s; ?

  52. 1 f3 t, g: G! @! ~, C, n
  53. #define UINT64_MAX __ESCAPE__(18446744073709551615ull)
复制代码

/ `' ~: N8 H( U4 K& s: T

9 s) u3 V) ?$ m# W; R

由上述可知:

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 = 9223372036854775807ll4 m( @+ b* U9 G2 R4 F

3 f7 ?# D2 {3 S, s

4 C/ X% O2 T" p2 y

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
    2 l1 u, i+ ]8 n

9 @& s7 [5 K$ u  p
# J/ I1 l' E/ e$ n! x' O) E

3、浮点型

  •     float ——4个byte,有符号型,可以表达负数/小数; Float 类型至少要能精确表示到小数点后6位。
  •    double——8个byte,有符号型,可以表达负数/小数;Double 类型至少要能精确到小数点后 10 位。
    2 e5 I) H0 Z" m' l
( j0 N. C5 F( P. a9 p
/ `. A9 Z1 M0 X3 H' ?: T9 n0 H, j
转载自:路途…
- }( s% w- [0 D) t3 {如有侵权请联系删除
& U) p% A! H" }7 Q) d/ z1 w% X! W) K( I* B+ [$ `
" {. n/ [# H" p$ l

2 V0 u% [8 V( y( m- Y6 q$ J
赞 收藏 评论0 发布时间:2023-5-14 15:34

举报

0个回答

所属标签

相似技术帖

官网相关资源

关于
我们是谁
投资者关系
意法半导体可持续发展举措
创新与技术
意法半导体官网
联系我们
联系ST分支机构
寻找销售人员和分销渠道
社区
媒体中心
活动与培训
隐私策略
隐私策略
Cookies管理
行使您的权利
官方最新发布
人形机器人运动控制、感知与智能配电
半导体创新技术与应用方向
EE架构与软件定义汽车
12V/48V 汽车智能配电(SPD)
区域控制单元(ZCU)与分区架构
关注我们
st-img 微信公众号
st-img 手机版