本帖最后由 lowell 于 2019-1-19 15:12 编辑
使用TIM14通道一PF9发出PWM波来模拟外部信号,TIM3的ETR引脚PD2输入外部时钟信号,TIM3计数产生中断,中断函数内翻转电平。
但是查看TIM3的计数寄存器发现计数值始终不变,不知道哪里配置错了,各位不吝赐教。
PWM代码:
void TIM14_PWM_Init(u32 arr,u32 psc)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14 ,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF ,ENABLE);
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_Init(GPIOF,&GPIO_InitStruct);
//引脚复用功能选择
GPIO_PinAFConfig(GPIOF,GPIO_PinSource9,GPIO_AF_TIM14);
//时基模块初始化
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_TimeBaseInitStructure.TIM_Period = arr;
TIM_TimeBaseInitStructure.TIM_Prescaler = psc;
TIM_TimeBaseInitStructure.TIM_ClockDivision = 0 ;
TIM_TimeBaseInitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up ;
TIM_TimeBaseInit(TIM14,&TIM_TimeBaseInitStructure);
//输出比较模块初始化
TIM_OCInitTypeDef TIM_OCInitStruct;
TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High ;
TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;
TIM_OC1Init(TIM14,&TIM_OCInitStruct);
TIM_OC1PreloadConfig(TIM14, TIM_OCPreload_Enable ); //使能TIM14在CCR1上的预加载寄存器
TIM_ARRPreloadConfig(TIM14, ENABLE); //ARPE使能
TIM_Cmd(TIM14,ENABLE);
}
TIM3外部时钟:
//通用定时器3中断初始化
//arr:自动重装值。
//psc:时钟预分频数
//定时器溢出时间计算方法:Tout=((arr+1)*(psc+1))/Ft us.
//Ft=定时器工作频率,单位:Mhz
//这里使用的是定时器3!
void TIM3_Int_Init(u32 arr,u32 psc)
{
/************时基单元配置******************************/
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE); ///使能TIM3时钟
TIM_DeInit(TIM3);
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_TimeBaseInitStructure.TIM_Period = arr; //自动重装载值
TIM_TimeBaseInitStructure.TIM_Prescaler=psc; //定时器分频
TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up ; //向上计数模式
TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);//初始化TIM3
/************NVIC配置******************************/
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitStructure.NVIC_IRQChannel=TIM3_IRQn; //定时器3中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x01; //抢占优先级1
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x03; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
/************所用到的ETR外部输入引脚配置******************************/
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_OType =GPIO_OType_PP ;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOD,GPIO_PinSource2,GPIO_AF_TIM3);
TIM_ETRClockMode2Config(TIM3,TIM_ExtTRGPSC_OFF ,TIM_ExtTRGPolarity_Inverted, 0x00);
TIM_ClearFlag (TIM3,TIM_IT_Update);
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE); //允许定时器3更新中断
TIM_SetCounter( TIM3, 0);
TIM_Cmd(TIM3,ENABLE); //使能定时器3
}
//定时器3中断服务函数
void TIM3_IRQHandler(void)
{
if(TIM_GetITStatus(TIM3,TIM_IT_Update|TIM_IT_Trigger)==SET) //溢出中断
{
LED1=!LED1;//DS1翻转
}
TIM_ClearITPendingBit(TIM3,TIM_IT_Update); //清除中断标志位
}
主函数:
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "timer.h"
#include "pwm.h"
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置系统中断优先级分组2
delay_init(168); //初始化延时函数
uart_init(115200);
LED_Init(); //初始化LED端口
TIM14_PWM_Init(500-1,8400-1);//定时器时钟84M,分频系数8400,84M/8400=10Khz的计数频率
TIM_SetCompare1(TIM14,250);
TIM3_Int_Init(5-1,840-1);
while(1)
{
delay_ms(300);
printf("Counter:%d
",TIM_GetCounter (TIM2));
}
}
这是TIM3初始化之后寄存器的值:
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
谢谢,我试下
一周热门 更多>