|
stm32编程过程经常定义变量类型,经常担心数据运算过程中 超过变量类型范围。因为在编程过程中,不同的CPU,其数据类型的意义各不相同,所以一定要注意相应变量数据类型的定义和转换,否则在计算中可能会出现不确定的错误。所以下面列出常见数据类型: # h2 R! i8 {+ H; O' X
一、C语言数据类型stm32使用的数据类型定义在 stm32f30x.h中 - This file contains all the peripheral registers definitions, bits 0 g9 v3 L% I1 w! M- a$ A5 W
- * definitions and memory mapping for STM32F30x devices.
复制代码6 @7 H9 l* L# t n/ E, z
整型定义: - #include "core_cm4.h" /* Cortex-M4 processor and core peripherals */
% d- R: O6 i A& o# i -
+ \' F, `( B0 B0 e- i - #include "system_stm32f4xx.h"
2 u2 n& u( p* \& d) ]$ v9 o: ]2 z - 2 o8 w6 T) i/ v7 M
- #include <stdint.h>
5 e e9 M& m+ X3 u+ v* W5 k -
- o/ _! Q# k* S1 ?5 t: j* x: { -
4 c# T! U0 O# N% U7 s5 g - /** @addtogroup Exported_types
+ \. F, R8 W0 v+ |% v. x -
3 W7 `( e% ~3 U# z% m- D/ _. ^9 s - * @{+ K9 H$ d \3 m6 ~' g+ Z8 B! q4 u& Z
- / }6 f! x4 |) U" {# X- e0 ?
- */
7 z- J* R+ x) ~ -
& j- Q0 g- d) K: d: b - /*!< STM32F10x Standard Peripheral Library old types (maintained for legacy purpose) */9 } A% l1 Q# f- S G$ ?$ K
-
) G& \* M+ E% u d( w7 ?. Z - typedef int32_t s32;- m% c. | u" B6 A
-
! N- p# K1 d" d1 G* f, C - typedef int16_t s16;, a( x k- P# I
- 8 \5 I3 n2 h! x* N7 V, A& c
- typedef int8_t s8;. O' N, p4 ^8 D4 H1 k& C
-
, H/ E4 g h/ a- A/ a1 P -
. E: w- g+ t' |! ^* Y) V - typedef const int32_t sc32; /*!< Read Only */1 \( Z! Q; p. Z7 N; P: a
- 4 { Y8 M) y; {: U
- typedef const int16_t sc16; /*!< Read Only */7 X* j0 E' R( h; J/ Q' j9 r
-
7 z; ]8 L3 p& j# B* u- B9 } - typedef const int8_t sc8; /*!< Read Only */& u3 ~; k% x2 C" G/ r
-
" C* g' t, a+ w( _1 ? -
6 W* H5 |9 j% a' y. }/ Y- E - typedef __IO int32_t vs32;
6 y: D5 `4 x+ o; ~9 n5 e/ I+ | - , ~2 i, w+ A9 U
- typedef __IO int16_t vs16;1 q- q% U& v. s, H; p
-
; P1 @8 c( ^7 f# G/ E+ P( | - typedef __IO int8_t vs8;1 X \; v D6 w/ r. Y# k
- 3 c+ o2 G _! a! A
- % @- S9 K& B) b2 T/ L* q
- typedef __I int32_t vsc32; /*!< Read Only */7 z4 M. f6 m" ?! D! O. c R
-
* w+ y3 S$ E: R$ \% @) `5 E0 o! ` - typedef __I int16_t vsc16; /*!< Read Only */
" p# x, E& ~$ b' y* ^ -
, J1 h! ^3 Y3 K; K - typedef __I int8_t vsc8; /*!< Read Only */
8 |# F) q, P& _8 c: b- B -
$ B9 k9 K5 c, N2 H1 ~ -
5 `) o2 X$ {) Z8 J% W2 d! H - <span style="color:#ff6666;">typedef uint32_t u32; /*常用类型*/
i0 w; m: r8 `( ~# n - ) P, U1 y9 Z" _8 j( h6 o) x
- typedef uint16_t u16;# @( J5 e% O0 B1 q) N0 E. @
-
7 j! Q" q2 C# `3 C - typedef uint8_t u8;</span>
! e2 [2 L' m4 ~9 Y! _3 Y; R -
: e! t* a" x9 B+ S* _* e - 9 Q6 p- E" Y) {2 ?% C
- typedef const uint32_t uc32; /*!< Read Only */
$ u) p7 ~) A' X - - a3 a% k* q& B
- typedef const uint16_t uc16; /*!< Read Only */
" t; h% C: V+ X, D - ; L" r; V3 K6 ~5 H
- typedef const uint8_t uc8; /*!< Read Only */
2 i( b1 [4 h( f( ^ -
2 q$ J6 [* z/ d! P -
( Y2 q2 n; ]" @7 A: \ - typedef __IO uint32_t vu32;$ n% Q0 T3 \' i' W
-
$ J8 g! V4 r9 ^ - typedef __IO uint16_t vu16;! X8 _% ~) _+ ~4 R' I$ L" L) I
- 2 [- ~! @3 U4 L1 Z5 f' E7 G
- typedef __IO uint8_t vu8;
+ T; W( T! D7 e+ [ - 2 ?* f/ Z. B V7 E, m% a
-
+ U5 b, J9 t7 W) J- g - typedef __I uint32_t vuc32; /*!< Read Only */9 p" ]+ o7 c; \: y
-
1 D* z. g8 i; q - typedef __I uint16_t vuc16; /*!< Read Only */) B8 T. o3 F3 L Q) ^
- - r& S0 @0 Y1 D- F# h! V) b
- typedef __I uint8_t vuc8; /*!< Read Only *
复制代码 # ? I0 o* W9 C4 t+ G& |7 n- K, R; c
6 R. l* Q8 h d* n% Y
浮点型: - #if !defined(__STRICT_ANSI__) || defined(__USE_C99_MATH)& P; I6 R+ o H, u
-
$ y z8 c6 `& \2 G! w$ R G - /* C99 additions */
3 P: W+ y1 Y9 j+ g b, z - 6 m& t. l$ ^5 c* l7 t/ ?% X4 |( n
- typedef float float_t;6 i5 }4 Q! d$ b+ v1 k/ V0 o
-
5 w8 u- V I [$ A - typedef double double_t;
复制代码! K4 e. T: r8 q# c: O; U; u( t
注:还有float 浮点型 编译器中不能看到其定义(估计已编译了)。 而uint32_t 、uint16_t、uint8_t在哪里定义?在stdint.h文件中,详见下面: - /* exact-width signed integer types */
8 Q% c7 r9 r5 K- A8 M2 y -
/ w" |/ b3 c" x4 _) y - typedef signed char int8_t;. Q6 v2 n% z- m1 J- v* V+ c
-
* e- k; h$ l2 Z, c - typedef signed short int int16_t;
( b6 B- k; `/ c( P$ ~2 @ -
2 I' F! t+ W: `- Z3 B9 @ - typedef signed int int32_t;
+ `! D8 U- s" T" V0 g - ! _$ p7 t I, J L# s# @! [- [/ j9 c
- typedef signed __int64 int64_t;
) u1 x1 w( i( c% E" e# M - + W. z+ O3 ~& S3 I
- ; q" V& E4 m/ v4 ^$ v. f8 [" H
- /* exact-width unsigned integer types */7 I9 ^! L# X' k5 P$ h. z0 A
- ( R7 A2 @1 ^: k: r# m( U( e
- typedef unsigned char uint8_t;
7 n2 W+ U7 G# h0 d8 j - / L, f6 Y. f, {
- typedef unsigned short int uint16_t;2 i3 \ {8 ]2 G. y# F* G
-
_/ e4 \: t! U k& {7 g - typedef unsigned int uint32_t;
! c4 |% O. J: Y4 f/ H( m! w -
! {7 P$ J8 G& x- b: B: x- }- M - typedef unsigned __int64 uint64_t;. X0 ^& V5 M4 E5 A* ?
-
; R( f: z7 i# Q0 U( I: {2 S -
/ m6 {7 j0 |3 K7 t; o - /* minimum values of exact-width signed integer types */
0 s8 T- T7 E/ h9 S -
4 \' @/ v0 D; |/ D7 \9 d' t3 V; [ - #define INT8_MIN -128 /* s8 占用1个byte,数据范围 -2^7 到 (2^7-1) */
! ]$ j- u' D0 p0 D% f: w - ! y6 ]' u% K& x, S, g: D/ f
- #define INT16_MIN -32768 /* s16 占用2个byte,数据范围 -2^15 到 (2^15-1) */
2 d8 ~$ w4 |, ]: _" q - ; J* G! P4 q* \8 V d, K* j o$ f
- #define INT32_MIN (~0x7fffffff) /* -2147483648 is unsigned s32 占用 4个byte,数据范围 -2^31 到 (2^31-1) */' o* Z; h7 h* P( C6 w
-
* w" T9 m2 J Y0 u4 l - #define INT64_MIN __ESCAPE__(~0x7fffffffffffffffll) /* -9223372036854775808 is unsigned int64_t占用8个byte,数据范围 -2^63 到 (2^63-1) */$ `( S/ e; t% W, Z/ @$ X
-
3 |; Q5 P$ [) }0 O - 4 v# c* a+ u/ u. q) ]- H/ ^5 t# \: ?
- /* maximum values of exact-width signed integer types */& k& N- @, ~% F' d6 u1 ^
-
* _7 P% Z5 Q7 ]2 p# z2 M8 \ - #define INT8_MAX 127& \: v; y; o1 H; N2 ?
-
" u& I+ c+ L$ r2 a: n5 D - #define INT16_MAX 32767( T; U: h9 _$ e u( X5 N4 `5 a
-
% J2 k" e! n; r& K/ A3 L0 \1 N - #define INT32_MAX 2147483647! o7 q d9 `, y# b
- . \, F2 _9 M' y% Q o
- #define INT64_MAX __ESCAPE__(9223372036854775807ll)
@: p; O2 B% K: }9 H A- w2 m -
3 h% v' V* i a" P - * v9 w8 d& _' U; R5 z$ @+ m. X
- /* maximum values of exact-width unsigned integer types */, k# j2 l9 [& K1 g ~
-
- T! F8 c3 H- E! T( O* H - #define UINT8_MAX 255 /* u8 占用1个byte, 数据范围 0 - 2^8*/ y. b; J& m0 f. k( {! T- `
- H; h! n- d; a- K4 |0 A& M
- #define UINT16_MAX 65535 /* u16 占用2个byte, 数据范围 0 - 2^16*/ X) a6 E4 ?/ m C% a+ E8 l
-
# ~+ M% `9 P4 H0 v: T" T# q& p - #define UINT32_MAX 4294967295u /* u32 占用4个byte, 数据范围 0 - 2^32*/5 a2 h- V9 c. v
- # B" k3 u6 f7 X6 A( u5 a
- #define UINT64_MAX __ESCAPE__(18446744073709551615ull)
复制代码
* V, I3 [4 w4 ?) N% x6 U+ t" j& B; b- E" o
由上述可知: 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 = 9223372036854775807ll5 Q2 N% s7 V5 w* B' [: ^1 k v! ]
3 A( {1 {( C) A6 {* C5 d
* m1 a: g7 F6 {$ {7 r! 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 = 18446744073709551615
# @* `# G/ _# {( R& X4 T & W3 l8 q* \' T+ W. F" G# Y2 L
* c9 h: `! [- c3、浮点型 - float ——4个byte,有符号型,可以表达负数/小数; Float 类型至少要能精确表示到小数点后6位。
- double——8个byte,有符号型,可以表达负数/小数;Double 类型至少要能精确到小数点后 10 位。, A) e; ~; @& {+ P9 S) ~7 s
5 M9 x J0 Z0 c6 h
0 a& [9 H$ H! r& s. U7 d7 g
转载自:路途…3 @$ I% H% h% }* |
如有侵权请联系删除
8 K9 r. h4 `7 l
+ n) q/ O3 g K: [# M8 o( M' U M+ g- b+ b) D
, N z+ a! Q2 c3 ?# R$ { |