当前位置:网站首页>[Jiangsu University Self-Chemistry Association stm32F103c8t6] Notes [Introduction to 32 MCUs and Using TIM Output to Compare and Configure PWM]
[Jiangsu University Self-Chemistry Association stm32F103c8t6] Notes [Introduction to 32 MCUs and Using TIM Output to Compare and Configure PWM]
2022-07-30 07:14:00 【Clockwisee】
接着上一篇笔记写
八、OC输出比较
- OC (output compare) 输出比较 比较CNT 和 CCR 寄存器的关系
- IC (input compare) 输入捕获
- CC (capture compare)输入捕获 and output compare unit
CNT计数
CCRis a value we give
九、PWM配置
Afterglow and persistence of human vision(控制LED灯、电机、舵机(pwm作为一种通信协议)等)--------------------------------------->惯性的系统
一般来说,Only use the ones in the red box above
————————————————————————————想要输出PWM波,You need to open the following modules————————————————————————
1、RCC开启时钟
打开TIM外设和GPIO外设时钟
2、配置时基单元
包括前面的时钟源选择 + 时基单元配置
This code was written before
3、配置输出比较单元
里面包括CCR的值、输出比较模式、极性选择、The output enables these parameters,In the library function, it is also configured with a unified structure
4、配置GPIO
把PWM对应的GPIO口,初始化为复用推挽输出的配置(GPIOThe initialization code is used to be placed above)
5、运行控制
启动计数器,这样就能输出PWM了
——————————————————————————————————————————————————————————————
下面展示一下代码(Including remapping):
#include "stm32f10x.h" // Device header
void PWM_Init(void)
{
//RCC开启时钟,和TIMThe same as the timed interrupt
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
//RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); //Pin remapping is usedAFIO,开启AFIO时钟
//GPIO_PinRemapConfig(GPIO_PartialRemap1_TIM2, ENABLE); //Pin remapping configuration (部分重映射1,使能) 这样就能把PA0换成PA15了 //查表TIM2复用功能重映像
//GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);//解除JTAG调试端口,可以使用PA15,PB3,PB4(Usually we liftJTAG的调试端口,保留SWD调试端口),GPIO_PinRemapConfig()is the remapping function
//总结:如果你想让PA15、PB3、PB4These three pins are treated as GPIO来使用的话,Just add the comment above1、3句,先打开AFIO,再用AFIO将JTAGmultiplex release
// If you want to remap the multiplexed pins of timers or other peripherals,Then add the commented out above1、2句,先打开AFIO,再用AFIORemap pins multiplexed by peripherals
// If the pin you remapped happens to be the debug port,The three sentences commented out above must be added,打开AFIO,Remap pins multiplexed by peripherals,Release the debug port
//4、配置GPIO
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //Select multiplexed push-pull output
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //重映射之后 GPIO_Pin_15;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//2、配置时基单元
TIM_InternalClockConfig(TIM2);//Select the clock for the time base unit,选择内部时钟,可以不写,Because the internal clock is selected by default after the timer is powered on
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStructure.TIM_Period = 100 - 1; //ARR=====固定住的话,周期就是100
TIM_TimeBaseInitStructure.TIM_Prescaler = 720 - 1; //PSC=====固定住的话,周期就是100
TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStructure);
//3、配置输出比较单元
TIM_OCInitTypeDef TIM_OCInitStructure;//初始化输出比较单元
TIM_OCStructInit(&TIM_OCInitStructure); //给结构体赋初始值 It defines the initial value given by default,Prevent advanced timer errors
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //PWM1模式
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出比较极性 high 高级性 极性不翻转 有效电平为高电平
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 0; //CCR寄存器的值 How much is the percent duty cycle
TIM_OC1Init(TIM2, &TIM_OCInitStructure);//PA0The port corresponds to the first output compare channelOC1
//CCR ARR PSC共同决定pwm的周期和占空比 *看公式
//5、运行控制
TIM_Cmd(TIM2, ENABLE);
}
void PWM_SetCompare1(uint16_t Compare)
{
TIM_SetCompare1(TIM2, Compare); //CCR为0,通过setcompare可以设置CCR的值,Change channels individually1的CCR的值
}
Function introduction for output comparison
These four functions are the configuration output comparison module,A function configures a unit
void TIM_OC1Init(TIM_TypeDef* TIMx, TIM_OCInitTypeDef* TIM_OCInitStruct);
void TIM_OC2Init(TIM_TypeDef* TIMx, TIM_OCInitTypeDef* TIM_OCInitStruct);
void TIM_OC3Init(TIM_TypeDef* TIMx, TIM_OCInitTypeDef* TIM_OCInitStruct);
void TIM_OC4Init(TIM_TypeDef* TIMx, TIM_OCInitTypeDef* TIM_OCInitStruct);
Used to assign a default value to the output comparison structure
void TIM_OCStructInit(TIM_OCInitTypeDef* TIM_OCInitStruct);
//——————————————————————————————————————————————**********———————————————————————————————到这里,The configuration of the output comparison is basically complete————————————————————————
单独修改CCR寄存器值的函数————————————————————————————————————**********——————————————————Four functions used to change the duty cycle—————————————————————————
void TIM_SetCompare1(TIM_TypeDef* TIMx, uint16_t Compare1);
void TIM_SetCompare2(TIM_TypeDef* TIMx, uint16_t Compare2);
void TIM_SetCompare3(TIM_TypeDef* TIMx, uint16_t Compare3);
————————————————————————————————————————————————**********——————————
十、重映射(重映像)
使用AFIO复用功能引脚重映射
Remapping method and pin correspondence ,Select a remapping method 查看参考手册
gpio.c Select a remapping method
GPIO_PartialRemap1_TIM2 //部分重映射1
GPIO_PartialRemap2_TIM2//部分重映射2
GPIO_FullRemap_TIM2//完全重映射
GPIO_PinRemapConfig()is the remapping function
—————————————————————————————————————————————————————————————————————————
Unlock the debug port parameter:
* @arg GPIO_Remap_SWJ_NoJTRST : Full SWJ Enabled (JTAG-DP + SW-DP) but without JTRST//解除JTRST引脚的复用 PB4------>GPIO
* @arg GPIO_Remap_SWJ_JTAGDisable : JTAG-DP Disabled and SW-DP Enabled//解除JTAGMultiplexing of debug ports PA15、PB3、PB4
* @arg GPIO_Remap_SWJ_Disable : Full SWJ Disabled (JTAG-DP + SW-DP)//把SWD和JTAGAll debug ports are disabled
- 把SWD和JTAGAll debug ports are disabled,5All pins becomeGPIO,There is no debugging function anymore,之后st-linkCan't download the program,Only download using serial port,Download a new program without the debug port,This will bring the debug port back,This parameter cannot be used indiscriminately
十一、了解部分
1、Configure forced output mode
void TIM_ForcedOC1Config(TIM_TypeDef* TIMx, uint16_t TIM_ForcedAction);
void TIM_ForcedOC2Config(TIM_TypeDef* TIMx, uint16_t TIM_ForcedAction);
void TIM_ForcedOC3Config(TIM_TypeDef* TIMx, uint16_t TIM_ForcedAction);
void TIM_ForcedOC4Config(TIM_TypeDef* TIMx, uint16_t TIM_ForcedAction);
2、配置CCRof the preload function of the registers,影子寄存器,The written value does not take effect immediately,而是在更新事件才会生效
void TIM_OC1PreloadConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPreload);
void TIM_OC2PreloadConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPreload);
void TIM_OC3PreloadConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPreload);
void TIM_OC4PreloadConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPreload);
3、Configure fast-enabled
void TIM_OC1FastConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCFast);
void TIM_OC2FastConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCFast);
void TIM_OC3FastConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCFast);
void TIM_OC4FastConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCFast);
4、This function is in the manual,Cleared on external eventsREF信号,那一节有介绍
void TIM_ClearOC1Ref(TIM_TypeDef* TIMx, uint16_t TIM_OCClear);
void TIM_ClearOC2Ref(TIM_TypeDef* TIMx, uint16_t TIM_OCClear);
void TIM_ClearOC3Ref(TIM_TypeDef* TIMx, uint16_t TIM_OCClear);
void TIM_ClearOC4Ref(TIM_TypeDef* TIMx, uint16_t TIM_OCClear);
5、Set the polarity of the output compare individually
void TIM_OC1PolarityConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPolarity);
void TIM_OC1NPolarityConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCNPolarity);//这里带“N”It is the configuration of the complementary channel in the advanced timer
void TIM_OC2PolarityConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPolarity);
void TIM_OC2NPolarityConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCNPolarity);
void TIM_OC3PolarityConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPolarity);
void TIM_OC3NPolarityConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCNPolarity);
void TIM_OC4PolarityConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPolarity);//OC4There are no complementary channels
6、Modify the output enable parameters individually
void TIM_CCxCmd(TIM_TypeDef* TIMx, uint16_t TIM_Channel, uint16_t TIM_CCx);
void TIM_CCxNCmd(TIM_TypeDef* TIMx, uint16_t TIM_Channel, uint16_t TIM_CCxN);
7、选择输出比较模式
void TIM_SelectOCxM(TIM_TypeDef* TIMx, uint16_t TIM_Channel, uint16_t TIM_OCMode);
边栏推荐
- 联影医疗二面
- The application of Meta analysis in the field of ecological environment
- 迪文串口屏幕制作(连载一)=====准备工作
- 昆仑通态屏幕制作(连载1)---接触篇
- DeepLearing4j's deep learning Yolo Tiny realizes target detection
- 动态规划入门 JS
- [Punctuality Atom] Simple application of sys.c, sys.h bit-band operations
- 基于全球模式比较计划CMIP6与区域气候-化学耦合模式 WRF-Chem 的未来大气污染变化模拟
- R language application in the field of ecological environment
- 边境的悍匪—机器学习实战:第八章 降维
猜你喜欢
基于全球模式比较计划CMIP6与区域气候-化学耦合模式 WRF-Chem 的未来大气污染变化模拟
Atmospheric particulate matter PMF source analysis
边境的悍匪—机器学习实战:第六章 决策树
R - GIS: how to use R language implementation of GIS geospatial analysis and model prediction
influxDB运维记录
QT串口和CAN数据动态实时显示最后日志
QT串口动态实时显示大量数据波形曲线(四)========“界面的美化与处理”
边境的悍匪—机器学习实战:第十二章 使用TensorFlow自定义模型和训练
Target detection, object classification and semantic segmentation of UAV remote sensing images based on PyTorch deep learning
2021 soft exam intermediate pass
随机推荐
R - GIS: how to use R language implementation of GIS geospatial analysis and model prediction
QT serial and CAN dynamic real-time display the log data
重磅揭晓!第十四届深创赛福田预选赛区暨华秋第八届硬创大赛华南分赛区晋
DeepLearing4j深度学习之Yolo Tiny实现目标检测
无人机生态环境监测、图像处理与GIS数据分析
QT串口动态实时显示大量数据波形曲线(四)========“界面的美化与处理”
边境的悍匪—机器学习实战:第五章 支持向量机
vs编译boost库脚本
Map making of environmental impact assessment based on remote sensing interpretation and GIS technology (the latest guidelines)
QT串口和CAN数据动态实时显示最后日志
Through the bit operations to convert the characters are case sensitive
【Qingdao Station】High-level application of SWAT model and modeling of areas without data, uncertainty analysis and climate change, improvement of land use surface pollution impact model and case analy
[Punctuality Atom] Simple application of sys.c, sys.h bit-band operations
边境的悍匪—机器学习实战:第十五章 使用CNN和RNN处理序列
大气颗粒物 PMF 源解析
Application of remote sensing, GIS and GPS technology in hydrology, meteorology, disaster, ecology, environment and health
【江科大自化协stm32F103c8t6】笔记之【入门32单片机及EXTI外部中断初始化参数配置】
经典排序之插入排序
QT serial 3: LORA test platform based on QT and STM32H750 (2)
R language application in the field of ecological environment