|
stm32编程过程经常定义变量类型,经常担心数据运算过程中 超过变量类型范围。因为在编程过程中,不同的CPU,其数据类型的意义各不相同,所以一定要注意相应变量数据类型的定义和转换,否则在计算中可能会出现不确定的错误。所以下面列出常见数据类型: , t/ @% Z9 \- i% Y
一、C语言数据类型stm32使用的数据类型定义在 stm32f30x.h中 - This file contains all the peripheral registers definitions, bits ' r/ x/ k' c: W) [/ x1 v U1 S7 D: @
- * definitions and memory mapping for STM32F30x devices.
复制代码" w& n2 o" S4 `, S3 N$ l! X4 @! F
整型定义: - #include "core_cm4.h" /* Cortex-M4 processor and core peripherals */
: Y( |& k8 H7 ~3 M' Q - $ C7 V; q( d3 o$ J3 M
- #include "system_stm32f4xx.h"
- S$ Q3 N( R+ e' f5 N - : F+ w; `8 q# ]6 P
- #include <stdint.h>
c, _/ q4 p0 Z, ?# X - & T/ {9 v0 u. o( E! h- `
- n9 K0 b' M' G6 q5 G2 V# i2 B* p
- /** @addtogroup Exported_types
4 L$ {0 [6 i7 I( @/ E - ; ]; I( s" T& ~4 U# @* O
- * @{* g0 h. J: {& {! y V8 g
- 1 |9 `6 V# D5 K& a) ]: d! V
- */
. c, y3 _' \6 M2 V _- B0 d -
+ s% p0 N( q: I/ d# W - /*!< STM32F10x Standard Peripheral Library old types (maintained for legacy purpose) */) H& d) j3 u% E' I0 X" Z
-
) ]( o' G$ X3 \' X# ] - typedef int32_t s32;
5 F- @/ m5 o' Q" Q# @+ d4 q+ } - 0 [: A9 m. u S' m$ w
- typedef int16_t s16;3 u# y1 j6 @9 l2 |9 d
- ( ]! s# f2 u3 f2 J7 k8 l
- typedef int8_t s8;" i) F; ?, h5 g- Y0 u9 o
-
# w" L5 u0 R9 N" G( o- [3 L - & G( e1 g( o/ q0 C
- typedef const int32_t sc32; /*!< Read Only */2 `" }% T7 B4 g4 {
-
- {6 E6 r+ H9 V; B4 | - typedef const int16_t sc16; /*!< Read Only */ k. C+ y5 T: a4 I( h! Z$ z7 P
-
! Q1 Y# I- H; e B" z A1 ^ - typedef const int8_t sc8; /*!< Read Only *// c% A6 u3 G6 y$ c1 V
- 5 K% X+ m, d( ]" Z7 Y) ]) _1 N
- 9 n |1 Z y9 J! A0 N
- typedef __IO int32_t vs32;
+ x: k* H- V/ Z6 W -
! s& \; r7 W. V% ?) g( ~9 m - typedef __IO int16_t vs16;
2 R" F5 m. P# \0 }0 ~8 s3 w: ?: ] - 9 J T$ t6 B3 l! R2 S
- typedef __IO int8_t vs8;
# H& U# Q* u- g! n -
7 V0 f1 E9 [; Y3 e( a -
' ?" S' o8 M) j: M - typedef __I int32_t vsc32; /*!< Read Only */
$ h3 q( v; C: B, }/ I6 g. {3 ~ -
; [- `" u, u$ W- Q/ Q - typedef __I int16_t vsc16; /*!< Read Only */
* l* m; V7 c, s, ^ - 3 v9 M( ^7 [8 O3 f5 P2 X
- typedef __I int8_t vsc8; /*!< Read Only */- P J- E( D) Y6 y6 B; D
- / T( l9 {& m, J' L
- / {( t ^: q$ z5 F+ Y- D; \8 A8 |7 S
- <span style="color:#ff6666;">typedef uint32_t u32; /*常用类型*/) \1 d, l" G+ n, [. g6 M# R2 X" y
-
L6 g. c2 m* L- [ - typedef uint16_t u16;
; L" g( [5 X& G8 J -
& L4 Y' w' l. \/ ]! ]2 Y - typedef uint8_t u8;</span>
8 w$ ^1 B5 l6 V6 N8 q& x P/ q - ! e. z( O: G* E9 P( M+ i
- - N3 c( A2 Y/ o {0 I2 l3 ~
- typedef const uint32_t uc32; /*!< Read Only */# H; |# X5 k; x1 u
-
9 e/ T _0 R; ^/ W7 W. ? - typedef const uint16_t uc16; /*!< Read Only */. {+ r7 R: ]1 R
- ! W4 u9 `+ C5 ?0 X
- typedef const uint8_t uc8; /*!< Read Only *// F5 M$ U! O; M- i1 N% D2 {& i
- 6 c2 R- ?, l" l P
-
! o7 J8 ]$ _* } - typedef __IO uint32_t vu32;
9 k+ E* O8 B. e; X4 B0 y -
! t; _3 {% }: S3 ~ - typedef __IO uint16_t vu16;
3 A7 B$ b* u4 d4 C0 n - # H9 O1 X, X+ w1 O$ y
- typedef __IO uint8_t vu8;
! U* X! R; y: v# E# ^ -
. K3 _8 Q) b ^! J - 5 P* X. I1 |1 G5 _' C2 W
- typedef __I uint32_t vuc32; /*!< Read Only */7 n9 P7 Z9 G, F7 X4 T$ h
-
8 K# @" \8 g; N% h1 n - typedef __I uint16_t vuc16; /*!< Read Only */
4 b5 ^* K' U% | M. Z - . r( G" C( ^+ n) h
- typedef __I uint8_t vuc8; /*!< Read Only *
复制代码
' H& l. m' m' ]; m
' m1 e$ N; }8 |
浮点型: - #if !defined(__STRICT_ANSI__) || defined(__USE_C99_MATH)
9 g2 M' I( L1 s }+ X4 K4 P -
- v/ a: r+ R( ~ - /* C99 additions */
9 _' G1 G) q6 N$ Z2 F9 ` {- r9 E - 9 X+ N9 s$ V( t! ]2 @( v- n6 G2 p
- typedef float float_t;
+ v7 m' B8 m( D - 9 i# w) k! \% j" R" [: U2 ?4 X
- typedef double double_t;
复制代码5 X9 N; N" }& v7 a: \& ~6 r
注:还有float 浮点型 编译器中不能看到其定义(估计已编译了)。 而uint32_t 、uint16_t、uint8_t在哪里定义?在stdint.h文件中,详见下面: - /* exact-width signed integer types */
' Q2 B7 I+ {) ~* C -
. F; k/ r* R' O) e0 J5 x& y% u; ? - typedef signed char int8_t;; y5 y* {. C1 _
-
# E2 j+ s& u9 U - typedef signed short int int16_t;
: `7 S$ r: P$ q& d s2 G+ s - 0 h2 c6 r7 N9 N
- typedef signed int int32_t;
: V* e2 b9 g. E- ^( ~' `( ]' j, ] -
( v3 P3 z5 o2 Z8 ^ - typedef signed __int64 int64_t;
4 O+ A0 Q' z) B( {0 Z: w& ] -
# o2 {. X* c6 s- D7 g$ y -
' T) P X* h6 t2 b4 S1 \ - /* exact-width unsigned integer types */
, l8 {& U4 a, B8 `7 s - 1 q$ M' d* o+ }
- typedef unsigned char uint8_t;2 z0 A4 L; X# O
- " M2 a* [9 f: ?7 n; k
- typedef unsigned short int uint16_t;
+ {/ a% k' {, B9 h: q7 `* a2 A - / D: c/ \* {5 r) M, I$ k
- typedef unsigned int uint32_t;5 @! R3 e3 }' L& V- \! H$ V
-
8 s, o: ?9 u6 K$ Z - typedef unsigned __int64 uint64_t;
9 h' M8 H y/ ?0 K3 ^# { - / a/ Z! T& I# U0 m
- 8 A# D7 U2 _+ P( U" ~1 @* a$ D
- /* minimum values of exact-width signed integer types */
& t( L- k) Y/ O T7 a -
! b8 I! E# L+ f, M+ [ - #define INT8_MIN -128 /* s8 占用1个byte,数据范围 -2^7 到 (2^7-1) */" m& f1 w) G6 e3 M& q
-
7 d' }# F* ?* a# V0 [# c8 K# ] - #define INT16_MIN -32768 /* s16 占用2个byte,数据范围 -2^15 到 (2^15-1) */; @& y- I. N m8 v7 G( A
- , W- u7 v9 l4 u7 ]6 F
- #define INT32_MIN (~0x7fffffff) /* -2147483648 is unsigned s32 占用 4个byte,数据范围 -2^31 到 (2^31-1) */- p( [' W# F& P! {
-
/ R! b5 Y: R+ [. Q. r9 i2 u - #define INT64_MIN __ESCAPE__(~0x7fffffffffffffffll) /* -9223372036854775808 is unsigned int64_t占用8个byte,数据范围 -2^63 到 (2^63-1) */. ^: O8 x% q* f; q8 s
- ! x5 b6 W0 E% A
-
. t7 |( e0 \5 }' \1 G) D - /* maximum values of exact-width signed integer types */
+ j) r6 w; p2 u7 A- u- d -
3 W1 t$ X0 [6 M' E; G9 [7 D, T - #define INT8_MAX 127
1 N: Y3 B7 H m7 `8 A -
0 @+ c% T$ f* g - #define INT16_MAX 32767* H V# _! V: j! I7 t
-
. t4 ]7 n% {2 z - #define INT32_MAX 21474836479 K; S; z2 I7 o; ~$ |0 s" k
-
: D ^# S3 z+ x$ I& K( O/ a( ? - #define INT64_MAX __ESCAPE__(9223372036854775807ll)
( Q! n4 J% G/ l9 x% J% |7 V7 F- p -
- b" N* j2 f0 T* ~) P8 q% s6 g -
3 f' ~# f3 P+ s - /* maximum values of exact-width unsigned integer types */$ E, Y3 O; F& o4 w" ^* z9 W
-
9 k& i. U; \- s7 e - #define UINT8_MAX 255 /* u8 占用1个byte, 数据范围 0 - 2^8*/
8 @) [1 w! g( A) L - 4 n7 P4 k: B0 B4 X s, a1 y/ X
- #define UINT16_MAX 65535 /* u16 占用2个byte, 数据范围 0 - 2^16*/
- t1 V2 [1 ~6 L; M7 K/ v% a1 R- Z - ( O7 R, D7 G- | n. }8 V! r* Z
- #define UINT32_MAX 4294967295u /* u32 占用4个byte, 数据范围 0 - 2^32*/- @5 K% m, L2 n4 E3 K
- $ R9 _1 s3 \2 M& B
- #define UINT64_MAX __ESCAPE__(18446744073709551615ull)
复制代码 , I+ F& F: Y4 O9 h
$ H, C5 `1 \* K1 p3 D$ N) u
由上述可知: 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
6 _% v) y. K0 n0 l8 p* e
: R; {+ ~& x2 }( p" J; [% A- F1 f" s- ^' j( H
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 = 184467440737095516155 S; V+ D4 v& J7 x& r7 ~
9 ?% ]/ ^: A& i- P; m
0 W' s' I4 J _6 M d) h) O+ P. b
3、浮点型 - float ——4个byte,有符号型,可以表达负数/小数; Float 类型至少要能精确表示到小数点后6位。
- double——8个byte,有符号型,可以表达负数/小数;Double 类型至少要能精确到小数点后 10 位。
{* S, \8 C; G, s4 k( ?& b4 Z% Q8 z 9 ]; ?4 I- {7 B. k( p
G) s& Q+ C3 z0 k6 q2 p
转载自:路途…
* _3 I. Z3 C/ u' p如有侵权请联系删除9 U2 S8 B2 x5 L$ d! S" X
- M1 J# g* w# l. m( S
* o7 R& o' f" R7 h4 {# ~5 j
0 r# B# M" J+ o
|