|
stm32编程过程经常定义变量类型,经常担心数据运算过程中 超过变量类型范围。因为在编程过程中,不同的CPU,其数据类型的意义各不相同,所以一定要注意相应变量数据类型的定义和转换,否则在计算中可能会出现不确定的错误。所以下面列出常见数据类型: : [/ n! k" t4 i/ v
一、C语言数据类型stm32使用的数据类型定义在 stm32f30x.h中 - This file contains all the peripheral registers definitions, bits
; b2 c @6 q; t, k, U - * definitions and memory mapping for STM32F30x devices.
复制代码) K9 c/ E' V2 U0 r% ^( |( i
整型定义: - #include "core_cm4.h" /* Cortex-M4 processor and core peripherals */ P0 s- E' K0 M/ J- n- x+ H3 B
-
. l2 n& l0 ^# @: E - #include "system_stm32f4xx.h"
! L1 E: Q2 [3 |) B; N3 t - 1 p9 Y$ @- r: p$ |
- #include <stdint.h>
/ F2 a" D/ I2 {2 k -
# s9 U t+ o! B3 i# a -
, B% w; W1 w4 p, o; z9 e+ d* O - /** @addtogroup Exported_types
1 \# N F0 c+ q, H; H3 D - : w+ W8 m# V6 ?" N; i n0 Z. N
- * @{
; }* R) I P! S _& A: J) t, A+ ? - 2 R. V9 T2 v4 h% _7 E8 z4 \
- */8 Q$ S: i5 i7 Q
-
2 J7 R' k1 E1 v! G- `) G - /*!< STM32F10x Standard Peripheral Library old types (maintained for legacy purpose) */& d4 K$ g$ |) i2 J1 i
- / H u( K2 V/ |
- typedef int32_t s32;, G- M) K- u2 ~
-
3 v0 f [3 L9 q; s: a( _1 }* p5 K; B - typedef int16_t s16;/ h# o1 S8 N0 ~- ?+ q
-
- D* Y% ]4 b1 { - typedef int8_t s8;
9 |, \5 r3 {3 Y7 d3 { -
+ S0 R) T4 ~8 F. f -
3 d2 Q" w' r& g: Z$ k* S - typedef const int32_t sc32; /*!< Read Only */& `1 m9 h: U* o+ ]; q
-
4 j; Y; c% B* } - typedef const int16_t sc16; /*!< Read Only */
, V, e% t4 U- @$ v0 N - & P& L8 v# o+ h' h4 t/ a
- typedef const int8_t sc8; /*!< Read Only */
8 B o% F6 |; {& A2 F- ` - ( N: @# N# W6 o b* ]7 `0 o, P+ I
- " C$ c' O' d* j5 n4 O$ Z
- typedef __IO int32_t vs32;" e0 T B1 ]3 k6 M) [3 B
- 5 z) C. g! T/ I5 I1 j
- typedef __IO int16_t vs16;
7 _" K0 N3 x# O! R% i1 ]# q -
& m2 o+ |! \7 A8 ^" X - typedef __IO int8_t vs8;1 V8 J( w# C+ b+ I3 A4 U; ~
-
4 N( D) C. m) \2 r0 ]' g5 ? -
% n1 o" ?; g' }6 @# n - typedef __I int32_t vsc32; /*!< Read Only */
( `, H* q; p8 ~9 G' H3 V -
9 Z3 a& J! j. q1 {. A5 T1 N - typedef __I int16_t vsc16; /*!< Read Only */4 O0 m* J$ {$ `- R
-
$ g1 J& e2 D) y" O - typedef __I int8_t vsc8; /*!< Read Only */
. M0 l7 p! e9 C& |5 k6 o5 X -
3 _2 k) u1 I5 w( c' A9 G -
2 V5 r2 e; @8 S - <span style="color:#ff6666;">typedef uint32_t u32; /*常用类型*/
$ ^4 U0 t H) F( h! P - * d' |; ^) g5 [ R
- typedef uint16_t u16;) t* `2 o E$ ?) }
-
- i1 @1 E, K" o- S - typedef uint8_t u8;</span>
# r6 X2 l6 M# K8 X7 ^/ B+ c - , h* M2 J% U- d. ]" L" @2 M
-
B. x. V* l& O, a1 G5 x2 x6 U1 i - typedef const uint32_t uc32; /*!< Read Only */. x: g( T4 P. P: w9 Y9 z9 [
- % O" b3 _. P7 o2 z2 c
- typedef const uint16_t uc16; /*!< Read Only */
" d1 }* K8 p; ~0 g% ^) t+ | - ! T- g' ?5 r1 a3 l0 E, t) O, n
- typedef const uint8_t uc8; /*!< Read Only */
5 Z9 b% r& D& L# ` -
- ]4 E" {' g) q - + T$ Q, [2 |, v3 m+ ^# d! B
- typedef __IO uint32_t vu32;' ?0 m0 {# A" c) f2 g N
-
+ L: H! a4 r9 ` - typedef __IO uint16_t vu16;- X" w- D3 @6 p8 y, G3 E/ W7 P8 @% U
-
# R) t+ N$ m' K2 f* ]! ?4 ` - typedef __IO uint8_t vu8;/ f: l0 x [+ c# c( d
- 6 i! e! U$ x1 H
-
( e& l: E8 h( T - typedef __I uint32_t vuc32; /*!< Read Only */
) ^4 O% i: t' q! x* v& o -
9 `- R4 h7 f8 g - typedef __I uint16_t vuc16; /*!< Read Only */! Y2 Z' s2 a: Z" ]1 A; i {. _
- $ E4 T; m+ {' G N0 }
- typedef __I uint8_t vuc8; /*!< Read Only *
复制代码
3 j; W0 g6 `5 o# T$ b! p$ b' L( J
浮点型: - #if !defined(__STRICT_ANSI__) || defined(__USE_C99_MATH)3 I) S+ p+ _8 H6 k1 ?% i- b8 y8 q" M
-
, ]# F1 U6 U. e. E, f/ C - /* C99 additions */. K3 [4 z3 d. F
- 9 J" ? p! V3 G0 x8 @
- typedef float float_t;
8 K: k8 X% J0 k& ~- D# | -
& g/ i5 s/ S* I$ j( Z0 b. Z' u - typedef double double_t;
复制代码* o: J2 I% q: \: U! h7 O: q
注:还有float 浮点型 编译器中不能看到其定义(估计已编译了)。 而uint32_t 、uint16_t、uint8_t在哪里定义?在stdint.h文件中,详见下面: - /* exact-width signed integer types */
% E/ }9 _" [ W, E2 `* }8 L0 A" R3 \ - / w" d) n0 v$ d% R5 h6 R9 {
- typedef signed char int8_t;
x3 D" F; P/ d* ?; C% D' h! l - . |1 m- v* S5 n# Y# [
- typedef signed short int int16_t;
- Q: q; B8 M, T# k% O( @% T - ' q7 C6 _- x4 G" M& s& q5 j: w( f
- typedef signed int int32_t;
9 u; Z7 k; p9 h1 x+ l3 a - . [' V# o! E. u+ p( C
- typedef signed __int64 int64_t;7 b Q) A# H1 i# A: J" `; C
- 2 ]; K( v5 J3 k- o
-
% ~( K9 y, Y. f- W. c/ D l, |7 L$ E - /* exact-width unsigned integer types */
- o- S# {% T/ H -
; u. W' W+ H0 n. l- a - typedef unsigned char uint8_t;
0 a9 m3 Z" ]# b& t c$ x0 W - 5 G/ [2 \. K6 d/ l6 O9 f+ c
- typedef unsigned short int uint16_t;
7 B- Z) @( J' q% a+ _/ }1 l - * e, n4 z! ?# B
- typedef unsigned int uint32_t;- S7 {" C" M) U" t7 S. \6 J9 o2 `
- 7 M4 g1 B- F: Q9 h7 W
- typedef unsigned __int64 uint64_t;
9 O5 ~* q# R0 o% S7 u# R - ' ?) q. K+ A" Y3 l# Z* l0 }' r- C
-
7 T6 U' I j1 D! L6 b* b/ z# P - /* minimum values of exact-width signed integer types */( ^* K& `0 J/ z: b
- * U- X2 g7 N' n: A8 E
- #define INT8_MIN -128 /* s8 占用1个byte,数据范围 -2^7 到 (2^7-1) */
9 e+ k& B8 C) J, a+ N( k - o; v) |! G! a) M( J v' n/ k
- #define INT16_MIN -32768 /* s16 占用2个byte,数据范围 -2^15 到 (2^15-1) */
; C5 [+ g) L3 _) ^) t' @+ v9 ] -
4 u4 p0 ~( Q+ p/ Q - #define INT32_MIN (~0x7fffffff) /* -2147483648 is unsigned s32 占用 4个byte,数据范围 -2^31 到 (2^31-1) *// z9 O6 i7 Y" A( _4 F! O; E
-
; n! h6 w6 M$ W6 Q# f6 | - #define INT64_MIN __ESCAPE__(~0x7fffffffffffffffll) /* -9223372036854775808 is unsigned int64_t占用8个byte,数据范围 -2^63 到 (2^63-1) */8 K/ r7 x# D L' a4 T
-
2 N+ }: t' r4 h2 y7 M4 s' a$ X -
, N2 l* _% U' _! M( _4 A - /* maximum values of exact-width signed integer types */
; g5 P: J1 r- W$ m - 5 ^! p+ Z& ?( U
- #define INT8_MAX 127
3 G! b+ t1 w, S4 x+ S- E - * |1 E) s; F. U) {4 M' r
- #define INT16_MAX 32767, x% g9 Y, C$ u
-
. Q+ K% Q5 P& [& Q9 q3 e - #define INT32_MAX 2147483647
2 G2 X7 B( ]' ^: X - 6 N% J, J# w5 ]! k
- #define INT64_MAX __ESCAPE__(9223372036854775807ll). \, `+ @" r' F* o7 w- G4 Q
-
/ U9 _( G* L2 { j7 U! H -
2 s8 p3 v- n1 ^( z0 L! \9 n3 T - /* maximum values of exact-width unsigned integer types */& I9 U0 @9 Q& j
- 8 i7 b8 }/ \3 u" p
- #define UINT8_MAX 255 /* u8 占用1个byte, 数据范围 0 - 2^8*/
* b+ J1 A* d! _8 t - 6 r& c) |0 C0 q" k
- #define UINT16_MAX 65535 /* u16 占用2个byte, 数据范围 0 - 2^16*/
8 J/ Q8 a4 V- L! h9 K - . i/ y0 v, o5 D- j9 n
- #define UINT32_MAX 4294967295u /* u32 占用4个byte, 数据范围 0 - 2^32*/
2 f: C" {& U* k$ p - 7 w1 E. Z+ }, a& I% a
- #define UINT64_MAX __ESCAPE__(18446744073709551615ull)
复制代码
& ^ q% j! R+ n$ _4 q! Z; t. W6 {; Q1 a! B% a: @
由上述可知: 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 = 9223372036854775807ll8 p0 }9 u: A. H$ i, q
; B0 p& ^" h2 L! T! L9 B6 \; Z0 l
9 ~% v8 \* O E9 c* M6 h) {6 L 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 = 184467440737095516157 e5 ^( u; F* e5 P6 S1 P
* o. z2 r3 i4 T) K& n; ?: \
# l' A U. B# x B3、浮点型 - float ——4个byte,有符号型,可以表达负数/小数; Float 类型至少要能精确表示到小数点后6位。
- double——8个byte,有符号型,可以表达负数/小数;Double 类型至少要能精确到小数点后 10 位。
# N9 N4 V4 L. J5 @
6 E5 a$ k% j8 t7 K+ f
9 Z# ~: u1 N; L7 d2 a转载自:路途… a! F4 f0 p4 S8 Y
如有侵权请联系删除
; b' Z# o9 ^: X( N2 w. B3 p
; v$ U. t3 N. A6 g0 {8 L$ _( ~& O& ?- |2 E; @2 l O; p, p, y
# C1 C! N& p3 B0 k/ v% a, A5 |- t) D& V |