注意 ,本文的內容過於老舊,不建議實際使用,僅保留以作為參考用。
前言 在先前的[STM32學習記錄-3] 基本輸入與輸出教學-GPIO相關程式寫法 中已經介紹過STM32設定GPIO的相關函式,但如果是常用Arduion的人一定不太習慣這種設定方式,畢竟每設定一個GPIO就要打4行程式,雖然可以複製貼上,但程式一多難免看起來混亂複雜,所以我自己寫了一些GPIO相關的函式,方便自己未來使用。
GPIO模式設定函式 程式如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 #include "GPIO_mapping.h" #define OUT (0) #define IN (1) #define GPPP (0) #define GPOD (1) #define AFPP (2) #define AFOD (3) #define FL (0) #define AN (1) #define PD (2) #define PU (3) #define S2M (2) #define S10M (10) #define S50M (50) void Pin_Mod (u8 PortPin, u8 INout, u8 Mode, u8 Speed) { GPIO_InitTypeDef GPIO_InitStructure; switch (Speed) { case S2M: GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; break ; case S10M: GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz; break ; case S50M: GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; break ; default : break ; } if (INout == OUT) { switch (Mode) { case GPPP: GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; break ; case GPOD: GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD; break ; case AFPP: GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; break ; case AFOD: GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD; break ; default : break ; } } else if (INout == IN) { switch (Mode) { case FL: GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; break ; case AN: GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; break ; case PD: GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; break ; case PU: GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; break ; default : break ; } } if (PortPin <= 15 ) { GPIO_InitStructure.GPIO_Pin = ((uint16_t )(0x0001 << PortPin)); GPIO_Init(GPIOA, &GPIO_InitStructure); } else if (PortPin <= 31 ) { GPIO_InitStructure.GPIO_Pin = ((uint16_t )(0x0001 << (PortPin - 16 ))); GPIO_Init(GPIOB, &GPIO_InitStructure); } else if (PortPin <= 47 ) { GPIO_InitStructure.GPIO_Pin = ((uint16_t )(0x0001 << (PortPin - 32 ))); GPIO_Init(GPIOC, &GPIO_InitStructure); } else if (PortPin <= 63 ) { GPIO_InitStructure.GPIO_Pin = ((uint16_t )(0x0001 << (PortPin - 48 ))); GPIO_Init(GPIOD, &GPIO_InitStructure); } else if (PortPin <= 79 ) { GPIO_InitStructure.GPIO_Pin = ((uint16_t )(0x0001 << (PortPin - 64 ))); GPIO_Init(GPIOE, &GPIO_InitStructure); } else ; }#undef OUT #undef IN #undef GPPP #undef GPOD #undef AFPP #undef AFOD #undef FL #undef AN #undef PD #undef PU #undef S2M #undef S10M #undef S50M
可以看到裡面就是增加了“void Pin_Mod(u8 PortPin, u8 INout, u8 Mode, u8 Speed)”此一函式。其用法如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 Pin_Mod(PA5, OUT, GPPP, S2M); Pin_Mod(PC13, IN, FL, S2M); Pin_Mod(PA2, OUT, AFPP, S50M); Pin_Mod(PA3, IN, FL, S50M);
這樣的話就可以更簡單方便地設定GPIO了。
至於其引入的“GPIO_mapping.h”內容如下:(只適用於STM32F103RB,請依照自己的MCU腳位更改)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 #ifndef __GPIO_MAPPING_H #define __GPIO_MAPPING_H #define PA0 (0) #define PA1 (1) #define PA2 (2) #define PA3 (3) #define PA4 (4) #define PA5 (5) #define PA6 (6) #define PA7 (7) #define PA8 (8) #define PA9 (9) #define PA10 (10) #define PA11 (11) #define PA12 (12) #define PA13 (13) #define PA14 (14) #define PA15 (15) #define PB0 (16) #define PB1 (17) #define PB2 (18) #define PB3 (19) #define PB4 (20) #define PB5 (21) #define PB6 (22) #define PB7 (23) #define PB8 (24) #define PB9 (25) #define PB10 (26) #define PB11 (27) #define PB12 (28) #define PB13 (29) #define PB14 (30) #define PB15 (31) #define PC0 (32) #define PC1 (33) #define PC2 (34) #define PC3 (35) #define PC4 (36) #define PC5 (37) #define PC6 (38) #define PC7 (39) #define PC8 (40) #define PC9 (41) #define PC10 (42) #define PC11 (43) #define PC12 (44) #define PC13 (45) #define PC14 (46) #define PC15 (47) #define PD0 (48) #define PD1 (49) #define PD2 (50) #define PD3 (51) #define PD4 (52) #define PD5 (53) #define PD6 (54) #define PD7 (55) #define PD8 (56) #define PD9 (57) #define PD10 (58) #define PD11 (59) #define PD12 (60) #define PD13 (61) #define PD14 (62) #define PD15 (63) #define PE0 (64) #define PE1 (65) #define PE2 (66) #define PE3 (67) #define PE4 (68) #define PE5 (69) #define PE6 (70) #define PE7 (71) #define PE8 (72) #define PE9 (73) #define PE10 (74) #define PE11 (75) #define PE12 (76) #define PE13 (77) #define PE14 (78) #define PE15 (79) #define A0 (0) #define A1 (1) #define A2 (4) #define A3 (16) #define A4 (33) #define A5 (32) #define D0 (3) #define D1 (2) #define D2 (10) #define D3 (19) #define D4 (21) #define D5 (20) #define D6 (26) #define D7 (8) #define D8 (9) #define D9 (39) #define D10 (22) #define D11 (7) #define D12 (6) #define D13 (5) #define D14 (25) #define D15 (24) #define Button_User (45) #define B1 (45) #define LED_User (5) #define LD2 (5) #endif
結語 這次簡單地分享了自己打的程式,不敢說自己的程式很完善、漂亮,但希望它有幫助到你。 如有問題或錯誤也歡迎提出討論!
注意 ,本文的內容過於老舊,不建議實際使用,僅保留以作為參考用。