问答

汇集网友智慧,解决技术难题

21ic问答首页 - 串口波特率不同,发送数据长度过长时会自动分包

串口波特率不同,发送数据长度过长时会自动分包

qgy10072024-03-04
回答 +关注 14
12104人浏览 14人回答问题 分享 举报
14 个回答
  • 本帖最后由 qgy1007 于 2024-3-5 09:27 编辑


    /******************************************************************************
    * Include files
    ******************************************************************************/
    #include "ddl.h"
    #include "uart.h"
    #include "gpio.h"
    #include "sysctrl.h"

    /******************************************************************************
    * Local pre-processor symbols/macros ('#define')                           
    ******************************************************************************/

    /******************************************************************************
    * Global variable definitions (declared in header file with 'extern')
    ******************************************************************************/

    /******************************************************************************
    * Local type definitions ('typedef')                                         
    ******************************************************************************/

    /******************************************************************************
    * Local function prototypes ('static')
    ******************************************************************************/

    /******************************************************************************
    * Local variable definitions ('static')                                      *
    ******************************************************************************/
    uint8_t u8TxData[50] = {  
      0xAA,0x55,0xAA,0x55,0xAA,0xAA,0x55,0xAA,0x55,0xAA,
      0xAA,0x55,0xAA,0x55,0xAA,0xAA,0x55,0xAA,0x55,0xAA,
      0xAA,0x55,0xAA,0x55,0xAA,0xAA,0x55,0xAA,0x55,0xAA,
      0xAA,0x55,0xAA,0x55,0xAA,0xAA,0x55,0xAA,0x55,0xAA,
      0xAA,0x55,0xAA,0x55,0xAA,0xAA,0x55,0xAA,0x55,0xAA
    };

    /*****************************************************************************
    * Function implementation - global ('extern') and local ('static')
    ******************************************************************************/
    void App_PortInit(void);
    void App_UartCfg(void);

    /**
    ******************************************************************************
    ** \brief  Main function of project
    **
    ** \return uint32_t return value, if needed
    **
    ** This sample
    **
    ******************************************************************************/
    int32_t main(void)
    {  
        uint8_t i;
       
        App_PortInit();       //端口初始化
       
        App_UartCfg();        //串口模块配置
       
        while(1)
        {
            if((Uart_GetStatus(M0P_UART1, UartFE))||(Uart_GetStatus(M0P_UART1, UartPE)))  //错误请求
            {
                Uart_ClrStatus(M0P_UART1, UartFE);            //清除帧错误标记
                Uart_ClrStatus(M0P_UART1, UartPE);            //清除奇偶校验错误标记
            }
            if(Uart_GetStatus(M0P_UART1,UartRC))              //接收到数据
            {
                Uart_ClrStatus(M0P_UART1,UartRC);
                u8TxData[0] = Uart_ReceiveData(M0P_UART1);    //接收数据一字节数据后,回复50字节数据
                for(i=0;i<50;i++)
                {
                    Uart_SendDataPoll(M0P_UART1,u8TxData); //查询方式发送数据           }
            }
        }
    }
    //串口引脚配置
    void App_PortInit(void)
    {
        stc_gpio_cfg_t stcGpioCfg;
       
        DDL_ZERO_STRUCT(stcGpioCfg);
       
        Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio,TRUE); //GPIO外设模块时钟使能
       
        stcGpioCfg.enDir = GpioDirOut;
        Gpio_Init(GpioPortD,GpioPin0,&stcGpioCfg);
        Gpio_SetAfMode(GpioPortD,GpioPin0,GpioAf3); //配置PA09 为UART0 TX
        stcGpioCfg.enDir = GpioDirIn;
        Gpio_Init(GpioPortD,GpioPin1,&stcGpioCfg);
        Gpio_SetAfMode(GpioPortD,GpioPin1,GpioAf3);//配置PA10 为UART0 RX
    }

    //串口模块配置
    void App_UartCfg(void)
    {
        stc_uart_cfg_t  stcCfg;
        stc_uart_multimode_t stcMulti;
        stc_uart_baud_t stcBaud;

        DDL_ZERO_STRUCT(stcCfg);
        DDL_ZERO_STRUCT(stcMulti);
        DDL_ZERO_STRUCT(stcBaud);
       
        Sysctrl_SetPeripheralGate(SysctrlPeripheralUart1,TRUE);//UART0外设模块时钟使能
       
        stcCfg.enRunMode = UartMskMode3;     //模式3
        stcCfg.enStopBit = UartMsk1bit;      //1位停止位
        stcCfg.enMmdorCk = UartMskEven;      //偶校验
        stcCfg.stcBaud.u32Baud = 2400;       //波特率9600
        stcCfg.stcBaud.enClkDiv = UartMsk8Or16Div;         //通道采样分频配置
        stcCfg.stcBaud.u32Pclk = Sysctrl_GetPClkFreq();    //获得外设时钟(PCLK)频率值
        Uart_Init(M0P_UART1, &stcCfg);       //串口初始化

        Uart_ClrStatus(M0P_UART1,UartRC);    //清接收请求
        Uart_ClrStatus(M0P_UART1,UartTC);    //清发送请求
        Uart_EnableIrq(M0P_UART1,UartRxIrq); //使能串口接收中断
        Uart_EnableIrq(M0P_UART1,UartTxIrq); //使能串口发送中断

    }

    /******************************************************************************
    * EOF (not truncated)
    ******************************************************************************/



  • hjl2832 发表于 2024-3-5 08:13
    仔细检查自己的代码,看是不是哪里有时间限制(超时处理类)的操作,这个不关芯片的事。 ...

    我用的是官方例子代码,轮询发送数据
  • 仔细检查自己的代码,看是不是哪里有时间限制(超时处理类)的操作,这个不关芯片的事。
  • 先查软件!
  • 例如我通过串口发50字节数据,会分成两部分数据,一部分32字节,一部分剩下的字节,两部分数据相隔50ms
12

您需要登录后才可以回复 登录 | 注册