当发送指令放置在接收中断时,一切正常。如果把发送中断移值主程序,偶尔就会出现发送指令不执行或延迟执行的现象 。 //=================================== // 初始化Lpuart //=================================== void lpuart_init(void) { uint16_t u16Scnt = 0; stc_gpio_config_t stcGpioCfg; stc_lpuart_sclk_sel_t stcSclk; stc_lpuart_config_t stcConfig; stc_lpuart_irq_cb_t stcLPUartIrqCb; stc_lpuart_multimode_t stcMulti; stc_lpuart_baud_t stcBaud; DDL_ZERO_STRUCT(stcConfig); DDL_ZERO_STRUCT(stcLPUartIrqCb); DDL_ZERO_STRUCT(stcMulti); DDL_ZERO_STRUCT(stcBaud); DDL_ZERO_STRUCT(stcGpioCfg); DDL_ZERO_STRUCT(stcSclk); Sysctrl_SetPeripheralGate(SysctrlPeripheralGpio, TRUE); Sysctrl_SetPeripheralGate(SysctrlPeripheralLpUart1, TRUE); stcGpioCfg.enDir = GpioDirOut; Gpio_Init(GpioPortA, GpioPin0, &stcGpioCfg); //TX stcGpioCfg.enDir = GpioDirIn; stcGpioCfg.enPuPd = GpioPu; Gpio_Init(GpioPortA, GpioPin1, &stcGpioCfg); //RX Gpio_SetAfMode(GpioPortA, GpioPin0, GpioAf2); Gpio_SetAfMode(GpioPortA, GpioPin1, GpioAf2); stcLPUartIrqCb.pfnRxIrqCb = LPUartRxIntCallback; stcLPUartIrqCb.pfnTxIrqCb = NULL; stcLPUartIrqCb.pfnRxFEIrqCb = NULL; stcLPUartIrqCb.pfnPEIrqCb = NULL; stcLPUartIrqCb.pfnCtsIrqCb = NULL; stcConfig.pstcIrqCb = &stcLPUartIrqCb; stcConfig.bTouchNvic = TRUE; stcConfig.enStopBit = LPUart1bit; stcConfig.enRunMode = LPUartMode1;// stcSclk.enSclk_Prs = LPUart4Or8Div; stcSclk.enSclk_sel = LPUart_Rcl; stcConfig.pstcLpuart_clk = &stcSclk; stcMulti.enMulti_mode = LPUartNormal; stcConfig.pstcMultiMode = &stcMulti; LPUart_Init(LPUART1, &stcConfig); LPUart_SetClkDiv(LPUART1, LPUart4Or8Div); stcBaud.u32Sclk = LPUart_GetSclk(LPUART1); stcBaud.enRunMode = LPUartMode1; stcBaud.u32Baud = 9600; u16Scnt = LPUart_CalScnt(LPUART1, &stcBaud); LPUart_SetBaud(LPUART1, u16Scnt); LPUart_EnableIrq(LPUART1, LPUartRxIrq); LPUart_ClrStatus(LPUART1, LPUartRC); LPUart_EnableFunc(LPUART1, LPUartRx); } //=================================== // LPUART 中断服务函数 //=================================== void LPUartRxIntCallback(void) { Uartbuff[UartRX_dp++] = LPUart_ReceiveData(LPUART1);; if(UartRX_dp >= 8) { if( (Uartbuff[0] == PollAdd) || (Uartbuff[0] == 0) ) { if((Uartbuff[1] == 3) || (Uartbuff[1] == 6) || (Uartbuff[1] == 8)) { ISRcrc16 = ISRcal_crc16(Uartbuff, 6); if( (Uartbuff[6] == ((uint8_t)ISRcrc16)) && ( Uartbuff[7] == ((uint8_t)(ISRcrc16 >> 8))) ) { //准备发送 UartHartrecok = 0x55; if((Uartbuff[1] == 3) || (Uartbuff[1] == 6)) **.ReportSum** = 0; // Rxreal**=0; UartRX_dp = 0; } code16 = 0; } else if(Uartbuff[1] == 0x10) { if(UartRX_dp >= (Uartbuff[6] + 9)) { ISRcrc16 = ISRcal_crc16(Uartbuff, Uartbuff[6] + 7); if( (Uartbuff[Uartbuff[6] + 7] == ((uint8_t)ISRcrc16)) && ( Uartbuff[Uartbuff[6] + 8] == ((uint8_t)(ISRcrc16 >> 8))) ) { //准备发送 UartHartrecok = 0x55; **.ReportSum** = 0; // Rxreal**=0; UartRX_dp = 0; } code16 = 0; //20220419 } else code16 = 0x55; //未收完 } else code16 = 0; } if( (UartHartrecok != 0x55) && (code16 != 0x55) ) //数据不对,移除最旧的一个字节 { uint8_t i; for(i = 0; i < UartRX_dp - 1; i++) Uartbuff = Uartbuff[i + 1]; UartRX_dp -= 1; } if(UartHartrecok == 0x55) { MODBUSManagement(); } } } //=================================== // LPUART发送命令处理函数 //=================================== void MODBUSManagement(void) { if(UartHartrecok == 0x55) { uint8_t UartTX_cn; UartHartrecok = 0; UartTX_cn = Uart_rx_deal(Uartbuff); /*数据发送*/ for(uint8_t i = 0; i < UartTX_cn; i++) { LPUart_SendData(LPUART1, Uartbuff); } } } //=================================== // 主函数 //=================================== int32_t main(void) { lpuart_init(); 初始化 while(1) { Wdt_Feed(); MODBUSManagement(); if((!**.M50** || Resetdly > 0) && Ovmagdly == 0) { //LCDLIB_PrintRSSI(4);//关闭信号强度显示 stcConfig.enSLEEPDEEP = SlpDpEnable; Lpm_Config(&stcConfig); Lpm_GotoLpmMode(); } else { stcConfig.enSLEEPDEEP = SlpDpDisable; Lpm_Config(&stcConfig); Lpm_GotoLpmMode(); } } } |