本帖最后由 rainic 于 2017-9-7 19:26 编辑
方法二:过零检测(用的比较器),然后TIM5输入捕获(2个上升沿),TIM2设置为单脉冲输出,TIM3作为定时器。TIM5计算连续两个过零点出时间t0,在第二个过零点开启TIM3时间为t0/2,时间到则产生中断在中断里开启TIM2输出脉冲波。 这是我的逻辑方法,老铁门感觉对不对??
#include "timer.h"
#include "led.h"
#include "usart.h"
u16 zhouqi=300;//*0.1ms
u16 gaodianping=100;//*0.1ms
void TIM2_OPMGATE_Init()
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
TIM_OCInitTypeDef TIM_OCInitStruct;
GPIO_InitTypeDef GPIO_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);//
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStruct);
// GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6;
// GPIO_Init(GPIOA,&GPIO_InitStruct);
//TIM2工作在单脉冲下
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;//向上计数
TIM_TimeBaseInitStruct.TIM_Prescaler = 7199;
TIM_TimeBaseInitStruct.TIM_Period = zhouqi-1; //
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStruct);
TIM_SelectOnePulseMode(TIM2,TIM_OPMode_Single);
TIM_SelectOutputTrigger(TIM2,TIM_TRGOSource_OC1Ref);
TIM_OC1PreloadConfig(TIM2,TIM_OCPreload_Enable);
TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStruct.TIM_Pulse = zhouqi-gaodianping;
TIM_OC1Init(TIM2,&TIM_OCInitStruct);
TIM_Cmd(TIM2,DISABLE); //Enable the TIM Counter
}
void TIM2_Open()
{
TIM_Cmd(TIM2,ENABLE);//使能TIM2
}
/*************************************************************/
void TIM3_Int_Init(u16 arr3,u16 psc3)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
//¶¨Ê±Æ÷TIM3³õʼ»¯
TIM_TimeBaseStructure.TIM_Period = arr3;
TIM_TimeBaseStructure.TIM_Prescaler =psc3;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE );
//ÖжÏÓÅÏȼ¶NVICÉèÖÃ
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_Cmd(TIM3, DISABLE);
}
void TIM3_Open()
{
TIM_Cmd(TIM3,ENABLE);
}
//中断服务函数
void TIM3_IRQHandler(void)
{
if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) //检测到中断标志
{
TIM2_Open();
}
TIM_ClearITPendingBit(TIM3, TIM_IT_Update ); //清除标志
}
/***********************************************************/
//TIM5输入捕获
TIM_ICInitTypeDef TIM5_ICInitStructure;
void TIM5_Cap_Init(u16 arr5,u16 psc5)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_ResetBits(GPIOA,GPIO_Pin_0);
//³õʼ»¯¶¨Ê±Æ÷5 TIM5
TIM_TimeBaseStructure.TIM_Period = arr5;
TIM_TimeBaseStructure.TIM_Prescaler =psc5;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure);
//初始化输入捕获参数
TIM5_ICInitStructure.TIM_Channel = TIM_Channel_1; //CC1S=01
TIM5_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM5_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM5_ICInitStructure.TIM_ICFilter = 0x00;
TIM_ICInit(TIM5, &TIM5_ICInitStructure);
//中断分组初始化
NVIC_InitStructure.NVIC_IRQChannel = TIM5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_ITConfig(TIM5,TIM_IT_Update|TIM_IT_CC1,ENABLE);
TIM_Cmd(TIM5,ENABLE );
}
u8 TIM5CH1_CAPTURE_STA=0; //输入捕获状态
u16 TIM5CH1_CAPTURE_VAL; //输入捕获值
u32 temp=0;
//中断程序
void TIM5_IRQHandler(void)
{
if((TIM5CH1_CAPTURE_STA&0X80)==0)//还未成功捕获
{
if (TIM_GetITStatus(TIM5, TIM_IT_Update) != RESET)
{
if(TIM5CH1_CAPTURE_STA&0X40)//已经捕获到高电平
{
if((TIM5CH1_CAPTURE_STA&0X3F)==0X3F)//高电平时间过长
{
TIM5CH1_CAPTURE_STA|=0X80;//标记捕获成功
TIM5CH1_CAPTURE_VAL=0XFFFF;
}else TIM5CH1_CAPTURE_STA++;
}
}
if (TIM_GetITStatus(TIM5, TIM_IT_CC1) != RESET)//²¶»ñ1·¢Éú²¶»ñʼþ
{
if(TIM5CH1_CAPTURE_STA&0X40)
{
TIM5CH1_CAPTURE_STA|=0X80;
TIM5CH1_CAPTURE_VAL=TIM_GetCapture1(TIM5);
TIM_OC1PolarityConfig(TIM5,TIM_ICPolarity_Rising); //设置为上升沿捕获
}else
{
TIM5CH1_CAPTURE_STA=0;
TIM5CH1_CAPTURE_VAL=0;
TIM_SetCounter(TIM5,0);
TIM5CH1_CAPTURE_STA|=0X40;
TIM_OC1PolarityConfig(TIM5,TIM_ICPolarity_Rising); //设置为上升沿捕获
}
}
}
if(TIM5CH1_CAPTURE_STA&0X80)
{
//TIM3_Open();
temp=TIM5CH1_CAPTURE_STA&0X3F;
temp*=65536;//溢出时间总和
temp+=TIM5CH1_CAPTURE_VAL;//两个零点时间
printf("HIGH:%d us\r\n",temp);//打印输出
TIM5CH1_CAPTURE_STA=0;//清零 下次捕获用
}
TIM_ClearITPendingBit(TIM5, TIM_IT_CC1|TIM_IT_Update); //清除中断标志
}
|