1、普通定时使用
#include"stm32f10x.h"
#include"time.h"
static Time_NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0000);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = TIMx_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
static void Time_Config(void)
{
TIM_TimeBaseInitTypeDef
TIM_TimeBaseInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIMx,ENABLE);
//TIM_DeInit(TIM2);
TIM_TimeBaseInitStructure.TIM_Prescaler =(10000-1); //时钟分频系数
TIM_TimeBaseInitStructure.TIM_Period =(7200-1); //自动重装寄存器
TIM_TimeBaseInitStructure.TIM_CounterMode =TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseInitStructure.TIM_ClockDivision =0; //时钟分割,这里不涉及
#if 0
TIM_TimeBaseInitStructure.TIM_RepetitionCounter; //This parameter is valid only for TIM1 and TIM8
#endif
TIM_TimeBaseInit(TIMx,&TIM_TimeBaseInitStructure); //配置参数
TIM_ClearITPendingBit(TIMx,TIM_IT_Update); //清除中断标示位
TIM_ITConfig(TIMx,TIM_IT_Update,ENABLE); //中断配置
TIM_Cmd(TIMx,ENABLE); //启动定时器
}
void Time_Init(void)
{
Time_Config();
//定时器配置
Time_NVIC_Config();
//嵌套中断向量初始化
}
2、PWM输出
#include"stm32f10x.h"
#include"time.h"
#if 0
static Time_NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0000);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = TIMx_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
#endif
void Time_OUT_PWM_GPIO_Config(void)
{
GPIO_InitTypeDef
GPIO_InitStructure;
RCC_APB2PeriphClockCmd(TIMx_OUT_GPIO_RCC,ENABLE);
GPIO_InitStructure.GPIO_Pin =TIMx_OUT_Pin;
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_AF_PP; //复用推免式输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(TIMx_OUT_Port, &GPIO_InitStructure);
}
static void Time_OUT_PWM_Config(void)
{
TIM_TimeBaseInitTypeDef
TIM_TimeBaseInitStructure;
TIM_OCInitTypeDef
TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIMx,ENABLE); //开启时钟
TIM_DeInit(TIM2);
TIM_TimeBaseInitStructure.TIM_Prescaler =(100-1); //时钟分频系数
TIM_TimeBaseInitStructure.TIM_Period =(1000-1); //自动重装寄存器 PWM频率:72M/100/1000=720Hz
TIM_TimeBaseInitStructure.TIM_CounterMode =TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseInitStructure.TIM_ClockDivision =0; //时钟分频,这里不涉及
#ifdef Time1
TIM_TimeBaseInitStructure.TIM_RepetitionCounter; //This parameter is valid only for TIM1 and TIM8
#endif
TIM_TimeBaseInit(TIMx,&TIM_TimeBaseInitStructure); //配置参数
TIM_ClearITPendingBit(TIMx,TIM_IT_Update); //清除中断标示位
TIM_OCInitStructure.TIM_OCMode
=TIM_OCMode_PWM1;
//PWM1模式
OCx电平根据ARR与CCRx比较结果发生反转
TIM_OCInitStructure.TIM_OutputState
=TIM_OutputState_Enable;
//输出使能通道1
#ifdef Time1
TIM_OCInitStructure.TIM_OutputNState =;
#endif
TIM_OCInitStructure.TIM_Pulse
=CCR1_VAL;
//占空比800/1000
TIM_OCInitStructure.TIM_OCPolarity
=TIM_OCPolarity_Low;
//计数器值小于CCR值时输出高电平
#ifdef Time1
TIM_OCInitStructure.TIM_OCNPolarity
=;
TIM_OCInitStructure.TIM_OCIdleState
=;
TIM_OCInitStructure.TIM_OCNIdleState =;
#endif
TIM_OC1Init(TIMx,&TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIMx,TIM_OCPreload_Enable);
//使能CCR1预装载
TIM_OCInitStructure.TIM_OutputState =TIM_OutputState_Enable; //输出使能通道2
TIM_OCInitStructure.TIM_Pulse =CCR2_VAL;
TIM_OC2Init(TIMx,&TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIMx,TIM_OCPreload_Enable);