stm32中的數據類型定義


STM32F10X.H

 1 #include "core_cm3.h"
 2 #include "system_stm32f10x.h"
 3 #include <stdint.h>
 4 
 5 /** @addtogroup Exported_types
 6   * @{
 7   */  
 8 
 9 /*!< STM32F10x Standard Peripheral Library old types (maintained for legacy purpose) */
10 typedef int32_t  s32;
11 typedef int16_t s16;
12 typedef int8_t  s8;
13 
14 typedef const int32_t sc32;  /*!< Read Only */
15 typedef const int16_t sc16;  /*!< Read Only */
16 typedef const int8_t sc8;   /*!< Read Only */
17 
18 typedef __IO int32_t  vs32;
19 typedef __IO int16_t  vs16;
20 typedef __IO int8_t   vs8;
21 
22 typedef __I int32_t vsc32;  /*!< Read Only */
23 typedef __I int16_t vsc16;  /*!< Read Only */
24 typedef __I int8_t vsc8;   /*!< Read Only */
25 
26 typedef uint32_t  u32;
27 typedef uint16_t u16;
28 typedef uint8_t  u8;
29 
30 typedef const uint32_t uc32;  /*!< Read Only */
31 typedef const uint16_t uc16;  /*!< Read Only */
32 typedef const uint8_t uc8;   /*!< Read Only */
33 
34 typedef __IO uint32_t  vu32;
35 typedef __IO uint16_t vu16;
36 typedef __IO uint8_t  vu8;
37 
38 typedef __I uint32_t vuc32;  /*!< Read Only */
39 typedef __I uint16_t vuc16;  /*!< Read Only */
40 typedef __I uint8_t vuc8;   /*!< Read Only */
41 
42 typedef enum {RESET = 0, SET = !RESET} FlagStatus, ITStatus;
43 
44 typedef enum {DISABLE = 0, ENABLE = !DISABLE} FunctionalState;
45 #define IS_FUNCTIONAL_STATE(STATE) (((STATE) == DISABLE) || ((STATE) == ENABLE))
46 
47 typedef enum {ERROR = 0, SUCCESS = !ERROR} ErrorStatus;

 源定義在#include<stdint.h>中

 1 /*
 2  * 'signed' is redundant below, except for 'signed char' and if
 3  * the typedef is used to declare a bitfield.
 4  */
 5 
 6     /* 7.18.1.1 */
 7 
 8     /* exact-width signed integer types */
 9 typedef   signed          char int8_t;
10 typedef   signed short     int int16_t;
11 typedef   signed           int int32_t;
12 typedef   signed       __INT64 int64_t;
13 
14     /* exact-width unsigned integer types */
15 typedef unsigned          char uint8_t;
16 typedef unsigned short     int uint16_t;
17 typedef unsigned           int uint32_t;
18 typedef unsigned       __INT64 uint64_t;
19 
20     /* 7.18.1.2 */
21 
22     /* smallest type of at least n bits */
23     /* minimum-width signed integer types */
24 typedef   signed          char int_least8_t;
25 typedef   signed short     int int_least16_t;
26 typedef   signed           int int_least32_t;
27 typedef   signed       __INT64 int_least64_t;
28 
29     /* minimum-width unsigned integer types */
30 typedef unsigned          char uint_least8_t;
31 typedef unsigned short     int uint_least16_t;
32 typedef unsigned           int uint_least32_t;
33 typedef unsigned       __INT64 uint_least64_t;
34 
35     /* 7.18.1.3 */
36 
37     /* fastest minimum-width signed integer types */
38 typedef   signed           int int_fast8_t;
39 typedef   signed           int int_fast16_t;
40 typedef   signed           int int_fast32_t;
41 typedef   signed       __INT64 int_fast64_t;
42 
43     /* fastest minimum-width unsigned integer types */
44 typedef unsigned           int uint_fast8_t;
45 typedef unsigned           int uint_fast16_t;
46 typedef unsigned           int uint_fast32_t;
47 typedef unsigned       __INT64 uint_fast64_t;
48 
49     /* 7.18.1.4 integer types capable of holding object pointers */
50 #if __sizeof_ptr == 8
51 typedef   signed       __INT64 intptr_t;
52 typedef unsigned       __INT64 uintptr_t;
53 #else
54 typedef   signed           int intptr_t;
55 typedef unsigned           int uintptr_t;
56 #endif
57 
58     /* 7.18.1.5 greatest-width integer types */
59 typedef   signed     __LONGLONG intmax_t;
60 typedef unsigned     __LONGLONG uintmax_t;

 由上述可知:

 

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

 

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
3、浮點型

 

  •     float ——4個byte,有符號型,可以表達負數/小數; Float 類型至少要能精確表示到小數點后6位。
  •    double——8個byte,有符號型,可以表達負數/小數;Double 類型至少要能精確到小數點后 10 位。

 

二、不同數據類型混合運算

在C語言中,不同類型的數據間是可以混合運算的。在進行運算時,不同類型的數據要先轉換成同一類型,然后進行運算。轉換的規則如下:

注意:箭頭的方向只表示數據類型級別的高低,由低向高轉換,這個轉換過程是一步到位的。

(三)數據類型轉換規則

各類數據類型的轉換,分為兩種方式:隱式(編譯軟件自動完成),顯式(程序強制轉換)

隱式轉換規則:

字符必須先轉換為整數(C語言規定字符類型數據和整型數據之間可以通用) 
   short型轉換為int型(同屬於整型) 
   float型數據在運算時一律轉換為雙精度(double)型,以提高運算精度(同屬於實型) 
   賦值時,一律是右部值轉換為左部類型 
[注] 
     當整型數據和雙精度數據進行運算時,C先將整型數據轉換成雙精度型數據,再進行運算,結果為雙精度類型數據 
     當字符型數據和實型數據進行運算時,C先將字符型數據轉換成實型數據,然后進行計算,結果為實型數據

顯式轉換規則:

例:(int)(x+y);

注:強制類型轉換時,得到一個所需要的中間變量,原來變量的類型未發生變化。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM