123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- #include "light_driver.h"
- void LightDriver_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStruct;
- /* GPIO Ports Clock Enable */
- __HAL_RCC_GPIOA_CLK_ENABLE();
- __HAL_RCC_GPIOB_CLK_ENABLE();
-
- /*Configure GPIO pin Output Level */
- HAL_GPIO_WritePin(LIGHT_F_GPIO_Port, LIGHT_F_Pin, GPIO_PIN_RESET);
- HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_RESET);
-
- /*Configure GPIO pins : PBPin PBPin */
- GPIO_InitStruct.Pin = LIGHT_F_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
- HAL_GPIO_Init(LIGHT_F_GPIO_Port, &GPIO_InitStruct);
-
- /*Configure GPIO pins : PAPin PAPin */
- GPIO_InitStruct.Pin = LIGHT_B_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
- HAL_GPIO_Init(LIGHT_B_GPIO_Port, &GPIO_InitStruct);
- }
- void LightDriver_Process(MC_TailLight_Mode_Struct_t TailLight_Mode, TrueOrFalse_Flag_Struct_t IsBreakFlag, MC_LightSwitch_Struct_t LightSwitchCode)
- {
- //控制前灯
- if(LightSwitchCode == MC_LightSwitch_ON)
- {
- HAL_GPIO_WritePin(LIGHT_F_GPIO_Port, LIGHT_F_Pin, GPIO_PIN_SET);
- }
- else if(LightSwitchCode == MC_LightSwitch_OFF)
- {
- HAL_GPIO_WritePin(LIGHT_F_GPIO_Port, LIGHT_F_Pin, GPIO_PIN_RESET);
- }
-
- //控制尾灯
- static uint16_t FlashTimeCnt = 0;
- uint16_t OnTime = 8;
- uint16_t Period = 15;
- switch(TailLight_Mode)
- {
- case MC_TAIL_LIGHT_MODE1://连接尾灯,开灯时低亮,刹车时高亮
- {
- if(IsBreakFlag == TRUE)//刹车时,高亮
- {
- HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_SET);
- FlashTimeCnt = 0;
- }
- else//无刹车时,开灯低亮
- {
- if(LightSwitchCode == MC_LightSwitch_ON)
- {
- if(FlashTimeCnt < OnTime)
- {
- HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_SET);
- }
- else if(FlashTimeCnt < Period) //控制周期:15K / Period
- {
- HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_RESET);
- }
- else
- {
- FlashTimeCnt = 0;
- }
- FlashTimeCnt++;
- }
- else if(LightSwitchCode == MC_LightSwitch_OFF)
- {
- HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_RESET);
- FlashTimeCnt = 0;
- }
- }
- break;
- }
- case MC_TAIL_LIGHT_MODE2: default://连接尾灯,开灯时高亮,刹车时闪烁
- {
- if(IsBreakFlag == TRUE)//刹车时,快闪
- {
- if(FlashTimeCnt < 600)
- {
- HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_SET);
- }
- else if(FlashTimeCnt < 1200) //闪烁频率:15K / 1200 = 12.5Hz
- {
- HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_RESET);
- }
- else
- {
- FlashTimeCnt = 0;
- }
- FlashTimeCnt++;
- }
- else//无刹车时,开灯亮
- {
- if(LightSwitchCode == MC_LightSwitch_ON)
- {
- HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_SET);
- }
- else if(LightSwitchCode == MC_LightSwitch_OFF)
- {
- HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_RESET);
- }
- FlashTimeCnt = 0;
- }
- break;
- }
- case MC_TAIL_LIGHT_MODE3://连接刹车灯,刹车高亮
- {
- if(IsBreakFlag == TRUE)//刹车时,高亮
- {
- HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_SET);
- }
- else//无刹车时,关闭
- {
- HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_RESET);
- }
- break;
- }
- case MC_TAIL_LIGHT_MODE4://连接刹车灯,刹车闪烁
- {
- if(IsBreakFlag == TRUE)//刹车时,快闪
- {
- if(FlashTimeCnt < 600)
- {
- HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_SET);
- }
- else if(FlashTimeCnt < 1200) //闪烁频率:15K / 1200 = 12.5Hz
- {
- HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_RESET);
- }
- else
- {
- FlashTimeCnt = 0;
- }
- FlashTimeCnt++;
- }
- else//无刹车时,关闭
- {
- HAL_GPIO_WritePin(LIGHT_B_GPIO_Port, LIGHT_B_Pin, GPIO_PIN_RESET);
- FlashTimeCnt = 0;
- }
- break;
- }
- }
- }
|