General-purpose timer configuration based on stm32 microcontroller
The timer resources of the stm32 MCU are quite rich. Its timers are divided into advanced control timers, general-purpose timers and basic timers. In particular, the series of timer resources have to be viewed in different manuals. What is the difference between them? I just came into contact with this. It is also awkward to look at his data sheet. The main reason is that it is just beginning to touch, and those functions are not used. Any kind of timer can be used anytime. Here I will summarize my own method and short experience of configuring the general-purpose timer, in case I forget it later. I configured Timer 2 (TIM2).
The general-purpose timer clock can come from external or internal, and the default is internal. The clock source for the general-purpose timer is the APB1 bus, so first, the APB1 peripheral clock must be turned on.
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
Secondly, it is the configuration of the relevant parameter information of the timer. These parameter information are all in the structure of TIM_TImeBaseInitTypeDef, so it is necessary to define such a structure variable. The relevant members of the structure have the following meanings:
TIM_TimeBaseInitStrcture.TIM_Prescaler = 0x1c20; //clock prescaler coefficient, you can see the meaning of the word, that is, the APB1 clock is divided accordingly to get a suitable frequency.
TIM_TimeBaseInitStrcture.TIM_CounterMode = TIM_CounterMode_Up; //Count mode
TIM_TimeBaseInitStrcture.TIM_Period = 0x270f; / / count cycle, that is, an automatic reload value filled into the ARR register, this value is equal to the target value -1
TIM_TimeBaseInitStrcture.TIM_ClockDivision=TIM_CKD_DIV1; //This is the relevant setting of the digital filter. When timing, no external clock is needed, so this is set to 0. TIM_CKD_DIV1 is equivalent to 0. If you set it to other values, it will not be affected.
The following is naturally to initialize the parameter information in this structure.
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStrcture);
Again, the relevant configuration of the timer interrupt, in the configuration interrupt, we must pay special attention to the order, otherwise there will be problems, I did not pay attention to this order in the beginning, looking at the error still do not know how it happened, okay I was discovered later, haha. Always disable the interrupt interrupt flag bit and the interrupt pending bit before enabling the interrupt, as follows:
TIM_ClearFlag(TIM2, TIM_FLAG_Update);
TIM_ClearITPendingBit(TIM2, TIM_FLAG_Update);
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
If you do not clear the pending bit and the flag bit first, then if the interrupt is enabled, it will immediately enter the interrupt service function. Obviously this is not what we want, I will be solved by this problem. The next step is to enable the timer, TIM_Cmd (TIM2, ENABLE); this is self-evident.
Finally, the NVIC Nested Vector Interrupt Control Register is configured.
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x00);//the second parameter must be a multiple of 0x200
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //2 bits for pre-emption priority, 2 bits for subpriority
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
In fact, I think that when calling the timer configuration function and the NVIC configuration function in the main function, the NVIC configuration function should be called first, because the relevant parameter information such as the interrupt packet is set first, then the timer is turned on, and the timer interrupt can be followed. NVIC information goes to work.
The next step is to write your own interrupt service function in stm32f10x_it.c.
Void TIM2_IRQHandler(void)
{
............
}
At this point, the general-purpose timer configuration is successful.
58 Jack.China 8P8C RJ45 Jack DIP,RJ45 8P8C Singking manufacturer, choose the high quality RJ45 Without LED Indicator,EMI Modular Connector, etc.
The RJ-45 interface can be used to connect the RJ-45 connector. It is suitable for the network constructed by twisted pair. This port is the most common port, which is generally provided by Ethernet hub. The number of hubs we usually talk about is the number of RJ-45 ports. The RJ-45 port of the hub can be directly connected to terminal devices such as computers and network printers, and can also be connected with other hub equipment and routers such as switches and hubs.
8P8C RJ45 Jack DIP,RJ45 8P8C Singking,RJ45 Without LED Indicator,EMI Modular Connector
ShenZhen Antenk Electronics Co,Ltd , https://www.antenkwire.com