Hello UU们,有做汽车电子的吗?
如果做汽车电子可能会用到很多高边开关,高边开关带的负载是让容性负载,或者是感性负载时候会比较恶劣,容性负载可能一下子不容易带起来.因为电池和负载电容上的巨大压差,高边开关上流过的电流非常之大,为此我们可以使用缓慢的打开高边开关,高边开关的EN不像是自己用Mosfet一样内阻可以缓慢的线性变化,为此Frank Pan提出用PWM渐变去控制高边开关,使负载电容上的电压缓慢增加,又不让高边开关过流保护.
但每个MCU的资源有限,可能没那么多PWM资源和定时器资源,在MCU不变的情况下,尽量的让程序复用。图1是类比高边开关的产品
图1:类比智能高边产品
以下是程序代码:
#include "main.h" #include "string.h" void SystemClock_Config(void); /*注释:步骤1 定义GPIO 端口*/ #define HSD_Pin GPIO_PIN_4 #define HSD_Port GPIOG #define HSD_Clk_Enable __HAL_RCC_GPIOG_CLK_ENABLE() #define HSD_Pin_H HAL_GPIO_WritePin(HSD_Port,HSD_Pin,GPIO_PIN_SET) #define HSD_Pin_L HAL_GPIO_WritePin(HSD_Port,HSD_Pin,GPIO_PIN_RESET) /*注释:步骤2 初始化引脚*/ void HSD_Init(void) { GPIO_InitTypeDef GPIO_InitStruct = {0}; /*注释:步骤3 打开引脚时钟*/ HSD_Clk_Enable; GPIO_InitStruct.Pin = HSD_Pin; /*注释:步骤4 设置为输出模式*/ GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(HSD_Port, &GPIO_InitStruct); } /*注释:步骤5 控制HSD 启动的函数*/ /* HSD State 设置高边使能或者关闭 设置任意不为0的参数就使能 参数设置为0就关闭 */ void HSD_Ctrl(uint16_t HSDState) { /* 注释:步骤6 i设置最大步数 改下面1000的这个参数 j设置最大占空比比例 改下面50的这个参数 k设置每步延时步长 改下面30的这个参数 */ uint16_t i,j,k; if(HSDState) { for(i=0;i<1000;i++) { HSD_Pin_L; for(j=0;j<50;j++) for(k=0;k<30;k++); HSD_Pin_H; for(j=0;j<i;j++) for(k=0;k<30;k++); HSD_Pin_L; } HSD_Pin_H; } else HSD_Pin_L; } /* USER CODE END 0 */ /** * @brief The application entry point. * @retval int */ int main(void) { HAL_Init(); SystemClock_Config(); /* USER CODE BEGIN 2 */ /*注释:步骤6 主程序初始化高边开关*/ HSD_Init(); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /*注释:步骤7 设置高边开关开*/ HSD_Ctrl(0x00); HAL_Delay(1000); /*注释:步骤7 设置高边开关关*/ HSD_Ctrl(0x01); HAL_Delay(10); /* USER CODE BEGIN 3 */ } /* USER CODE END 3 */ } /** * @brief System Clock Configuration * @retval None */ void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; /** Supply configuration update enable */ HAL_PWREx_ConfigSupply(PWR_LDO_SUPPLY); /** Configure the main internal regulator output voltage */ __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE0); while(!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {} /** Initializes the RCC Oscillators according to the specified parameters * in the RCC_OscInitTypeDef structure. */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48|RCC_OSCILLATORTYPE_HSE; RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS; RCC_OscInitStruct.HSI48State = RCC_HSI48_ON; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; RCC_OscInitStruct.PLL.PLLM = 4; RCC_OscInitStruct.PLL.PLLN = 275; RCC_OscInitStruct.PLL.PLLP = 1; RCC_OscInitStruct.PLL.PLLQ = 4; RCC_OscInitStruct.PLL.PLLR = 2; RCC_OscInitStruct.PLL.PLLRGE = RCC_PLL1VCIRANGE_1; RCC_OscInitStruct.PLL.PLLVCOSEL = RCC_PLL1VCOWIDE; RCC_OscInitStruct.PLL.PLLFRACN = 0; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } /** Initializes the CPU, AHB and APB buses clocks */ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2 |RCC_CLOCKTYPE_D3PCLK1|RCC_CLOCKTYPE_D1PCLK1; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV2; RCC_ClkInitStruct.APB3CLKDivider = RCC_APB3_DIV2; RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV2; RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV2; RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV2; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_3) != HAL_OK) { Error_Handler(); } } /** * @brief This function is executed in case of error occurrence. * @retval None */ void Error_Handler(void) { /* USER CODE BEGIN Error_Handler_Debug */ /* User can add his own implementation to report the HAL error return state */ __disable_irq(); while (1) { } /* USER CODE END Error_Handler_Debug */ } #ifdef USE_FULL_ASSERT /** * @brief Reports the name of the source file and the source line number * where the assert_param error has occurred. * @param file: pointer to the source file name * @param line: assert_param error line source number * @retval None */ void assert_failed(uint8_t *file, uint32_t line) { /* USER CODE BEGIN 6 */ /* User can add his own implementation to report the file name and line number, ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ /* USER CODE END 6 */ } #endif /* USE_FULL_ASSERT */ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |
以上都比较容易看懂,其中有一段解释下,如代码2所示。
代码2:
- 其中K参数可以不用看他,单纯用于延时,不想在写个Delay函数
- j参数控制低电平持续的时间是不变的
- 随着i越来越大,HSD_Pin_H持续的比例慢慢增加
- 最大占空比就是i和j的比值,等高边打开不保护的占空比即可
- 运行到最大占空比之后高边就常高了
void HSD_Ctrl(uint16_t HSDState) { uint16_t i,j,k; if(HSDState) { for(i=0;i<1000;i++) { HSD_Pin_L; for(j=0;j<50;j++) for(k=0;k<30;k++); HSD_Pin_H; for(j=0;j<i;j++) for(k=0;k<30;k++); HSD_Pin_L; } HSD_Pin_H; } else HSD_Pin_L; } |
测试结果
关注公众号不迷路