我用定時器9配置更新中斷,在主函數(shù)讀取cnt寄存器的值可以讀到值一直在變化,但是配置輸入捕獲讀到的一直是0,觸發(fā)捕獲中斷不應(yīng)該是cnt的值存入ccr嗎,ccr讀到值但是感覺不太對,我現(xiàn)在要搞紅外得讀取準確時間
你好,可以試試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 );
}
我現(xiàn)在主循環(huán)每隔一秒會串口打印,用的串口2(PA2),讀到的時間是有問題的,串口1沒問題,PA2是通道1是不是還是影響到定時器了?