GPIO.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #include "main.h"
  2. static void GPIO_Config(void);
  3. static void GPIO_Set(uint8_t who);
  4. static void GPIO_Reset(uint8_t who);
  5. static void GPIO_Toggle(uint8_t who);
  6. static uint8_t GPIO_Get(uint8_t who);
  7. GPIOClassStruct GPIOClass = {
  8. .Init = GPIO_Config,
  9. .Set = GPIO_Set,
  10. .Reset = GPIO_Reset,
  11. .Toggle = GPIO_Toggle,
  12. .Read = GPIO_Get
  13. };
  14. // LEDRun
  15. #define LEDRun_GPIO_PORT GPIOB /* GPIO端口 */
  16. #define LEDRun_GPIO_CLK RCC_APB2Periph_GPIOB /* GPIO端口时钟 */
  17. #define LEDRun_GPIO_PIN GPIO_Pin_8 /* 连接到SCL时钟线的GPIO */
  18. #define LEDRun_GPIO_TOGGLE LEDRun_GPIO_PORT->BSRR = ((LEDRun_GPIO_PORT->ODR & LEDRun_GPIO_PIN) << 16) | (~LEDRun_GPIO_PORT->ODR & LEDRun_GPIO_PIN)
  19. #define LEDRun_GPIO_SET LEDRun_GPIO_PORT->BSRR = (uint32_t)LEDRun_GPIO_PIN // 置1
  20. #define LEDRun_GPIO_RESET LEDRun_GPIO_PORT->BSRR = (uint32_t)LEDRun_GPIO_PIN<<16 // 置0
  21. #define LEDRun_GPIO_READ ((LEDRun_GPIO_PORT->IDR & LEDRun_GPIO_PIN) != 0)
  22. // LED1
  23. #define LED1_GPIO_PORT GPIOA /* GPIO端口 */
  24. #define LED1_GPIO_CLK RCC_APB2Periph_GPIOA /* GPIO端口时钟 */
  25. #define LED1_GPIO_PIN GPIO_Pin_6 /* 连接到SCL时钟线的GPIO */
  26. #define LED1_GPIO_TOGGLE LED1_GPIO_PORT->BSRR = ((LED1_GPIO_PORT->ODR & LED1_GPIO_PIN) << 16) | (~LED1_GPIO_PORT->ODR & LED1_GPIO_PIN)
  27. #define LED1_GPIO_SET LED1_GPIO_PORT->BSRR = (uint32_t)LED1_GPIO_PIN // 置1
  28. #define LED1_GPIO_RESET LED1_GPIO_PORT->BSRR = (uint32_t)LED1_GPIO_PIN<<16 // 置0
  29. #define LED1_GPIO_READ ((LED1_GPIO_PORT->IDR & LED1_GPIO_PIN) != 0)
  30. // LED2
  31. #define LED2_GPIO_PORT GPIOA /* GPIO端口 */
  32. #define LED2_GPIO_CLK RCC_APB2Periph_GPIOA /* GPIO端口时钟 */
  33. #define LED2_GPIO_PIN GPIO_Pin_5 /* 连接到SCL时钟线的GPIO */
  34. #define LED2_GPIO_TOGGLE LED2_GPIO_PORT->BSRR = ((LED2_GPIO_PORT->ODR & LED2_GPIO_PIN) << 16) | (~LED2_GPIO_PORT->ODR & LED2_GPIO_PIN)
  35. #define LED2_GPIO_SET LED2_GPIO_PORT->BSRR = (uint32_t)LED2_GPIO_PIN // 置1
  36. #define LED2_GPIO_RESET LED2_GPIO_PORT->BSRR = (uint32_t)LED2_GPIO_PIN<<16 // 置0
  37. #define LED2_GPIO_READ ((LED2_GPIO_PORT->IDR & LED2_GPIO_PIN) != 0)
  38. // LED3
  39. #define LED3_GPIO_PORT GPIOA /* GPIO端口 */
  40. #define LED3_GPIO_CLK RCC_APB2Periph_GPIOA /* GPIO端口时钟 */
  41. #define LED3_GPIO_PIN GPIO_Pin_4 /* 连接到SCL时钟线的GPIO */
  42. #define LED3_GPIO_TOGGLE LED3_GPIO_PORT->BSRR = ((LED3_GPIO_PORT->ODR & LED3_GPIO_PIN) << 16) | (~LED3_GPIO_PORT->ODR & LED3_GPIO_PIN)
  43. #define LED3_GPIO_SET LED3_GPIO_PORT->BSRR = (uint32_t)LED3_GPIO_PIN // 置1
  44. #define LED3_GPIO_RESET LED3_GPIO_PORT->BSRR = (uint32_t)LED3_GPIO_PIN<<16 // 置0
  45. #define LED3_GPIO_READ ((LED3_GPIO_PORT->IDR & LED3_GPIO_PIN) != 0)
  46. /**
  47. * @brief IO配置
  48. * @param None
  49. * @retval None
  50. */
  51. static void GPIO_Config(void)
  52. {
  53. /*定义一个GPIO_InitTypeDef类型的结构体*/
  54. GPIO_InitTypeDef GPIO_InitStructure;
  55. /*开启LED相关的GPIO外设时钟*/
  56. RCC_APB2PeriphClockCmd(LEDRun_GPIO_CLK, ENABLE);
  57. GPIO_InitStructure.GPIO_Pin = LEDRun_GPIO_PIN;
  58. /*设置引脚模式为*/
  59. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  60. /*设置引脚速率*/
  61. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  62. /*调用库函数,初始化GPIO*/
  63. GPIO_Init(LEDRun_GPIO_PORT, &GPIO_InitStructure);
  64. /*关闭*/
  65. LEDRun_GPIO_SET;
  66. RCC_APB2PeriphClockCmd(LED1_GPIO_CLK, ENABLE);
  67. GPIO_InitStructure.GPIO_Pin = LED1_GPIO_PIN | LED2_GPIO_PIN | LED3_GPIO_PIN;
  68. GPIO_Init(LED1_GPIO_PORT, &GPIO_InitStructure);
  69. LED1_GPIO_SET;
  70. LED2_GPIO_SET;
  71. LED3_GPIO_SET;
  72. }
  73. /**
  74. * @brief IO置高
  75. * @param None
  76. * @retval None
  77. */
  78. static void GPIO_Set(uint8_t who)
  79. {
  80. switch(who)
  81. {
  82. case LED_Run: LEDRun_GPIO_SET;break;
  83. case LED1: LED1_GPIO_SET;break;
  84. case LED2: LED2_GPIO_SET;break;
  85. case LED3: LED3_GPIO_SET;break;
  86. }
  87. }
  88. /**
  89. * @brief IO置低
  90. * @param None
  91. * @retval None
  92. */
  93. static void GPIO_Reset(uint8_t who)
  94. {
  95. switch(who)
  96. {
  97. case LED_Run: LEDRun_GPIO_RESET;break;
  98. case LED1: LED1_GPIO_RESET;break;
  99. case LED2: LED2_GPIO_RESET;break;
  100. case LED3: LED3_GPIO_RESET;break;
  101. }
  102. }
  103. /**
  104. * @brief IO翻转
  105. * @param None
  106. * @retval None
  107. */
  108. static void GPIO_Toggle(uint8_t who)
  109. {
  110. switch(who)
  111. {
  112. case LED_Run: LEDRun_GPIO_TOGGLE;break;
  113. case LED1: LED1_GPIO_TOGGLE;break;
  114. case LED2: LED2_GPIO_TOGGLE;break;
  115. case LED3: LED3_GPIO_TOGGLE;break;
  116. }
  117. }
  118. /**
  119. * @brief IO读取
  120. * @param None
  121. * @retval None
  122. */
  123. static uint8_t GPIO_Get(uint8_t who)
  124. {
  125. uint8_t temp = 0;
  126. switch(who)
  127. {
  128. case LED_Run: temp = LEDRun_GPIO_READ;break;
  129. case LED1: temp = LED1_GPIO_READ;break;
  130. case LED2: temp = LED2_GPIO_READ;break;
  131. case LED3: temp = LED3_GPIO_READ;break;
  132. }
  133. return temp;
  134. }