stm32输出pwm波形定性分析
部分资料参考网友
部分资料参考网友
/****************************************************************
* 函数名:void GPIO_Config(void)
* 描述 :配置复用输出PWM时用到的I/O
* 输入 :无
* 输出 :无
* 调用 :main()调用
***************************************************************/
void GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* GPIOA and GPIOB clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
/*GPIOA Configuration: TIM3 channel 1 and 2 as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // 复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
Timer初始化:
/****
函数名:void TIM3_Config(void)
描述 :配置TIM3输出的PWM信号的模式
CH1:输出 T=2.5ms(f=1/2.5ms=400Hz) D=0.6的PWM波(高电平在前,低电平在后)
CH2:输出 T=2.5ms(f=1/2.5ms=400Hz) D=0.4的PWM波(高电平在后,低电平在前)
步骤一:通过T和TIMxCLK的时钟源确定TIM_Period和TIM_Prescaler
T=(TIM_Period+1)(TIM_Prescaler+1)/TIMxCLK=2.5ms
因为 TIM_Period<65535,所以 TIM_Prescaler>1,即 TIM_Prescaler=2
所以 TIM_Period=59999=0xEA5F
步骤二:根据TIM_Period的值,高低电平的先后D,确定CCR和TIM_OCPolarity
CH1:因为D=0.6,先高后低;
所以CCR1=(TIM_Period+1) D=36000;TIM_OCPolarity=TIM_OCPolarity_High
CH2:因为D=0.4,先高后低;
所以CCR1=(TIM_Period+1) (1-D)=36000;TIM_OCPolarity=TIM_OCPolarity_Low
步骤三:基础寄存器初始化
步骤四:通道寄存器初始化
步骤五:使能TIM3重载寄存器ARR
步骤六:使能TIM3
输入 :无
输出 :无
* 调用 :main()调用
***/
void TIM3_Config(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
/ PWM??????? /
// u16 CCR1= 36000; //36 000/60000=0.6
// u16 CCR2= 36000;
// u16 CCR1= 3000; // 1/2= 0.5
u16 CCR1= 1; // 1/2= 0.5
u16 CCR2= 1;
/PCLK1??2?????TIM3??????72MHz/
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
/ Time base configuration /
// TIM_TimeBaseStructure.TIM_Period =0xEA5F; //59 999
// TIM_TimeBaseStructure.TIM_Period =5999;// 24M/6000=0.004M 250us ÒѾʵÏÖ
// TIM_TimeBaseStructure.TIM_Period =599;// 24M/600=0.04M 25us ÒѾʵÏÖ
// TIM_TimeBaseStructure.TIM_Period =59;// 24M/60=0.4M 2.5us ÒѾʵÏÖ
// TIM_TimeBaseStructure.TIM_Period =5;// 24M/6=4M 0.25us ÒѾʵÏÖ
//TIM_TimeBaseStructure.TIM_Period =5;// 36M/6=6M 0.16us ÒѾʵÏÖ
// TIM_TimeBaseStructure.TIM_Period =5;// 72M/6=12M 0.08us ÒѾʵÏÖ
// TIM_TimeBaseStructure.TIM_Period =3;// 72M/4=18M 0.05us ÒѾʵÏÖ
// TIM_TimeBaseStructure.TIM_Period =2;// 72M/3=24M 0.04us µ«ÊÇÕ¼¿Õ±È 1/3 ÒѾʵÏÖ
TIM_TimeBaseStructure.TIM_Period =1;// 72M/2=36M 0.02us µ«ÊÇÕ¼¿Õ±È 0.5 ·ÂÕ沨ÐÎÒѾ²»ÄÜʵÏÖ
//TIM_TimeBaseStructure.TIM_Prescaler = 2; // ÉèÖÃÔ¤·ÖÅä:Ô¤·ÖÅä=2,72/3=24MHz ÒѾʵÏÖ
//TIM_TimeBaseStructure.TIM_Prescaler = 1; // ÉèÖÃÔ¤·ÖÅä:Ô¤·ÖÅä=2,72/2=36MHz ÒѾʵÏÖ
TIM_TimeBaseStructure.TIM_Prescaler = 0; // ÉèÖÃÔ¤·ÖÅä:Ô¤·ÖÅä=2,72/1=72MHz
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //????????:???
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //????????
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
/ PWM1 Mode configuration: Channel1 /
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //???PWM??1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR1; //?????,???????????,??????
TIM_OCInitStructure.TIM_OCPolarity =TIM_OCPolarity_High; //?????????CCR1?????
TIM_OC1Init(TIM3, &TIM_OCInitStructure); //????1
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
/ PWM1 Mode configuration: Channel2 /
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = CCR2; //????2??????,??????????PWM
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //?????????CCR2?????
TIM_OC2Init(TIM3, &TIM_OCInitStructure); //????2
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM3, ENABLE); //??TIM3?????ARR
/ TIM3 enable counter /
TIM_Cmd(TIM3, ENABLE); //??TIM3
}
上边测试的结果如下图