思路是:
外部上升沿中断给定时器计数器的值置0 ,下降沿读出定时器的值。如果超过20ms这进入定时器中断,表示没有信号输入
[mw_shl_code=c,true]#include "PPM.h"
#include "USART1.h"
int ppm=0;
void TIM14_PPM_Init(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = TIM14_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_TimeBaseStructure.TIM_Period = 30000; // ×?????×°???????÷????????(??????)
TIM_TimeBaseStructure.TIM_Prescaler = (45); //?±???¤·?????
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//?ò??????????
TIM_TimeBaseInit(TIM14, &TIM_TimeBaseStructure);
TIM_ClearFlag(TIM14, TIM_FLAG_Update); // ????????????±ê??
TIM_ITConfig(TIM14,TIM_IT_Update,ENABLE);
TIM_Cmd(TIM14, ENABLE);
}
void PPM_EXTI_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
EXTI_InitTypeDef EXTI_InitStruct;
NVIC_InitTypeDef NVIC_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_Level_2;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStruct);
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource4);
EXTI_ClearITPendingBit(EXTI_Line4);
EXTI_InitStruct.EXTI_Line = EXTI_Line4;
EXTI_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitStruct.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStruct);
NVIC_InitStruct.NVIC_IRQChannel = EXTI4_15_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPriority = 0x01;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStruct);
}
void Falling_()
{ EXTI_InitTypeDef EXTI_InitStruct;
EXTI_InitStruct.EXTI_Line = EXTI_Line4;
EXTI_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStruct.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStruct);
}
void Rising_()
{
EXTI_InitTypeDef EXTI_InitStruct;
EXTI_InitStruct.EXTI_Line = EXTI_Line4;
EXTI_InitStruct.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStruct.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitStruct.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStruct);
}
//=============================================================================
//??????????EXTI4_15_IRQHandler
//??????????°??ü????????
//???????÷????
//????·???????
//=============================================================================
char Rising;
char Falling;
char Rising=1;
char Falling=0;
int now;
int last;
void EXTI4_15_IRQHandler(void)
{
if ( EXTI_GetITStatus(EXTI_Line4)!= RESET )
{
if(Rising==1)
{
TIM_SetCounter(TIM14,0);
Falling_();
Rising=0;
Falling=1;
}
if(Falling==1)
{
now=TIM_GetCounter(TIM14);Rising_();
printf("last????%d\r\n",TIM_GetCounter(TIM14));
Rising=1;
Falling=0;
}
EXTI_ClearITPendingBit(EXTI_Line4);
}
}
void TIM14_IRQHandler(void)
{
if (TIM_GetITStatus(TIM14, TIM_IT_Update) != RESET)
{
TIM_ClearITPendingBit(TIM14,TIM_FLAG_Update);
ppm=0;
printf("???± %d\r\n",ppm);
}
}
[/mw_shl_code]
为啥这样采集的读出的结果的值不正确??
|