本帖最后由 hxyqq87 于 2017-1-4 01:54 编辑
void TIM1_PWM_Init(u32 arr,u32 psc)
{
GPIO_InitTypeDef GPIO_InitStructure; //GPIO结构体变量
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; //TIM结构体变量
TIM_OCInitTypeDef TIM_OCInitStructure; //TIM 通道结构体变量
NVIC_InitTypeDef NVIC_InitStructure; //中断
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE); //TIM1时钟使能
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE,ENABLE); //使能GPIOE时钟
GPIO_PinAFConfig(GPIOE,GPIO_PinSource9|GPIO_PinSource11|GPIO_PinSource13|GPIO_PinSource14,GPIO_AF_TIM1); //GPIOE9 E11 E13 E14复用为定时器1
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_11|GPIO_Pin_13|GPIO_Pin_14; //GPIOE9 E11 E13 E14
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //复用功能
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //速度100MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN; //下拉
GPIO_Init(GPIOE,&GPIO_InitStructure); //初始化PE9 E11 E13 E14
TIM_TimeBaseStructure.TIM_Prescaler=psc; //定时器分频 60-1
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseStructure.TIM_Period=arr; //自动重装载值 1000
TIM_TimeBaseStructure.TIM_RepetitionCounter=0; //重复计数器重装载值
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1; //设置时钟分割
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure); //初始化定时器1
//初始化TIM1 Channel_1 PWM模式
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //选择定时器模式:TIM脉冲宽度调制模式1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高
TIM_OCInitStructure.TIM_Pulse = 500;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set; //这个对高级定时器很重要
TIM_OC1Init(TIM1, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM1 OC1
TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable); //使能TIM1在CCR1上的预装载寄存器
//初始化TIM1 Channel_2 PWM模式
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //选择定时器模式:TIM脉冲宽度调制模式1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高
TIM_OCInitStructure.TIM_Pulse = 500;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set; //这个对高级定时器很重要
TIM_OC2Init(TIM1, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM1 OC1
TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable); //使能TIM1在CCR1上的预装载寄存器
//初始化TIM1 Channel_3 PWM模式
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //选择定时器模式:TIM脉冲宽度调制模式1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高
TIM_OCInitStructure.TIM_Pulse = 500;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set; //这个对高级定时器很重要
TIM_OC3Init(TIM1, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM1 OC1
TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable); //使能TIM1在CCR1上的预装载寄存器
//初始化TIM1 Channel_4 PWM模式
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //选择定时器模式:TIM脉冲宽度调制模式1
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性高
TIM_OCInitStructure.TIM_Pulse = 500;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set; //这个对高级定时器很重要
TIM_OC4Init(TIM1, &TIM_OCInitStructure); //根据T指定的参数初始化外设TIM1 OC1
TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable); //使能TIM1在CCR1上的预装载寄存器
TIM_CtrlPWMOutputs(TIM1, ENABLE); //高级定时器PWM输出使能,普通定时器无需此配置
TIM_ARRPreloadConfig(TIM1,ENABLE); //ARPE使能,ARR下个周期有效
TIM_ITConfig(TIM1,TIM_IT_Update,DISABLE); //允许定时器1更新中断
NVIC_InitStructure.NVIC_IRQChannel=TIM1_UP_TIM10_IRQn; //定时器1中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x00; //抢占优先级1
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x00; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; //定时器中断使能
NVIC_Init(&NVIC_InitStructure); //定时器中断初始化
TIM_Cmd(TIM1,ENABLE);
}
使用TIM1的 CH1(PE9) CH2(PE11) CH3(PE13) CH4(PE14)
MAIN函数如下
int main(void)
{
int t=0;
int start_ARR=1000;
LED2_Init();
delay_init(120);
uart_init(115200);
uart2_init(115200);
uart3_init(115200);
TIM1_PWM_Init(1000-1,60-1);//定时器1初始化 分频系数为60-1 计数频率为60M/60=1M Hz 自动重装载值为1000,所以PWM的频率为1M/1000=1K Hz
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //中断分组配置
while (1)
{
printf("程序正在运行
");
LED2=!LED2;
usart2_SendByte(USART2_TX_LEN,USART2_TX_BUF);
usart3_SendByte(USART3_TX_LEN,USART3_TX_BUF);
delay_ms(500);
}
}
现在已经快2点了 实在找不出原因,求大神帮帮忙,指出问题出在哪里,不胜感激!!!
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>