初级会员

- 积分
- 81
- 金钱
- 81
- 注册时间
- 2019-1-12
- 在线时间
- 34 小时
|
5金钱
用STM32F207 P8口作为输入捕获,一直进不了中断。把TIM_INT()换成TIM_PWMICONFIG()能进中断,但是读的数全是0.
#include "pwm.h"
#include "stm32f2xx_tim.h"
#include "stm32f2xx_rcc.h"
//PB8, TIM4 CH3,TIM10 CH1
//PB9, TIM4 CH4, TIM11 CH1
//PB10,TIM2 CH3
u32 up = 0;
u32 down = 0;
int status = 0;
void PWM_Int(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
TIM_ICInitTypeDef TIM_ICInitStruct;
NVIC_InitTypeDef NVIC_InitStruct;
// 1.打开时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);//开不开这个好像没有用。
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
// 2.GPIO初始化
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOB, &GPIO_InitStruct);
// 3.引脚复用
GPIO_PinAFConfig(GPIOB, GPIO_PinSource8, GPIO_AF_TIM4);
// 4.采样周期配置
TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStruct.TIM_Period = 20;
TIM_TimeBaseInitStruct.TIM_Prescaler = 0x3fff;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseInitStruct);
// 5.TIM配置
TIM_ICInitStruct.TIM_Channel = TIM_Channel_3;
TIM_ICInitStruct.TIM_ICFilter = 4;
TIM_ICInitStruct.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStruct.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStruct.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInit(TIM4,&TIM_ICInitStruct);//这里换成TIM_PWMICONFIG能进中断
// 6.中断使能
TIM_ITConfig(TIM4, TIM_IT_CC3, ENABLE);
// 7.中断优先级设置
NVIC_InitStruct.NVIC_IRQChannel = TIM4_IRQn;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 2;
NVIC_Init(&NVIC_InitStruct);
// 8.使能TIM4
TIM_Cmd(TIM4, ENABLE);
}
void TIM4_IRQHandler(void)
{
if (TIM_GetITStatus(TIM4, TIM_IT_CC3) != RESET)
{
if(status == 0)
{
down = TIM_GetCapture3(TIM4); //使用TIM_PWMICONFIG能进中断,但DEBUG,这个数一直是0.
TIM_SetCounter(TIM4,0);
status = 1;
TIM_OC3PolarityConfig(TIM4, TIM_OCPolarity_Low);
}
if(status == 1)
{
up = TIM_GetCapture3(TIM4);
TIM_SetCounter(TIM4,0);
status = 0;
TIM_OC3PolarityConfig(TIM4, TIM_OCPolarity_High);
}
}
TIM_ClearITPendingBit(TIM4, TIM_IT_CC3); //???????
}
|
|