light_driver.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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, uint8_t OnTime, uint8_t PeriodTime, 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. uint8_t Light_CrlCode = 0;//高4位:控制方式,1-常亮,2-闪烁; 低4位:开关状态,1-开,2-关
  35. static uint32_t FlashTimeCnt = 0;
  36. switch(TailLight_Mode)
  37. {
  38. case MC_TAIL_LIGHT_LAMP_FlASH: //照明闪烁
  39. {
  40. Light_CrlCode = (LightSwitchCode == MC_LightSwitch_ON) ? 0x21 : 0x20;
  41. break;
  42. }
  43. case MC_TAIL_LIGHT_BREAK_FLASH: //刹车闪烁
  44. {
  45. Light_CrlCode = (IsBreakFlag == TRUE) ? 0x21 : 0x20;
  46. break;
  47. }
  48. case MC_TAIL_LIGHT_BREAK: //刹车常亮
  49. {
  50. Light_CrlCode = (IsBreakFlag == TRUE) ? 0x11 : 0x10;
  51. break;
  52. }
  53. case MC_TAIL_LIGHT_LAMP: //照明常亮
  54. default: //未配置时为照明常亮
  55. {
  56. Light_CrlCode = (LightSwitchCode == MC_LightSwitch_ON) ? 0x11 : 0x10;
  57. break;
  58. }
  59. }
  60. if((Light_CrlCode & 0xF0) == 0x10)//常亮
  61. {
  62. if((Light_CrlCode & 0x0F) == 0x01)//开
  63. {
  64. HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_SET);
  65. }
  66. else if((Light_CrlCode & 0x0F) == 0x00)//关
  67. {
  68. HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_RESET);
  69. }
  70. }
  71. else if((Light_CrlCode & 0xF0) == 0x20)//闪烁
  72. {
  73. if((Light_CrlCode & 0x0F) == 0x01)//开
  74. {
  75. if((HAL_GetTick() - FlashTimeCnt) < OnTime * 20)
  76. {
  77. HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_SET);
  78. }
  79. else if((HAL_GetTick() - FlashTimeCnt) < PeriodTime * 20)
  80. {
  81. HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_RESET);
  82. }
  83. else
  84. {
  85. FlashTimeCnt = HAL_GetTick();
  86. }
  87. }
  88. else if((Light_CrlCode & 0x0F) == 0x00)//关
  89. {
  90. HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_RESET);
  91. FlashTimeCnt = HAL_GetTick();
  92. }
  93. }
  94. }