当前位置:网站首页>[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
边栏推荐
- error: field ‘XXX’ declared as a function
- The most complete solution for distributed transactions
- Arduino drive Lora module node
- Getting started with web security - open source firewall pfsense installation configuration
- Source insight 3.5 comment garbled
- Openstack network neutron knowledge point "openstack"
- PHP Basics - PHP magic constants
- How to improve office efficiency through online collaborative documents
- Cloud primordial (12) | introduction to kubernetes foundation of kubernetes chapter
- Cess test online line! The first decentralized storage network to provide multiple application scenarios
猜你喜欢

程序的编译与链接

Spark Learning: using RDD API to implement inverted index

Development history of the first commercial humanoid biped robot in China
![[robot learning] mechanism kinematics analysis and MATLAB simulation (3D model +word report +matlab program)](/img/dd/d29a5be7306580ad388ba6487d230f.png)
[robot learning] mechanism kinematics analysis and MATLAB simulation (3D model +word report +matlab program)

Common evaluation indexes of medical image segmentation

MySQL query database capacity size

Spark Learning: a form of association in a distributed environment?

Web page opening speed is very slow, how to solve it?

Leetcode skimming: dynamic planning 03 (climb stairs with minimum cost)
![[C language] implementation of three versions of address book small project (including source code)](/img/3b/926001332ec05378de4c35dc28ed55.png)
[C language] implementation of three versions of address book small project (including source code)
随机推荐
What's the difference between testing / developing programmers' professionalism and salted fish? They don't want to be excellent coders?
Centos7 install mysql8.0
An article takes you to understand the operation of C language files in simple terms
Hucang integrated e-commerce project (I): introduction to the project background and structure
Raspberry Pie: serial port login does not display print information
CUDA day 2: GPU core and Sm core components [easy to understand]
JS 84*148=b6a8 how many decimal places can you make both sides equal
Spark Learning: compile spark source code in win10
Detailed LinkedList
[don't bother to strengthen learning] video notes (III) 2. SARS learning realizes maze walking
Vscode failed to use SSH Remote Connection (and a collection of other problems)
ASI-20220222-Implicit PendingIntent
How to improve office efficiency through online collaborative documents
Arduino- how to light the LED?
Arduino drive Lora module master node
Foreign lead operation takes one month to collect money, and the sideline still needs it
[200 opencv routines] 236. Principal component analysis of feature extraction (openCV)
[don't bother with reinforcement learning] video notes (I) 3. Why use reinforcement learning?
Little dolphin "transformed" into a new intelligent scheduling engine, which can be explained in simple terms in the practical development and application of DDS
JS string method