#include "timer.h"
#include "stdio.h"
#include "stm32f10x.h"
u8 status1=1;
u8 status2=1;
u8 MODE1=1;
u8 MODE2=0;
u8 TIM3_CH1_CAPTURE_MODE=0 ;
u32 TIM3_CH1_CAPTURE_H ;
u32 TIM3_CH1_CAPTURE_HL ;
u8 TIM3_CH2_CAPTURE_MODE=0 ;
u32 TIM3_CH2_CAPTURE_H ;
u32 TIM3_CH2_CAPTURE_HL ;
void TIM2_PWM_OUTPUT()
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1|GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel=TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
NVIC_Init(&NVIC_InitStructure);
TIM_TimeBaseInitStructure.TIM_Period=0xffff;
TIM_TimeBaseInitStructure.TIM_Prescaler=71;
TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInitStructure.TIM_ClockDivision=0;
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure);
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_Toggle;
TIM_OCInitStructure.TIM_OutputState=ENABLE;
TIM_OCInitStructure.TIM_Pulse=1000;
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_Low;
TIM_OC2Init(TIM2,&TIM_OCInitStructure);
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_Toggle;
TIM_OCInitStructure.TIM_OutputState=ENABLE;
TIM_OCInitStructure.TIM_Pulse=2000;
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_Low;
TIM_OC3Init(TIM2,&TIM_OCInitStructure);
TIM_ITConfig(TIM2,TIM_IT_CC2|TIM_IT_CC3,ENABLE);
TIM_Cmd(TIM2,ENABLE);
TIM_SetCounter(TIM2,0);
TIM_SetCompare2(TIM2,0);
TIM_SetCompare3(TIM2,0);
}
void TIM3_CAPTURE_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_ICInitTypeDef TIM_ICInitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_TimeBaseInitStructure.TIM_Period = 0xffff;
TIM_TimeBaseInitStructure.TIM_Prescaler = 71;
TIM_TimeBaseInitStructure.TIM_ClockDivision = 0x0;
TIM_TimeBaseInitStructure.TIM_CounterMode = 0x0;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);
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_ICInitStructure.TIM_ICFilter = 0x0;
TIM_ICInit(TIM3, &TIM_ICInitStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x0;
TIM_ICInit(TIM3, &TIM_ICInitStructure);
TIM_Cmd(TIM3, ENABLE);
TIM_ITConfig(TIM3, TIM_IT_CC1 , ENABLE);
}
void TIM2_IRQHandler()
{
u32 temp;
if(TIM_GetITStatus(TIM2,TIM_IT_CC2)==1)
{
TIM_ClearFlag(TIM2,TIM_FLAG_CC2);
temp=TIM_GetCapture2(TIM2);
if(status1)
{
TIM_SetCompare2(TIM2,temp+400);
}else
{
TIM_SetCompare2(TIM2,temp+600);
}
status1 ^=1;
}
if(TIM_GetITStatus(TIM2,TIM_IT_CC3)==1)
{
TIM_ClearFlag(TIM2,TIM_FLAG_CC3);
if(status2)
{
temp=TIM_GetCapture3(TIM2);
TIM_SetCompare3(TIM2,temp+400);
}else
{
temp=TIM_GetCapture3(TIM2);
TIM_SetCompare3(TIM2,temp+1600);
}
status2 ^=1;
}
}
void TIM3_IRQHandler()
{
if(TIM_GetITStatus(TIM3,TIM_IT_CC1)==1)
{
TIM_ClearITPendingBit(TIM3,TIM_IT_CC1);
// if(MODE1)
// {
switch(TIM3_CH1_CAPTURE_MODE)
{
case 0: TIM3_CH1_CAPTURE_H=0;
TIM3_CH1_CAPTURE_HL=0;
TIM3_CH1_CAPTURE_MODE=1;
TIM_SetCounter(TIM3,0);
TIM_OC1PolarityConfig(TIM3,TIM_ICPolarity_Falling);
break;
case 1: TIM3_CH1_CAPTURE_H=TIM_GetCounter(TIM3);
TIM3_CH1_CAPTURE_MODE=2;
TIM_OC1PolarityConfig(TIM3,TIM_ICPolarity_Rising);
break;
case 2: TIM3_CH1_CAPTURE_HL=TIM_GetCounter(TIM3);
TIM3_CH1_CAPTURE_MODE=3;
TIM_ITConfig(TIM3,TIM_IT_CC1,DISABLE);
TIM_ITConfig(TIM3,TIM_IT_CC2,ENABLE);
//TIM_OC1PolarityConfig(TIM3,TIM_ICPolarity_Rising);//êÇ2»êÇ2»óÃÕa¾ä
break;
default:break;
}
// }
}
if(TIM_GetITStatus(TIM3,TIM_IT_CC2)==1)
{
TIM_ClearITPendingBit(TIM3,TIM_IT_CC2);
// if(MODE2)
// {
switch(TIM3_CH2_CAPTURE_MODE)
{
case 0: TIM3_CH2_CAPTURE_H=0;
TIM3_CH2_CAPTURE_HL=0;
TIM3_CH2_CAPTURE_MODE=1;
TIM_SetCounter(TIM3,0);
TIM_OC1PolarityConfig(TIM3,TIM_ICPolarity_Falling);
break;
case 1: TIM3_CH2_CAPTURE_H=TIM_GetCounter(TIM3);
TIM3_CH2_CAPTURE_MODE=2;
TIM_OC2PolarityConfig(TIM3,TIM_ICPolarity_Rising);
break;
case 2: TIM3_CH2_CAPTURE_HL=TIM_GetCounter(TIM3);
TIM3_CH2_CAPTURE_MODE=3;
TIM_ITConfig(TIM3,TIM_IT_CC1,ENABLE);
TIM_ITConfig(TIM3,TIM_IT_CC2,DISABLE);
//TIM_OC1PolarityConfig(TIM3,TIM_ICPolarity_Rising);//êÇ2»êÇ2»óÃÕa¾ä
break;
default:break;
}
// }
}
}
#include "stdio.h"
#include "lcd.h"
#include "rtc.h"
#include "timer.h"
#include "delay.h"
u8 string[20];
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
STM3210B_LCD_Init();
LCD_Clear(Blue);
LCD_SetBackColor(Blue);
LCD_SetTextColor(Yellow);
SysTick_Config(SystemCoreClock/1000);
LCD_DisplayStringLine(Line1, (u8*)" hhhhhhhhhhh ");
TIM2_PWM_OUTPUT();
TIM3_CAPTURE_Init();
while(1)
{
if(TIM3_CH1_CAPTURE_MODE == 3)
{
sprintf((char*)string,"ch1_fre:%d ",1000000 / TIM3_CH1_CAPTURE_HL);
LCD_DisplayStringLine(Line3, string);
sprintf((char*)string,"ch1_duty:%d ",TIM3_CH1_CAPTURE_H * 100/TIM3_CH1_CAPTURE_HL);
LCD_DisplayStringLine(Line4, string);
}
if(TIM3_CH2_CAPTURE_MODE == 3)
{
sprintf((char*)string,"ch2_fre:%d ",1000000 / TIM3_CH2_CAPTURE_HL);
LCD_DisplayStringLine(Line7, string);
sprintf((char*)string,"ch2_duty:%d ",TIM3_CH2_CAPTURE_H * 100/TIM3_CH2_CAPTURE_HL);
LCD_DisplayStringLine(Line8, string);
}
}
}
单独测改改都能捕获
查你的逻辑吧
一周热门 更多>