• 大小: 1KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-05-16
  • 语言: 其他
  • 标签: STM32  PWM  输入捕获  

资源简介

用STM32的TIM定时器进行PWM的输入捕获实验,调试航模遥控器。

资源截图

代码片段和文件信息

#include“ppmin.h“
Contral My_Contral;
void PWMInConfiguration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_TimebaseInitTypeDef TIM_TimebaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2|RCC_APB1Periph_TIM3|RCC_APB1Periph_TIM4 ENABLE); //时钟配置 
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_TIM1 ENABLE); 

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_6|GPIO_Pin_8;    //GPIO配置 
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; 
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
GPIO_Init(GPIOA &GPIO_InitStructure); 

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;    //GPIO配置 
GPIO_Init(GPIOB &GPIO_InitStructure);

TIM_TimebaseStructure.TIM_Period= 0xffff;
TIM_TimebaseStructure.TIM_Prescaler=2;
TIM_TimebaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimebaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimebaseStructure.TIM_RepetitionCounter=0;
TIM_TimebaseInit(TIM1&TIM_TimebaseStructure);
TIM_TimebaseInit(TIM2&TIM_TimebaseStructure);
TIM_TimebaseInit(TIM3&TIM_TimebaseStructure);
TIM_TimebaseInit(TIM4&TIM_TimebaseStructure);

TIM_ICInitStructure.TIM_Channel = TIM_Channel_1; //通道选择 
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //上升沿触发 
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //管脚与寄存器对应关系 
TIM_ICInitStructure. TIM_ICPrescaler=TIM_ICPSC_DIV1;//输入预分频,意思是控制在多少个输入周期做一个捕获,如果输入的信号频率没有变,测得的周期也不会变,比如选择四分频,则没四个输入周期做一次捕获,这昂再输入信号不频繁的情况下,可以减少软件被不断中断的次数 

TIM_PWMIConfig(TIM1 &TIM_ICInitStructure); //根据参数配置TIM外设信息
TIM_PWMIConfig(TIM2 &TIM_ICInitStructure); //根据参数配置TIM外设信息
TIM_PWMIConfig(TIM3 &TIM_ICInitStructure); //根据参数配置TIM外设信息
TIM_PWMIConfig(TIM4 &TIM_ICInitStructure); //根据参数配置TIM外设信息  
TIM_SelectInputTrigger(TIM1 TIM_TS_TI1FP1);
TIM_SelectInputTrigger(TIM2 TIM_TS_TI1FP1);
TIM_SelectInputTrigger(TIM3 TIM_TS_TI1FP1);
TIM_SelectInputTrigger(TIM4 TIM_TS_TI1FP1);
TIM_SelectMasterSlaveMode(TIM1TIM_MasterSlaveMode_Enable);
TIM_SelectMasterSlaveMode(TIM2TIM_MasterSlaveMode_Enable);
TIM_SelectMasterSlaveMode(TIM3TIM_MasterSlaveMode_Enable);
TIM_SelectMasterSlaveMode(TIM4TIM_MasterSlaveMode_Enable); 
TIM_SelectSlaveMode(TIM1TIM_SlaveMode_Reset);
TIM_SelectSlaveMode(TIM2TIM_SlaveMode_Reset);
TIM_SelectSlaveMode(TIM3TIM_SlaveMode_Reset);
TIM_SelectSlaveMode(TIM4TIM_SlaveMode_Reset);
TIM_Cmd(TIM1 ENABLE);
TIM_Cmd(TIM2 ENABLE);
TIM_Cmd(TIM3 ENABLE);
TIM_Cmd(TIM4 ENABLE);  
}
void PPMInCompute(void)
{
int temp1temp2temp3temp4;
temp1=TIM2->CCR2;
temp2=TIM4->CCR2;
temp3=TIM1->CCR2;
temp4=TIM3->CCR2;
if(temp1>3880)
{
My_Contral.NS=temp1-3880;
}
else if(temp1<3680)
{
My_Contral.NS=temp1-3680;
}
else
{
My_Contral.NS=0;
}
if(temp2>3800)
{
My_Contral.WE=temp2-3800;
}
else if(temp2<3600)
{
My_Contral.WE=temp2-3600;
}
else
{
My_Contral.WE=0;
}
if(temp2>3800)

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     文件         217  2012-08-16 16:44  ppmin.h
     文件        3371  2012-08-16 16:55  ppmin.c

评论

共有 条评论