使用STM32定时器

时间:2014-07-27 13:34:09

标签: timer clock stm32

我正在研究STM32的计时器。我需要两个脉冲,如图中所示。

enter image description here

CLK的频率为50 kHz,SI的频率为381.67 Hz。我需要SI在下一个时钟脉冲的上升沿之前变低,但在Keil的逻辑分析仪中,我看到每个脉冲的SI相对于CLK处于不同的位置,如下图所示。我该如何解决?

enter image description here

void Init_TIMER(void)
{
    TIM_TimeBaseInitTypeDef TIM_BaseInitStructure2, TIM_BaseInitStructure3, TIM_BaseInitStructure4;
    TIM_OCInitTypeDef TIM_OCInitStructure2, TIM_OCInitStructure3;

    TIM_DeInit(TIM2);
    TIM_DeInit(TIM3);
    TIM_DeInit(TIM4);

    TIM_InternalClockConfig(TIM2);
    TIM_InternalClockConfig(TIM3);
    TIM_InternalClockConfig(TIM4);

    /************************************************/
    /********TIMER2**********************************/
    /************************************************/
    TIM_BaseInitStructure2.TIM_Period = 180; //180/9000000=..ms 50KHZ                                                                                                                                                                   
    TIM_BaseInitStructure2.TIM_Prescaler = 7; //SYSCLK=72M, TIM1_CLK=72/8=9MHz 
    TIM_BaseInitStructure2.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_BaseInitStructure2.TIM_CounterMode = TIM_CounterMode_Up;
    //TIM_BaseInitStructure.TIM_RepetitionCounter = 0;      
    TIM_TimeBaseInit(TIM2, & TIM_BaseInitStructure2);

    TIM_ClearFlag(TIM2, TIM_FLAG_Update);

    TIM_OCInitStructure2.TIM_OCMode = TIM_OCMode_PWM1;
    TIM_OCInitStructure2.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure2.TIM_Pulse = 90;
    TIM_OCInitStructure2.TIM_OCPolarity = TIM_OCPolarity_Low;
    TIM_OC2Init(TIM2, & TIM_OCInitStructure2);
    TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable);

    /*****************************************************/
    /********TIMER3***************************************/
    /*****************************************************/
    TIM_BaseInitStructure3.TIM_Period = 23580; //23580/9000000=... ms  381.67HZ    50KHZ/131=381.67HZ                                                                                                                                                                   
    TIM_BaseInitStructure3.TIM_Prescaler = 7; //SYSCLK=72M, TIM1_CLK=72/8=9MHz 
    TIM_BaseInitStructure3.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_BaseInitStructure3.TIM_CounterMode = TIM_CounterMode_Up;
    //TIM_BaseInitStructure.TIM_RepetitionCounter = 0;      
    TIM_TimeBaseInit(TIM3, & TIM_BaseInitStructure3);

    TIM_ClearFlag(TIM3, TIM_FLAG_Update);

    TIM_OCInitStructure3.TIM_OCMode = TIM_OCMode_PWM1;
    TIM_OCInitStructure3.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure3.TIM_Pulse = 50;
    TIM_OCInitStructure3.TIM_OCPolarity = TIM_OCPolarity_High;
    TIM_OC2Init(TIM3, & TIM_OCInitStructure3);
    TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);

    /********************************************************/
    /********TIMER4 to make sampling frequency **************/
    /*******************************************************/
    TIM_BaseInitStructure4.TIM_Period = 90; //90/9000000=0.01ms    100KHZ                                                                                                                                                               
    TIM_BaseInitStructure4.TIM_Prescaler = 7; //SYSCLK=72M, TIM1_CLK=72/8=9MHz 
    TIM_BaseInitStructure4.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_BaseInitStructure4.TIM_CounterMode = TIM_CounterMode_Up;
    //TIM_BaseInitStructure.TIM_RepetitionCounter = 0;      
    TIM_TimeBaseInit(TIM4, & TIM_BaseInitStructure4);
    TIM_ARRPreloadConfig(TIM4, DISABLE);
    TIM_ClearFlag(TIM4, TIM_FLAG_Update);

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

    TIM_ARRPreloadConfig(TIM2, ENABLE);

    TIM_Cmd(TIM2, ENABLE);

    TIM_ARRPreloadConfig(TIM3, ENABLE);

    TIM_Cmd(TIM3, ENABLE);

    TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE);
    TIM_Cmd(TIM4, ENABLE);
}

void GPIO_Configuration()
{
    GPIO_InitTypeDef GPIO_InitStructure;
    /*PA1  TIM2_CH2*/
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, & GPIO_InitStructure);
    /*PA7  TIM3_CH2*/
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, & GPIO_InitStructure);
}

1 个答案:

答案 0 :(得分:2)

我有类似的项目,我使用了taos TSL1402R矩阵。 我没有使用定时器,而是使用带有圆形DMA的SPI。 DMA缓冲区为0x0,0x0 ...... 0x0,0x1。

SI连接到SPI MISO, CLK连接到SPI_CLK。

希望这会有所帮助。