 
- UID
- 1029342
- 性别
- 男
|

看datasheet,发现定时器几大功能之一就是对PWM信号的捕获比较.说明定时器即可以产生PWM信号,还可以对外部的PWM信号捕获.
自己仿真一下吧,将D0脚作用PWM信号的输出,用定时器3调整其高低电平输出时间,就算是一路占空比可调的PWM信号了.
将定时器2的CH2作为PWM信号的输入脚,即GPIOA1脚,将D0接到A1脚上.
先配置一下吧
//GPIO配置
RCC_AHBxPeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE); //A口
RCC_AHBxPeriphClockCmd(RCC_AHB1Periph_GPIOD,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_TIM2);//A0口复用为定时器2
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //配置为复用脚
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
//两路定时器的配置,这个定时器的配置还是有玄机在里面的,等会再讲
void TIM3_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
/* ---------------------------------------------------------------
PCLK1=120/4=30MHz
TIM2 CLK = 30MHz * 2 = 60MHz, Prescaler = 300, TIM3 counter clock = 50,周期就为4K,定时器时间是0.25mS
--------------------------------------------------------------- */
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 50;
TIM_TimeBaseStructure.TIM_Prescaler =(300-1);
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
/*----------------------------------------------------------------
TIM IT enable 使能或者失能指定的TIM中断
TIM3: TIM 中断源
TIM_IT_Update | TIM_IT_Trigger: TIM 触发中断源
----------------------------------------------------------------*/
TIM_ITConfig(TIM3, TIM_IT_Update | TIM_IT_Trigger, ENABLE);
/* TIM3 enable counter 使能TIMx外设*/
TIM_Cmd(TIM3, ENABLE);
}
//这个函数数F2的标准库自带的例子,大家可以自己去研究下
void TIM2_Configuration(void)
{
TIM_ICInitTypeDef TIM_ICInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
/* Time base configuration */
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
TIM_ICInitStructure.TIM_ICPolarity =TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_PWMIConfig(TIM2, &TIM_ICInitStructure);
/* Select the TIM2 Input Trigger: TI2FP2 */
TIM_SelectInputTrigger(TIM2, TIM_TS_TI2FP2);
/* Select the slave Mode: Reset Mode */
TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Reset);
/* Enable the Master/Slave Mode */
TIM_SelectMasterSlaveMode(TIM2, TIM_MasterSlaveMode_Enable);
/* TIM enable counter */
/* Enable the CC2 Interrupt Request */
TIM_ITConfig(TIM2, TIM_IT_CC2, ENABLE);
TIM_Cmd(TIM2, ENABLE);
} |
|