OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 3319|回复: 2

关于f429其他几个串口的问题

[复制链接]

2

主题

8

帖子

0

精华

新手上路

积分
32
金钱
32
注册时间
2019-3-27
在线时间
11 小时
发表于 2019-4-2 14:50:31 | 显示全部楼层 |阅读模式
5金钱
我照着原子哥的视频把串口1换成了串口2,结果无法返回发送的数据,自己找了半天也找不到哪里出了错,大佬们帮忙看看程序代码。



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


u8 rdata[1];
//UART_HandleTypeDef usart1_handler;
UART_HandleTypeDef usart2_handler;

/*void uart1_init()      //串口1初始化
{
    usart1_handler.Instance=USART1;
        usart1_handler.Init.BaudRate=115200;
        usart1_handler.Init.WordLength=UART_WORDLENGTH_8B;
        usart1_handler.Init.StopBits=UART_STOPBITS_1;
        usart1_handler.Init.HwFlowCtl=UART_HWCONTROL_NONE;
        usart1_handler.Init.Mode=UART_MODE_TX_RX;
        usart1_handler.Init.Parity=UART_PARITY_NONE;       
        HAL_UART_Init(&usart1_handler);

}*/

void uart2_init()     //串口2初始化
{
    usart2_handler.Instance=USART2;
        usart2_handler.Init.BaudRate=115200;
        usart2_handler.Init.WordLength=UART_WORDLENGTH_8B;
        usart2_handler.Init.StopBits=UART_STOPBITS_1;
        usart2_handler.Init.HwFlowCtl=UART_HWCONTROL_NONE;
        usart2_handler.Init.Mode=UART_MODE_TX_RX;
        usart2_handler.Init.Parity=UART_PARITY_NONE;       
        HAL_UART_Init(&usart2_handler);

}




void HAL_UART_MspInit(UART_HandleTypeDef *huart)
{
        GPIO_InitTypeDef GPIO_Initure;
       
   /* if(huart->Instance==USART1)
         {

            __HAL_RCC_GPIOA_CLK_ENABLE();                        //使能GPIOA时钟
                __HAL_RCC_USART1_CLK_ENABLE();                        //使能USART1时钟
       
                GPIO_Initure.Pin=GPIO_PIN_9;                        //PA9
                GPIO_Initure.Mode=GPIO_MODE_AF_PP;                //复用推挽输出
                GPIO_Initure.Pull=GPIO_PULLUP;                        //上拉
                GPIO_Initure.Speed=GPIO_SPEED_FAST;                //高速
                GPIO_Initure.Alternate=GPIO_AF7_USART1;        //复用为USART1
                HAL_GPIO_Init(GPIOA,&GPIO_Initure);                   //初始化PA9

                GPIO_Initure.Pin=GPIO_PIN_10;                        //PA10
                HAL_GPIO_Init(GPIOA,&GPIO_Initure);       
                 
                 HAL_NVIC_SetPriority(USART1_IRQn,3,3);
                 HAL_NVIC_EnableIRQ(USART1_IRQn);
         }*/

           if(huart->Instance==USART2)
         {

            __HAL_RCC_GPIOA_CLK_ENABLE();                        //使能GPIOA时钟
                __HAL_RCC_USART2_CLK_ENABLE();                        //使能USART2时钟
       
                GPIO_Initure.Pin=GPIO_PIN_2;                        //PA2
                GPIO_Initure.Mode=GPIO_MODE_AF_PP;                //复用推挽输出
                GPIO_Initure.Pull=GPIO_PULLUP;                        //上拉
                GPIO_Initure.Speed=GPIO_SPEED_FAST;                //高速
                GPIO_Initure.Alternate=GPIO_AF7_USART2;        //复用为USART2
                HAL_GPIO_Init(GPIOA,&GPIO_Initure);                   //初始化PA2

                GPIO_Initure.Pin=GPIO_PIN_3;                        //PA3
                 
                 HAL_NVIC_SetPriority(USART2_IRQn,3,3);
                 HAL_NVIC_EnableIRQ(USART2_IRQn);
         }
         
       
}

/*void USART1_IRQHandler(void)
{
    HAL_UART_IRQHandler(&usart1_handler);
       
        HAL_UART_Receive_IT(&usart1_handler,rdata,sizeof(rdata));

}*/

void USART2_IRQHandler(void)
{
    HAL_UART_IRQHandler(&usart2_handler);
       
        HAL_UART_Receive_IT(&usart2_handler,rdata,sizeof(rdata));

}


/*void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
    u8 rec;
    if(huart->Instance==USART1)
    {
           rec=*(--(huart->pRxBuffPtr));
           HAL_UART_Transmit(&usart1_handler,&rec,sizeof(rec),1000);
        }


}*/

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
    u8 rec;
    if(huart->Instance==USART2)
    {
           rec=*(--(huart->pRxBuffPtr));
           HAL_UART_Transmit(&usart2_handler,&rec,1,1000);
        }


}


int main(void)
{
   
        //u8 buff[]="test";
       
    HAL_Init();                     //初始化HAL库   
    Stm32_Clock_Init(360,25,2,8);   //设置时钟,180Mhz
    delay_init(180);
  
        //uart1_init();
        uart2_init();
        HAL_UART_Receive_IT(&usart2_handler,rdata,sizeof(rdata));
        //HAL_UART_Receive_IT(&usart1_handler,rdata,sizeof(rdata));
       
        while(1)
        {
       
        //HAL_UART_Transmit(&usart2_handler,buff,sizeof(buff),1000);
               
        //delay_ms(300);
                       
        }
       
}
[/mw_shl_code]

main.rar

212 Bytes, 下载次数: 0

最佳答案

查看完整内容[请看2#楼]

参考下这个767的8个串口:http://www.openedv.com/forum.php?mod=viewthread&tid=278777
正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

31

主题

2183

帖子

0

精华

资深版主

Rank: 8Rank: 8

积分
14424
金钱
14424
注册时间
2018-8-3
在线时间
1157 小时
发表于 2019-4-2 14:50:32 | 显示全部楼层
回复

使用道具 举报

2

主题

8

帖子

0

精华

新手上路

积分
32
金钱
32
注册时间
2019-3-27
在线时间
11 小时
 楼主| 发表于 2019-4-2 20:21:58 | 显示全部楼层
找到错误了,是我少使能了一遍引脚
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-6-10 04:05

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表