gpio.c 5.9 KB

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