第一个波的低脉冲是5us,第二个波的高脉冲是1us,怎么在每个周期内把第二个波的高脉冲显示在第一个波的低脉冲里面
就是显示成这个样子。
void PWM4_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
TIM_OCInitTypeDef TIM_OCInitStruct;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD,ENABLE);
GPIO_PinAFConfig(GPIOD,GPIO_PinSource12,GPIO_AF_TIM4);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_12;
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_100MHz;
GPIO_Init(GPIOD,&GPIO_InitStructure);
TIM_TimeBaseInitStruct.TIM_Period=10000-1;
TIM_TimeBaseInitStruct.TIM_Prescaler=84-1;
TIM_TimeBaseInitStruct.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInitStruct.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM4,&TIM_TimeBaseInitStruct);
TIM_OCInitStruct.TIM_OCMode=TIM_OCMode_PWM1;
TIM_OCInitStruct.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStruct.TIM_Pulse=9999;
TIM_OCInitStruct.TIM_OCPolarity=TIM_OCPolarity_High;
TIM_OC1Init(TIM4,&TIM_OCInitStruct);
TIM_OC1PreloadConfig(TIM4,TIM_OCPreload_Disable);
TIM_OCInitStruct.TIM_OCPolarity=TIM_OCPolarity_Low;
TIM_SelectMasterSlaveMode(TIM4,TIM_MasterSlaveMode_Enable);
TIM_SelectInputTrigger(TIM4,TIM_TS_ITR2);
TIM_SelectSlaveMode(TIM4,TIM_SlaveMode_Trigger);
TIM_ARRPreloadConfig(TIM4,ENABLE);
TIM_SelectMasterSlaveMode(TIM4,TIM_MasterSlaveMode_Enable);
TIM_SelectOutputTrigger(TIM4,TIM_TRGOSource_OC1Ref);
TIM_Cmd(TIM4,DISABLE);
}
void PWM5_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
TIM_OCInitTypeDef TIM_OCInitStruct;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource0,GPIO_AF_TIM5);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;
GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_100MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
TIM_TimeBaseInitStruct.TIM_Period=10000-1;
TIM_TimeBaseInitStruct.TIM_Prescaler=84-1;
TIM_TimeBaseInitStruct.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInitStruct.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM5,&TIM_TimeBaseInitStruct);
TIM_OCInitStruct.TIM_OCMode=TIM_OCMode_PWM1;
TIM_OCInitStruct.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStruct.TIM_OCPolarity=TIM_OCPolarity_Low;
TIM_OCInitStruct.TIM_Pulse=9999;
TIM_OC1Init(TIM5,&TIM_OCInitStruct);
TIM_OC1PreloadConfig(TIM5,TIM_OCPreload_Disable);
TIM_SelectMasterSlaveMode(TIM5,TIM_MasterSlaveMode_Enable);
TIM_SelectInputTrigger(TIM5,TIM_TS_ITR2);
TIM_SelectSlaveMode(TIM5,TIM_SlaveMode_Trigger);
TIM_ARRPreloadConfig(TIM5,ENABLE);
TIM_Cmd(TIM5,DISABLE);
}
我用的是定时器4为主模式,定时器5为从模式。没对准是因为触发的方式不对吗,就是TIM_SlaveMode_Trigger和TIM_SlaveMode_Gated模式用哪一个,这两个模式有什么区别吗。
还是计数器和分频系数有问题。
一周热门 更多>