内容为江科大STM32标准库学习记录
高级定时器
重复次数计数器:可以实现每隔几个周期才发生一次更新事件和更新中断,相当于对更新的输出信号作了一次分频。
定时器的内部基准时钟都是72MHZ
定时器相关的寄存器可以看下手册
案列1:定时器每隔1s自加,使用内部时钟
#include "Timer.h"
uint16_t num;
void Timer_Init(void)
{
//开启时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
TIM_InternalClockConfig(TIM2);//使用内部时钟
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = 10000 - 1;//ARR自动重装值
TIM_TimeBaseStructure.TIM_Prescaler = 7200 - 1;//PCS 预分频器的值
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);
TIM_ClearFlag(TIM2,TIM_FLAG_Update);//解决上电立刻先进入一次中断,手动把更新中断标志位清除一下
//配置启用指定的TIM中断
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
//配置优先级分组:抢占优先级和子优先级
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
//配置NVIC
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_Init(&NVIC_InitStructure);
TIM_Cmd(TIM2,ENABLE);//启动定时器
}
void TIM2_IRQHandler(void)
{
if(TIM_GetITStatus(TIM2,TIM_IT_Update) == SET){//判断定时器中断标志
num++;
TIM_ClearITPendingBit(TIM2,TIM_IT_Update);//清除中断定时器中断标志
}
}
案列二:使用外部时钟计数,其实内部时钟计数就是按照内部定时自加,使用外部时钟就是计外面进来的信号(例如电平信号和边沿信号)
#include "Timer.h"
uint16_t num;
void Timer_Init(void)
{
//开启时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//推挽输出
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
//配置外部时钟模式2,数外面进来的信号
TIM_ETRClockMode2Config(TIM2,TIM_ExtTRGPSC_OFF,TIM_ExtTRGPolarity_NonInverted,0x0F);
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = 10 - 1;//ARR自动重装值
TIM_TimeBaseStructure.TIM_Prescaler = 1 - 1;//PCS 预分频器的值
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);
TIM_ClearFlag(TIM2,TIM_FLAG_Update);//解决上电立刻先进入一次中断,手动把更新中断标志位清除一下
//配置启用指定的TIM中断
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
//配置优先级分组:抢占优先级和子优先级
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
//配置NVIC
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_Init(&NVIC_InitStructure);
TIM_Cmd(TIM2,ENABLE);//启动定时器
}
uint16_t Timer_GetCounter(void)
{
return TIM_GetCounter(TIM2);
}
void TIM2_IRQHandler(void)
{
if(TIM_GetITStatus(TIM2,TIM_IT_Update) == SET){//判断定时器中断标志
num++;
TIM_ClearITPendingBit(TIM2,TIM_IT_Update);//清除中断定时器中断标志
}
}
PWM(Pulse Width Modulation)脉冲宽度调制
在具有惯性的系统中,可以通过对一系列脉冲的宽度进行调制,来等效地获得所需要的模拟参量,常应用于电机控速等领域
PWM参数:
频率 = 1 / TS 占空比 = TON / TS 分辨率 = 占空比变化步距
输出比较通道(通用)
输出模式控制器:是CNT和CCR的大小关系,输出的是REF的高低电平
具体看下面输出比较模式
置有效电平:可以理解为高电平
置无效电平:可以理解为低电平
PWM基本结构
黄色线:ARR的值
红色线:CCR的值
蓝色线:CNT的计数值
绿色线为REF输出,极性是否翻转取决于极性选择了,最终通向GPIO口
参数计算
上图看出PWM的频率等于计数器的更新频率
输出比较通道(高级)
案例一:呼吸灯,通过改变CCR寄存器的值,改变占空比
#include "pwm.h"
void PWM_Init(void)
{
//开启时钟
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;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
//选择内部时钟
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;//PCS 预分频器的值
TIM_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模式
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //指定输出极性。
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//指定TIM输出比较状态
TIM_OCInitStructure.TIM_Pulse = 0;//CCR寄存器的值
TIM_OC1Init(TIM2,&TIM_OCInitStructure);//初始化TIM2 Channel1
TIM_Cmd(TIM2,ENABLE);//启动定时器
}
void Set_Compare1(uint16_t Set_Compare1)
{
TIM_SetCompare1(TIM2,Set_Compare1);
}
LED_PWM_Init();
PWM_Init();
while(1){
for(i=0;i<=100;i++){
Set_Compare1(i);
Delay_ms(10);
OLED_ShowNum(2,3,i,5);
}
for(i=0;i<=100;i++){
Set_Compare1(100-i);
Delay_ms(10);
OLED_ShowNum(2,3,(100-i),5);
}
}
}
打开AFIO时钟,再使用AFIO重映射外设复用的引脚,如果重映射的GPIO是调试端口,需要解除调试功能,变为普通的GPIO才可以正常使用
//开启时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
//IO复用
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
GPIO_PinRemapConfig(GPIO_PartialRemap1_TIM2,ENABLE);//重映射
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);//解除JTAG的调试端口
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//推挽输出
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
案例二:控制舵机
#include "pwm.h"
void PWM_Init(void)
{
//开启时钟
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;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
//选择内部时钟
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 = 20000 - 1;//ARR自动重装值
TIM_TimeBaseInitStructure.TIM_Prescaler = 72 - 1;//PCS 预分频器的值
TIM_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模式
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //指定输出极性。
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;//指定TIM输出比较状态
TIM_OCInitStructure.TIM_Pulse = 0;//CCR寄存器的值
TIM_OC1Init(TIM2,&TIM_OCInitStructure);//初始化TIM2 Channel1
TIM_Cmd(TIM2,ENABLE);//启动定时器
}
void Set_Compare1(uint16_t Compare)
{
//Set_Compare1 = Set_Compare1*20;
TIM_SetCompare1(TIM2,Compare);
}
void Servo_SetAngle(uint16_t Angle)
{
uint16_t reNum;
reNum = (Angle*2000)/180 + 500;
Set_Compare1(reNum);
}
案例三:控制电机
#include "motor.h"
void Motor_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//推挽输出
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
PWM_Init();
}
void Motor_SetSpeed(int8_t speed)
{
if(speed >= 0){
GPIO_SetBits(GPIOA,GPIO_Pin_4);
GPIO_ResetBits(GPIOA,GPIO_Pin_5);//正转
Set_Compare1(speed);//控制占空比来调整速度
}else {
GPIO_SetBits(GPIOA,GPIO_Pin_5);
GPIO_ResetBits(GPIOA,GPIO_Pin_4);//反转
Set_Compare1(speed);//控制占空比来调整速度
}
}
输入捕获简介
频率的定义:在时间T内出现N个重复的周期,那么f = N/T
频率测量
测频法适合测量高频信号,测周法适合测量低频信号,以中界频率为界限。
输入捕获通道
主从触发模式
输入捕获基本结构
输入捕获基本结构
PWMI基本结构
案例一:PA0输出pwm,使用PA6捕获输入pwm,测量频率
#include "IC.h"
void IC_Init(void)
{
//开启对应时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
//配置GPIO
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//推挽输出
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
//配置时基单元
TIM_InternalClockConfig(TIM3);//选择内部时钟
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;//计数器模式
TIM_TimeBaseInitStructure.TIM_Period = 65536 - 1;//ARR
TIM_TimeBaseInitStructure.TIM_Prescaler = 72 -1;//PSC
TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);
//输入捕获配置
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
TIM_ICInitStructure.TIM_ICFilter = 0xF;//指定输入捕获过滤器。
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;//指定输入信号的活动边缘
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;//指定输入捕获预caler
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;//指定输入
TIM_ICInit(TIM3,&TIM_ICInitStructure);//根据指定初始化TIM外设
TIM_SelectInputTrigger(TIM3,TIM_TS_TI1FP1);//选择输入触发器源
TIM_SelectSlaveMode(TIM3,TIM_SlaveMode_Reset);//选择TIMx从模式
TIM_Cmd(TIM3,ENABLE);
}
//f = 标准频率/计时次数;目前标准频率为1MHz
uint32_t Get_Frequency(void)
{
return (1000000/TIM_GetCapture1(TIM3));
}
案例二:PA0输出pwm,使用PA6捕获输入pwm,测量频率和占空比
#include "IC2.h"
void IC2_Init(void)
{
//开启对应时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
//配置GPIO
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//推挽输出
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
//配置时基单元
TIM_InternalClockConfig(TIM3);//选择内部时钟
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;//计数器模式
TIM_TimeBaseInitStructure.TIM_Period = 65536 - 1;//ARR
TIM_TimeBaseInitStructure.TIM_Prescaler = 72 -1;//PSC
TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);
//输入捕获配置
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
TIM_ICInitStructure.TIM_ICFilter = 0xF;//指定输入捕获过滤器。
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;//指定输入信号的活动边缘,上升沿
TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;//指定输入捕获预caler
TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;//指定输入,这里直接
TIM_ICInit(TIM3,&TIM_ICInitStructure);//根据指定初始化TIM外设
TIM_PWMIConfig(TIM3,&TIM_ICInitStructure);//传入配置好的通道1,就可以配置和通道1的相反配置
TIM_SelectInputTrigger(TIM3,TIM_TS_TI1FP1);//选择输入触发器源
TIM_SelectSlaveMode(TIM3,TIM_SlaveMode_Reset);//选择TIMx从模式
TIM_Cmd(TIM3,ENABLE);
}
//f = 标准频率/计时次数;目前标准频率为1MHz
uint32_t Get_Frequency2(void)
{
return (1000000/TIM_GetCapture1(TIM3));
}
uint32_t Get_Duty(void)
{
return ((TIM_GetCapture2(TIM3)+1)*100/TIM_GetCapture1(TIM3));
}
正交编码器
编码器接口基本结构
工作模式
实例(均不反相)
实例(TI1反相)
案例一:编码器接口测速
A相——PA6 B相——PA7 对应TIM3的通道1和通道2
#include "Encoder.h"
void Encoder_Init(void)
{
//开启对应时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
//GPIO初始化
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;//上拉输入
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_TimeBaseInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInitStructure.TIM_Period = 65506 - 1;//ARR 自动重装值
TIM_TimeBaseInitStructure.TIM_Prescaler = 1 - 1; //PSC
TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);
//输入捕获配置
TIM_ICInitTypeDef TIM_ICInitStructure;
TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;
TIM_ICInitStructure.TIM_ICFilter = 0xF;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInit(TIM3,&TIM_ICInitStructure);
TIM_ICInitStructure.TIM_Channel = TIM_Channel_2;
TIM_ICInitStructure.TIM_ICFilter = 0xF;
TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM_ICInit(TIM3,&TIM_ICInitStructure);
//编码器接口配置
TIM_EncoderInterfaceConfig(TIM3,TIM_EncoderMode_TI12,TIM_ICPolarity_Rising,TIM_ICPolarity_Rising);
TIM_Cmd(TIM3,ENABLE);//开启定时器
}
uint16_t Encoder_Get(void)
{
uint16_t temp;
TIM_GetCounter(TIM3);//获取CNT的值
TIM_SetCounter(TIM3,0);//CNT清0
}