2019-07-24 12:44发布
余泽煌 发表于 2018-6-1 01:04 void PPM_Init(u16 arr,u16 psc) { GPIO_InitTypeDef GPIO_InitStructure;
最多设置5个标签!
void PPM_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_ICInitTypeDef TIM8_ICInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8,ENABLE); //TIM5时钟使能
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); //使能PORTA时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //GPIOA0
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用功能
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //速度100MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; //下拉
GPIO_Init(GPIOC,&GPIO_InitStructure); //初始化PA0
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //GPIOA0
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //下拉
GPIO_Init(GPIOC,&GPIO_InitStructure); //初始化PA0
GPIO_PinAFConfig(GPIOC,GPIO_PinSource8,GPIO_AF_TIM8); //PA0复用位定时器5
GPIO_PinAFConfig(GPIOC,GPIO_PinSource9,GPIO_AF_TIM8);
TIM_TimeBaseStructure.TIM_Prescaler=psc; //定时器分频
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseStructure.TIM_Period=arr; //自动重装载值
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM8,&TIM_TimeBaseStructure);
//初始化TIM5输入捕获参数
TIM8_ICInitStructure.TIM_Channel = TIM_Channel_3; //CC1S=01 选择输入端 IC1映射到TI1上
TIM8_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //上升沿捕获
TIM8_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //映射到TI1上
TIM8_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //配置输入分频,不分频
TIM8_ICInitStructure.TIM_ICFilter = 0xf0;//IC1F=0000 配置输入滤波器 不滤波
TIM_ICInit(TIM8, &TIM8_ICInitStructure);
TIM8_ICInitStructure.TIM_Channel =TIM_Channel_4; //通道2下降沿捕获
TIM8_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling ; //下降沿捕获
TIM_ICInit(TIM8, &TIM8_ICInitStructure);
TIM_ITConfig(TIM8,TIM_IT_CC3|TIM_IT_CC4,ENABLE);//允许更新中断 ,允许CC1IE捕获中断
TIM_Cmd(TIM8,ENABLE ); //使能定时器5
NVIC_InitStructure.NVIC_IRQChannel = TIM8_CC_IRQn ;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;//抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority =0; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器、
}
u16 PPM_CAPTURE_VAL,time6,new_time6,PWM[8]; //输入捕获值(TIM2/TIM5是32位)
u8 pwm_i=0;
void TIM8_CC_IRQHandler(void)
{
if(TIM_GetITStatus(TIM8, TIM_IT_CC4) != RESET){
new_time6=TIM_GetCapture4(TIM8);
if(time6<new_time6){
PPM_CAPTURE_VAL=new_time6-time6;
}
else{
PPM_CAPTURE_VAL=new_time6+(49999-time6);
}
time6=new_time6;
if(PPM_CAPTURE_VAL>10000){
pwm_i=0;
}else{
PWM[pwm_i++]=PPM_CAPTURE_VAL;
}
}
TIM_ClearITPendingBit(TIM8, TIM_IT_CC3|TIM_IT_CC4); //清除中断标志位
}
我两年前写的,用的是STM32F407。大概就是这样,输入上升或者下降沿捕获,得到周期。两包PPM之间间隔周期较长,用来判断包头。
谢谢,我再学习一下
一周热门 更多>