打印
[STM32F0]

STM32F030PWM输入捕获

[复制链接]
81|2
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
略略u|  楼主 | 2024-3-31 11:50 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
void TIM3_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
uint16_t TimerPeriod;

TimerPeriod = (SystemCoreClock / 48 ) - 1;        //精度为1us,  SystemCoreClock =48M

RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);//使能TIM3时钟
// PA6   TIM3_CH1
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;            //推挽输出模式
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;            //下拉
GPIO_Init(GPIOA, &GPIO_InitStructure);

GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_1);

TIM_TimeBaseInitStructure.TIM_Prescaler =23;                //预分频器参数  计数频率 2M   定时器总线时钟  48M
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;  // Time 定时设置为上升沿计算模式
TIM_TimeBaseInitStructure.TIM_Period = 65535;        //周期
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;     //时钟分频     
TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;    //计数周期

TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStructure);

TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
TIM_ICInitStructure.TIM_ICFilter = 0x00;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_PWMIConfig(TIM3,&TIM_ICInitStructure);// TIM_ICInit(TIM2,&TIM_ICInitStructure);

/*   */
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
TIM_ICInitStructure.TIM_ICFilter = 0x00;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_IndirectTI;
TIM_PWMIConfig(TIM3,&TIM_ICInitStructure);


使用特权

评论回复
沙发
略略u|  楼主 | 2024-3-31 11:50 | 只看该作者
//选择输入捕获的触发信号;
TIM_SelectInputTrigger(TIM3, TIM_TS_TI1FP1); // TIM_TS_TI1FP1
// 选择从模式
// PWM输入模式时,从模式必须工作在复位模式,当捕获开始时,计数器CNT被复位清零;
TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Reset);
TIM_SelectMasterSlaveMode(TIM3,TIM_MasterSlaveMode_Enable);

NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;        //pwm中断使能
NVIC_InitStructure.NVIC_IRQChannelPriority = 2;
NVIC_Init(&NVIC_InitStructure);

TIM_ClearITPendingBit(TIM3,TIM_IT_Update | TIM_IT_CC1);    //清理中断标志
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);
TIM_ITConfig(TIM3,TIM_IT_CC1,ENABLE);

TIM_ClearITPendingBit(TIM3,TIM_IT_Update | TIM_IT_CC1);    //清理中断标志
TIM_Cmd(TIM3,ENABLE);

使用特权

评论回复
板凳
略略u|  楼主 | 2024-3-31 11:50 | 只看该作者
}

/在定时器中断里面添加中断处理函数/
u16 cnt_tim2;
u16 cnt_tim3;
void TIM3_IRQHandler (void)
{

if(TIM_GetITStatus(TIM3,TIM_IT_Update)!=RESET)
{

     TIM_ClearITPendingBit(TIM3,TIM_IT_Update);
}

if(TIM_GetITStatus(TIM3,TIM_IT_CC1)!=RESET)
{
        cnt_tim2=TIM_GetCapture1(TIM3); // 周期
        cnt_tim3=TIM_GetCapture2(TIM3);// 高电平
        TIM_ClearITPendingBit(TIM3,TIM_IT_CC1);
}

if(TIM_GetITStatus(TIM3,TIM_IT_CC2)!=RESET)
{
      
       TIM_ClearITPendingBit(TIM3,TIM_IT_CC2);
}

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

62

主题

510

帖子

0

粉丝