打印
[其他ST产品]

进不了中断,求大神指点

[复制链接]
559|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
onlycook|  楼主 | 2023-1-5 10:05 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include "bsp_spwm_output.h"
uint16_t  CCR1_Val ;
uint16_t  CCR2_Val = 1500 ;
uint16_t  CCR3_Val = 900;
static void TIM1_Mode_Config(void)
{
   
  GPIO_InitTypeDef GPIO_InitStructure;
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
    TIM_OCInitTypeDef  TIM_OCInitStructure;

    /* GPIOA clocks enable */
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
    /* TIM1 clock enable */
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);

    GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_9 | GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
    GPIO_Init(GPIOA,&GPIO_InitStructure);

    /* Connect TIM pins to AF6 */
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_6);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_6);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_6);

    /* Time Base configuration */
     
    TIM_TimeBaseStructure.TIM_Period =2999;      
  TIM_TimeBaseStructure.TIM_Prescaler = 0;
  TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;    //ÉèÖÃÔ¤·ÖƵ£º²»Ô¤·ÖƵ£¬¼´Îª72MHz
  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;    //ÉèÖÃʱÖÓ·ÖƵϵÊý£º²»·ÖƵ(ÕâÀïÓò»µ½)
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned1;  //ÏòÉϼÆÊýģʽ
  TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);

  /*PWM1 Mode configuration: Channel1 */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;        //ÅäÖÃΪPWMģʽ1
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;   
  TIM_OCInitStructure.TIM_Pulse = CCR2_Val;       //ÉèÖÃÌø±äÖµ£¬µ±¼ÆÊýÆ÷¼ÆÊýµ½Õâ¸öֵʱ£¬µçƽ·¢ÉúÌø±ä
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;  //µ±¶¨Ê±Æ÷¼ÆÊýֵСÓÚCCR1_ValʱΪ¸ßµçƽ
    TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;      
    TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;     
    TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCNIdleState_Set;  

  TIM_OC2Init(TIM1, &TIM_OCInitStructure);     //ʹÄÜͨµÀ1
  TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);
  TIM_ARRPreloadConfig( TIM1,ENABLE);
   
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;   
  TIM_OCInitStructure.TIM_Pulse = CCR3_Val;   
  TIM_OC3Init(TIM1, &TIM_OCInitStructure);     //ʹÄÜͨµÀ4
  TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);
  TIM_ARRPreloadConfig( TIM1,ENABLE);
   
  TIM_ARRPreloadConfig( TIM1,ENABLE);
    TIM_UpdateRequestConfig( TIM1, TIM_UpdateSource_Regular);
  TIM_ITConfig(TIM1,TIM_IT_Update | TIM_IT_Trigger,ENABLE);/* TIM3 enable counter */
  TIM_Cmd(TIM1, ENABLE);     //ʹÄܶ¨Ê±Æ÷3   
    TIM_ClearFlag(TIM1, TIM_FLAG_Update);
    TIM_CtrlPWMOutputs(TIM1, ENABLE);
}

     void NVIC_Configuration(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
  NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn;   //TIM1ÖжÏ
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 6;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;  
  NVIC_Init(&NVIC_InitStructure);      
}         

void TIM1_SPWM_Init(void)
{
     TIM1_Mode_Config();
     NVIC_Configuration();
} // /*****/     

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

435

主题

1620

帖子

3

粉丝