TIM2_CH2的引脚为PA1,我用杜邦线接到了PB.5 , PB.5是DS0的引脚接口
灯亮了,但是就是没有进行亮度变化
求解
主函数
#include "stm32f10x.h"
#include "led.h"
#include "pwm.h"
#include "delay.h"
#include "sys.h"
int main(void)
{
u16 led0pwmval=0;
u8 dir=1;
delay_init();
TIM2_PWM_Init(179,0);
while(1)
{
if(dir)led0pwmval++;
else led0pwmval--;
if(led0pwmval>150)dir=0;
if(led0pwmval==0)dir=1;
TIM_SetCompare2(TIM2,led0pwmval);
}
}
#include "pwm.h"
#include "sys.h"
void TIM2_PWM_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitSture;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitSture;
TIM_OCInitTypeDef TIM_OCInitSture;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitSture.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitSture.GPIO_Pin=GPIO_Pin_1;
GPIO_InitSture.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitSture);
TIM_TimeBaseInitSture.TIM_ClockDivision=0;
TIM_TimeBaseInitSture.TIM_CounterMode=TIM_CounterMode_Down;
TIM_TimeBaseInitSture.TIM_Period=arr;
TIM_TimeBaseInitSture.TIM_Prescaler=psc;
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseInitSture);
TIM_OCInitSture.TIM_OCMode=TIM_OCMode_PWM1;
TIM_OCInitSture.TIM_OutputState =TIM_OutputState_Enable;
TIM_OCInitSture.TIM_OCPolarity=TIM_OCPolarity_Low;
TIM_OC2Init(TIM2,&TIM_OCInitSture);
TIM_OC2PreloadConfig(TIM2,TIM_OCPreload_Enable);
TIM_Cmd(TIM2,ENABLE);
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
试了,连灯都不亮了= =,这个应该是输出吧,定时器输出PWM波控制灯亮度
按照你的改了,还是不行,只能亮个灯
一周热门 更多>