当前位置:网站首页>[STM32 learning] (8) stm32f1 general timer configuration
[STM32 learning] (8) stm32f1 general timer configuration
2022-07-24 09:50:00 【Use small materials】
(1) Enable timer clock
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);// Can make TIM4 The clock
(2) Initialize timer parameters , Contains auto reload values , Division coefficient , Counting method, etc
voidTIM_TimeBaseInit(TIM_TypeDef*TIMx,TIM_TimeBaseInitTypeDef* TIM_TimeBaseInitStruct);
typedef struct
{
uint16_t TIM_Prescaler; // Timer prescaler
uint16_t TIM_CounterMode; // Count mode
uint32_t TIM_Period; // Timer cycle
uint16_t TIM_ClockDivision; // The clock frequency division
uint8_t TIM_RepetitionCounter; // Repeat counter
} TIM_TimeBaseInitTypeDef;
After understanding the functions of structure members , You can configure , for example :
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_TimeBaseInitStructure.TIM_Period=1000; // Auto load values
TIM_TimeBaseInitStructure.TIM_Prescaler=35999; // Division coefficient
TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up; // Set up count mode
TIM_TimeBaseInit(TIM4,&TIM_TimeBaseInitStructure);
The calculation formula of timer timing time is as follows :Tout= ((per+1)*(psc+1))/Tclk;
(3) Set timer interrupt type , And enable
void TIM_ITConfig(TIM_TypeDef* TIMx, uint16_t TIM_IT, FunctionalState NewState);
(4) Set timer interrupt priority , Enable timer interrupt channel
NVIC The initialization library function is NVIC_Init();
typedef struct
{
uint8_t NVIC_IRQChannel; // Interrupt source
uint8_t NVIC_IRQChannelPreemptionPriority; // preemption
uint8_t NVIC_IRQChannelSubPriority; // Response priority
FunctionalState NVIC_IRQChannelCmd; // Interrupt enable or disable
} NVIC_InitTypeDef;
After understanding the functions of structure members , You can configure , for example :
NVIC_InitTypeDef NVIC_InitStruct;
NVIC_InitStruct.NVIC_IRQChannel=TIM4_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=2;
NVIC_InitStruct.NVIC_IRQChannelSubPriority=3;
NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStruct);
(5) Turn on timer
void TIM_Cmd(TIM_TypeDef* TIMx, FunctionalState NewState);
(6) Write timer interrupt service function
TIM4_IRQHandler
ITStatus TIM_GetITStatus(TIM_TypeDef* TIMx, uint16_t TIM_IT);
if(TIM_GetITStatus(TIM4,TIM_IT_Update))
{
...// perform TIM4 Update interrupt internal control
}
void TIM_ClearITPendingBit(TIM_TypeDef* TIMx, uint16_t TIM_IT);
There are also two functions in the firmware library to read the status flag bit and clear the interrupt flag bit , The functions are TIM_GetFlagStatus and TIM_ClearFlag
The code is as follows :
main.c
#include "stm32f10x.h" // Device header
#include "stm32f10x_gpio.h"
#include "sys.h"
#include "led.h"
void delay(void);
unsigned char cnt=0;
// Universal timer 3 Interrupt initialization
// Here, the clock is selected as APB1 Of 2 times , and APB1 by 36M
//arr: Auto reload value .
//psc: Clock presplitting frequency
// Here's a timer 3!
void TIM3_Int_Init(u16 arr,u16 psc)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); // Clock enable
// Timer TIM3 initialization
TIM_TimeBaseStructure.TIM_Period = arr; // Set the value of the auto reload register cycle for the next update event load activity
TIM_TimeBaseStructure.TIM_Prescaler =psc; // Set as TIMx Prescaled value of clock frequency divisor
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; // Set the clock split :TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM Upcount mode
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); // Initialize... According to the specified parameters TIMx Unit of time base
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE ); // Enable to designate TIM3 interrupt , Allow update interrupt
// Interrupt priority NVIC Set up
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; //TIM3 interrupt
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; // Take precedence 0 level
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; // From the priority 3 level
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ The channel is energized
NVIC_Init(&NVIC_InitStructure); // initialization NVIC register
TIM_Cmd(TIM3, ENABLE); // Can make TIMx
}
// Timer 3 Interrupt service routine
void TIM3_IRQHandler(void) //TIM3 interrupt
{
if (TIM_GetITStatus(TIM3, TIM_IT_Update) != RESET) // Check TIM3 Whether the update interrupt occurs or not
{
cnt++;
TIM_ClearITPendingBit(TIM3, TIM_IT_Update ); // eliminate TIMx Update interrupt flag
if(cnt>=15)
{
LED1=!LED1;
cnt=0;
}
}
}
//LED1 The flashing period is 500ms,LED0 The flashing period is 200ms, The phenomenon seen is LED1 Slow flicker ,LED0 Flash fast
int main(void)
{
LED_init_wt();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); // Set up NVIC Interrupt grouping 2:2 Bit preemption priority ,2 Bit response priority
TIM3_Int_Init(4999,7199);//10Khz The counting frequency of , Count to 5000 by 500ms
while(1)
{
LED0=!LED0;
delay();
}
}
void delay(void)
{
int i,j;
for(i=0;i<400;i++)
{
for(j=0;j<400;j++);
}
}
led.h
#ifndef __LED_H
#define __LED_H
#include "sys.h"
//
#define LED0 PCout(0)// PC0
#define LED1 PCout(1)// PC1
#define LED2 PCout(2)// PC2
void LED_init_wt(void);// initialization
#endif
led.c
#include "led.h"
void LED_init_wt(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE); // To enable or disable APB2 Peripheral clock
GPIO_InitTypeDef GPIO_InitStruct; // Defining structure Contains peripherals GPIO Configuration information
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1; // Select all pins
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP; // Push pull output
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; // Maximum output rate 50MHz
GPIO_Init(GPIOC,&GPIO_InitStruct); // according to GPIO_InitStruct The parameter specified in GPIOA register
}The code is done . Please test the effect by yourself .O(∩_∩)O
边栏推荐
- OPENCV学习DAY5
- [don't bother with reinforcement learning] video notes (I) 2. Summary of reinforcement learning methods
- Basic knowledge of PHP - complete collection of PHP functions
- Cess test online line! The first decentralized storage network to provide multiple application scenarios
- Learning transformer: overall architecture and Implementation
- 财务数字化转型
- ASI-20220222-Implicit PendingIntent
- Write a simple memo using localstorage
- 云原生(十二) | Kubernetes篇之Kubernetes基础入门
- Web page opening speed is very slow, how to solve it?
猜你喜欢

ASI-20220222-Implicit PendingIntent

The most complete solution for distributed transactions

Es document CRUD

Detailed LinkedList

Do you really understand the concept of buffer? Take you to uncover the buffer zone~
![Calculate CPU utilization [Prometheus]](/img/00/d9f297e3013cabbf3d41be58105fc7.png)
Calculate CPU utilization [Prometheus]

error: field ‘XXX’ declared as a function

07 Jason module

Openstack network neutron knowledge point "openstack"

What if path is deleted by mistake when configuring system environment variables?
随机推荐
Arduino serial port information reading and output
Racecar multi-point navigation experiment based on ROS communication mechanism
[don't bother with reinforcement learning] video notes (I) 1. What is reinforcement learning?
PHP Basics - session control - cookies
CAS principle [concurrent programming]
With 8 years of product experience, I have summarized these practical experience of continuous and efficient research and development
[robot learning] mechanism kinematics analysis and MATLAB simulation (3D model +word report +matlab program)
配置系统环境变量的时候误删了Path怎么办?
What if path is deleted by mistake when configuring system environment variables?
Embedded development: Tools - optimizing firmware using DRT
Use of jstack "JVM common commands"
Firewall off and on command
华为无线设备安全策略配置命令
[don't bother to strengthen learning] video notes (III) 3. SARS (lambda)
PHP Basics - session control - Session
S2b2b system standardizes the ordering and purchasing process and upgrades the supply chain system of household building materials industry
Centos7 install mysql8.0
Compilation and linking of programs
Synchronized scope "concurrent programming"
Cess test online line! The first decentralized storage network to provide multiple application scenarios