当前位置:网站首页>PWM output experiment based on stm32f103zet6 library function
PWM output experiment based on stm32f103zet6 library function
2022-06-29 17:46:00 【It's Beichen bupiacra】
be based on STM32F103ZET6 Library function PWM Output experiment
send use TIM3 The passage of 2, Put the channel 2 Remap to PB5, produce PWM To control DS0 The brightness of .
PWM brief introduction
A little bit more simple , It's the control of pulse width .
STM32 In addition to TIM6 and 7. Other timers can be used to generate PWM Output . Among them, high-level setting Chronometer TIM1 and TIM8 It can produce as many as 7 On the road PWM Output . And universal timers can also generate up to 4 On the road PWM Output , such ,STM32 At most... Can be generated at the same time 30 road PWM Output ! Here we only use TIM3 Of CH2 All the way to PWM Output . If you want to generate multiple outputs , You can make some modifications according to our code .
Configure this through library functions Functional steps . PWM The related functions are set in the library function file stm32f10x_tim.h and stm32f10x_tim.c In file .
1. Turn on TIM3 Clock and multiplexing function clock , To configure PB5 For multiplex output .
To use TIM3, We have to turn on TIM3 The clock of . Here we have to configure PB5 For multiplex output , This is because TIM3_CH2 The channel will be remapped to PB5 On , here ,PB5 It belongs to multiplexing function output . Library functions enable TIM3 The way to clock is :
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); // Enable timer 3 The clock
Library function settings AFIO The way to clock is :
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); // Multiplex clock enable
Here is a brief list of GPIO Just initialize one line of code :
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // Multiplexing push pull output
2. Set up TIM3_CH2 Remap to PB5 On .
because TIM3_CH2 The default is connected to PA7 Upper , So we need to set TIM3_REMAP For partial remapping ( through too AFIO_MAPR To configure ), Give Way TIM3_CH2 Remap to PB5 above . Set the remapping function in the library function The number is :
void GPIO_PinRemapConfig(uint32_t GPIO_Remap, FunctionalState NewState);
STM32 Remapping can only remap to a specific port . first The entry parameter can be understood as setting the type of remapping , such as TIM3 Some remapping entry parameters are GPIO_PartialRemap_TIM3, this Dot can be just as the name suggests . therefore TIM3 The library function implementation method of partial remapping is :
GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, ENABLE);
3. initialization TIM3, Set up TIM3 Of ARR and PSC.
In the open TIM3 After the clock , We need to set ARR and PSC Two registers to control the output PWM Of cycle . When PWM The cycle is too slow ( lower than 50Hz) When , We'll obviously feel the flicker . therefore ,PWM Zhou The period should not be set too small here . This is in the library function by TIM_TimeBaseInit Functionally implemented
TIM_TimeBaseStructure.TIM_Period = arr; // Set auto reload load value
TIM_TimeBaseStructure.TIM_Prescaler =psc; // Set the prescaler value
TIM_TimeBaseStructure.TIM_ClockDivision = 0; // Set the clock split :TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; // Upcount mode
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); // Initialize... According to the specified parameters TIMx Of
4. Set up TIM3_CH2 Of PWM Pattern , Can make TIM3 Of CH2 Output .
We need to set TIM3_CH2 by PWM Pattern ( The default is frozen ), Because of our DS0 It's low power Ping Liang , And we want to be CCR2 When the value is small ,DS0 It's dark ,CCR2 When it's high ,DS0 Just light up , So I We need to configure TIM3_CCMR1 To control TIM3_CH2 The pattern of . In library functions ,PWM Channel setting Setting is through the function TIM_OC1Init()~TIM_OC4Init() To set up , Different channels have different settings , Here I am They use channels 2, So the function used is TIM_OC2Init().
void TIM_OC2Init(TIM_TypeDef* TIMx, TIM_OCInitTypeDef* TIM_OCInitStruct);
Look at the structure TIM_OCInitTypeDef The definition of :
typedef struct
{
uint16_t TIM_OCMode;
uint16_t TIM_OutputState;
uint16_t TIM_OutputNState;
uint16_t TIM_Pulse;
uint16_t TIM_OCPolarity;
uint16_t TIM_OCNPolarity;
uint16_t TIM_OCIdleState;
uint16_t TIM_OCNIdleState;
}TIM_OCInitTypeDef;
Here we will explain several member variables related to our requirements :
Parameters TIM_OCMode The setting mode is PWM Or output comparison , Here we are PWM Pattern .
Parameters TIM_OutputState Used to set the compare output enable , That is to enable PWM Output to port .
Parameters TIM_OCPolarity Used to set whether the polarity is high or low .
Other parameters TIM_OutputNState,TIM_OCNPolarity,TIM_OCIdleState and TIM_OCNIdleState yes Advanced timer TIM1 and TIM8 Just used .
To implement the scenario we mentioned above , The method is :
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; // choice PWM Pattern 2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; // Compare output enable
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; // High output polarity
TIM_OC2Init(TIM3, &TIM_OCInitStructure); // initialization TIM3 OC2
5. Can make TIM3.
After completing the above settings , We need to enable TIM3.
TIM_Cmd(TIM3, ENABLE); // Can make TIM3
6. modify TIM3_CCR2 To control the duty cycle .
After the above settings ,PWM In fact, it has started to output , But its duty cycle and frequency are fixed Of , And we modify it TIM3_CCR2 You can control CH2 The output duty cycle of . And then control DS0 The brightness of .
In library functions , modify TIM3_CCR2 The function of the duty cycle is :
void TIM_SetCompare2(TIM_TypeDef* TIMx, uint16_t Compare2);
For other channels , There is a function name , The function format is TIM_SetComparex(x=1,2,3,4).
Through the above 6 A step , We can control TIM3 Of CH2 Output PWM Wave .
Hardware design
The hardware resources used in this experiment are :
1) Indicator light DS0
2) Timer TIM3
software design
stay timer.c It's added to it The following code :
//TIM3 PWM Partial initialization
//PWM Output initialization
//arr: Auto reload value
//psc: Clock presplitting frequency
void TIM3_PWM_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); // Enable timer 3 The clock
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE); // Can make GPIO Peripherals and AFIO Multiplexing function module clock
GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, ENABLE); //Timer3 Partial remapping TIM3_CH2->PB5
// Set this pin to multiplex output function , Output TIM3 CH2 Of PWM Pulse shape GPIOB.5
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //TIM_CH2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // Multiplexing push pull output
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure); // initialization GPIO
// initialization TIM3
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 = 0; // Set the clock split :TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM Upcount mode
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); // according to TIM_TimeBaseInitStruct The parameter specified in TIMx Unit of time base
// initialization TIM3 Channel2 PWM Pattern
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; // Select timer mode :TIM Pulse width modulation mode 2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; // Compare output enable
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; // Output polarity :TIM High output polarity
TIM_OC2Init(TIM3, &TIM_OCInitStructure); // according to T The specified parameter initializes the peripheral TIM3 OC2
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable); // Can make TIM3 stay CCR2 Pre loaded registers on
TIM_Cmd(TIM3, ENABLE); // Can make TIM3
}
The header file timer.h The difference from the previous one is that TIM3_PWM_Init Statement of
#ifndef __TIMER_H
#define __TIMER_H
#include "sys.h"
void TIM3_Int_Init(u16 arr,u16 psc);
void TIM3_PWM_Init(u16 arr,u16 psc);
#endif
main.c
#include "led.h"
#include "delay.h"
#include "key.h"
#include "sys.h"
#include "usart.h"
#include "timer.h"
int main(void)
{
u16 led0pwmval=0;
u8 dir=1;
delay_init(); // Delay function initialization
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); // Set up NVIC Interrupt grouping 2:2 Bit preemption priority ,2 Bit response priority
uart_init(115200); // The serial port is initialized to 115200
LED_Init(); //LED Port initialization
TIM3_PWM_Init(899,0); // Regardless of the frequency .PWM frequency =72000000/900=80Khz
while(1)
{
delay_ms(10);
if(dir)
{
led0pwmval++;
}
else
{
led0pwmval--;
}
if(led0pwmval>300)
{
dir=0;
}
if(led0pwmval==0)
{
dir=1;
}
TIM_SetCompare2(TIM3,led0pwmval);
}
}
here , We can see from the dead cycle function that , We will led0pwmval This value is set to PWM It's worth , It's just It's through led0pwmval To control PWM Duty cycle of , Then control led0pwmval From 0 Change to 300, And then from 300 Change to 0, So circular , therefore DS0 The brightness will also change from dark to bright , And then it goes from light to dark . As for the In the value of the , Why do we take 300, Because PWM When the output duty cycle of reaches this value , our LED brightness There will be little change ( Although the maximum value can be set to 899), Therefore, it is unnecessary to design too large a value here .
边栏推荐
- Error:Connection refused: connect
- R language uses user-defined functions to write deep learning leaky relu activation functions and visualize leaky relu activation functions
- 基于gis三维可视化的智慧城市行业运用
- 位图的详细介绍及模拟实现
- mysql查询视图命令是哪个
- Does MySQL support foreign keys
- R语言使用自定义函数编写深度学习Leaky ReLU激活函数、并可视化Leaky ReLU激活函数
- 基于注解和拦截器防止表单重复提交
- SCM系统是什么?供应链管理系统有哪些优势?
- 面试中问最常问的海量数据处理你拿捏了没?
猜你喜欢

0 basic self-study STM32 (wildfire) -- use register to light LED -- Explanation of GPIO function block diagram

从一个被应用商店坑了的BUG说起

第42期:MySQL 是否有必要多列分区

如何使用B/S开发工具DevExtreme的图表控件 - 自定义轴位置?

两种Controller层接口鉴权方式

Two controller layer interface authentication methods

Face recognition 4- research on Baidu commercial solutions
![填充每个节点的下一个右侧节点指针[利用好每个点->尽可能降低时空复杂度]](/img/33/bda0a898bfe3503197026d1f62e851.png)
填充每个节点的下一个右侧节点指针[利用好每个点->尽可能降低时空复杂度]

Opencv+yolo-v3 for target tracking

剑桥大学教授:经常吃早餐害处多,很危险 - 知乎
随机推荐
The R language inputs the distance matrix to the hclust function for hierarchical clustering analysis. The method parameter specifies the distance calculation method between two combined data points,
Epoll analysis
剑指 Offer 13. 机器人的运动范围 (BFS)
What is a SCM system? What are the advantages of a supply chain management system?
How to use the chart control of the b/s development tool devextreme - customize the axis position?
DevCloud加持下的青软,让教育“智”上云端
selenium上传文件
基于STM32F103ZET6库函数独立看门狗(IWDG)实验
selenium 组合键操作
Web Scraping with Beautiful Soup for Data Scientist
ISO 32000-2 国际标准7.7
ISO 32000-2 international standard 7.7
Self taught structure (small turtle C language)
selenium 文件上传方法
Basic operations such as MySQL startup under Windows platform
Industry application of smart city based on GIS 3D visualization
Online text digit recognition list summation tool
测试dble split功能执行+导入耗时shell脚本参考
R语言dplyr包filter函数通过组合逻辑(与逻辑)过滤dataframe数据中的数据、其中一个字段的内容等于指定向量中的其中一个,并且另外一个字段值大于某一阈值
Mac installation php7.2