ch32v307定时器问题

我用定时器9配置更新中断,在主函数读取cnt寄存器的值可以读到值一直在变化,但是配置输入捕获读到的一直是0,触发捕获中断不应该是cnt的值存入ccr吗,ccr读到值但是感觉不太对,我现在要搞红外得读取准确时间

你好,可以试试tim9的通道3


/********************************** (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"


/*********************************************************************

 * @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 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_TIM9, ENABLE );


GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

GPIO_Init( GPIOA, &GPIO_InitStructure);

GPIO_ResetBits( GPIOA, GPIO_Pin_4 );


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( TIM9, &TIM_TimeBaseInitStructure);


TIM_ICInitStructure.TIM_Channel = 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_PWMIConfig( TIM1, &TIM_ICInitStructure );

TIM_ICInit(TIM9, &TIM_ICInitStructure);

NVIC_InitStructure.NVIC_IRQChannel = TIM9_CC_IRQn;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

NVIC_Init(&NVIC_InitStructure);


TIM_ITConfig( TIM9, TIM_IT_CC3, ENABLE );


TIM_SelectInputTrigger( TIM9, TIM_TS_TI1FP1 );

TIM_SelectSlaveMode( TIM9, TIM_SlaveMode_Reset );

TIM_SelectMasterSlaveMode( TIM9, TIM_MasterSlaveMode_Enable );

TIM_Cmd( TIM9, ENABLE );

}


/*********************************************************************

 * @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, 48000-1 );


while(1);

}

/********************************** (C) COPYRIGHT *******************************

* File Name          : ch32v30x_it.c

* Author             : WCH

* Version            : V1.0.0

* Date               : 2024/03/06

* Description        : Main Interrupt Service Routines.

*********************************************************************************

* 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.

*******************************************************************************/

#include "ch32v30x_it.h"


void NMI_Handler(void) __attribute__((interrupt("WCH-Interrupt-fast")));

void HardFault_Handler(void) __attribute__((interrupt("WCH-Interrupt-fast")));

void TIM9_CC_IRQHandler(void) __attribute__((interrupt("WCH-Interrupt-fast")));


/*********************************************************************

 * @fn      NMI_Handler

 *

 * @brief   This function handles NMI exception.

 *

 * @return  none

 */

void NMI_Handler(void)

{

  while (1)

  {

  }

}


/*********************************************************************

 * @fn      HardFault_Handler

 *

 * @brief   This function handles Hard Fault exception.

 *

 * @return  none

 */

void HardFault_Handler(void)

{

  while (1)

  {

  }

}


/*********************************************************************

 * @fn      TIM1_CC_IRQHandler

 *

 * @brief   This function handles TIM1  Capture Compare Interrupt exception.

 *

 * @return  none

 */

void TIM9_CC_IRQHandler(void)

{

if( TIM_GetITStatus( TIM9, TIM_IT_CC3 ) != RESET )

{

printf( "CH3_Val:%d\r\n", TIM_GetCapture3( TIM9 ) );

}




TIM_ClearITPendingBit( TIM9, TIM_IT_CC1 | TIM_IT_CC3 );

}




我现在主循环每隔一秒会串口打印,用的串口2(PA2),读到的时间是有问题的,串口1没问题,PA2是通道1是不是还是影响到定时器了?


只有登录才能回复,可以选择微信账号登录