下为主/从定时器配置函数代码:#include"stm32f10x.h"
#include "MS_Counter.h"
/***************
主定时器配置函数
period:PWM周期
prescaler:预分频系数
pulse:占空比控制变量 也就是PWM有效电平的宽度
PWM输出IO为GPIOC_7
完全重映射至 TIM3_CH2
***************/
void Master_TIM(u16 period,u16 prescaler,u16 pulse)
{
TIM_OCInitTypeDef TIM_OCInitStructure; //OC2初始化结构体
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure; //TIM3时基初始化结构体
GPIO_InitTypeDef GPIO_InitTypeStructure; //GPIO初始化结构体
RCC_APB2PeriphClockCmd(PWM_CLK|RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO,ENABLE);
//使能PWM输出的时钟和复用IO时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE); //使能主定时器时钟
GPIO_InitTypeStructure.GPIO_Mode=GPIO_Mode_Out_PP;
//沙雕核心板默认蜂鸣器开启状态,每次还要专门写代码关蜂鸣器...
GPIO_InitTypeStructure.GPIO_Pin=GPIO_Pin_8;
GPIO_InitTypeStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitTypeStructure);
GPIO_ResetBits(GPIOB,GPIO_Pin_8);
GPIO_InitTypeStructure.GPIO_Mode=GPIO_Mode_AF_PP; //PWM输出IO设为复用推挽
GPIO_InitTypeStructure.GPIO_Pin=GPIO_Pin_7;
GPIO_InitTypeStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOC,&GPIO_InitTypeStructure);
GPIO_PinRemapConfig(GPIO_FullRemap_TIM3,ENABLE); //完全重映射至TIM3_CH2
/*********
初始化TIM3的时基
设定PWM周期和预分频系数
*********/
TIM_TimeBaseInitStructure.TIM_ClockDivision=1; //时钟分频为1即为72MHz
TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInitStructure.TIM_Period=period;
TIM_TimeBaseInitStructure.TIM_Prescaler=prescaler;
TIM_TimeBaseInitStructure.TIM_RepetitionCounter=0;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);
/***********
初始化PWM输出通道OC2
设定PWM占空比
***********/
TIM_OCInitStructure.TIM_OCIdleState=ENABLE;
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1; //PWM模式1
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High; //高电平为有效位
TIM_OCInitStructure.TIM_OutputState=ENABLE;
TIM_OCInitStructure.TIM_Pulse=pulse;
TIM_OC2Init(TIM3,&TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM3,TIM_OCPreload_Enable); //预装载TIM3的pulse值
TIM_Cmd(TIM3,DISABLE); //初始化过程中不开启TIM3
//中断响应后再开启,保证主从定时器同步工作
TIM_SelectMasterSlaveMode(TIM3,TIM_MasterSlaveMode_Enable); //TIM3选择使能主从模式主定时器
TIM_SelectOutputTrigger(TIM3,TIM_TRGOSource_Update); //TIM3选择更新事件作为
trgo触发源
}
/************
从定时器配置函数
period:TIM4的溢出值 即设定的PWM脉冲数
***********/
void Slave_TIM(u16 period)
{
NVIC_InitTypeDef NVIC_InitStructure; //中断初始化结构体
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure; //TIM4时基初始化结构体
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE); //使能TIM4时钟
/********
初始化TIM4时基
设定所需PWM脉冲数
********/
TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1; //时钟不分频即直接使用
//TIM3脉冲
TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInitStructure.TIM_Period=period;
TIM_TimeBaseInitStructure.TIM_Prescaler=0; //PWM不分频
TIM_TimeBaseInitStructure.TIM_RepetitionCounter=0;
TIM_TimeBaseInit(TIM4,&TIM_TimeBaseInitStructure);
TIM_SelectSlaveMode(TIM4,TIM_SlaveMode_External1); //TIM4选择从定时器模式
TIM_SelectInputTrigger(TIM4,TIM_TS_ITR2); //TIM4选择内部触发来源为
//TIM3
TIM_Cmd(TIM4,DISABLE); //同样初始化中不使能TIM4
/***************
配置TIM4的中断
利用TIM4计数溢出作为中断事件关闭TIM3
可达到精确计数的目的
****************/
NVIC_InitStructure.NVIC_IRQChannel=TIM4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=2;
NVIC_Init(&NVIC_InitStructure);
TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE); //TIM4中断触发事件为update
}
/*************
方向控制IO配置函数
GPIOE_5
高电平顺时针
低电平逆时针
*************/
void DIR_Crl(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE,ENABLE);
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOE,&GPIO_InitStructure);
}
|