light_driver.c 3.6 KB

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