Sau nhiều năm làm dự án hôm nay mình mới có dịp chia sẻ kiến thức !

Đối với STM32F4 GPIO

Giới thiệu GPIO

GPIO (General-purpose input/output) hiểu nôm na là đầu vào – đầu ra sử dụng chung.

– Đối với các dòng STM32 thì mỗi port có 16 chân IO

– Ngoài chức năng IO muốn sử dụng ngoại vi phải thiết lập chân đó là Alternate hoặc Analog.

– Mỗi chân IO bên trong chip đều gắn thêm điện trở nội pull up và pull down

Về phần cứng các bạn nên tự thiết kế cho mình sẽ tốt

Đầu tiên cấu hình cho cổng hoạt động

cấu hình input

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);

Cấu hình output

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
2
3
4
5
6
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13| GPIO_Pin_14| GPIO_Pin_15 | GPIO_Pin_5;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOD, &GPIO_InitStructure);
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
/**
  ******************************************************************************
  * @file    External LED
  * @author  Lee Dyche – Tecsploit.com
  * @version V1.0.0
  * @date    11/11/2012
  * @brief   External LED Blinks on and off, along with board leds that blink in a cirle.
  * Based on the hello world example with a few more lines of code!
  ******************************************************************************
  * @attention
  * This is program is provided as is with no warranty!!
  ******************************************************************************
  */
/* Includes ——————————————————————*/
#include “stm32f4_discovery.h”
/* Private typedef ———————————————————–*/
GPIO_InitTypeDef  GPIO_InitStructure;
/* Private define ————————————————————*/
/* Private macro ————————————————————-*/
/* Private variables ———————————————————*/
/* Private function prototypes ———————————————–*/
void Delay(__IO uint32_t nCount);
/* Private functions ———————————————————*/
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /*!< At this stage the microcontroller clock setting is already configured,
       this is done through SystemInit() function which is called from startup
       file (startup_stm32f4xx.s) before to branch to application main.
       To reconfigure the default setting of SystemInit() function, refer to
        system_stm32f4xx.c file
     */
  /* GPIOD Periph clock enable */
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
  /* Configure PD12, PD13, PD14,PD15 and PD5 in output pushpull mode */
  /* Note that PD12,13,14 and 15 relate to the on board LEDs, pin PD5 maps to the Pin labelled PD5 on the
   * STM board, and this is referrenced in the code below using GPIO_Pin_5 (refering to pin 5). When we actually
   * toggle the pin using GPIO_SetBits is when we specify the D part using GPIOD ie GPIO_SetBits(GPIOD, GPIO_Pin_5);
   **/
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13| GPIO_Pin_14| GPIO_Pin_15 | GPIO_Pin_5;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_Init(GPIOD, &GPIO_InitStructure);
  while (1)
  {
/* PD5 to be toggled – this is your extenal pin */
GPIO_SetBits(GPIOD, GPIO_Pin_5);
/* Insert delay */
Delay(0x3FFFFF);
    /* PD12 to be toggled */
    GPIO_SetBits(GPIOD, GPIO_Pin_12);
    /* Insert delay */
    Delay(0x3FFFFF);
    /* PD13 to be toggled */
    GPIO_SetBits(GPIOD, GPIO_Pin_13);
    /* Insert delay */
    Delay(0x3FFFFF);
    /* PD14 to be toggled */
    GPIO_SetBits(GPIOD, GPIO_Pin_14);
    /* Insert delay */
    Delay(0x3FFFFF);
    /* PD15 to be toggled */
    GPIO_SetBits(GPIOD, GPIO_Pin_15);
    /* Insert delay */
    Delay(0x7FFFFF);
    // This line resets the pins, turning off the LED’s and setting the voltage on pin PD55 to 0
    GPIO_ResetBits(GPIOD, GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15 | GPIO_Pin_5);
    /* Insert delay */
    Delay(0xFFFFFF);
  }
}
/**
  * @brief  Delay Function.
  * @param  nCount:specifies the Delay time length.
  * @retval None
  */
void Delay(__IO uint32_t nCount)
{
  while(nCount)
  {
  }
}
#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{
  /* User can add his own implementation to report the file name and line number,
     ex: printf(“Wrong parameters value: file %s on line %d\r\n”, file, line) */
  /* Infinite loop */
  while (1)
  {
  }
}
#endif
/**
  * @}
  */
/**
  * @}
  */
về code bạn có thể liên hệ với linhkienagv.com
gửi mail đến:buiprohd@gmail.com

Bình luận

Bình luận