D触发器?
一、输出比较
二、PWM
1、简介
2、结构
三、外部设备
1.舵机
2.直流电机
我的理解是xO1 = xIN1 & PWMx; xO2 = xIN2 & PWMx;引入PWMx可以更方便的控制特定的电路。
四、函数学习
/*****单独设置输出比较极性*****/
void TIM_OC1PolarityConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPolarity);
void TIM_OC1NPolarityConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCNPolarity);//带N的就是高级定时器里互补通道的配置
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);//OC4没有互补通道//结构体初始化也可以设置极性,这里就是单独更改输出极性的,两者效果一样
/*****单独修改输出使能参数*****/
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);
/*****单独更改输出比较模式*****/
void TIM_SelectOCxM(TIM_TypeDef* TIMx, uint16_t TIM_Channel, uint16_t TIM_OCMode);
四、实验
1.硬件电路
2.代码分析
main.c
#include "stm32f10x.h" // Device header
#include "Delay.h"
#include "OLED.h"
#include "PWM.h"uint8_t i;
int main(void)
{OLED_Init();PWM_Init();while(1){for(i=0;i<=100;i++){PWM_SetCompare1(100-i);//设置CCR值,占空比由CCR和ARR共同决定Delay_ms(10);}}}
PWM.c
#include "stm32f10x.h" // Device headervoid PWM_Init(void)
{RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);//时钟RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);GPIO_InitTypeDef GPIO_InitStructure;GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;//一般使用输出数据寄存器控制//想让定时器控制//就要复用输出,输出控制权转移给片上外设GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;//查找引脚定义知,TIM2_CH1_ETR在PA0口GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;GPIO_Init(GPIOA,&GPIO_InitStructure);TIM_InternalClockConfig(TIM2);//选择时基单元时钟源,上电默认使用内部时钟,不写也行/*****时基单元初始化*****/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的值TIM_TimeBaseInitStructure.TIM_Prescaler = 720 - 1 ;//PSCTIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;//重复计数器(高级计数器才有)TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitStructure);TIM_OCInitTypeDef TIM_OCInitStructure;TIM_OCStructInit(&TIM_OCInitStructure);//总结:赋一个初始值//下面再改自己想改的参数//防止配置通用定时器时未配置全部结构体成员导致改为高级定时器时出现错误TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;//输出模式TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;//输出极性TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//输出使能TIM_OCInitStructure.TIM_Pulse = 0 ;//初始化CCRTIM_OC1Init( TIM2,&TIM_OCInitStructure);TIM_Cmd(TIM2,ENABLE);
}void PWM_SetCompare1(uint16_t Compare)
{TIM_SetCompare1(TIM2,Compare);
}
用PA0原因
复用推挽原因