gpio.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file gpio.c
  5. * @brief This file provides code for the configuration
  6. * of all used GPIO pins.
  7. ******************************************************************************
  8. * @attention
  9. *
  10. * Copyright (c) 2023 STMicroelectronics.
  11. * All rights reserved.
  12. *
  13. * This software is licensed under terms that can be found in the LICENSE file
  14. * in the root directory of this software component.
  15. * If no LICENSE file comes with this software, it is provided AS-IS.
  16. *
  17. ******************************************************************************
  18. */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "gpio.h"
  22. /* USER CODE BEGIN 0 */
  23. /* USER CODE END 0 */
  24. /*----------------------------------------------------------------------------*/
  25. /* Configure GPIO */
  26. /*----------------------------------------------------------------------------*/
  27. /* USER CODE BEGIN 1 */
  28. /* USER CODE END 1 */
  29. /** Configure pins as
  30. * Analog
  31. * Input
  32. * Output
  33. * EVENT_OUT
  34. * EXTI
  35. * Free pins are configured automatically as Analog (this feature is enabled through
  36. * the Code Generation settings)
  37. */
  38. void MX_GPIO_Init(void)
  39. {
  40. GPIO_InitTypeDef GPIO_InitStruct = {0};
  41. /* GPIO Ports Clock Enable */
  42. __HAL_RCC_GPIOC_CLK_ENABLE();
  43. __HAL_RCC_GPIOD_CLK_ENABLE();
  44. __HAL_RCC_GPIOA_CLK_ENABLE();
  45. __HAL_RCC_GPIOB_CLK_ENABLE();
  46. /*Configure GPIO pin Output Level */
  47. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0|GPIO_PIN_12|GPIO_PIN_3|GPIO_PIN_4
  48. |GPIO_PIN_5|GPIO_PIN_6, GPIO_PIN_RESET);
  49. /*Configure GPIO pins : PC13 PC14 PC15 PC3
  50. PC4 PC5 PC6 PC7
  51. PC8 PC9 PC10 PC11
  52. PC12 */
  53. GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15|GPIO_PIN_3
  54. |GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7
  55. |GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11
  56. |GPIO_PIN_12;
  57. GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  58. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  59. /*Configure GPIO pins : PA0 PA1 PA2 PA3
  60. PA4 PA5 PA6 PA7 */
  61. GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3
  62. |GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;
  63. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  64. GPIO_InitStruct.Pull = GPIO_NOPULL;
  65. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  66. /*Configure GPIO pins : PB0 PB12 PB3 PB4
  67. PB5 PB6 */
  68. GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_12|GPIO_PIN_3|GPIO_PIN_4
  69. |GPIO_PIN_5|GPIO_PIN_6;
  70. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  71. GPIO_InitStruct.Pull = GPIO_NOPULL;
  72. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  73. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  74. /*Configure GPIO pins : PB1 PB2 PB13 PB14
  75. PB15 PB7 PB8 PB9 */
  76. GPIO_InitStruct.Pin = GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_13|GPIO_PIN_14
  77. |GPIO_PIN_15|GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9;
  78. GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  79. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  80. /*Configure GPIO pins : PA8 PA11 PA12 PA15 */
  81. GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_15;
  82. GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  83. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  84. /*Configure GPIO pin : PD2 */
  85. GPIO_InitStruct.Pin = GPIO_PIN_2;
  86. GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  87. HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  88. }
  89. /* USER CODE BEGIN 2 */
  90. // 定义GPIO_Pin_t结构体
  91. typedef struct {
  92. GPIO_TypeDef* port;
  93. uint16_t pin;
  94. } GPIO_Pin_t;
  95. // 定义8个需要处理的引脚
  96. const GPIO_Pin_t GPIO_PINS[8] = {
  97. {GPIOA, GPIO_PIN_0},
  98. {GPIOA, GPIO_PIN_1},
  99. {GPIOA, GPIO_PIN_2},
  100. {GPIOA, GPIO_PIN_3},
  101. {GPIOA, GPIO_PIN_4},
  102. {GPIOA, GPIO_PIN_5},
  103. {GPIOA, GPIO_PIN_6},
  104. {GPIOA, GPIO_PIN_7}
  105. };
  106. // 读取引脚状态的函数
  107. uint8_t ReadPinStatus(GPIO_TypeDef* port, uint16_t pin) {
  108. return HAL_GPIO_ReadPin(port, pin) == GPIO_PIN_RESET;
  109. }
  110. // 获取输入状态的函数
  111. uint8_t GetPaInputStatus(void)
  112. {
  113. uint8_t input = 0;
  114. // 循环处理8个引脚
  115. for (uint8_t i = 0; i < 8; i++) {
  116. uint16_t count = 0;
  117. // 读取引脚状态,并进行去抖处理
  118. for (uint8_t j = 0; j < DEBOUNCE_COUNT; j++) {
  119. count += ReadPinStatus(GPIO_PINS[i].port, GPIO_PINS[i].pin);
  120. }
  121. // 将处理结果存储到input变量中
  122. if(count >= 5){
  123. input |= 1 << i;
  124. }else {
  125. input |= 0 << i;
  126. }
  127. }
  128. return input;
  129. }
  130. uint8_t GetPbOutputStatus(void)
  131. {
  132. uint8_t gpio_odr_byte = (GPIOB->ODR >> 3) & 0x0F; // 读取GPIOB的3-6位的状态
  133. return gpio_odr_byte;
  134. }
  135. void updatePbStatus(uint16_t temp)
  136. {
  137. if(temp == UINT16_MAX) {
  138. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, GPIO_PIN_RESET);
  139. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, GPIO_PIN_RESET);
  140. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, GPIO_PIN_RESET);
  141. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, GPIO_PIN_RESET);
  142. } else {
  143. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_3, ((temp & 0x01) == 0x01) ? GPIO_PIN_SET : GPIO_PIN_RESET);
  144. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, ((temp & 0x02) == 0x02) ? GPIO_PIN_SET : GPIO_PIN_RESET);
  145. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_5, ((temp & 0x04) == 0x04) ? GPIO_PIN_SET : GPIO_PIN_RESET);
  146. HAL_GPIO_WritePin(GPIOB, GPIO_PIN_6, ((temp & 0x08) == 0x08) ? GPIO_PIN_SET : GPIO_PIN_RESET);
  147. }
  148. }
  149. // 计算CRC校验值
  150. uint16_t calculate_crc(uint8_t* buffer, uint8_t length)
  151. {
  152. uint16_t crc = 0xFFFF;
  153. for (int i = 0; i < length; i++) {
  154. crc ^= buffer[i];
  155. for (int j = 0; j < 8; j++) {
  156. if (crc & 0x0001) {
  157. crc = (crc >> 1) ^ 0xA001;
  158. } else {
  159. crc >>= 1;
  160. }
  161. }
  162. }
  163. return crc;
  164. }
  165. /* USER CODE END 2 */