【STM32F103奇怪问题】USART1和TIM1PWM相互影响

2019-08-13 20:04发布

最近做项目遇到了一个奇怪的问题 就是TIM1PWM和usart1相互影响, 就是谁放后面,就会覆盖前面的,
TIM_Config();
USART_Config();  例如这样的话就会覆盖掉前面的TIM1PWM波,

USART_Config();  
TIM_Config();    例如这样的话就会覆盖掉前面的USART1,

我怀疑是端口复用搞得鬼,大家有解决的办法吗?

[mw_shl_code=c,true]void TIM_Config(void)
{
                GPIO_InitTypeDef GPIO_InitStructure;
                TIM_TimeBaseInitTypeDef TIM_TimeBaseStructureInit;
                TIM_OCInitTypeDef TIM_OCInitStructure;
                TIM_BDTRInitTypeDef  TIM_BDTRInitStructure;

                RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);  
                RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);  
                RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);       
                RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);

                GPIO_DeInit(GPIOA);
                GPIO_DeInit(GPIOB);

/*****TIM1_CH1 2 3*****/
                GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10;    //PA8
                GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;  
                GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;

                GPIO_Init(GPIOA,&GPIO_InitStructure);

/*****TIM1_CH1N 2N 3N*****/
                GPIO_InitStructure.GPIO_Pin=GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;   //PB13
                GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;  
                GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;

                GPIO_Init(GPIOB,&GPIO_InitStructure);[/mw_shl_code]


[mw_shl_code=c,true]void USART_Config(void)
{
                GPIO_InitTypeDef GPIO_InitStructure;       
                USART_InitTypeDef USART_InitStructure;
                NVIC_InitTypeDef NVIC_InitStructure;

                RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
          RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); //¿ªÆô¸´ÓÃʱÖÓ
                GPIO_PinRemapConfig(GPIO_Remap_USART1,ENABLE);

                GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_6 ;
                GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF_PP;
                GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
                GPIO_Init(GPIOB, &GPIO_InitStructure);

                GPIO_InitStructure.GPIO_Pin   = GPIO_Pin_7 ;
                GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_IN_FLOATING;
                GPIO_Init(GPIOB, &GPIO_InitStructure);[/mw_shl_code]

友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
11条回答
hxyqq87
2019-08-14 08:14
我也遇到了 求解!!!
/*
  适用于STM32F103CBT6 / STM32F103C8T6
*/
int main(void)
{       
        u16 led0pwmval=0;   
        u8 dir=1;       
        u8 T_Clock;
        delay_init();                       //延时函数初始化          
        LED_GPIO_Config();         //LED I/O初始化
//        uart_init(115200);         //初始化串口1的波特率为115200 初始化串口1(PA9 PA10)放TIM1_PWM_Init()前PA8(不正常)无电压变化-0V 串口发送正常
        TIM1_PWM_Init(900-1,1-1); //3分频,则计数频率为72M/3=24 Hz,PWM频率=24M/24000=1Khz (PA8)
        uart_init(115200);         //初始化串口1的波特率为115200 初始化串口1(PA9 PA10)放TIM1_PWM_Init()后PA8正常 串口发送也正常
        while(1)
        {
               
                if(dir)led0pwmval++;
                else led0pwmval--;         
                if(led0pwmval>300)dir=0;
                if(led0pwmval==0)dir=1;                                                    
                TIM_SetCompare1(TIM1,led0pwmval);       
               
                if(T_Clock==200)
                {
                        LED0 = !LED0;
                }
                T_Clock++;
                printf("程序正在运行 ");
                delay_ms(5);                        
        }
}

一周热门 更多>