|
stm32编程过程经常定义变量类型,经常担心数据运算过程中 超过变量类型范围。因为在编程过程中,不同的CPU,其数据类型的意义各不相同,所以一定要注意相应变量数据类型的定义和转换,否则在计算中可能会出现不确定的错误。所以下面列出常见数据类型:
2 h. y( @" i. K% H$ t; I 一、C语言数据类型stm32使用的数据类型定义在 stm32f30x.h中 - This file contains all the peripheral registers definitions, bits Y2 {- m. N% x9 \. D4 a
- * definitions and memory mapping for STM32F30x devices.
复制代码
, j0 x; k+ o) P& x1 I
整型定义: - #include "core_cm4.h" /* Cortex-M4 processor and core peripherals */* l6 Z: Y; | e( b" n. ]
- # x, X* q5 `6 Q2 E( i1 m
- #include "system_stm32f4xx.h"/ w5 i5 C. H# m5 Z
-
7 A' k% P7 m0 h. U - #include <stdint.h>) e, s4 f& l) J
- 7 K' z4 w" l8 x. ?
-
5 w( L; \9 j* i8 q: n9 z - /** @addtogroup Exported_types8 p6 r( q6 u! y1 f
- ( t3 q/ ^! E' w0 E9 ?
- * @{& T8 B3 L- ]/ W- S, p& L
- . A5 P5 [' q9 H
- */* i( p. j- R2 t4 x# }4 b; D6 h
-
' ]0 `0 ^4 k8 J h$ P - /*!< STM32F10x Standard Peripheral Library old types (maintained for legacy purpose) */1 ~; ?# o& a- t+ f: a3 `& I a
-
4 o7 ?6 a! `/ c$ i+ `6 q, V/ _0 H - typedef int32_t s32;
& H2 M7 e. B- E5 @ -
L9 m$ K$ C; E: { - typedef int16_t s16;( S' l% g B2 y
-
) T2 V# B! Y! H! p. E' K - typedef int8_t s8;8 k6 }. T- B3 x
- - e! s0 _( p" [$ y! r* S
- . ?8 ]# d% P. @9 Z D
- typedef const int32_t sc32; /*!< Read Only */# |( { a, r N9 R
-
5 Z% ^ ], ]0 s( ^7 `8 @ - typedef const int16_t sc16; /*!< Read Only */
: j- x( u# Y8 V -
; o. P/ r6 q$ l# }4 O) Y. C* V - typedef const int8_t sc8; /*!< Read Only */: [" W: K. X' Q' n# r: v0 j
- 7 c. \! h; b' Y6 Q4 j6 ]" v
-
2 I* X8 {, n9 d, X, b - typedef __IO int32_t vs32;
8 M4 p7 X, X' y D -
( B2 {3 R5 S* J c" x9 g7 f - typedef __IO int16_t vs16;6 q8 a9 u. _7 I, j
- , O/ O; r# O# P! ~2 D8 y/ f8 h
- typedef __IO int8_t vs8;
' G& s" t$ O; |9 v5 p -
2 U& L& x- f0 Y8 k -
: k# I( p( L8 E" n/ D - typedef __I int32_t vsc32; /*!< Read Only */( q j# V$ v R- J! |8 ^
-
0 E# Z9 o' m/ m. t8 f" v ?: V8 @5 } - typedef __I int16_t vsc16; /*!< Read Only */
' a, }2 U* K! d0 g5 K+ U0 T1 _8 Q - ( i4 M: [* e% h) w! v, \) M! Z: O
- typedef __I int8_t vsc8; /*!< Read Only */( E) Y- g8 }8 S, W7 H
- / H u1 N5 {/ X! d, N+ n
-
+ J4 P4 o" r7 H9 u( r - <span style="color:#ff6666;">typedef uint32_t u32; /*常用类型*/
! F- n o% {3 X2 @ -
6 w' _/ {2 Z' I! o R* H3 ] - typedef uint16_t u16;+ E* h3 `/ }6 w2 G/ o8 n" }
-
! |+ f) J6 d. r }+ U4 j - typedef uint8_t u8;</span>
/ A6 A6 L i9 z - ! H7 Z9 I* D) L$ W n: g+ G5 `( i
- $ R# P; P9 }# H" ]1 X% B, f6 }
- typedef const uint32_t uc32; /*!< Read Only */
/ C: F$ ?" X' e - 6 j+ t0 ?: ?) D+ B
- typedef const uint16_t uc16; /*!< Read Only */
& t! `9 {0 @' f% X, G - + U% \. X- }. T1 r* p# ?5 L! r2 C
- typedef const uint8_t uc8; /*!< Read Only */
+ O x* i3 J, e9 Z$ I! x - 7 D2 u6 u0 t0 ?* a0 ?
-
3 T' b, N- ^1 l+ O R6 X - typedef __IO uint32_t vu32;3 l6 `: Y8 I/ p4 R1 z
-
. P: @5 m+ c" ]! d6 J; } - typedef __IO uint16_t vu16;
# k: h2 D7 B9 J( A1 ` -
/ n# ~% N+ ?: y# I, N' S1 I3 Z; [( E - typedef __IO uint8_t vu8;$ @5 G, W7 s( M$ f
- 9 G' f/ G4 W& ?& q
-
. \# `" T3 \5 q5 t. n - typedef __I uint32_t vuc32; /*!< Read Only */
# K0 Q( b8 j" P! v0 |; \$ `2 i - 1 w* K, A, a2 R0 Q( d; ]( Y. V
- typedef __I uint16_t vuc16; /*!< Read Only */
2 t4 [! K( \1 b3 T/ |# g& i -
1 _+ w5 Q, d4 h/ b# A( L& q - typedef __I uint8_t vuc8; /*!< Read Only *
复制代码
( m6 d) L& S. U. N/ Q0 g; f( }
1 S3 j- F9 Z# t/ M( ]1 `
浮点型: - #if !defined(__STRICT_ANSI__) || defined(__USE_C99_MATH)$ j p6 C: g a
- - Q2 A2 z8 [7 K
- /* C99 additions */- u. H5 q' ?' T8 J/ K, D, c' j
-
+ [/ Z$ C. G) V- O8 h - typedef float float_t;2 k7 K& W( {, `$ t$ g
- / ?3 ^ H9 ~% a# [
- typedef double double_t;
复制代码8 s: W/ G5 Y* N( Y# Q3 Z7 U% ?) |
注:还有float 浮点型 编译器中不能看到其定义(估计已编译了)。 而uint32_t 、uint16_t、uint8_t在哪里定义?在stdint.h文件中,详见下面: - /* exact-width signed integer types */
$ L; h; [2 b u( w; J7 h -
; K' q* C9 c' J( m4 @/ u - typedef signed char int8_t;! M" ]" D& Z8 j& w: R
-
) z2 M+ k& y" K$ p6 p% x - typedef signed short int int16_t;! h: O* U' I$ O. J' U
- * D, `4 t6 Y y1 i2 |: z6 U
- typedef signed int int32_t;
2 X1 a: f, n$ U1 \ - ( @9 y3 v, N# W j1 O
- typedef signed __int64 int64_t;
& r& [6 } g( f# t$ f, I6 n - ; ?6 M3 N, l' E3 V$ _
- " k; q' R. n R1 u# ^# |- K. W
- /* exact-width unsigned integer types */) ?7 j9 {9 P' C5 j" t7 n- k
- : g4 o% A! [' K6 P
- typedef unsigned char uint8_t;, K( N- a6 k5 X0 S% U* H# @
- $ I U# d. ^* S3 y0 t% f
- typedef unsigned short int uint16_t;
" r( V7 j# ^9 m$ c* i - $ I) {0 d1 s' h- }1 f' `
- typedef unsigned int uint32_t;8 u$ X: Q- x) u
- " y& W9 T+ ^" U! k( u2 N$ x
- typedef unsigned __int64 uint64_t;, C1 P) J+ L. ~. S* S
-
& u1 |, ^- e) e6 J& k# x - ! ] y! R, C" Q$ B# e: Z, l/ A/ {, G5 F
- /* minimum values of exact-width signed integer types */
. |# A, g' J! e9 R: l" j -
% j$ S; v7 i& n' F) _7 Q. D - #define INT8_MIN -128 /* s8 占用1个byte,数据范围 -2^7 到 (2^7-1) */& u( L; {1 ~: y" J( H! A
- & P# T' c2 R3 j9 i$ a, m3 {
- #define INT16_MIN -32768 /* s16 占用2个byte,数据范围 -2^15 到 (2^15-1) */; d0 `$ W1 Y, @8 K: X9 J
- , i& w/ i0 O, L/ Q
- #define INT32_MIN (~0x7fffffff) /* -2147483648 is unsigned s32 占用 4个byte,数据范围 -2^31 到 (2^31-1) */, K8 B; @' {3 Q2 C" ~, G8 ?0 i
- 5 z z3 [% q" d3 n8 H
- #define INT64_MIN __ESCAPE__(~0x7fffffffffffffffll) /* -9223372036854775808 is unsigned int64_t占用8个byte,数据范围 -2^63 到 (2^63-1) */" Q1 j0 G7 I0 A3 A
- 6 V% l S; |/ r$ N/ C) o$ ]
- + ^& X1 S7 V5 ]6 E8 \* E3 J/ }' k. a
- /* maximum values of exact-width signed integer types */4 f% C; ^/ Z5 Z$ s/ D; G6 ]% X. |
- ! l- @1 Y) b8 W2 s% o' B
- #define INT8_MAX 1270 y9 V, ?% S$ G s# Y8 y
- 7 e n: O& ~; C) x4 ?7 j( N
- #define INT16_MAX 32767
" ?3 |2 o2 x" H8 u f - ' W9 a. A+ \9 l! X( D
- #define INT32_MAX 2147483647: m/ B; a+ A% e! ~/ ~1 H
- " G5 f* k) J/ R( w' p0 C& M0 e
- #define INT64_MAX __ESCAPE__(9223372036854775807ll)
) ?( m3 b2 ]9 i -
+ g- N- G! G z( s% {( Q" G2 v -
/ J! w/ a. U8 _- |) u! I C - /* maximum values of exact-width unsigned integer types */
# a9 P i0 |8 B - + W2 B+ U) L d
- #define UINT8_MAX 255 /* u8 占用1个byte, 数据范围 0 - 2^8*/& L f/ \% h4 k0 Y; R
- # S$ v9 G# D0 g; ] s/ R1 r7 [$ _
- #define UINT16_MAX 65535 /* u16 占用2个byte, 数据范围 0 - 2^16*/
2 ]" Q. U0 B! W3 K; p; t4 @5 q7 G% R -
; M- z$ ^9 I+ s: H6 F - #define UINT32_MAX 4294967295u /* u32 占用4个byte, 数据范围 0 - 2^32*/
( s- x8 `8 ^ G* U$ ~9 E -
! ]$ k+ T# a+ q [1 W - #define UINT64_MAX __ESCAPE__(18446744073709551615ull)
复制代码
4 Z- |( W8 E& O+ V2 G* b' U. Y7 S/ O5 l( h
由上述可知: 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
1 v5 {* X: E+ i% P( [/ N
' j* B5 c1 H9 [! M4 L& c# l" ? B8 `$ X, @: f
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, @ ]6 s, [4 ~: j
3 M* @6 F' ]# i6 G* S
' U( v# N9 X6 n! E( G1 j
3、浮点型 - float ——4个byte,有符号型,可以表达负数/小数; Float 类型至少要能精确表示到小数点后6位。
- double——8个byte,有符号型,可以表达负数/小数;Double 类型至少要能精确到小数点后 10 位。1 \3 k3 S! N1 ^: C, Y0 ~
; v& t! M) @! E! r7 V% F
+ C+ ]! G: K, n5 e1 [4 S: S转载自:路途…: |) ]3 D1 K" B' ]% G% N4 n
如有侵权请联系删除5 F) V' y' L3 K
& o) e, ?0 S0 i( e) H* M% {4 [7 `2 a6 O6 M. m9 e2 \
@) U% N8 g$ H1 h Y: a |