STM32有关通用定时器产生PWM波

2019-07-21 02:31发布

#include "stm32f4xx.h"
#include <stdio.h>
#define  DS1   GPIOF_Pin_9   
#define  DS2   GPIO_Pin_10
   
//???±????//?¨????????????????????????????????????C?????????????????ú??????·¨·???????????
static void Delay(__IO uint32_t nCount) 
{
  for (; nCount != 0; nCount--)
  {
  }
}   int main (void)
{
   
 
   uint16_t CCR1_Val=333;
   uint16_t CCR2_Val=249;
   uint16_t CCR3_Val=166;
   uint16_t CCR4_Val=83;
    u16 led0pwmval=0;   
   u8 dir=1;
 // uint16_t  PrescalerValue=0;
   GPIO_InitTypeDef  GPIO_InitStructure;
   TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
   //NVIC_InitTypeDef NVIC_InitStructure;
   TIM_OCInitTypeDef  TIM_OCInitStructure;    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE); 
   RCC_APB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE);
   
   
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9| GPIO_Pin_10;
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
   GPIO_Init(GPIOF, &GPIO_InitStructure);
   
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9;
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
   GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
   GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
   GPIO_Init(GPIOC, &GPIO_InitStructure);
   
   
   
   
   //????±í??io??c×é??????????
   GPIO_PinAFConfig(GPIOC,GPIO_PinSource6,GPIO_AF_TIM3);
   GPIO_PinAFConfig(GPIOC,GPIO_PinSource7,GPIO_AF_TIM3);
   GPIO_PinAFConfig(GPIOC,GPIO_PinSource8,GPIO_AF_TIM3);
   GPIO_PinAFConfig(GPIOC,GPIO_PinSource9,GPIO_AF_TIM3);
   
   
    
   
   //PrescalerValue=(uint16_t) (SystemCoreClock/12000)-1;
   TIM_TimeBaseInitStructure.TIM_Period =500-1;  
   TIM_TimeBaseInitStructure.TIM_Prescaler=84-1; 
   TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;
   TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;
     TIM_TimeBaseInit(TIM3,&TIM_TimeBaseInitStructure);
   
   
   
   //????±à?????·PWM?????????ò
   
   
   
   /*Channel1*/
   TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;//????PWM??????
   TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;//±???????????
   TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;//?????????§
   TIM_OCInitStructure.TIM_Pulse=CCR1_Val;
   TIM_OC1Init(TIM3,&TIM_OCInitStructure);
   TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
   
   TIM_ARRPreloadConfig(TIM3,ENABLE);
   TIM_Cmd(TIM3,ENABLE);
   
   
   
   /*Channel2*/
   
   TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;//????PWM??????
   TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;//±???????????
   TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;//?????????§
   TIM_OCInitStructure.TIM_Pulse=CCR2_Val;
   TIM_OC2Init(TIM3,&TIM_OCInitStructure);
   TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
   
   TIM_ARRPreloadConfig(TIM3,ENABLE);
   TIM_Cmd(TIM3,ENABLE);
   
   
   
    /*Channel3*/ 
   
   TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;//????PWM??????
   TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;//±???????????
   TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;//?????????§
   TIM_OCInitStructure.TIM_Pulse=CCR3_Val;
   TIM_OC3Init(TIM3,&TIM_OCInitStructure);
   TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);
   
   
   TIM_ARRPreloadConfig(TIM3,ENABLE);
   TIM_Cmd(TIM3,ENABLE);
   
   
    /*Channel4*/ 
   TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;//????PWM??????
   TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;//±???????????
   TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;//?????????§
   TIM_OCInitStructure.TIM_Pulse=CCR4_Val;
   TIM_OC4Init(TIM3,&TIM_OCInitStructure);
   TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);
   
   
   TIM_ARRPreloadConfig(TIM3,ENABLE);
   TIM_Cmd(TIM3,ENABLE);
   
   
   
   
   
   
   
   
   //?????¨????????????????±?PWM??????±???±???
   
   //void TIM_SetCompare1();   ??????·¨?????è????????×é????????°???×é??????????????TIMx_CCR??????±?????±???????????ABC?à?????ò??·??ò????
   
   
   
   
   
//   TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);
//   
//     
//   
//   NVIC_InitStructure.NVIC_IRQChannel=TIM3_IRQn;
//   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
//   NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
//   NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
//   NVIC_Init(&NVIC_InitStructure);
//      TIM_Cmd(TIM3,ENABLE);
      GPIO_ResetBits(GPIOF, GPIO_Pin_9);
   GPIO_SetBits(GPIOF, GPIO_Pin_10);
      while(1)
     {
     Delay(1000); 
    if(dir)led0pwmval++;
    else led0pwmval--; 
    if(led0pwmval>300)dir=0;
    if(led0pwmval==0)dir=1; 
    TIM_SetCompare1(TIM3,led0pwmval);
    TIM_SetCompare2(TIM3,led0pwmval);
    TIM_SetCompare3(TIM3,led0pwmval);
    TIM_SetCompare4(TIM3,led0pwmval);    
    //GPIO_ResetBits(GPIOF, GPIO_Pin_9);
     
     
      // GPIO_ResetBits(GPIOF, GPIO_Pin_10);
     
     
     
     
    //GPIO_ResetBits(GPIOF, GPIO_Pin_7);
   //Delay(100);
     }      
}  // void   TIM3_IRQHandler(void)
// {
//    if (TIM_GetITStatus(TIM3,TIM_IT_Update)==SET)
//  {
//  
//  
//   GPIO_SetBits(GPIOF, GPIO_Pin_10);
//    GPIO_ResetBits(GPIOF, GPIO_Pin_9);
//  }   
//   TIM_ClearITPendingBit(TIM3,TIM_IT_Update);
//
//
// }



这是修改的程序,在MDK上模拟仿真时,显示不出PWM波?请大侠们,指点。

友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。