初始化函数:
void TIM1_PWM_Init(void)
{
GPIO_Init(GPIOD, GPIO_Pin_1, GPIO_Mode_Out_PP_Low_Fast); //输出推拉,低电平,10MHz
TIM1_DeInit();
TIM1_TimeBaseInit(1599, TIM1_CounterMode_Up, 2000, 0); //初始化TIM1时基单元 预分频值() 向上计数 自动重装载值(2000) 重复计数器值(0)
//PD1 输出PWM波形
TIM1_ARRPreloadConfig(ENABLE);
TIM1_OC1Init( TIM1_OCMode_PWM1 , TIM1_OutputState_Enable, TIM1_OutputNState_Disable, 1000, TIM1_OCPolarity_High , TIM1_OCNPolarity_High, TIM1_OCIdleState_Set, TIM1_OCNIdleState_Set); // PWM1模式2 启用互补输出比较 脉冲宽度初值是1000 输出比较极性(低电平) 互补输出比较极性 (低电平) 输出比较空闲状态(置位) 互补输出比较空闲状态(复位)
TIM1_OC2Init( TIM1_OCMode_PWM1 , TIM1_OutputState_Enable, TIM1_OutputNState_Disable, 1000, TIM1_OCPolarity_High , TIM1_OCNPolarity_High, TIM1_OCIdleState_Set, TIM1_OCNIdleState_Set);
TIM1_OC3Init( TIM1_OCMode_PWM1 , TIM1_OutputState_Enable, TIM1_OutputNState_Disable, 1000, TIM1_OCPolarity_High , TIM1_OCNPolarity_High, TIM1_OCIdleState_Set, TIM1_OCNIdleState_Set);
TIM1_CCxCmd(TIM1_Channel_1, ENABLE);
TIM1_CCxCmd(TIM1_Channel_2, ENABLE);
TIM1_CCxCmd(TIM1_Channel_3, ENABLE);
TIM1_OC1PreloadConfig(ENABLE);
TIM1_OC2PreloadConfig(ENABLE);
TIM1_OC3PreloadConfig(ENABLE);
TIM1_CtrlPWMOutputs(ENABLE);
TIM1_Cmd(ENABLE);
}
主函数:
void BSP_Configuration(void);
void main(void)
{
BSP_Configuration();
while (1)
{
GPIO_ToggleBits(GPIOC,GPIO_Pin_6);
delay_ms(500);
}
}
void BSP_Configuration(void)
{
CLK_SYSCLKSourceConfig(CLK_SYSCLKSource_HSI); // 设置系统时钟为 HSI
CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1); // 设置时钟分频数为 1 (16 MHz)
while (CLK_GetSYSCLKSource() != CLK_SYSCLKSource_HSI)
{}
LED_Init();//LED GPIO 管脚初始化
// Beep_Init();
// KEY_Init();
Init_Timer4();
TIM1_PWM_Init();
__enable_interrupt();
}
void BSP_Configuration(void);
void main(void)
{
BSP_Configuration();
while (1)
{
GPIO_ToggleBits(GPIOC,GPIO_Pin_6);
delay_ms(500);
}
}
void BSP_Configuration(void)
{
CLK_SYSCLKSourceConfig(CLK_SYSCLKSource_HSI); // 设置系统时钟为 HSI
CLK_SYSCLKDivConfig(CLK_SYSCLKDiv_1); // 设置时钟分频数为 1 (16 MHz)
while (CLK_GetSYSCLKSource() != CLK_SYSCLKSource_HSI)
{}
LED_Init();//LED GPIO 管脚初始化
// Beep_Init();
// KEY_Init();
Init_Timer4();
TIM1_PWM_Init();
__enable_interrupt();
} |