串口1换串口3,出现问题???

2019-07-20 06:36发布

[mw_shl_code=c,true]#include "stm32f4xx.h"
#include "stdio.h"

u8 RES;

void USART3_Init()
{
        USART_InitTypeDef USART_InitStruct;
        GPIO_InitTypeDef GPIO_InitStruct;
        NVIC_InitTypeDef NVIC_InitStruct;
       
        /*使能USART、对应GPIO的时钟*/
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);
                /*连接USART3与GPIO*/

        GPIO_PinAFConfig(GPIOB,GPIO_PinSource10,GPIO_AF_USART3);
        GPIO_PinAFConfig(GPIOB,GPIO_PinSource11,GPIO_AF_USART3);       
       
        /*初始化GPIO,设置复用模式*/
        GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF;
        GPIO_InitStruct.GPIO_OType=GPIO_OType_PP;
        GPIO_InitStruct.GPIO_PuPd=GPIO_PuPd_UP;
        GPIO_InitStruct.GPIO_Speed=GPIO_Speed_100MHz;       
        GPIO_InitStruct.GPIO_Pin=GPIO_Pin_10;       
        GPIO_Init(GPIOB,&GPIO_InitStruct);
        GPIO_InitStruct.GPIO_Pin=GPIO_Pin_11;       
        GPIO_Init(GPIOB,&GPIO_InitStruct);//由于是用GPIO做其他的事,就没有使能GPIO的东西
               
        /*初始化USART3*/
        USART_InitStruct.USART_BaudRate=115200;
        USART_InitStruct.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
        USART_InitStruct.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;//有三种选择:RX、 TX、 R,TX
        USART_InitStruct.USART_Parity=USART_Parity_No;
        USART_InitStruct.USART_StopBits=USART_StopBits_1;
        USART_InitStruct.USART_WordLength=USART_WordLength_8b;
        USART_Init(USART3,&USART_InitStruct);
        /*使能中断*/
        USART_Cmd(USART3,ENABLE);
       
        /*配置中断NVIC*/
        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//选择优先组
        NVIC_InitStruct.NVIC_IRQChannel=USART3_IRQn;
        NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
        NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=1;
        NVIC_InitStruct.NVIC_IRQChannelSubPriority=1;
        NVIC_Init(&NVIC_InitStruct);
        /*使能NVIC*/
        USART_ITConfig(USART3,USART_IT_RXNE,ENABLE);
}
/*中断服务函数,在startup_stm32f40_41xxx.s的87行*/
void USART3_IRQHandler(void)
{
        if(USART_GetITStatus(USART3,USART_IT_RXNE))//        上面用了RXNE使能,那么这里就要用RXNE判断
                {               
                        RES=USART_ReceiveData(USART3);//接收
                        USART_SendData(USART3,RES);//发送
                }
}

int main(void)
{
                USART3_Init();
                while(1)
                {
                        if(RES=='A')
                        {
                                printf("123456 ");
                        }
                }
}


[/mw_shl_code][mw_shl_code=c,true]#include "stm32f4xx.h"
#include "stdio.h"

u8 RES;

void USART1_Init()
{
        USART_InitTypeDef USART_InitStruct;
        GPIO_InitTypeDef GPIO_InitStruct;
        NVIC_InitTypeDef NVIC_InitStruct;
       
        /*使能USART、对应GPIO的时钟*/
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
                /*连接USART1与GPIO*/
        GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_USART1);
        GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_USART1);       
       
        /*初始化GPIO,设置复用模式*/
        GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF;
        GPIO_InitStruct.GPIO_OType=GPIO_OType_PP;
        GPIO_InitStruct.GPIO_PuPd=GPIO_PuPd_UP;
        GPIO_InitStruct.GPIO_Speed=GPIO_Speed_100MHz;       
        GPIO_InitStruct.GPIO_Pin=GPIO_Pin_9;       
        GPIO_Init(GPIOA,&GPIO_InitStruct);
        GPIO_InitStruct.GPIO_Pin=GPIO_Pin_10;       
        GPIO_Init(GPIOA,&GPIO_InitStruct);//由于是用GPIO做其他的事,就没有使能GPIO的东西
               
        /*初始化USART1*/
        USART_InitStruct.USART_BaudRate=115200;
        USART_InitStruct.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
        USART_InitStruct.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;//有三种选择:RX、 TX、 R,TX
        USART_InitStruct.USART_Parity=USART_Parity_No;
        USART_InitStruct.USART_StopBits=USART_StopBits_1;
        USART_InitStruct.USART_WordLength=USART_WordLength_8b;
        USART_Init(USART1,&USART_InitStruct);
        /*使能中断*/
        USART_Cmd(USART1,ENABLE);
       
        /*配置中断NVIC*/
        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//选择优先组
        NVIC_InitStruct.NVIC_IRQChannel=USART1_IRQn;
        NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
        NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=1;
        NVIC_InitStruct.NVIC_IRQChannelSubPriority=1;
        NVIC_Init(&NVIC_InitStruct);
        /*使能NVIC*/
        USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
}
/*中断服务函数,在startup_stm32f40_41xxx.s的87行*/
void USART1_IRQHandler(void)
{
        if(USART_GetITStatus(USART1,USART_IT_RXNE))//        上面用了RXNE使能,那么这里就要用RXNE判断
                {               
                        RES=USART_ReceiveData(USART1);//接收
                        USART_SendData(USART1,RES);//发送
                }
}

int main(void)
{
                USART1_Init();
                while(1)
                {
                        if(RES=='A')
                        {
                                printf("123456 ");
                        }

                }
}


[/mw_shl_code]



串口1可以打印printf("123456 ");
但是串口3为什么没有打印出来??

友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
12条回答
1208
1楼-- · 2019-07-21 01:56
peng1554 发表于 2018-8-16 20:28
估计是fputc函数没有指向串口3,或者是串口3引脚没接USB转TTL电路

这个我得好好去看下
1208
2楼-- · 2019-07-21 06:41
 精彩回答 2  元偷偷看……
TT1998
3楼-- · 2019-07-21 10:55
 精彩回答 2  元偷偷看……
1208
4楼-- · 2019-07-21 16:43
TT1998 发表于 2018-8-17 18:31
对对对,就是重定义的fputc的函数,其实可以定义一个宏,当用到什么串口就传入那个

是的,很顽强。接下来,你需要用串口3来干什么
TT1998
5楼-- · 2019-07-21 20:27
 精彩回答 2  元偷偷看……
1208
6楼-- · 2019-07-21 22:01
TT1998 发表于 2018-8-18 08:43
我用串口三和EXTI2控制led,观察一下优先级的结果

可以哦!很有想法,结果怎么样有点小期待

一周热门 更多>