当前位置:网站首页>STM32__ 03 - beginner timer
STM32__ 03 - beginner timer
2022-04-23 16:46:00 【The God of C language】
One ,TIM brief introduction
TIM The essence of is the counter , Count the input clock , The reference clock is the main frequency 72MHZ, Without frequency division, it can generate 72M Pulse .TIM Sub advanced , Universal , Basic timer , I'm using f102c8t6 Only TIM1~TIM4 Four timers , among TIM1 For advanced timer , Others are general timers , This time mainly focuses on the general timer .
1, General timer internal structure
On the left is the clock input , This time we mainly understand TIMx_ETR External clock and internal clock TIMxCLK,TIMx_CH1, Capture... For input , This time does not involve .
The above structure is too complex. Here we refer to B A picture made by Jiangke University
For this picture , We just need to initialize the timer from left to right
2, Initialize the timer
1) First, enable the internal clock . Turn on TIM2( Used this time is TIM) The clock , Use internal clock mode .
The functions used are :
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
Both basic and general timers are connected to APB1 Bus , The advanced timer is connected to APB2 Bus .
TIM_InternalClockConfig(TIM2);
Use the internal clock , Even if this function is not called, the system uses the internal clock by default .
2) Configuration of time base unit , And configuration GPIO be similar , It is to configure structural variables
TIM_TimeBaseInitTypeDef TIM_TimerBaseStructure;
Define the structure variable name
TIM_TimerBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
The clock frequency division , Not used here .
TIM_TimerBaseStructure.TIM_CounterMode=TIM_CounterMode_Up ;
counter mode , Here, select the normal up count
TIM_TimerBaseStructure.TIM_Period=10000-1;
Yes ARR Automatic reloader for configuration
TIM_TimerBaseStructure.TIM_Prescaler=7200-1;
Yes PSC The prescaler is configured
Be careful : Here we use a timed one second operation , You can refer to the formula
-1 To eliminate errors , We can understand that , The frequency of the internal clock is 72MHZ, First use the prescaler to divide into 7200 Share , So each is 10000HZ, Then we use a capacity of 10000 To calculate the pulse ,10000HZ Generate... For one second 10000 Pulse , In this way, the counter is filled up in one second , Create spillover .
TIM_TimerBaseStructure.TIM_RepetitionCounter=0;
Repeat counter , Advanced timer , We have... Here 0.
TIM_TimeBaseInit(TIM2,&TIM_TimerBaseStructure);
Call initialization function , Complete the configuration of time base unit
3) Interrupt enable
TIM_ClearFlag(TIM2,TIM_FLAG_Update);
This function clears the interrupt flag bit , Why this extra step ? We can have a look TIM_TimeBaseInit() The definition of
Generate an update event immediately to reload the prescaler and repeat counter values , It can be understood as , An update event will be generated immediately after initializing the time base unit , Cause interrupt flag position 1, Enter the interrupt function immediately after reset , Will cause the program to run one step ahead , So don't forget this step after initializing the time base unit .
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
Update interrupted to NVIC
4)NVIC Configuration of
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC Priority groups
NVIC_InitTypeDef NVIC_InitStructure;
Structure variable naming
NVIC_InitStructure.NVIC_IRQChannel=TIM2_IRQn ;
Timer TIM2 stay NCIV Interrupt channel
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
Channel enable
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;
priority
NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
priority
NVIC_Init(&NVIC_InitStructure);
NVIC Initialization function
TIM_Cmd(TIM2,ENABLE);
Enable or disable the specified TIM peripherals
5) Interrupt the call of the service function
void TIM2_IRQHandler();
A complete timer initialization is complete .
Two , Code section
1,Timer.c
#include "stm32f10x.h" // Device header
void Timer_Init()
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);// Turn on TIM2 The clock , Note that APB1Z Bus
TIM_InternalClockConfig(TIM2);// After power on, the internal clock is used by default , But for the integrity of the program , Still called this function
// Initialization of time base unit
TIM_TimeBaseInitTypeDef TIM_TimerBaseStructure;
TIM_TimerBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;// The clock frequency division , Filtering uses
TIM_TimerBaseStructure.TIM_CounterMode=TIM_CounterMode_Up ;// Count up
TIM_TimerBaseStructure.TIM_Period=10000-1;//ARR The value of the automatic reloader
TIM_TimerBaseStructure.TIM_Prescaler=7200-1;//PSC Value of prescaled frequency
// Here can be understood as giving 72MHZ Signal division 7200 Share , Each one is 10000hz, Then use a capacity of 10000 The container of
// Go and pack , Each fill is 1S
TIM_TimerBaseStructure.TIM_RepetitionCounter=0;// Repeat counter , Advanced timer
TIM_TimeBaseInit(TIM2,&TIM_TimerBaseStructure);
// To interrupt
TIM_ClearFlag(TIM2,TIM_FLAG_Update);
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);// Update interrupted to NVIC
//NVIC To configure
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//NVIC Priority groups
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel=TIM2_IRQn ;// Timer TIM2 stay NCIV Interrupt channel
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;// priority
NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;// priority
NVIC_Init(&NVIC_InitStructure);
// Start timer
TIM_Cmd(TIM2,ENABLE);
}
// Interrupt service function
//void TIM2_IRQHandler()
//{
// if(TIM_GetITStatus(TIM2,TIM_IT_Update)==SET)
// {
// TIM_ClearITPendingBit(TIM2,TIM_IT_Update);
// }
//}
2,oled.c
#include "stm32f10x.h" // Device header
void LED_Init()
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1 | GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_SetBits(GPIOA,GPIO_Pin_1 | GPIO_Pin_2);
}
void LED1_ON()
{
GPIO_ResetBits(GPIOA,GPIO_Pin_1);
}
void LED1_OFF()
{
GPIO_SetBits(GPIOA,GPIO_Pin_1);
}
void LED1_Turn()
{
if(GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_1)==0)
{
GPIO_SetBits(GPIOA,GPIO_Pin_1);
}
else
{
GPIO_ResetBits(GPIOA,GPIO_Pin_1);
}
}
void LED2_Turn()
{
if(GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_2)==0)
{
GPIO_SetBits(GPIOA,GPIO_Pin_2);
}
else
{
GPIO_ResetBits(GPIOA,GPIO_Pin_2);
}
}
void LED2_ON()
{
GPIO_ResetBits(GPIOA,GPIO_Pin_2);
}
void LED2_OFF()
{
GPIO_SetBits(GPIOA,GPIO_Pin_2);
}
3,main.c
#include "stm32f10x.h" // Device header
#include "Delay.h"
#include "LED.h"
#include "Key.h"
#include "Buzzer.h"
#include "OLED.h "
#include "Timer.h"
uint16_t Num;
int main()
{
OLED_Init();
Timer_Init();
OLED_ShowString(1,1,"Num:");
while(1)
{
OLED_ShowNum(1,5,Num,5);
OLED_ShowNum(2,5,TIM_GetCounter(TIM2),5);
}
}
void TIM2_IRQHandler()
{
if(TIM_GetITStatus(TIM2,TIM_IT_Update)==SET)
{
Num++;
TIM_ClearITPendingBit(TIM2,TIM_IT_Update);
}
}
Realization Num Add per second 1, And display on the screen .
3、 ... and , summary
stm32 It took a long time to figure out . Everything is difficult at the beginning , But I didn't know it would be so difficult .
版权声明
本文为[The God of C language]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231640379753.html
边栏推荐
- On the value, breaking and harvest of NFT project
- Ali developed three sides, and the interviewer's set of combined punches made me confused on the spot
- PHP 零基础入门笔记(13):数组相关函数
- Solution of garbled code on idea console
- [pimf] openharmony paper Club - what is the experience of wandering in ACM survey
- There is a problem with the light switch from 1 to 100
- Loading order of logback configuration file
- Phpstudy V8, a commonly used software for station construction 1 graphic installation tutorial (Windows version) super detailed
- Detailed explanation of UWA pipeline function | visual configuration automatic test
- 安装及管理程序
猜你喜欢
Selenium IDE and XPath installation of chrome plug-in
详解牛客----手套
批量制造测试数据的思路,附源码
On the security of key passing and digital signature
MySQL master-slave synchronization pit avoidance version tutorial
Idea of batch manufacturing test data, with source code
Modify the test case name generated by DDT
VMware Workstation cannot connect to the virtual machine. The system cannot find the specified file
How magical is the unsafe class used by all major frameworks?
The solution of not displaying a whole line when the total value needs to be set to 0 in sail software
随机推荐
Set the color change of interlaced lines in cells in the sail software and the font becomes larger and red when the number is greater than 100
Use case labeling mechanism of robot framework
Introduction to new functions of camtasia2022 software
Detailed explanation of Niuke - Gloves
[pyGame games] how did angry birds, a mobile game that became popular all over the world 10 years ago, dominate the list? Classic return
◰GL-着色器处理程序封装
Redis "8" implements distributed current limiting and delay queues
Modify the test case name generated by DDT
05 Lua 控制结构
100 deep learning cases | day 41 - convolutional neural network (CNN): urbansound 8K audio classification (speech recognition)
Change the icon size of PLSQL toolbar
Knowledge points and examples of [seven input / output systems]
File system read and write performance test practice
Take according to the actual situation, classify and summarize once every three levels, and see the figure to know the demand
Creation of RAID disk array and RAID5
Custom implementation of Baidu image recognition (instead of aipocr)
Install MySQL on MAC
RAID磁盘阵列与RAID5的创建
伪分布安装spark
MySql主从复制