当前位置:网站首页>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 .
边栏推荐
- R语言ggplot2可视化:使用patchwork包(直接使用加号+)将两个ggplot2可视化结果横向组合、接着再和第三个图像横向组合起来(三幅图各占比例为50%、25%、25%)
- Mysql database literacy, do you really know what a database is
- mysql支持外键吗
- Multi mode concurrent implementation of tortoise and rabbit race in go language
- 两种Controller层接口鉴权方式
- sequential detector
- mysql视图能不能创建索引
- 第42期:MySQL 是否有必要多列分区
- 基于注解和拦截器防止表单重复提交
- How to create a virtual image
猜你喜欢

布隆过滤器:

How to create a virtual image

Tencent cloud released orbit, an automated delivery and operation and maintenance product, to promote enterprise applications to be fully cloud native

迈动互联中标大家保险集团
![[webdriver] upload files using AutoIT](/img/69/8c27626d515976b47f1df4831d09c8.png)
[webdriver] upload files using AutoIT

How to solve the 2003 error of MySQL in Linux

mysql支持外键吗

剑桥大学教授:经常吃早餐害处多,很危险 - 知乎

The soft youth under the blessing of devcloud makes education "smart" in the cloud

基于注解和拦截器防止表单重复提交
随机推荐
ISO 32000-2 国际标准7.7
[webdriver] upload files using AutoIT
自定义HandlerInterceptor拦截器实现用户鉴权
Basic operations such as MySQL startup under Windows platform
数字孪生能源系统,打造低碳时代“透视”眼
MATLAB 最远点采样(FPS)
What are the usage scenarios for locks in MySQL
0 basic self-study STM32 (wildfire) -- use register to light LED -- Explanation of GPIO function block diagram
Does MySQL support foreign keys
mac安装php7.2
On adding and subtracting dates
Open source warehouse contribution - submit pr
R language ggplot2 visualization: use the patchwork package (directly use the plus sign +) to horizontally combine a ggplot2 visualization result and a plot function visualization result to form the f
第42期:MySQL 是否有必要多列分区
3h精通OpenCV(九)-最简单的人脸检测
SRM供应商协同管理系统功能介绍
Set double click to run the jar file
Sword finger offer 13 Robot range of motion (BFS)
Graduation season | Huawei experts teach interview tips: how to get a high salary offer from a large factory?
mysql游标的作用是什么