用原子哥的精英版用WAKE-UP捕获高电平控制 定时器3输出PWM控制led0
代码如下:
#include "timer.h"
#include "delay.h"
//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输出的PWM通过该LED显示
//设置该引脚为复用输出功能,输出TIM3 CH2的PWM脉冲波形
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //TIM3_CH2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
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的时间基数单位
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //选择定时器模式:TIM脉冲宽度调制模式2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_Pulse = 0; //设置待装入捕获比较寄存器的值 使PWM得一直输出高电平
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高
TIM_OC2Init(TIM3, &TIM_OCInitStructure); //根据TIM_OCInitStruct中指定的参数初始化外设TIMx
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable); //使能TIMx在CCR2上的预装载寄存器
TIM_ARRPreloadConfig(TIM3, ENABLE); //使能TIMx在ARR上的预装载寄存器
TIM_Cmd(TIM3, ENABLE); //使能TIMx外设
}
//定时器5通道CH1输入捕获配置
void TIM5_Cap_Init(u16 arr,u16 psc)
{
TIM_ICInitTypeDef TIM5_ICInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE); //使能TIM5时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); //使能GPIO外设时钟使能
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //PA0
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //PA0 下拉输入
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_ResetBits(GPIOA,GPIO_Pin_0); //PA0 设为0
//初始化定时器5 TIM5
TIM_TimeBaseStructure.TIM_Period = arr; //设定计数器自动重装值
TIM_TimeBaseStructure.TIM_Prescaler =psc; //预分频器
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
//初始化TIM3输入捕获参数
TIM5_ICInitStructure.TIM_Channel = TIM_Channel_1; //CC1S=01 选择输入端 IC1映射到TI1上
TIM5_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //上升沿捕获
TIM5_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //映射到TI1上
TIM5_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //配置输入分频,不分频
TIM5_ICInitStructure.TIM_ICFilter = 0x03;//IC1F=0011 配置输入滤波器 8个采样都是高电平认为是真的高电平到来
TIM_ICInit(TIM5, &TIM5_ICInitStructure);
//中断分组初始化
NVIC_InitStructure.NVIC_IRQChannel = TIM5_IRQn; //TIM5中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //先占优先级2级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //从优先级0级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
NVIC_Init(&NVIC_InitStructure); //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器
TIM_ITConfig(TIM5,TIM_IT_CC1,ENABLE);//允许CC1IE捕获中断
TIM_Cmd(TIM5,ENABLE ); //使能定时器5
}
u16 TIMCompare2;
static u8 dir =0;
void TIM5_IRQHandler(void)
{
if(TIM_GetITStatus(TIM5,TIM_IT_CC1)==1&&dir==0)
{
delay_ms(5);
for(TIMCompare2=0;TIMCompare2<=300;TIMCompare2++)
{
if(TIMCompare2>300) dir=1;
TIM_SetCompare2(TIM3,TIMCompare2);
delay_ms(50);
}
}
else if(TIM_GetITStatus(TIM5,TIM_IT_CC1)==1&&dir==1)
{
delay_ms(5);
for(TIMCompare2=300;TIMCompare2>0;TIMCompare2--)
{
if(TIMCompare2==0) dir=0;
TIM_SetCompare2(TIM3,TIMCompare2);
delay_ms(50);
}
}
TIM_ClearITPendingBit(TIM5, TIM_IT_CC1);
}
主程序为:
#include "delay.h"
#include "sys.h"
#include "timer.h"
int main(void)
{
delay_init(); //延时函数初始化
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
TIM3_PWM_Init(999,71); //71分频 频率1mh PWM周期1ms
TIM5_Cap_Init(0XFFFF,72-1); //以1Mhz的频率计数
while(1)
{
}
}
情况是中断服务程序中单独的由灯灭到灯亮或者灯亮衰减到灯灭都可以实现,但是放到一起就不能,只能有灭慢慢变亮,随后再按一次wake-up又从灭慢慢变亮,应该第二次又亮慢慢变灭的,不知道问题出在哪里,求助大神帮助!谢谢
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
void TIM5_IRQHandler(void)
{
if(TIM_GetITStatus(TIM5,TIM_IT_CC1)==1&&dir==0)
{while(dir==0)
{TIMCompare1++;
if(TIMCompare1>800)dir=1;
TIM_SetCompare2(TIM3,TIMCompare1);
delay_ms(10);
}
}
else if(TIM_GetITStatus(TIM5,TIM_IT_CC1)==1&&dir==1)
{
while(dir==1)
{ TIMCompare1--;
if(TIMCompare1==0)dir=0;
TIM_SetCompare2(TIM3,TIMCompare1);
delay_ms(10);
}
}
TIM_ClearITPendingBit(TIM5, TIM_IT_CC1);
}
static u16 TIMCompare1=0
一周热门 更多>