void PWM_Int(u16 arr,u16 psc) { GPIO_InitTypeDef GPIO_InitStructure; //¶¨Òå½á¹¹ÌåGPIO_InitStructure TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; //¶¨Òå½á¹¹ÌåTIM_TimeBaseStructure TIM_OCInitTypeDef TIM_OCInitStructure; //¶¨Òå½á¹¹ÌåTIM_OCInitStructure RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//ʹÄÜPB¶Ë¿ÚʱÖÓ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);//ʹÄܶ¨Ê±Æ÷3 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //¸´ÓÃģʽÊä³ö GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1; //PB0 ¡¢PB1 GPIO_InitStructure.GPIO_Speed= GPIO_Speed_50MHz; //IO¿ÚËÙ¶È GPIO_Init(GPIOB,&GPIO_InitStructure); //GPIO³õʼ»¯ TIM_TimeBaseStructure.TIM_Period = arr; //ÉèÖÃÏÂÒ»¸ö¸üлµÄ×Ô¶¯ÖØ×°ÔؼĴæÆ÷µÄÖµ TIM_TimeBaseStructure.TIM_Prescaler = psc; //Ô¤·ÖÅäÖµ TIM_TimeBaseStructure.TIM_ClockDivision = 0; //ʱÖÓ·Ö¸î TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //ÏòÉϼÆÊý TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure); TIM_OCInitStructure.TIM_OCMode= TIM_OCMode_PWM1; //PWMÂö³å¿í¶Èµ÷ÖÆ1 TIM_OCInitStructure.TIM_Pulse = 0; //ÉèÖôý×°È벶»ñ±È½Ï¼Ä´æÆ÷µÄÂö³åÖµ TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //ÉèÖÃTIMÊä³ö¼«ÐÔΪ¸ß TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//±È½ÏÊä³öʹÄÜ TIM_OC3Init(TIM3,&TIM_OCInitStructure); TIM_OC4Init(TIM3,&TIM_OCInitStructure); TIM_CtrlPWMOutputs(TIM3,ENABLE);//Ö÷Êä³öʹÄÜ TIM_OC3PreloadConfig(TIM3,TIM_OCPreload_Enable); TIM_OC4PreloadConfig(TIM3,TIM_OCPreload_Enable);//ʹÄÜԤװÔؼĴæÆ÷ TIM_ARRPreloadConfig(TIM3,ENABLE); //ʹÄÜ×Ô¶¯×°ÔØÔÊÐíλ TIM_Cmd(TIM3,ENABLE);//Æô¶¯¶¨Ê±Æ÷3 } void Set_PWMA(int PWM) { TIM_SetCompare3(TIM3,PWM);//ÉèÖÃTIM3ͨµÀ3µÄÕ¼¿Õ±È 3000/7200 } void Set_PWMB(int PWM) { TIM_SetCompare4(TIM3,PWM); 这段代码是一个pwm驱动还是两个pwm驱动欸
时间: 2024-04-09 19:31:52 浏览: 125
PWM.zip_pwm_pwm频率_stm32 pwm_stm32 pwm 频率_可调pwm
这段代码是一个具有两个PWM通道的驱动程序。使用了TIM3定时器和GPIOB引脚的PB0和PB1作为输出。其中,函数`void PWM_Int(u16 arr, u16 psc)`用于初始化PWM的定时器和GPIO引脚,设置PWM的周期和预分频值。而函数`void Set_PWMA(int PWM)`和`void Set_PWMB(int PWM)`则用于设置PWM的占空比,即设置TIM3通道3和通道4的输出值。
阅读全文