light_driver.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. #include "light_driver.h"
  2. void LightDriver_Init(void)
  3. {
  4. GPIO_InitTypeDef GPIO_InitStruct;
  5. /* GPIO Ports Clock Enable */
  6. __HAL_RCC_GPIOA_CLK_ENABLE();
  7. __HAL_RCC_GPIOB_CLK_ENABLE();
  8. /*Configure GPIO pin Output Level */
  9. HAL_GPIO_WritePin(LIGHT_F_GPIO_Port, LIGHT_F_Pin, GPIO_PIN_RESET);
  10. HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_RESET);
  11. /*Configure GPIO pins : PBPin PBPin */
  12. GPIO_InitStruct.Pin = LIGHT_F_Pin;
  13. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  14. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  15. HAL_GPIO_Init(LIGHT_F_GPIO_Port, &GPIO_InitStruct);
  16. /*Configure GPIO pins : PAPin PAPin */
  17. GPIO_InitStruct.Pin = LIGHT_B_Pin;
  18. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  19. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  20. HAL_GPIO_Init(LIGHT_B_GPIO_Port, &GPIO_InitStruct);
  21. }
  22. void LightDriver_Process(MC_TailLight_Mode_Struct_t TailLight_Mode, TrueOrFalse_Flag_Struct_t IsBreakFlag, MC_LightSwitch_Struct_t LightSwitchCode)
  23. {
  24. //存在故障,车速为0时,前后灯闪烁,频率可粗略估计故障码
  25. if((MC_ErrorCode.Code != 0) && (MC_RunInfo.BikeSpeed == 0))
  26. {
  27. static uint32_t FlashTimeCnt = 0;
  28. uint16_t OnTime = 0;
  29. uint8_t ErrorBit = 0;
  30. for(ErrorBit = 0; ErrorBit < 32; ErrorBit++)
  31. {
  32. if(((MC_ErrorCode.Code >> ErrorBit) & 0x00000001) != 0)
  33. {
  34. OnTime = (ErrorBit + 1) * 4500;//300ms间隔
  35. break;
  36. }
  37. }
  38. if(FlashTimeCnt < OnTime)
  39. {
  40. HAL_GPIO_WritePin(LIGHT_F_GPIO_Port, LIGHT_F_Pin, GPIO_PIN_SET);
  41. HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_SET);
  42. }
  43. else if(FlashTimeCnt < 150000)//周期10s
  44. {
  45. HAL_GPIO_WritePin(LIGHT_F_GPIO_Port, LIGHT_F_Pin, GPIO_PIN_RESET);
  46. HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_RESET);
  47. }
  48. else
  49. {
  50. FlashTimeCnt = 0;
  51. }
  52. FlashTimeCnt++;
  53. return;
  54. }
  55. //控制前灯
  56. if(LightSwitchCode == MC_LightSwitch_ON)
  57. {
  58. HAL_GPIO_WritePin(LIGHT_F_GPIO_Port, LIGHT_F_Pin, GPIO_PIN_SET);
  59. }
  60. else if(LightSwitchCode == MC_LightSwitch_OFF)
  61. {
  62. HAL_GPIO_WritePin(LIGHT_F_GPIO_Port, LIGHT_F_Pin, GPIO_PIN_RESET);
  63. }
  64. //控制尾灯
  65. static uint16_t FlashTimeCnt = 0;
  66. uint16_t OnTime = 8;
  67. uint16_t Period = 15;
  68. switch(TailLight_Mode)
  69. {
  70. case MC_TAIL_LIGHT_MODE1://连接尾灯,开灯时低亮,刹车时高亮
  71. {
  72. if(IsBreakFlag == TRUE)//刹车时,高亮
  73. {
  74. HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_SET);
  75. FlashTimeCnt = 0;
  76. }
  77. else//无刹车时,开灯低亮
  78. {
  79. if(LightSwitchCode == MC_LightSwitch_ON)
  80. {
  81. if(FlashTimeCnt < OnTime)
  82. {
  83. HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_SET);
  84. }
  85. else if(FlashTimeCnt < Period) //控制周期:15K / Period
  86. {
  87. HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_RESET);
  88. }
  89. else
  90. {
  91. FlashTimeCnt = 0;
  92. }
  93. FlashTimeCnt++;
  94. }
  95. else if(LightSwitchCode == MC_LightSwitch_OFF)
  96. {
  97. HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_RESET);
  98. FlashTimeCnt = 0;
  99. }
  100. }
  101. break;
  102. }
  103. case MC_TAIL_LIGHT_MODE2: default://连接尾灯,开灯时高亮,刹车时闪烁
  104. {
  105. if(IsBreakFlag == TRUE)//刹车时,快闪
  106. {
  107. if(FlashTimeCnt < 600)
  108. {
  109. HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_SET);
  110. }
  111. else if(FlashTimeCnt < 1200) //闪烁频率:15K / 1200 = 12.5Hz
  112. {
  113. HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_RESET);
  114. }
  115. else
  116. {
  117. FlashTimeCnt = 0;
  118. }
  119. FlashTimeCnt++;
  120. }
  121. else//无刹车时,开灯亮
  122. {
  123. if(LightSwitchCode == MC_LightSwitch_ON)
  124. {
  125. HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_SET);
  126. }
  127. else if(LightSwitchCode == MC_LightSwitch_OFF)
  128. {
  129. HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_RESET);
  130. }
  131. FlashTimeCnt = 0;
  132. }
  133. break;
  134. }
  135. case MC_TAIL_LIGHT_MODE3://连接刹车灯,刹车高亮
  136. {
  137. if(IsBreakFlag == TRUE)//刹车时,高亮
  138. {
  139. HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_SET);
  140. }
  141. else//无刹车时,关闭
  142. {
  143. HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_RESET);
  144. }
  145. break;
  146. }
  147. case MC_TAIL_LIGHT_MODE4://连接刹车灯,刹车闪烁
  148. {
  149. if(IsBreakFlag == TRUE)//刹车时,快闪
  150. {
  151. if(FlashTimeCnt < 600)
  152. {
  153. HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_SET);
  154. }
  155. else if(FlashTimeCnt < 1200) //闪烁频率:15K / 1200 = 12.5Hz
  156. {
  157. HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_RESET);
  158. }
  159. else
  160. {
  161. FlashTimeCnt = 0;
  162. }
  163. FlashTimeCnt++;
  164. }
  165. else//无刹车时,关闭
  166. {
  167. HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_RESET);
  168. FlashTimeCnt = 0;
  169. }
  170. break;
  171. }
  172. }
  173. }