STM32F103_study56_The punctual atoms(STM32 PWM output experimental code analysis)

STM32F103_study56_The punctual atoms(STM32  PWM output experimental code analysis)
STM32F103_study56_The punctual atoms(STM32  PWM output experimental code analysis)
STM32F103_study56_The punctual atoms(STM32  PWM output experimental code analysis)
STM32F103_study56_The punctual atoms(STM32  PWM output experimental code analysis)
STM32F103_study56_The punctual atoms(STM32  PWM output experimental code analysis)
STM32F103_study56_The punctual atoms(STM32  PWM output experimental code analysis)
STM32F103_study56_The punctual atoms(STM32  PWM output experimental code analysis)
STM32F103_study56_The punctual atoms(STM32  PWM output experimental code analysis)
STM32F103_study56_The punctual atoms(STM32  PWM output experimental code analysis)
STM32F103_study56_The punctual atoms(STM32  PWM output experimental code analysis)
STM32F103_study56_The punctual atoms(STM32  PWM output experimental code analysis)STM32F103_study56_The punctual atoms(STM32  PWM output experimental code analysis)
STM32F103_study56_The punctual atoms(STM32  PWM output experimental code analysis)
STM32F103_study56_The punctual atoms(STM32  PWM output experimental code analysis)
STM32F103_study56_The punctual atoms(STM32  PWM output experimental code analysis)
STM32F103_study56_The punctual atoms(STM32  PWM output experimental code analysis)
STM32F103_study56_The punctual atoms(STM32  PWM output experimental code analysis)
STM32F103_study56_The punctual atoms(STM32  PWM output experimental code analysis)
STM32F103_study56_The punctual atoms(STM32  PWM output experimental code analysis)
STM32F103_study56_The punctual atoms(STM32  PWM output experimental code analysis)
STM32F103_study56_The punctual atoms(STM32  PWM output experimental code analysis)
STM32F103_study56_The punctual atoms(STM32  PWM output experimental code analysis)

STM32F103_study56_The punctual atoms(STM32  PWM output experimental code analysis)
STM32F103_study56_The punctual atoms(STM32  PWM output experimental code analysis)
STM32F103_study56_The punctual atoms(STM32  PWM output experimental code analysis)
STM32F103_study56_The punctual atoms(STM32  PWM output experimental code analysis)
STM32F103_study56_The punctual atoms(STM32  PWM output experimental code analysis)

一. Experiment Name:

PWM输出实验
二. Development steps:
STM32F103_study56_The punctual atoms(STM32  PWM output experimental code analysis)

三. Software and hardware used:
1.keil
2.Development board
3.The emulator
4.Two USB data cables
5.The simulation line

四. Functions and registers related to serial port operation:

五, logical analysis:
1.我们要做PW输出实验
这儿有四个捕获比较(值)寄存器

STM32F103_study56_The punctual atoms(STM32  PWM output experimental code analysis)

STM32F103_study56_The punctual atoms(STM32  PWM output experimental code analysis)
STM32F103_study56_The punctual atoms(STM32  PWM output experimental code analysis)

STM32F103_study56_The punctual atoms(STM32  PWM output experimental code analysis)

STM32F103_study56_The punctual atoms(STM32  PWM output experimental code analysis)
PWM输出,其实只是运用了定时器和GPIO引脚的一种功能运用。所以我们要使能定时器,而STM32上面有三种定时器,分别是高级定时器,基本定时器和通用定时器。而由于通用定时器是入门级用的,所以我们这里用通用定时器3,也可以用其他几个通用定时器,但连接在其他通用定时器上面的GPIO引脚可能不同,需要我们重新配置GPIO引脚(这个可以去参考手册和数据手册查找)
由于最终进行PWM输出的是GPIO引脚,所以我们需要使能GPIO时钟。

STM32F103_study56_The punctual atoms(STM32  PWM output experimental code analysis)

GPIO初始化:需要设置GPIO的那一组。那个引脚。引脚的功能等。反正就是GPIO结构体里面的成员变量。

STM32F103_study56_The punctual atoms(STM32  PWM output experimental code analysis)

由于我们需要看到我们PWM输出实验的现象,这里我们使用,LED灯的亮灭程度来体现PWM的输出现象。而通过分析开发板LED灯的原理图知道PB5连接这一个LED灯,而同时PB5的复用功能还是定时器3的输出引脚。所以用PB5.

为什么要重映射配置?开启AFIO?这个我也不知道

因为这个PB5使用了重定义功能
重映射必须使能AFIO时钟(规定,暂时不要去想为什么,这里可能涉及到硬件方面的知识)
STM32F103_study56_The punctual atoms(STM32  PWM output experimental code analysis)

STM32F103_study56_The punctual atoms(STM32  PWM output experimental code analysis)

基本操作,要用定时器就必须初始化,设置定时器里面相应的值
STM32F103_study56_The punctual atoms(STM32  PWM output experimental code analysis)

为什么?

应该这个开发板有4个通道,我们这里用的是通道2,所以也得初始化。这个通道也可以被别的功能使用,所以要初始化
STM32F103_study56_The punctual atoms(STM32  PWM output experimental code analysis)
STM32F103_study56_The punctual atoms(STM32  PWM output experimental code analysis)

为什么?

它也是一个单独的工具,可以被很多功能调用,而这个PWM输出函数一定会使用这个寄存器,所以先使能。
STM32F103_study56_The punctual atoms(STM32  PWM output experimental code analysis)
STM32F103_study56_The punctual atoms(STM32  PWM output experimental code analysis)
STM32F103_study56_The punctual atoms(STM32  PWM output experimental code analysis)

基本操作。你要用就必须使能。
STM32F103_study56_The punctual atoms(STM32  PWM output experimental code analysis)
代码分析:
1.
u16 arr:16位大的寄存器,所以该值也是16位大
u16 psc:16位大的寄存器,所以该值也是16位大
STM32F103_study56_The punctual atoms(STM32  PWM output experimental code analysis)STM32F103_study56_The punctual atoms(STM32  PWM output experimental code analysis)
2.
GPIO基本设置。
GPIO_Mode_AF_PP; //复用推挽输出

为什么选择这个模式呢?

STM32F103_study56_The punctual atoms(STM32  PWM output experimental code analysis)

		GPIO_InitTypeDef GPIO_InitStructure;
 //设置该引脚为复用输出功能,输出TIM3 CH2的PWM脉冲波形	GPIOB.5
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //TIM_CH2
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  //复用推挽输出
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
		GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化GPIO

	void TIM3_PWM_Init(u16 arr,u16 psc)

arr:重装的值
arr psc :设置了定时器的周期
STM32F103_study56_The punctual atoms(STM32  PWM output experimental code analysis)
STM32F103_study56_The punctual atoms(STM32  PWM output experimental code analysis)

		TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  //初始化TIM3
		TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
		TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值 
		TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
		TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIM向上计数模式
		TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
		

这段代码在,整个PWM输出实验的那个一个步骤会用到?

定时器作为PWM功能用时,这是一个完整的功能,所以会有相应的配置,所以就会有结构体。
STM32F103_study56_The punctual atoms(STM32  PWM output experimental code analysis)

STM32F103_study56_The punctual atoms(STM32  PWM output experimental code analysis)
STM32F103_study56_The punctual atoms(STM32  PWM output experimental code analysis)

		TIM_OCInitTypeDef  TIM_OCInitStructure;
		//初始化TIM3 Channel2 PWM模式	 
		TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //选择定时器模式:TIM脉冲宽度调制模式2
		TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
		TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高
		
		
		TIM_OC2Init(TIM3, &TIM_OCInitStructure);  //根据T指定的参数初始化外设TIM3 OC2

		

AFIO整个PWM输出实验的那个一个步骤会用到?

这是规定,因为我们使用了重映射,就必须使能AFIO,规定也的,可能这样的设计,涉及硬件层面,所以现在记住就好,不要去纠结。

		RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB  | 	RCC_APB2Periph_AFIO, ENABLE);  //使能GPIO外设和AFIO复用功能模块时钟
			TIM_OC2Init(TIM3, &TIM_OCInitStructure);  //根据T指定的参数初始化外设TIM3 OC2
		
		TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);  //使能TIM3在CCR2上的预装载寄存器
 
		TIM_Cmd(TIM3, ENABLE);  //使能TIM3
	

六. Code display:

	//TIM3 PWM部分初始化 
//PWM输出初始化
//arr:自动重装值
//psc:时钟预分频数
	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);
		RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB  | RCC_APB2Periph_AFIO, ENABLE);  //使能GPIO外设和AFIO复用功能模块时钟
		
		
		GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, ENABLE); //Timer3部分重映射  TIM3_CH2->PB5    
 
   //设置该引脚为复用输出功能,输出TIM3 CH2的PWM脉冲波形	GPIOB.5
		GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //TIM_CH2
		GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  //复用推挽输出
		GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
		GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化GPIO
		
		GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, ENABLE); //Timer3部分重映射  TIM3_CH2->PB5    
 
		  //初始化TIM3
		TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
		TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值 
		TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
		TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIM向上计数模式
		TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
		
		
		//初始化TIM3 Channel2 PWM模式	 
		TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //选择定时器模式:TIM脉冲宽度调制模式2
		TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
		TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高
		
		
		TIM_OC2Init(TIM3, &TIM_OCInitStructure);  //根据T指定的参数初始化外设TIM3 OC2

		
		
		
		TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);  //使能TIM3在CCR2上的预装载寄存器
 
		TIM_Cmd(TIM3, ENABLE);  //使能TIM3
	
	
		
	}
	

七、The idea of sublimation
1.在STM32开发板的所有外设,以及可能不能叫做一个外设,它或许只能叫做一个工具,但由于STM32开发板很小,但是它又要完成很多很复杂的功能,所以设计者,绝对不会把一个工具只是让你一个功能用,它绝对会是很多功能都会用到的。所以我们在使用每一个工具之前,我们都要问自己这个工具是不是需要初始化和使能
2.查看你这个实验所涉及的所有硬件:外设,寄存器,中断,通道等等
3.那些东西需要使能:在不用它的时候,会对它进行断电的一切外设和功能(比如中断它只是一种功能,而不是一种硬件。)

上一篇:STM32 PWM波比较输出


下一篇:stm32寄存器版学习笔记 定时计数器中断