3435 views|1 replies

1w

Posts

16

Resources
The OP
 

【ufun learning】Research on timer clock [Copy link]


For the timer, all I want to study is a sentence, and this sentence always makes me think about it.


Some readers think that what is said is not controversial. But practice will bring different results.

The following code is the general code for initialization:

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
    //???????
    TIM_ClearITPendingBit(TIM2,TIM_IT_Update);//TIM_ClearFlag(TIM2,TIM_FLAG_Update);//??????

    //????????0-0xffff,72M/36000=2000
    TIM_TimeBaseStructure.TIM_Period = arr;
    //????????0-oxffff,???36000??
    TIM_TimeBaseStructure.TIM_Prescaler = psc;
    ////???????????
    //????,???0,??????
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;

Is TIM_CKD_DIV1 a prescaler? ? ? ? ?

I checked the book:

Obviously, it is not a prescaler. Then only the prescaler is it. Some readers said, isn’t this a waste of words, does it say prescaler? ? ? ?

I am not very proficient in English.

The question arises again, who would set the prescaler to 1 for no reason? ? ? ?

I think the reason why this has not been a problem until now is that everyone has copied it, and no routine will set it to 1. So, is it really 36MHZ if it is set to 1? ? ? ?

I use the following proof:

#include "stm32f10x.h"
#include "stdio.h"
#include "main.h"
#include "stdlib.h"

#define B_ON GPIO_ResetBits(GPIOA,GPIO_Pin_0)
#define B_OFF GPIO_SetBits(GPIOA,GPIO_Pin_0)

void GPIO_Config(void);
void TIM2_Config(u16 arr,u16 psc);
void NVIC_Config(void);

uint32_t count;
//???
int main(void)
{
    GPIO_Config();
    //((1+arr )/72M)*(1+psc )=((1+1999)/72M)*(1+35999)=1?
    //TIM2_Config(1999,35999);
	  TIM2_Config(35999,1);
    NVIC_Config();
    while(1);

}

//???GPIO
void GPIO_Config(void)
{
    GPIO_InitTypeDef GPIO_InitStructure;
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);

    //PC13?
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
    GPIO_Init(GPIOA,&GPIO_InitStructure);
    GPIO_ResetBits(GPIOA,GPIO_Pin_0); //??LED
}


void TIM2_Config(u16 arr,u16 psc)
{
   
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
    TIM_ClearITPendingBit(TIM2,TIM_IT_Update);//TIM_ClearFlag(TIM2,TIM_FLAG_Update);//??????

    
    TIM_TimeBaseStructure.TIM_Period = arr;
  
    TIM_TimeBaseStructure.TIM_Prescaler = psc;
   
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
   
    
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

    TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);

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



void NVIC_Config(void)
{
    NVIC_InitTypeDef NVIC_InitStructure;
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
    NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);
}



//--------------------stm32f10x_it.c------------------------

void TIM2_IRQHandler(void)
{
    
    if(TIM_GetITStatus(TIM2,TIM_IT_Update)!=RESET)
    {
       
      count++;
			TIM_ClearITPendingBit(TIM2,TIM_IT_Update);
			if(count >= 1000)
			{	
    
    GPIO_WriteBit(GPIOA,GPIO_Pin_0,(BitAction)(!GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_0)));
			count = 0;
			}
    }

}





  


 








It is the same as the time without 1. This proves that as long as we do not use 1 in the pre-scaling, the timer clock is twice the APB clock, that is, 72MHZ.

Then again, who would be crazy to set the second parameter to 1?


This content is originally created by EEWORLD forum user ddllxxrr . If you want to reprint or use it for commercial purposes, you must obtain the author's consent and indicate the source

Latest reply

Very good information, thanks for sharing; long live the spirit of sharing. I hope the author will share more information in the future to help more electronics enthusiasts.   Details Published on 2019-7-23 08:29
Personal signaturehttp://shop34182318.taobao.com/
https://shop436095304.taobao.com/?spm=a230r.7195193.1997079397.37.69fe60dfT705yr
 
 

373

Posts

0

Resources
2
 

Very good information, thanks for sharing;
long live the spirit of sharing.
I hope the author will share more information in the future to help more electronics enthusiasts.

Personal signature很高兴认识大家,请多关照
 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Related articles more>>
Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list