赞
踩
void TIM1_Int_Init(u32 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_BDTRInitTypeDef TIM_BDTRStructure;
// NVIC_InitTypeDef NVIC_InitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE); ///使能TIM1时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA|RCC_AHB1Periph_GPIOB, ENABLE);//使能GPIO时钟
//互补方波
GPIO_PinAFConfig(GPIOA,GPIO_PinSource8, GPIO_AF_TIM1 );//CH1
GPIO_PinAFConfig(GPIOA,GPIO_PinSource7, GPIO_AF_TIM1 ); //CH1N
//互补方波
GPIO_PinAFConfig(GPIOA,GPIO_PinSource9, GPIO_AF_TIM1 );//CH2
GPIO_PinAFConfig(GPIOB,GPIO_PinSource0, GPIO_AF_TIM1 ); //CH2N
//互补方波
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10, GPIO_AF_TIM1 );//CH3
GPIO_PinAFConfig(GPIOB,GPIO_PinSource1, GPIO_AF_TIM1 ); //CH3N
GPIO_PinAFConfig(GPIOA,GPIO_PinSource11, GPIO_AF_TIM1 );//CH4
//GPIO初始化设置
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用输出模式
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用输出模式
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;//推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;//100MHz
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//上拉
GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化
//时基单元配置
TIM_TimeBaseInitStructure.TIM_Prescaler=psc; //定时器分频
TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseInitStructure.TIM_Period=arr; //自动重装载值
TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseInitStructure);
//刹车和死区
TIM_BDTRStructure.TIM_AutomaticOutput=TIM_AutomaticOutput_Enable;
TIM_BDTRStructure.TIM_OSSIState=TIM_OSSIState_Disable;
TIM_BDTRStructure.TIM_OSSRState=TIM_OSSRState_Disable;
TIM_BDTRStructure.TIM_Break=TIM_Break_Disable;
TIM_BDTRStructure.TIM_BreakPolarity=TIM_BreakPolarity_High;
TIM_BDTRStructure.TIM_LOCKLevel=TIM_LOCKLevel_OFF;
TIM_BDTRStructure.TIM_DeadTime=0x04;// 1/168=5.95 20/5.95=3.36
TIM_BDTRConfig(TIM1,&TIM_BDTRStructure);
//初始化TIM1 Channel1 PWM模式
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //选择定时器模式:TIM脉冲宽度调制模式2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
// TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性:TIM输出比较极性低
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //当定时器计数值小于CCR1_Val时为高电平
TIM_OCInitStructure.TIM_OutputNState=TIM_OutputNState_Enable;
TIM_OCInitStructure.TIM_OCNPolarity=TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState=TIM_OCIdleState_Reset; //输出空闲电平低
TIM_OCInitStructure.TIM_OCNIdleState=TIM_OCNIdleState_Reset;
TIM_OCInitStructure.TIM_Pulse = ((arr+1)/2);
TIM_SetCompare1(TIM1, ((arr+1)/2));
TIM_OC1Init(TIM1, &TIM_OCInitStructure); //使能通道1
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_SetCompare2(TIM1, ((arr+1)/2));
TIM_OC2Init(TIM1, &TIM_OCInitStructure); //使能通道2
TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_SetCompare3(TIM1, ((arr+1)/2));
TIM_OC3Init(TIM1, &TIM_OCInitStructure); //使能通道3
TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_SetCompare4(TIM1, ((arr+1)/2));
TIM_OC4Init(TIM1, &TIM_OCInitStructure); //使能通道4
TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM1,ENABLE);//ARPE使能
// TIM_ITConfig(TIM1,TIM_IT_Update,ENABLE); //允许定时器1更新中断
TIM_Cmd(TIM1,ENABLE); //使能定时器3
TIM_CtrlPWMOutputs(TIM1,ENABLE);
// NVIC_InitStructure.NVIC_IRQChannel=TIM1_IRQn; //定时器1中断
// NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x01; //抢占优先级1
// NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x03; //子优先级3
// NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
// NVIC_Init(&NVIC_InitStructure);
}
int main(void)
{
int i=0,fx=0;
delay_init(168); //初始化延时函数
TIM3_Int_Init(8400-1,10-1);
TIM1_Int_Init(6720-1,0);
while(1)
{
if(fx==0)
{
i+=100;;
if(i>=6718)
{
fx=1;
}
}
else
{
i--;
if(i==0)
{
fx=0;
}
}
TIM_SetCompare1(TIM1,i); //修改比较值,修改占空比
TIM_SetCompare2(TIM1,i); //修改比较值,修改占空比
TIM_SetCompare3(TIM1,i); //修改比较值,修改占空比
TIM_SetCompare4(TIM1,i); //修改比较值,修改占空比
}
}
看了手册只有三路的互补方波,第四路只有一个方波,没有互补的;
引脚就是这几个
如有错误请各位大佬指出,谢谢!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。