|
stm32编程过程经常定义变量类型,经常担心数据运算过程中 超过变量类型范围。因为在编程过程中,不同的CPU,其数据类型的意义各不相同,所以一定要注意相应变量数据类型的定义和转换,否则在计算中可能会出现不确定的错误。所以下面列出常见数据类型:
; k. A& \0 x7 f1 y5 H- [! a! G+ h 一、C语言数据类型stm32使用的数据类型定义在 stm32f30x.h中 - This file contains all the peripheral registers definitions, bits ! Y1 N# L4 O2 J) T
- * definitions and memory mapping for STM32F30x devices.
复制代码
: i R/ n8 c( S- W2 h% w9 }' n
整型定义: - #include "core_cm4.h" /* Cortex-M4 processor and core peripherals */
/ H8 m- [' |2 R -
# { A; O5 z! |( g; \ - #include "system_stm32f4xx.h"
% P! t# C8 P0 V% D, G- S" J - 4 _& i* D' C5 H7 D2 }; K6 I
- #include <stdint.h>5 J2 B9 l& \0 Q+ M6 j
- 0 O: x( A& A& P, d6 q2 m" e2 K5 V" G
-
7 h% ] I! y* t - /** @addtogroup Exported_types
J7 \3 f X( I1 J6 B7 }+ ` - ( V1 m9 f# U: j, `3 a
- * @{3 K2 _0 r9 N8 ?; J( `1 U1 u
- 0 `5 g7 h W$ P3 M8 q$ M- }
- */; i. @: k6 G4 W: J! X0 q* w8 j6 I: {
-
) q J% R7 O+ V. j% f, O - /*!< STM32F10x Standard Peripheral Library old types (maintained for legacy purpose) */
; Y9 u% t+ E. a2 j2 O7 D - " H+ D& v0 G; K8 `
- typedef int32_t s32;
8 e4 E+ {, N) L* z+ C. a' ] - 7 {- \2 f& @9 M4 M9 i6 I% R5 N
- typedef int16_t s16;
: Z) q6 M9 u$ ] H- o" S - 6 m5 o( u( _- K) Q- _+ u Q5 y7 {: R
- typedef int8_t s8;* [" c% Z) L2 L6 U- P
- g: ^9 ~% p3 ]9 s" A& |
- 8 g- ~3 L$ y6 q
- typedef const int32_t sc32; /*!< Read Only */
: Z# G A; s# x - # ?6 a3 _7 L) v+ p3 _
- typedef const int16_t sc16; /*!< Read Only */) h4 y9 s1 n" |
- 3 {8 s0 F* w* j
- typedef const int8_t sc8; /*!< Read Only */( R$ q/ U5 ^9 R. m1 x
-
" [% `) ^2 Z L" B -
7 x1 u2 d& z8 o: L9 _1 W2 N; o: y. J - typedef __IO int32_t vs32;, Z2 V$ v2 V% u/ V
-
7 U8 h1 ~# j& V+ _ - typedef __IO int16_t vs16;
) X9 h! L& n- @# h: U0 C* m - 4 l& u0 M$ T! s4 x6 J
- typedef __IO int8_t vs8;/ N- J0 H4 R8 W' {& a- U9 _9 c
- 9 R! U% \! K: P$ z" ]3 y/ o, J
-
$ U$ g& K4 D1 o8 C0 M - typedef __I int32_t vsc32; /*!< Read Only */
* }7 ~6 H- }- l: Z- X5 S - $ u% l# H! J6 w: C' {" e9 |8 ]
- typedef __I int16_t vsc16; /*!< Read Only */
Z; |' l# z8 m1 _/ g, e - + {' q. O% S/ Q2 ]! v7 B: u8 |+ l( f
- typedef __I int8_t vsc8; /*!< Read Only */
: E1 M1 I7 o- u$ ^' b d6 J -
1 c. V1 y$ @+ h( ~* _ -
, `" r% k3 [ @ - <span style="color:#ff6666;">typedef uint32_t u32; /*常用类型*/* Q/ q# q# L4 K' @
- 4 g6 b6 B* R+ f( c, P
- typedef uint16_t u16;( a+ t& t0 s9 \ |. R x5 H
-
: w6 Z1 W% j1 X' @1 W - typedef uint8_t u8;</span>3 s2 Q/ t# |" O2 w
-
2 F! k% i' X1 ]1 m: N# X" m- g -
( F, S k0 Q+ u* J+ A2 @4 W - typedef const uint32_t uc32; /*!< Read Only */3 u6 R6 b: w- l1 X, `
-
! ^: v' y8 I% A; ^ L# D: w5 P - typedef const uint16_t uc16; /*!< Read Only */
% t- z( U: ^+ n* x - % ^5 s; r: j6 S7 p7 P* T. D
- typedef const uint8_t uc8; /*!< Read Only */
. t2 d# D7 [9 X -
* m% y4 B. j R0 I; f" k -
0 B6 S3 w2 H- S7 l1 r& ~: }. x - typedef __IO uint32_t vu32;
" ?' h' u" E' e6 X9 @ N6 O$ a -
* u$ V: Q9 e; M: x! k/ ~, ^ - typedef __IO uint16_t vu16;( m& b' c$ V8 S7 b
-
1 { N2 i, a( }+ M - typedef __IO uint8_t vu8;1 B! s% U4 w; c; h/ y$ t
-
" a5 ?( B' C3 ?; t - / S; I$ Q, v: w
- typedef __I uint32_t vuc32; /*!< Read Only */
# p* h+ ?; W0 b) y/ [/ I -
% |# _$ p+ P3 b - typedef __I uint16_t vuc16; /*!< Read Only */5 L9 c) K; ?' U* m
-
! ^5 K+ o7 K2 t) [' ?2 J8 H* ? - 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
浮点型: - #if !defined(__STRICT_ANSI__) || defined(__USE_C99_MATH)- q: r; i! E3 C7 R. K$ J
-
/ }; v9 p# v7 {$ ~7 R. b3 \ - /* C99 additions */
' e, Y; o8 M/ k/ C) M& v: `, ~4 n' l -
& U- A9 W( Z. |: Q - typedef float float_t;
( ]0 t+ Z" j" I B6 ]8 K! g - 6 p l/ I4 S# ?0 m+ P6 @
- typedef double double_t;
复制代码
& i3 @) _7 p D: P2 `
注:还有float 浮点型 编译器中不能看到其定义(估计已编译了)。 而uint32_t 、uint16_t、uint8_t在哪里定义?在stdint.h文件中,详见下面: - /* exact-width signed integer types */
8 b w' \; _* K/ F+ G - & H/ E9 }# z: h! V% n4 y
- typedef signed char int8_t;
% k# w6 G+ h Y- t - 9 ^! ], u1 `) S* j" i) c
- typedef signed short int int16_t;* b* D8 _) z! y4 ]5 U/ B5 A
-
0 F9 r# J8 O" I+ T - typedef signed int int32_t;
$ z( H$ T+ o; N8 I% C - 8 A+ K! w* E0 w. S- v8 C
- typedef signed __int64 int64_t;# N r2 u v& x/ _
- # ~' x. t8 H8 a9 }+ X
-
S/ i+ }' |! j5 g! B - /* exact-width unsigned integer types */
4 p! h4 s- Z+ V% i q, l( ] - 2 l0 ?4 _. R" \3 W. _4 Z
- typedef unsigned char uint8_t;5 H+ H& ]- H) `6 J2 a5 x! Z1 [
-
$ n1 p( t! h) v. I$ L' V. W5 R7 p% @, P - typedef unsigned short int uint16_t;( Y1 z7 s/ W. e
-
! U; S7 V A( }% i5 I$ P" G - typedef unsigned int uint32_t;' {& q* @$ r/ D7 S3 X6 N
-
7 z' G& e* j. i, @% k+ N - typedef unsigned __int64 uint64_t;, q1 I/ h3 S. A( P$ {& [- ^' }' M
-
7 L9 j) |% E; q2 u - ! O" W8 A5 k! u6 H0 |' v/ r
- /* minimum values of exact-width signed integer types */5 N8 a4 v7 g/ p2 _
-
/ y+ N, l- Y4 ]) B% [ - #define INT8_MIN -128 /* s8 占用1个byte,数据范围 -2^7 到 (2^7-1) */0 v) p4 W7 E7 Z: Q8 ?9 j" P
- : I+ k' ?' |- r
- #define INT16_MIN -32768 /* s16 占用2个byte,数据范围 -2^15 到 (2^15-1) */0 d; b l9 B) t# S& P9 k
- + F6 M( n. |9 N
- #define INT32_MIN (~0x7fffffff) /* -2147483648 is unsigned s32 占用 4个byte,数据范围 -2^31 到 (2^31-1) */9 Z0 U% j7 f% Z1 N5 ?
-
: O) K! P% ?4 k% O% i - #define INT64_MIN __ESCAPE__(~0x7fffffffffffffffll) /* -9223372036854775808 is unsigned int64_t占用8个byte,数据范围 -2^63 到 (2^63-1) */
( N {- {& R6 e" ?6 F9 X - + `! S# S: v$ X' N5 C! S
- 6 D& i, {2 z6 A# Q" R
- /* maximum values of exact-width signed integer types */
% s* p3 J1 F3 v4 r8 z1 H -
- m# u( i3 M ? - #define INT8_MAX 127
9 m$ \) j2 d! z- T3 w3 X) U - & \3 i' V; [! {: {/ F/ R' ^5 \
- #define INT16_MAX 327679 Q4 k7 B9 v4 ~+ y4 x0 ?( H7 _
- 7 y/ j" |/ u# B& Y
- #define INT32_MAX 2147483647
; S4 f4 [- e! N -
# M& ?! \7 g. C( t4 _ H! ? - #define INT64_MAX __ESCAPE__(9223372036854775807ll)
, E5 h- X) E) }0 C, V: P' | -
' K# _1 s d0 K: ]2 P& @" e -
: _ J* ~% e* ?+ h/ j% h - /* maximum values of exact-width unsigned integer types */
$ Y+ g5 C( q8 `, b. S, h - - {4 w% n( [& n; P6 Q) m( O
- #define UINT8_MAX 255 /* u8 占用1个byte, 数据范围 0 - 2^8*/1 t: G2 {! y( C) }% l
-
8 E. n& ]/ t2 I8 k6 G" h* Z" ~ - #define UINT16_MAX 65535 /* u16 占用2个byte, 数据范围 0 - 2^16*/
- F. b1 n; f) q% x6 k) G - ! x( ?: `; x/ [0 F5 j- E* t
- #define UINT32_MAX 4294967295u /* u32 占用4个byte, 数据范围 0 - 2^32*/
& I3 Z: w/ t7 t p7 I7 s; ? -
1 f3 t, g: G! @! ~, C, n - #define UINT64_MAX __ESCAPE__(18446744073709551615ull)
复制代码
/ `' ~: N8 H( U4 K& s: T9 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 |