light_driver.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. //控制前灯
  25. if(LightSwitchCode == MC_LightSwitch_ON)
  26. {
  27. HAL_GPIO_WritePin(LIGHT_F_GPIO_Port, LIGHT_F_Pin, GPIO_PIN_SET);
  28. }
  29. else if(LightSwitchCode == MC_LightSwitch_OFF)
  30. {
  31. HAL_GPIO_WritePin(LIGHT_F_GPIO_Port, LIGHT_F_Pin, GPIO_PIN_RESET);
  32. }
  33. //控制尾灯
  34. static uint16_t FlashTimeCnt = 0;
  35. uint16_t OnTime = 8;
  36. uint16_t Period = 15;
  37. switch(TailLight_Mode)
  38. {
  39. case MC_TAIL_LIGHT_MODE1://连接尾灯,开灯时低亮,刹车时高亮
  40. {
  41. if(IsBreakFlag == TRUE)//刹车时,高亮
  42. {
  43. HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_SET);
  44. FlashTimeCnt = 0;
  45. }
  46. else//无刹车时,开灯低亮
  47. {
  48. if(LightSwitchCode == MC_LightSwitch_ON)
  49. {
  50. if(FlashTimeCnt < OnTime)
  51. {
  52. HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_SET);
  53. }
  54. else if(FlashTimeCnt < Period) //控制周期:15K / Period
  55. {
  56. HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_RESET);
  57. }
  58. else
  59. {
  60. FlashTimeCnt = 0;
  61. }
  62. FlashTimeCnt++;
  63. }
  64. else if(LightSwitchCode == MC_LightSwitch_OFF)
  65. {
  66. HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_RESET);
  67. FlashTimeCnt = 0;
  68. }
  69. }
  70. break;
  71. }
  72. case MC_TAIL_LIGHT_MODE2: default://连接尾灯,开灯时高亮,刹车时闪烁
  73. {
  74. if(IsBreakFlag == TRUE)//刹车时,快闪
  75. {
  76. if(FlashTimeCnt < 600)
  77. {
  78. HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_SET);
  79. }
  80. else if(FlashTimeCnt < 1200) //闪烁频率:15K / 1200 = 12.5Hz
  81. {
  82. HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_RESET);
  83. }
  84. else
  85. {
  86. FlashTimeCnt = 0;
  87. }
  88. FlashTimeCnt++;
  89. }
  90. else//无刹车时,开灯亮
  91. {
  92. if(LightSwitchCode == MC_LightSwitch_ON)
  93. {
  94. HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_SET);
  95. }
  96. else if(LightSwitchCode == MC_LightSwitch_OFF)
  97. {
  98. HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_RESET);
  99. }
  100. FlashTimeCnt = 0;
  101. }
  102. break;
  103. }
  104. case MC_TAIL_LIGHT_MODE3://连接刹车灯,刹车高亮
  105. {
  106. if(IsBreakFlag == TRUE)//刹车时,高亮
  107. {
  108. HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_SET);
  109. }
  110. else//无刹车时,关闭
  111. {
  112. HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_RESET);
  113. }
  114. break;
  115. }
  116. case MC_TAIL_LIGHT_MODE4://连接刹车灯,刹车闪烁
  117. {
  118. if(IsBreakFlag == TRUE)//刹车时,快闪
  119. {
  120. if(FlashTimeCnt < 600)
  121. {
  122. HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_SET);
  123. }
  124. else if(FlashTimeCnt < 1200) //闪烁频率:15K / 1200 = 12.5Hz
  125. {
  126. HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_RESET);
  127. }
  128. else
  129. {
  130. FlashTimeCnt = 0;
  131. }
  132. FlashTimeCnt++;
  133. }
  134. else//无刹车时,关闭
  135. {
  136. HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_RESET);
  137. FlashTimeCnt = 0;
  138. }
  139. break;
  140. }
  141. }
  142. }