|
stm32编程过程经常定义变量类型,经常担心数据运算过程中 超过变量类型范围。因为在编程过程中,不同的CPU,其数据类型的意义各不相同,所以一定要注意相应变量数据类型的定义和转换,否则在计算中可能会出现不确定的错误。所以下面列出常见数据类型: }; l: J3 b. H* T2 S
一、C语言数据类型stm32使用的数据类型定义在 stm32f30x.h中 - This file contains all the peripheral registers definitions, bits ' }6 n0 c. M- S3 Y) ^
- * definitions and memory mapping for STM32F30x devices.
复制代码
4 d0 r6 D d* g6 i# q
整型定义: - #include "core_cm4.h" /* Cortex-M4 processor and core peripherals */* ~' ?) w/ d: L
- - b$ g# k3 N. }: N4 k5 I C8 N
- #include "system_stm32f4xx.h"
: `1 o! \ U% w$ ]2 h( _ -
' @0 N% R/ a: B8 `$ J - #include <stdint.h>1 ]4 s3 g: Z5 H/ I" v
- " G7 h5 a; g% L: o
-
9 ]$ x$ j9 r/ s. r S+ ~& }/ q( S. [ - /** @addtogroup Exported_types
5 t; N, k" a' A) J# ?5 w/ x6 w - % q- w) c9 @+ `- v# ^, [1 R1 z4 V7 N
- * @{
4 \' o" H$ b n- m" x5 l -
7 T, j" \3 A: m, i& ^* C - */6 A) E+ U9 @1 X2 H, `
- ' ? {) J8 Z: m( J
- /*!< STM32F10x Standard Peripheral Library old types (maintained for legacy purpose) *// f5 z: A5 X A) j
-
/ m! t0 H) p7 X, V( H5 g! X) s7 G - typedef int32_t s32;
9 B" {. K+ Y2 U7 z% G -
- P! k( C, E, G! O) T! H& N - typedef int16_t s16;* Z" r3 B' Q8 Q2 W% Z
- # n( m6 O2 H8 `; V9 {% ?1 Y2 Z
- typedef int8_t s8;; V( z4 q" |8 C4 d( n3 A
-
- d' D7 K2 F" G" X: P -
/ j0 D& J2 }+ m - typedef const int32_t sc32; /*!< Read Only */
# f& U7 e+ Q9 I6 Y. v - & b! l0 h' I3 ]( |+ K1 {( P
- typedef const int16_t sc16; /*!< Read Only */( u( X/ [- Q) J0 q
-
! R' H- V1 `/ ]9 C# K. w - typedef const int8_t sc8; /*!< Read Only */
7 q# |% @7 _+ S' D G% M2 _ -
0 X* x( z3 {! s o" r, Y - 0 D: B% j u R s" o4 M2 z
- typedef __IO int32_t vs32;+ G# G3 a/ s- C3 t5 f9 o7 O
- % _; h, J* T2 z, ~' z! W1 ]" T \
- typedef __IO int16_t vs16;/ _3 k3 r) }/ ?; m
- $ g6 L* P, K9 \
- typedef __IO int8_t vs8; D0 F0 I# [7 \+ Y7 y
-
, W0 o. N/ `7 f# [* ?0 c - , T+ M( r, j0 Y9 B
- typedef __I int32_t vsc32; /*!< Read Only */
& ]* Q2 Q b, U. Q4 d8 K; I - H3 U8 W5 D0 D0 F, [4 n
- typedef __I int16_t vsc16; /*!< Read Only */
% x9 e0 P2 c. b/ B& E& k7 A -
- ^8 `$ _) Y( O7 s; c' j7 T - typedef __I int8_t vsc8; /*!< Read Only */
1 U/ e, ]6 u v# _. \$ k/ { - 3 j& v7 f4 ^+ \4 M. Z4 m' J
- ; k& E, Y* p& h8 X) ]" q% r
- <span style="color:#ff6666;">typedef uint32_t u32; /*常用类型*/
; G8 `- K# Z' C( ^ -
, F% Z" u; v/ E - typedef uint16_t u16;
3 R B& B( k( k, H4 c - % y# @3 f, {% T" ~
- typedef uint8_t u8;</span>1 N2 S" W% k Z* m/ ]
- 1 q1 ? i/ @9 F$ _
- 6 `4 q( e" [4 W4 W$ p3 J$ E
- typedef const uint32_t uc32; /*!< Read Only */; v3 K$ N+ y" i5 ?- d# Y. I" {4 S
- ' s7 ^3 K' ?2 a" P8 h! K7 H
- typedef const uint16_t uc16; /*!< Read Only */: [+ t- b# P: U' A" t9 ~
- , P' Q9 ?) c4 |) [, Y8 ]5 B+ L
- typedef const uint8_t uc8; /*!< Read Only */0 g% k4 j) F g" f; U, A. A9 }
-
( ~. V$ C3 {5 C0 m$ ^6 [ -
5 m9 D4 ]: \- W - typedef __IO uint32_t vu32;) |( Y0 S. F" t' E* i* N; s
- 0 V8 O( m8 }/ |) \8 Y$ G7 f% U
- typedef __IO uint16_t vu16;
6 N( ^( P" ^+ j -
+ H/ a5 R7 p& N6 K$ I - typedef __IO uint8_t vu8;$ C5 L& X, A' E+ w y+ }& P
- $ Q% I! M! d$ a- b# W2 m
- $ Q5 `( G% y3 W. Z
- typedef __I uint32_t vuc32; /*!< Read Only */. X5 Y. e/ v {5 K
-
O. Y4 E; Q7 S9 D* O8 k - typedef __I uint16_t vuc16; /*!< Read Only */
" V6 z3 x$ b7 ?1 n j* h8 e - 0 n8 f2 v4 s. p2 q2 u
- typedef __I uint8_t vuc8; /*!< Read Only *
复制代码 . }2 T. E5 Q: |# [
1 Y8 t6 k4 F* s" _
浮点型: - #if !defined(__STRICT_ANSI__) || defined(__USE_C99_MATH)
* h7 }. _0 F2 g! t U6 l - & E% }7 B& k M6 l6 x
- /* C99 additions */! g# k1 w+ W: E; e' D
-
. I9 s' Y5 h$ v2 O) C, q( x - typedef float float_t;4 P- i8 C; r' }- \9 j5 m$ V
- & A. V6 p; L) e9 N" s. H
- typedef double double_t;
复制代码5 [& o5 A0 B. }& M" M0 g$ d8 g2 l0 m
注:还有float 浮点型 编译器中不能看到其定义(估计已编译了)。 而uint32_t 、uint16_t、uint8_t在哪里定义?在stdint.h文件中,详见下面: - /* exact-width signed integer types */
, u! z9 `& \/ w1 I; Q -
5 |. m7 Z C% s7 p; J8 } - typedef signed char int8_t;
% K! F$ h, |6 c- S -
4 I) z3 {# L( W& A# ]* q - typedef signed short int int16_t;
% l2 [) U1 ~& |- \ - ) e2 w. Q9 a0 F% N1 b& q
- typedef signed int int32_t;/ F' U: U; M. b% g1 k
- 6 J" ^) M5 K' \" U# R
- typedef signed __int64 int64_t;: Q% E0 T5 M% ~8 ^& [
- ! D `; ]% Q5 R8 }8 l- {+ {4 T
- T; v1 c* d; Q4 n& x$ }
- /* exact-width unsigned integer types */* K* T0 b8 \* y) K; W: b( c
-
# d" H: o! \5 q& @- p - typedef unsigned char uint8_t;* O0 `$ p$ t1 J: q% C" x8 J2 z% P @
- 2 G: R: Q: w5 `: i+ s
- typedef unsigned short int uint16_t;
- f' o# W5 \3 ? -
0 U. D# z+ u- O/ l) S0 l. M: f) A. M - typedef unsigned int uint32_t;2 q3 U! |- |' t9 E/ H
- 9 _( f b# G7 W' }7 W: c$ \4 E
- typedef unsigned __int64 uint64_t;
* ^6 @$ k2 J6 z/ {+ T6 ]2 c -
- R0 L! s+ c( c, m - , G* u' p0 V2 ]) D4 u7 D
- /* minimum values of exact-width signed integer types */
0 C9 |# [3 u8 D8 \9 j$ g; C" q - 5 y0 w! N5 n% w% u
- #define INT8_MIN -128 /* s8 占用1个byte,数据范围 -2^7 到 (2^7-1) */5 B6 A F9 R5 I: n$ L& U# {) U% S" H
-
7 D6 e. ?2 g; ]. L% ^ - #define INT16_MIN -32768 /* s16 占用2个byte,数据范围 -2^15 到 (2^15-1) */8 a% h: a; j# L1 x: ]
- ! {3 L- a! Q7 C/ n! l# E7 ~7 v8 t
- #define INT32_MIN (~0x7fffffff) /* -2147483648 is unsigned s32 占用 4个byte,数据范围 -2^31 到 (2^31-1) */
+ Y- f* }; O$ P& B% w - 5 H' C* H$ M A( A7 p0 d
- #define INT64_MIN __ESCAPE__(~0x7fffffffffffffffll) /* -9223372036854775808 is unsigned int64_t占用8个byte,数据范围 -2^63 到 (2^63-1) */
7 \3 g* x7 t8 f9 L -
2 ]$ L) T5 X6 B0 Q* \9 x - * V4 u; `5 @5 Z
- /* maximum values of exact-width signed integer types */7 |9 z1 T* ^$ O+ S! I' R
-
& B* j! K; Y7 H: p8 c" d3 E - #define INT8_MAX 1271 @5 K) T- J4 Q& ~' F, a! r9 ]
-
+ H: g+ F. b7 m - #define INT16_MAX 32767
0 p5 {" j( ~6 T, m& J - * F1 f+ z$ J4 N- N# R
- #define INT32_MAX 2147483647
: ]9 s0 K3 V! s0 ~5 o+ v - ) v! A" J8 Y/ i
- #define INT64_MAX __ESCAPE__(9223372036854775807ll)/ d' a( a" [6 z. y. h3 u* ]; F @) m7 Q
- F9 O- n2 E+ b
-
/ P0 t4 z6 n$ s$ @3 S6 v" v - /* maximum values of exact-width unsigned integer types */2 Y ~! b% L. M C1 T
- 4 ?( j. t: I& i) F: [2 r* y* s
- #define UINT8_MAX 255 /* u8 占用1个byte, 数据范围 0 - 2^8*/6 @! N1 G! q/ D& j
- , @* y5 N& y& A1 U1 K1 H
- #define UINT16_MAX 65535 /* u16 占用2个byte, 数据范围 0 - 2^16*/4 _7 S. @ ~) Y5 s$ B
- 8 i3 l8 o: W- g
- #define UINT32_MAX 4294967295u /* u32 占用4个byte, 数据范围 0 - 2^32*/* A5 w3 a( a3 O
- * J3 p8 I/ c/ i8 p+ j5 _
- #define UINT64_MAX __ESCAPE__(18446744073709551615ull)
复制代码
# O# ]0 e4 {/ o3 Y- m4 N1 U/ ]; G& O8 A* P
由上述可知: 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+ S6 `, I' X5 F! y, S8 i
* g M: m% }: y$ \, m# M
0 n! ?+ S2 }/ T8 ?0 P* Q7 |
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. }- ^# z, F) i) F9 @
4 z& [; d/ N! H7 j2 c
" j% E8 c' i+ j! u" Q3、浮点型 - float ——4个byte,有符号型,可以表达负数/小数; Float 类型至少要能精确表示到小数点后6位。
- double——8个byte,有符号型,可以表达负数/小数;Double 类型至少要能精确到小数点后 10 位。 V; [2 _2 r9 e. r# K% X
' W5 Y" e6 t/ H+ A0 S, ^1 G
" W% `* S$ W5 e, Y转载自:路途…
$ Z8 _/ }7 j) X. Y5 J$ ~5 ]! l如有侵权请联系删除
! v& y8 s6 G- |0 Y7 v. |
4 U& ^! U4 z* g' m, x
# {4 _8 Q; I4 }7 }' w
. L n8 @. H6 d' p: e |