我不知道是什么原因,开启了UART1后 TIM8就不能正常输出PWM了

2019-10-16 02:28发布

本帖最后由 vention 于 2016-12-9 14:54 编辑

[mw_shl_code=applescript,true]#include "delay.h"
#include "sys.h"

void uart_init(u32 bound);
void time_init(void);

int main(void)
{
        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);          
           uart_init(115200);                  

    time_init();
       
    while(1)
        {  
        }
}

void uart_init(u32 bound)
{

  GPIO_InitTypeDef GPIO_InitStructure;
  USART_InitTypeDef USART_InitStructure;
  NVIC_InitTypeDef NVIC_InitStructure;
         
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE);         

        //USART1_TX   GPIOA.9
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;         
  GPIO_Init(GPIOA, &GPIO_InitStructure);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //PA.10
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

    NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;                 
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;                         
        NVIC_Init(&NVIC_InitStructure);         

        USART_InitStructure.USART_BaudRate = bound;
        USART_InitStructure.USART_WordLength = USART_WordLength_8b;
        USART_InitStructure.USART_StopBits = USART_StopBits_1;
        USART_InitStructure.USART_Parity = USART_Parity_No;
        USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
        USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;         

  USART_Init(USART1, &USART_InitStructure);  
  USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
  USART_Cmd(USART1, ENABLE);                     

}
void time_init()
{

      GPIO_InitTypeDef            GPIO_InitStructure;
      TIM_TimeBaseInitTypeDef     TIM_TimeBaseStructure;
      TIM_OCInitTypeDef           TIM_OCInitStructure;

   RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC| RCC_APB2Periph_TIM8, ENABLE);  //¶¨ê±Æ÷£¬GPIOA     

    /* TIM8 channels as alternate function push-pull */   
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;  
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
   GPIO_Init(GPIOC, &GPIO_InitStructure);   

    /* Time Base configuration */
    TIM_TimeBaseStructure.TIM_Prescaler = 720-1;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseStructure.TIM_Period = 98-1;
    TIM_TimeBaseStructure.TIM_ClockDivision = 0;
    TIM_TimeBaseStructure.TIM_RepetitionCounter = 5;  
    TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure);

    /* Channels Configuration in Toggle mode */
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Disable;
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
    TIM_OC1Init(TIM8, &TIM_OCInitStructure);
    TIM_OC3Init(TIM8, &TIM_OCInitStructure);
    TIM_OC2Init(TIM8, &TIM_OCInitStructure);
    TIM_OC4Init(TIM8, &TIM_OCInitStructure);

    TIM_CtrlPWMOutputs(TIM8, ENABLE);
    TIM_SelectOnePulseMode(TIM8, TIM_OPMode_Repetitive);
    TIM_Cmd(TIM8, ENABLE);      

}

[/mw_shl_code]照抄原子哥的UART1,然后UART1功能正常,TIM8就不能正常输出PWM了,屏蔽掉 UART1功能,TIM8就能正常输出PWM了,请问这是怎么回事??
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。