int main(void)
{
delay_init(); //延时函数初始化
LED_Init(); //初始化与LED连接的硬件接口
TIM3_PWM_Init(8999,0);//不分频。PWM频率=72000/(899+1)=80Khz
//arr=8999;
/*
频率:
//Fpwm = 72M / ((arr+1)*(psc+1))(单位:Hz)
*/
// dutyCircle(50,2);//占空比,频率
while(1)
{
delay_ms(10);
TIM_SetCompare1(TIM1,4000);
TIM_SetCompare4(TIM1,4000);
}
}
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); //使能定时器3时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO, ENABLE); //开启PC时钟
GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE);
//设置TIM3管脚为复用输出PB0->CH3 B1->ch4
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //TIM_CH4
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);//初始化GPIO
//初始化TIM3
TIM_TimeBaseStructure.TIM_Period = arr; //设置重装值
TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置分频数
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //初始化TIM3
//设置TIM3 Channe3 WM模式
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //选择PWM2模式
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_Pulse = 0; //设置待装入捕获比较寄存器的脉冲值
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出比较极性高
TIM_OC4Init(TIM3, &TIM_OCInitStructure); //CH3初始化TIM3 OC3
TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable); //使能CH4
//TIM_ARRPreloadConfig(TIM3, ENABLE); //使能TIMx在ARR上的预装载寄存器
TIM_Cmd(TIM3, ENABLE); //使能TIM3
}
void LED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO, ENABLE); //使能PA,PD端口时钟
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //LED0-->PA.8 端口配置
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz
GPIO_Init(GPIOC, &GPIO_InitStructure); //根据设定参数初始化GPIOA.8
// GPIO_SetBits(GPIOA,GPIO_Pin_8); //PA.8 输出高
GPIO_SetBits(GPIOC,GPIO_Pin_9);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //LED1-->PD.2 端口配置, 推挽输出
GPIO_Init(GPIOD, &GPIO_InitStructure); //推挽输出 ,IO口速度为50MHz
GPIO_SetBits(GPIOD,GPIO_Pin_2); //PD.2 输出高
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
把LED初始化函数RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO, ENABLE);
中的|RCC_APB2Periph_AFIO去掉
现在是可以了不?
可以了
一周热门 更多>