当前位置:网站首页>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
边栏推荐
- MySQL master-slave replication
- How does flash cache data in memory?
- loggie 源码分析 source file 模块主干分析
- 博士申请 | 厦门大学信息学院郭诗辉老师团队招收全奖博士/博后/实习生
- Idea of batch manufacturing test data, with source code
- How much do you know about the process of the interview
- Calculate pie chart percentage
- 阿里研发三面,面试官一套组合拳让我当场懵逼
- 批量制造测试数据的思路,附源码
- Bytevcharts visual chart library, I have everything you want
猜你喜欢
VMware Workstation cannot connect to the virtual machine. The system cannot find the specified file
1959年高考数学真题
Detailed explanation of Niuke - Gloves
Installation and management procedures
磁盘管理与文件系统
RAID磁盘阵列与RAID5的创建
无线鹅颈麦主播麦手持麦无线麦克风方案应当如何选择
On the security of key passing and digital signature
Do you really understand the principle of code scanning login?
漫画:什么是IaaS、PaaS、SaaS?
随机推荐
◰GL-阴影贴图核心步骤
信息摘要、数字签名、数字证书、对称加密与非对称加密详解
PyTorch:train模式与eval模式的那些坑
LVM与磁盘配额
vscode如何比较两个文件的异同
∑GL-透视投影矩阵的推导
Deepinv20 installation MariaDB
阿里研发三面,面试官一套组合拳让我当场懵逼
About background image gradient()!
File upload and download of robot framework
5-minute NLP: text to text transfer transformer (T5) unified text to text task model
详解牛客----手套
Day 10 abnormal mechanism
Detailed explanation of gzip and gunzip decompression parameters
Mock test using postman
安装及管理程序
Regular filtering of Intranet addresses and segments
DanceNN:字节自研千亿级规模文件元数据存储系统概述
众昂矿业:萤石浮选工艺
Log4j output log information to file