簡體   English   中英

在stm32f103c8t6上有TIM2_CH1的問題

[英]There is a problem with TIM2_CH1 on stm32f103c8t6

我使用3個定時器(TIM4_CH1,TIM3_CH1,TIM2_CH1)從AURORA9x捕獲輸入PWM信號。 一切都很好,除了TIM2_CH1。 我無法解決這個問題3天。 也許你有些人遇到過這個問題。 這里代碼為三個計時器。 所有計時器的配置都相同,但TIM2不起作用

#include "stm32f10x.h"
#include "stm32f10x_rcc.h"
#include "can.h"
#include "main.h"

void TIM4_Config(void);
void TIM3_Config(void);
void TIM2_Config(void);


uint32_t   DataCapture_TIM4=0,
           DataCapture_TIM3=0,
           DataCapture_TIM2=0;

用於TIM4_CH1捕獲模式的定時器配置

void TIM4_Config(void)
{ 
    TIM_TimeBaseInitTypeDef     TIM_Time_user;
    GPIO_InitTypeDef            port;
    TIM_ICInitTypeDef           TIM_ICInit_user;
    NVIC_InitTypeDef NVIC_InitStructure2;
// TIM4 clock enable
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
// GPIOB clock enable
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
// TIM4 channel_1 configuration : PB6
port.GPIO_Pin   = GPIO_Pin_6;
port.GPIO_Mode  = GPIO_Mode_IN_FLOATING;
port.GPIO_Speed = GPIO_Speed_2MHz;

GPIO_Init(GPIOB, &port);
// Connect TIM pin to AF2

// Enable the TIM4 global Ymtirrupt
NVIC_InitStructure2.NVIC_IRQChannel = TIM4_IRQn;
NVIC_InitStructure2.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure2.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure2.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure2);

// Time base configuration
TIM_Time_user.TIM_Period = PERIOD;
TIM_Time_user.TIM_Prescaler = PRESCALER;
TIM_Time_user.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_Time_user.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM4, &TIM_Time_user);

//Capture configuration
TIM_ICInit_user.TIM_Channel = TIM_Channel_1;
TIM_ICInit_user.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInit_user.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInit_user.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInit_user.TIM_ICFilter = 0x0;

//Inicialize our structure for capture signal
TIM_PWMIConfig(TIM4, &TIM_ICInit_user);
//input trigger
TIM_SelectInputTrigger(TIM4, TIM_TS_TI1FP1);

  TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_Reset);
    // Select the slave Mode: Reset Mode
   TIM_SelectMasterSlaveMode(TIM4,TIM_MasterSlaveMode_Enable);

// Enable the TIM4 Counter
TIM4->CR1 |= TIM_CR1_CEN;

// Enable the CC1 Interrupt request
TIM4->DIER |= TIM_IT_CC1;
}

TIM4_CH1的中斷處理程序

void TIM4_IRQHandler(void)
{

if (TIM4->SR & TIM_SR_CC1IF )
    {

        DataCapture_TIM4 = TIM4->CCR2;
    }

}

TIM3_Ch1的捕獲模式

void TIM3_Config(void)
{ 

    TIM_TimeBaseInitTypeDef     TIM_Time_user;
    GPIO_InitTypeDef            port;
    TIM_ICInitTypeDef           TIM_ICInit_user1;
    NVIC_InitTypeDef NVIC_InitStructure1;
// TIM4 clock enable
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
// GPIOB clock enable
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
// TIM channel1 confikurotion : PA6
port.GPIO_Pin   = GPIO_Pin_6;
port.GPIO_Mode  = GPIO_Mode_IN_FLOATING;///////////////////////////////
port.GPIO_Speed = GPIO_Speed_2MHz;

GPIO_Init(GPIOA, &port);

// Enable the TIM3 global interrupt
NVIC_InitStructure1.NVIC_IRQChannel = TIM3_IRQn;
NVIC_InitStructure1.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure1.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure1.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure1);
// Time base configuration
TIM_Time_user.TIM_Period = PERIOD;
TIM_Time_user.TIM_Prescaler = PRESCALER;
TIM_Time_user.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_Time_user.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_Time_user);
//
TIM_ICInit_user1.TIM_Channel = TIM_Channel_1;
TIM_ICInit_user1.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInit_user1.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInit_user1.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInit_user1.TIM_ICFilter = 0x0;
//
TIM_PWMIConfig(TIM3, &TIM_ICInit_user1);
//input trigger
TIM_SelectInputTrigger(TIM3, TIM_TS_TI1FP1);

  TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Reset);
    // Select the slave Mode: Reset Mode
   TIM_SelectMasterSlaveMode(TIM3,TIM_MasterSlaveMode_Enable);
// Enable the TIM3 Counter
TIM3->CR1 |= TIM_CR1_CEN;

// Enable the CC1 Intirrupt Request
TIM3->DIER |= TIM_IT_CC1;
}


Interrupt handler for TIM3_CH1

void TIM3_IRQHandler(void)
{
 if (TIM3->SR & TIM_SR_CC1IF )
    {

        DataCapture_TIM3 = TIM3->CCR2;
    }

}

這里的問題是TIM2_Ch1的捕獲模式

void TIM2_Config(void)
{ 
    TIM_TimeBaseInitTypeDef     TIM_Time_user;
    GPIO_InitTypeDef            port;
    TIM_ICInitTypeDef           TIM_ICInit_user;
    NVIC_InitTypeDef NVIC_InitStructure2;
// TIM4 clock enable
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
// GPIOB clock enable
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
// TIM2 channel_1 configuration : PA0
port.GPIO_Pin   = GPIO_Pin_0;
port.GPIO_Mode  = GPIO_Mode_IN_FLOATING;
port.GPIO_Speed = GPIO_Speed_2MHz;

GPIO_Init(GPIOA, &port);

// Enable the TIM2 global Interrupt
NVIC_InitStructure2.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure2.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure2.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure2.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure2);

// Time base configuration
TIM_Time_user.TIM_Period = PERIOD;
TIM_Time_user.TIM_Prescaler = PRESCALER;
TIM_Time_user.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_Time_user.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_Time_user);
//
TIM_ICInit_user.TIM_Channel = TIM_Channel_1;
TIM_ICInit_user.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInit_user.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM_ICInit_user.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM_ICInit_user.TIM_ICFilter = 0x0;
//
TIM_PWMIConfig(TIM2, &TIM_ICInit_user);
//input trigger
TIM_SelectInputTrigger(TIM2, TIM_TS_TI1FP1);

  TIM_SelectSlaveMode(TIM2, TIM_SlaveMode_Reset);
    // Select the slave Mode: Reset Mode
   TIM_SelectMasterSlaveMode(TIM2,TIM_MasterSlaveMode_Enable);
// Enable the TIM2 Counter
TIM2->CR1 |= TIM_CR1_CEN;
// Enable the CC1 Interrupt Request
TIM2->DIER |= TIM_IT_CC1;
}

TIM2_CH1的中斷處理程序

void TIM2_IRQHandler(void)
{
  if (TIM2->SR & TIM_SR_CC1IF )
    {

        DataCapture_TIM2 = TIM2->CCR2;
    }

}

作為替代答案,我建議您嘗試使用CubeMX 它是STM32的代碼生成器,可以幫助您快速編寫STM32低級驅動程序。

抱歉,我無法添加評論。您可以在調試模式下檢查您的設置寄存器值,並檢查您的工作和CubeMx設置之間的不同以查找問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM