一、场景描述:利用PB5产生一定数量的脉冲信号。PB5是定时器3的第2通道。
二、产生PWM代码:
void Timer3_Configuration(void )
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_DeInit(TIM3);
// ƵÂÊ30K
//Õ¼¿Õ±È50%
// TIM_TimeBaseStructure.TIM_Period = 100-1;
//TIM_TimeBaseStructure.TIM_Prescaler =24-1;
// ƵÂÊ20K
//Õ¼¿Õ±È50%
// TIM_TimeBaseStructure.TIM_Period = 100-1;
//TIM_TimeBaseStructure.TIM_Prescaler =36-1;
// ƵÂÊ10K
//Õ¼¿Õ±È50%
// TIM_TimeBaseStructure.TIM_Period = 100-1;
// TIM_TimeBaseStructure.TIM_Prescaler =72-1;
// ƵÂÊK
//Õ¼¿Õ±È50%
TIM_TimeBaseStructure.TIM_Period = 300-1;
TIM_TimeBaseStructure.TIM_Prescaler =72-1;
// ƵÂÊ1K
//Õ¼¿Õ±È10%
//TIM_TimeBaseStructure.TIM_Period = 1000-1;
//TIM_TimeBaseStructure.TIM_Prescaler =72-1;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
//ʱÖÓ·ÖƵ
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
//TIMÏòÉϼÆÊý
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_ClearFlag(TIM3,TIM_FLAG_Update);
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
//PWM1λÕý³£Õ¼¿Õ±Èģʽ£¬pwm2Ϊ·´¼«ÐÔģʽ
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
//ʹÄÜÊä³ö
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;
//»¥²¹Êä³ö¹¦ÄܽûÖ¹
TIM_OCInitStructure.TIM_Pulse = 30;
//Õ¼¿Õʱ¼ä
TIM_OC2Init(TIM3,&TIM_OCInitStructure); //ͨµÀ2
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
// TIM_CtrlPWMOutputs(TIM3,ENABLE);
TIM_ARRPreloadConfig(TIM3, ENABLE);
//ʹÄÜTIMxÔÚARRPÉϵÄԤװÔؼĴæÆ÷
//TIM_Cmd(TIM3, ENABLE);
TIM_Cmd(TIM3, DISABLE);
//ʹÄÜTIM3
}
三、引脚初始化
可以看出来,PB5复用功能是I2C的功能,需要进行重映射。
GPIO_InitStructure .GPIO_Pin=GPIO_Pin_8 ;
GPIO_InitStructure .GPIO_Speed =GPIO_Speed_10MHz;
GPIO_InitStructure .GPIO_Mode =GPIO_Mode_Out_PP ;//
GPIO_Init (GPIOB ,&GPIO_InitStructure );
GPIO_ResetBits(GPIOB,GPIO_Pin_8);
GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, ENABLE);
四、脉冲计数
void TIM3_IRQHandler(void)
{
if(TIM_GetITStatus (TIM3 ,TIM_IT_Update )!=RESET )
{
MD_Pulse_Cnt++;
}
TIM_ClearITPendingBit(TIM3 ,TIM_IT_Update );
}
void main()
{
if(MD_Pulse_Cnt>500)
TIM_Cmd(TIM3,DISABLE);
}
五、总体思路。
在定时器初始化中打开中断,然后在中断里进行脉冲计数。
上一篇:STM32独立看门狗IWDG的使用
下一篇:LPC11C14 启动代码分析
推荐阅读最新更新时间:2024-03-16 16:22