当前位置:网站首页>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
边栏推荐
- Oracle data pump usage
- RAID磁盘阵列与RAID5的创建
- How magical is the unsafe class used by all major frameworks?
- vscode如何比较两个文件的异同
- Detailed explanation of file operation (2)
- Derivation of Σ GL perspective projection matrix
- Findstr is not an internal or external command workaround
- The solution of not displaying a whole line when the total value needs to be set to 0 in sail software
- VMware Workstation cannot connect to the virtual machine. The system cannot find the specified file
- Database dbvisualizer Pro reported file error, resulting in data connection failure
猜你喜欢
How much do you know about the process of the interview
Use itextpdf to intercept the page to page of PDF document and divide it into pieces
LVM与磁盘配额
Nacos 详解,有点东西
如何建立 TikTok用户信任并拉动粉丝增长
On the security of key passing and digital signature
NVIDIA graphics card driver error
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
RAID磁盘阵列与RAID5的创建
ACL 2022 | dialogved: a pre trained implicit variable encoding decoding model for dialogue reply generation
随机推荐
Loggie source code analysis source file module backbone analysis
人脸识别框架之dlib
ByteVCharts可视化图表库,你想要的我都有
Custom implementation of Baidu image recognition (instead of aipocr)
MySQL master-slave replication
About background image gradient()!
【Pygame小游戏】10年前风靡全球的手游《愤怒的小鸟》,是如何霸榜的?经典回归......
◰GL-阴影贴图核心步骤
详解牛客----手套
Easyexcel reads the geographical location data in the excel table and sorts them according to Chinese pinyin
Bytevcharts visual chart library, I have everything you want
阿里研发三面,面试官一套组合拳让我当场懵逼
Nifi fast installation and file synchronization
Gartner 发布新兴技术研究:深入洞悉元宇宙
扫码登录的原理你真的了解吗?
∑GL-透视投影矩阵的推导
Sail soft calls the method of dynamic parameter transfer and sets parameters in the title
PHP 零基础入门笔记(13):数组相关函数
聊一聊浏览器缓存控制
Report FCRA test question set and answers (11 wrong questions)