This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

串口发送中断进不去



大哥:

这么些天天麻烦你帮忙解决问题,我自己都不好意思了,但估计最近一个月都还会问你许多我不懂的问题,

在问问题前,首先对你帮我结局的问题表示深深的感谢!!!

继昨天调试串口接收终端回环发送之后,我几天调试发送终端,把数据发送放在了中断服务函数中,但调试

中发现,所需要发送的字符串始终无法发送到终端,但感觉中断入服务函数了。还有昨天的回环接受实验,

发现发送到中断的数据会有错位,比如我在终端发送123给TM4C129,第一次按发送,返回的是12,第二

次发送返回的312。

  • Share 一个uart串口发送数据的代码给你。串口中断服务程序再收到一个任意字节时,会置位UartTxIntEnFlag标志。主循环检测到这个标志后,会打开串口发送中断,把UartTxBuf中的数据发出去。

    你的应用程序可以根据这个流程进行对应修改。这个代码我自己改的,可以用的。

    #include <stdint.h>
    #include <stdbool.h>
    #include "inc/hw_ints.h"
    #include "inc/hw_memmap.h"
    #include "driverlib/debug.h"
    #include "driverlib/fpu.h"
    #include "driverlib/gpio.h"
    #include "driverlib/interrupt.h"
    #include "driverlib/pin_map.h"
    #include "driverlib/rom.h"
    #include "driverlib/sysctl.h"
    #include "driverlib/uart.h"

    uint8_t UartRxDatFlag = false;
    uint8_t UartTxIntEnFlag = false;

    uint8_t UartTxBuf[10] = {0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39};
    uint16_t UartTxLen = 10;
    uint16_t UartTxIndex = 0;

    //*****************************************************************************
    //
    //! \addtogroup example_list
    //! <h1>UART Echo (uart_echo)</h1>
    //!
    //! This example application utilizes the UART to echo text. The first UART
    //! (connected to the USB debug virtual serial port on the evaluation board)
    //! will be configured in 115,200 baud, 8-n-1 mode. All characters received on
    //! the UART are transmitted back to the UART.
    //
    //*****************************************************************************


    //*****************************************************************************
    //
    // The error routine that is called if the driver library encounters an error.
    //
    //*****************************************************************************
    #ifdef DEBUG
    void
    __error__(char *pcFilename, uint32_t ui32Line)
    {
    }
    #endif

    //*****************************************************************************
    //
    // The UART interrupt handler.
    //
    //*****************************************************************************
    void
    UARTIntHandler(void)
    {
    uint32_t ui32Status;
    //uint32_t val;

    //
    // Get the interrrupt status.
    //
    ui32Status = ROM_UARTIntStatus(UART0_BASE, true);

    //
    // Clear the asserted interrupts.
    //
    ROM_UARTIntClear(UART0_BASE, ui32Status);

    if((ui32Status == UART_INT_RX) || (ui32Status == UART_INT_RT))
    {
    //
    // Loop while there are characters in the receive FIFO.
    //
    while(ROM_UARTCharsAvail(UART0_BASE))
    {
    //
    // Read the next character from the UART and write it back to the UART.
    //
    //ROM_UARTCharPutNonBlocking(UART0_BASE, ROM_UARTCharGetNonBlocking(UART0_BASE));
    ROM_UARTCharGetNonBlocking(UART0_BASE);

    UartTxIntEnFlag = true;
    //
    // Blink the LED to show a character transfer is occuring.
    //
    GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, GPIO_PIN_2);

    //
    // Delay for 1 millisecond. Each SysCtlDelay is about 3 clocks.
    //
    SysCtlDelay(SysCtlClockGet() / (1000 * 3));

    //
    // Turn off the LED
    //
    GPIOPinWrite(GPIO_PORTF_BASE, GPIO_PIN_2, 0);

    }
    }

    else if(ui32Status == UART_INT_TX)
    {
    if(UartTxIndex < UartTxLen)
    {
    ROM_UARTCharPut(UART0_BASE, UartTxBuf[UartTxIndex ++]);
    if(UartTxIndex >= UartTxLen)
    {
    UartTxIndex = 0;
    ROM_UARTIntDisable(UART0_BASE, UART_INT_TX);
    }
    }
    }
    }

    //*****************************************************************************
    //
    // Send a string to the UART.
    //
    //*****************************************************************************
    void
    UARTSend(const uint8_t *pui8Buffer, uint32_t ui32Count)
    {
    //
    // Loop while there are more characters to send.
    //
    while(ui32Count--)
    {

    //
    // Write the next character to the UART.
    //
    ROM_UARTCharPutNonBlocking(UART0_BASE, *pui8Buffer++);
    while(ROM_UARTBusy(UART0_BASE));
    }
    }

    //*****************************************************************************
    //
    // This example demonstrates how to send a string of data to the UART.
    //
    //*****************************************************************************
    int
    main(void)
    {
    //
    // Enable lazy stacking for interrupt handlers. This allows floating-point
    // instructions to be used within interrupt handlers, but at the expense of
    // extra stack usage.
    //
    ROM_FPUEnable();
    ROM_FPULazyStackingEnable();

    //
    // Set the clocking to run directly from the crystal.
    //
    ROM_SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
    SYSCTL_XTAL_16MHZ);

    //
    // Enable the GPIO port that is used for the on-board LED.
    //
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);

    //
    // Enable the GPIO pins for the LED (PF2).
    //
    ROM_GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_2);

    //
    // Enable the peripherals used by this example.
    //
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
    ROM_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);

    //
    // Enable processor interrupts.
    //
    ROM_IntMasterEnable();

    //
    // Set GPIO A0 and A1 as UART pins.
    //
    GPIOPinConfigure(GPIO_PA0_U0RX);
    GPIOPinConfigure(GPIO_PA1_U0TX);
    ROM_GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);


    //
    // Configure the UART for 115,200, 8-N-1 operation.
    //
    ROM_UARTConfigSetExpClk(UART0_BASE, ROM_SysCtlClockGet(), 115200,
    (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE |
    UART_CONFIG_PAR_NONE));

    ROM_UARTFIFODisable(UART0_BASE);
    //
    // Enable the UART interrupt.
    //
    ROM_IntEnable(INT_UART0);
    ROM_UARTIntEnable(UART0_BASE, UART_INT_RX | UART_INT_RT);

    //
    // Prompt for text to be entered.
    //
    UARTSend((uint8_t *)"\033[2JEnter text: ", 16);

    //
    // Loop forever echoing data through the UART.
    //
    while(1)
    {
    if(UartTxIntEnFlag == true)
    {
    UartTxIntEnFlag = false;
    ROM_UARTIntEnable(UART0_BASE, UART_INT_TX);
    }
    }
    }

  • 能否在调用发送函数时,在发送函数中软件触发串口中断,认为将中断屏蔽寄存器的发送位人为置1?

  • 也可以实现的。

    串口中断的机制是,使能UART_INT_TX中断时由于发送数据寄存器为空,就会立即进入中断服务程序。然后在中断服务程序中把数据写进去就发出去了。

    最后一个数据发送完毕后还会进中断,这时候判断数据发完了,就把中断关了就行了。