pwm和led两个历程放在一起,pwm输出为什么就会有问题呢

2019-07-21 00:49发布

 int main(void)
 {
u16 led0pwmval=0;    
u8 dir=1;
uart_init(9600); //?®??????????9600
delay_init();     //???±??????????  
LED_Init();   //????????LED??????????????
LCD_Init(); 
TIM1_PWM_Init(899,0);//??·?????PWM????=72000/(899+1)=80Khz 
LCD_ShowString(30,70,200,16,16,"TFTLCD TEST");
LCD_ShowString(30,90,200,16,16,"ATOM@ALIENTEK");
    while(1)
{

  delay_ms(10);  
if(dir)led0pwmval++;
else led0pwmval--;  
  if(led0pwmval>300)dir=0;
if(led0pwmval==0)dir=1;  
LCD_ShowxNum(30,110,led0pwmval,16,16,0);
TIM_SetCompare1(TIM1,300);   

}


//PWM??????????
//arr??×?????×°??
//psc???±???¤·?????
void TIM1_PWM_Init(u16 arr,u16 psc)
{  
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
TIM_OCInitTypeDef  TIM_OCInitStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);// 
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE);  //????GPIO???è?±??????
                                                                   

   //?è??????????????????????,????TIM1 CH1??PWM?????¨??
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //TIM_CH1
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  //???????ì????
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);


TIM_TimeBaseStructure.TIM_Period = arr; //?è???????????ü??????×°????????×?????×°???????÷???????? 80K
TIM_TimeBaseStructure.TIM_Prescaler =psc; //?è??????×÷??TIMx?±?????????????¤·?????  ??·???
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //?è???±??·???:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIM?ò??????????
TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //?ù??TIM_TimeBaseInitStruct?????¨????????????TIMx???±???ù??????

 
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //?????¨?±?÷????:TIM?????í???÷??????2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //±???????????
TIM_OCInitStructure.TIM_Pulse = 0; //?è????×°??????±????????÷????????
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //????????:TIM????±?????????
TIM_OC1Init(TIM1, &TIM_OCInitStructure);  //?ù??TIM_OCInitStruct?????¨???????????????èTIMx

  TIM_CtrlPWMOutputs(TIM1,ENABLE); //MOE ?÷????????

TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);  //CH1?¤×°??????  

TIM_ARRPreloadConfig(TIM1, ENABLE); //????TIMx??ARR?????¤×°???????÷

TIM_Cmd(TIM1, ENABLE);  //????TIM1
 
   
}

两个历程的内容都没有改,只是单纯的放在一起啊,为什么就不能输出pwm了呢
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
4条回答
lycreturn
1楼-- · 2019-07-21 03:05
 精彩回答 2  元偷偷看……
15565613775
2楼-- · 2019-07-21 04:58
回复【2楼】lycreturn:
---------------------------我知道没有改变的,可是压根没有输出占空比的。我只要屏蔽lcd初始化,就可以输出占空比。是不是计时器冲突啊
15565613775
3楼-- · 2019-07-21 07:42
 精彩回答 2  元偷偷看……
lycreturn
4楼-- · 2019-07-21 12:25
回复【4楼】15565613775:
---------------------------------
这可能是 库函数代码的BUG  可以这样改  
先TIM1_PWM_Init(899,0);  再LCD_Init();

一周热门 更多>