• 已解决 73482 个问题
  • 已帮助 5993 位优秀工程师

stm32串口实验可发送数据但是无法显示回传数据为什么?

liubo 2019-09-11 浏览量:1105
代码如下:
#include "stm32f10x.h"

void My_USART1_Init()
{
     GPIO_InitTypeDef GPIO_InitStrue;
     USART_InitTypeDef USART_InitStrue;
     NVIC_InitTypeDef NVIC_Initstrue;
     RCC_APB2PeriphResetCmd(RCC_APB2Periph_GPIOAENABLE);
     RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1ENABLE);
     
     GPIO_InitStrue.GPIO_Mode=GPIO_Mode_AF_PP;
     GPIO_InitStrue.GPIO_Pin=GPIO_Pin_9;
     GPIO_InitStrue.GPIO_Speed=GPIO_Speed_10MHz;
     GPIO_Init(GPIOA&GPIO_InitStrue);
     
     GPIO_InitStrue.GPIO_Mode=GPIO_Mode_IN_FLOATING;
     GPIO_InitStrue.GPIO_Pin=GPIO_Pin_10;
     GPIO_InitStrue.GPIO_Speed=GPIO_Speed_10MHz;
     GPIO_Init(GPIOA&GPIO_InitStrue);
     
     USART_InitStrue.USART_BaudRate=115200;
     USART_InitStrue.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
     USART_InitStrue.USART_Mode=USART_Mode_Tx|USART_Mode_Rx;
     USART_InitStrue.USART_Parity=USART_Parity_No;
     USART_InitStrue.USART_StopBits=USART_StopBits_1;
     USART_InitStrue.USART_WordLength=USART_WordLength_8b;
     USART_Init(USART1&USART_InitStrue);
     USART_Cmd(USART1ENABLE);
     USART_ITConfig(USART1USART_IT_RXNEENABLE);
     
     NVIC_Initstrue.NVIC_IRQChannel=USART1_IRQn;
     NVIC_Initstrue.NVIC_IRQChannelCmd=ENABLE;
     NVIC_Initstrue.NVIC_IRQChannelPreemptionPriority=1;
     NVIC_Initstrue.NVIC_IRQChannelSubPriority=1;
     
     NVIC_Init(&NVIC_Initstrue);
}
void  USART1_IRQHandler(void)
{
    u8 res;
    if(USART_GetITStatus(USART1USART_IT_RXNE))
    {
        res=USART_ReceiveData(USART1);
        USART_SendData(USART1res);
    }
   
}
int main(void)
{
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
     My_USART1_Init();
     while(1);
}


0 0 收起

我来回答

上传资料:
选择文件 文件大小不超过15M(格式支持:doc、ppt、xls、pdf、zip、rar、txt)
所有亮答 数量:2

  • 参考一下正点原子的程序

    void My_usart_Init(void)

    {
    GPIO_InitTypeDef GPIO_InitStructure; 
    USART_InitTypeDef USART_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;


    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1ENABLE);//使能时钟

    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;
    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_10MHz;
    GPIO_Init(GPIOA&GPIO_InitStructure); //GPIO配置

    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
    GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_10MHz;
    GPIO_Init(GPIOA&GPIO_InitStructure); //GPIO配置

    USART_InitStructure.USART_BaudRate=115200;
    USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
    USART_InitStructure.USART_Parity=USART_Parity_No;
    USART_InitStructure.USART_StopBits=USART_StopBits_1;
    USART_InitStructure.USART_WordLength=USART_WordLength_8b;
    USART_Init(USART1&USART_InitStructure); //串口参数初始化

    USART_Cmd(USART1ENABLE); //使能串口



    USART_ITConfig(USART1USART_IT_RXNEENABLE); //开启接收中断


    NVIC_InitStructure.NVIC_IRQChannel=USART1_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
    NVIC_Init(&NVIC_InitStructure); //中断初始化

    }



    void USART1_IRQHandler(void) //中断服务函数
    {
    u8 res;
    if(USART_GetITStatus(USART1USART_IT_RXNE)) // 获取中断状态标志位
    {
    res=USART_ReceiveData(USART1);
    USART_SendData(USART1res);
    }


    }




    int main(void)
    {
     
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置分组
    My_usart_Init();
    while(1);

    }

  • 参考一下

    void My_usart_Init(void)

    {
    GPIO_InitTypeDef GPIO_InitStructure; 
    USART_InitTypeDef USART_InitStructure;
    NVIC_InitTypeDef NVIC_InitStructure;


    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1ENABLE);//使能时钟

    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;
    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_10MHz;
    GPIO_Init(GPIOA&GPIO_InitStructure); //GPIO配置

    GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
    GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
    GPIO_InitStructure.GPIO_Speed=GPIO_Speed_10MHz;
    GPIO_Init(GPIOA&GPIO_InitStructure); //GPIO配置

    USART_InitStructure.USART_BaudRate=115200;
    USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
    USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
    USART_InitStructure.USART_Parity=USART_Parity_No;
    USART_InitStructure.USART_StopBits=USART_StopBits_1;
    USART_InitStructure.USART_WordLength=USART_WordLength_8b;
    USART_Init(USART1&USART_InitStructure); //串口参数初始化

    USART_Cmd(USART1ENABLE); //使能串口



    USART_ITConfig(USART1USART_IT_RXNEENABLE); //开启接收中断


    NVIC_InitStructure.NVIC_IRQChannel=USART1_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
    NVIC_Init(&NVIC_InitStructure); //中断初始化

    }








    void USART1_IRQHandler(void) //中断服务函数
    {
    u8 res;
    if(USART_GetITStatus(USART1USART_IT_RXNE)) // 获取中断状态标志位
    {
    res=USART_ReceiveData(USART1);
    USART_SendData(USART1res);
    }


    }




    int main(void)
    {
     
    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置分组
    My_usart_Init();

    while(1);

    }


相关问题

问题达人换一批

stm32串口实验可发送数据但是无法显示回传数据为什么?