想要利用ch32v307的输入捕获来获取3个通道中接收到上升沿信号时,定时器的计数值,可是每次上电后,发现立马就进入输入捕获中断,导致出错,是哪里的问题,代码是在例程的基础上进行修改的
main代码如下
/********************************** (C) COPYRIGHT *******************************
* File Name : main.c
* Author : WCH
* Version : V1.0.0
* Date : 2021/06/06
* Description : Main program body.
*********************************************************************************
* Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
* Attention: This software (modified or not) and binary are used for
* microcontroller manufactured by Nanjing Qinheng Microelectronics.
*******************************************************************************/
/*
*@Note
Input capture routine:
TIM1_CH1(PA8)
This example demonstrates the TIM_CH1(PA8) pin floating input, which detects an edge
transition to trigger a TIM1 capture interrupt,
The rising edge triggers the TIM_IT_CC1 interrupt, and the falling edge triggers the
TIM_IT_CC2 interrupt.
*/
#include "debug.h"
#include "math.h"
#define x1 0
#define y1 75
#define x2 0
#define y2 0
#define x3 100
#define y3 0
u32 mic1=0;
u32 mic2=0;
u32 mic3=0;
u16 time=0;
u16 state1=0;
u16 state2=0;
u16 state3=0;
u16 i=0;
u16 x;
u16 y;
u32 t12;
u32 t13;
u32 t23;
/*********************************************************************
* @fn Input_Capture_Init
*
* @brief Initializes TIM1 input capture.
*
* @param arr - the period value.
* psc - the prescaler value.
* ccp - the pulse value.
*
* @return none
*/
void cal(void);
void Input_Capture_Init( u16 arr, u16 psc )
{
GPIO_InitTypeDef GPIO_InitStructure={0};
TIM_ICInitTypeDef TIM_ICInitStructure={0};
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure={0};
NVIC_InitTypeDef NVIC_InitStructure={0};
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_TIM1, ENABLE );
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init( GPIOA, &GPIO_InitStructure);
GPIO_ResetBits( GPIOA, GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 );
TIM_TimeBaseInitStructure.TIM_Period = arr;
TIM_TimeBaseInitStructure.TIM_Prescaler = psc;
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0x00;
TIM_TimeBaseInit( TIM1, &TIM_TimeBaseInitStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1 | TIM_Channel_2 | TIM_Channel_3;
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter = 0x00;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInit(TIM1, &TIM_ICInitStructure);
NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_ITConfig( TIM1, TIM_IT_CC1 | TIM_IT_CC2 | TIM_IT_CC3, ENABLE );
TIM_SelectInputTrigger( TIM1, TIM_TS_TI1FP1 );
//TIM_SelectSlaveMode( TIM1, TIM_SlaveMode_Reset );
//TIM_SelectMasterSlaveMode( TIM1, TIM_MasterSlaveMode_Enable );
TIM_Cmd( TIM1, ENABLE );
}
void TIM1_CC_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void TIM1_UP_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));
void TIM1_UP_IRQHandler(void)
{
TIM_SetCounter( TIM1, 0 );
i++;
}
void TIM1_CC_IRQHandler(void)
{
if( (TIM_GetITStatus( TIM1, TIM_IT_CC1 ) != RESET) && (state1==0))
{
state1=1;
time++;
mic1=(TIM_GetCounter(TIM1)+(i*65535));
TIM_ClearITPendingBit( TIM1, TIM_IT_CC1 );
//printf( "CH4_Val:%d\r\n", TIM_GetCapture1( TIM1 ));
//printf("进入中断1\r");
printf("%d\r",mic1);
if(time==3)
{
state1=0;state2=0;state3=0;
cal();
Delay_Ms(1000);
TIM_SetCounter( TIM1, 0 );
}
}
if( TIM_GetITStatus( TIM1, TIM_IT_CC2 ) != RESET && state2==0)
{
state2=1;
time++;
mic2=(TIM_GetCounter(TIM1)+(i*65535));
TIM_ClearITPendingBit( TIM1, TIM_IT_CC2 );
//printf( "CH4_Val:%d\r\n", TIM_GetCapture2( TIM1 ));
//printf("进入中断2\r");
printf("%d\r",mic2);
if(time==3)
{
state1=0;state2=0;state3=0;
cal();
Delay_Ms(1000);
TIM_SetCounter( TIM1, 0 );
}
}
if( TIM_GetITStatus( TIM1, TIM_IT_CC3 ) != RESET && state3==0)
{
state3=1;
time++;
mic2=(TIM_GetCounter(TIM1)+(i*65535));
TIM_ClearITPendingBit( TIM1, TIM_IT_CC3 );
//printf( "CH4_Val:%d\r\n", TIM_GetCapture3( TIM1 ));
//printf("进入中断3\r");
//TIM_SetCounter( TIM1, 0 );
printf("%d\r",mic3);
if(time==3)
{
state1=0;state2=0;state3=0;
cal();
Delay_Ms(1000);
TIM_SetCounter( TIM1, 0 );
}
}
}
/*********************************************************************
* @fn main
*
* @brief Main program.
*
* @return none
*/
int main(void)
{
USART_Printf_Init(115200);
SystemCoreClockUpdate();
printf("SystemClk:%d\r\n",SystemCoreClock);
printf( "ChipID:%08x\r\n", DBGMCU_GetCHIPID() );
Input_Capture_Init( 0xFFFF, 144-1 );
while(1);
}
void cal()
{
t12=mic1-mic2;
t13=mic1-mic3;
t23=mic2-mic3;
printf("%d\r",t12);printf("%d\n",t13);printf("%d\n",t23);
for(;x<=100;x++)
{
for(;y<=75;y++)
{
if((fabs((sqrtf(x*x+(y1-y)*(y1-y))-sqrtf(x*x+y*y))-t12*0.034)<5) && (fabs((sqrtf(x*x+(y1-y)*(y1-y))-sqrtf(y*y+(x3-x)*(x3-x)))-t13*0.034)<5) && (fabs((sqrtf(x*x+y*y)-sqrtf(y*y+(x3-x)*(x3-x)))-t23*0.034)<5))
{
printf("X=%d\r",x);printf("Y=%d\n",y);
}
}
}
}