UART polling mode is much simpler than interrupt mode. The UART initialization code is as follows:
Copy code
1 voidUARTInit(void)
2 {
3 // Configure UART1 parameters
4 // Baud rate 115200 data bits 8 stop bits 1 no parity bit
5 UARTConfigSetExpClk(SOC_UART_1_REGS, UART_1_FREQ, BAUD_115200, UART_WORDL_8BITS, UART_OVER_SAMP_RATE_16);
6 // Enable UART1
7 UARTEnable(SOC_UART_1_REGS);
8 }
Copy code
Because the non-fifo mode is selected, the UART initialization code only needs to set the UART parameters, and there is no need to configure and enable the FIFO. At the same time, because the polling mode is used, there is no need to initialize the UART interrupt, so after initializing the UART, all the initialization steps are completed.
The main function is as follows:
Copy code
1 int main(void)
2 {
3 // Peripheral enable configuration
4 PSCInit();
5
6 // GPIO pin multiplexing configuration
7 GPIOBankPinMuxSet();
8
9 // UART initialization
10 UARTInit();
11
12 // Send string
13 unsignedchar i;
14 for(i = 0; i < 34; i++)
15 UARTCharPut(SOC_UART_1_REGS, Send);
16
17 // Receive buffer
18 unsignedchar Receive;
19 // Main loop
20 for(;;)
21 {
22 Receive=UARTCharGet(SOC_UART_1_REGS);
23 UARTCharPut(SOC_UART_1_REGS, Receive);
24 }
25 }
Copy code
The main function is also very simple. First, UARTCharPut(SOC_UART_1_REGS, Send); sends a defined string to the serial port. The UARTCharPut function is as follows:
Copy code
1 voidUARTCharPut(unsignedint baseAdd, unsignedchar byteTx)
2 {
3 unsignedint txEmpty;
4
5 txEmpty = (UART_THR_TSR_EMPTY | UART_THR_EMPTY);
6
7 /*
8 ** Here we check for the emptiness of both the Trasnsmitter Holding
9 ** Register( THR) and Transmitter Shift Register(TSR) before writing
10 ** data into the Transmitter FIFO(THR for non-FIFO mode).
11 */
12
13 while (txEmpty != (HWREG(baseAdd + UART_LSR) & txEmpty));
14
15 /*
16 ** Transmitter FIFO(THR register in non-FIFO mode) is empty.
17 ** Write the byte onto the THR register.
18 */
19 HWREG(baseAdd + UART_THR) = byteTx;
20 }
Copy code
UART serial port transmission mode. In this function, the program first continuously queries the TEMT and THRE bits of the LSR register to determine the THR register (transmitter holding register, THR) and TSR Check whether the register (transmitter shift register, TSR) is empty. If it is empty, write a byte of number into THR, otherwise continue to query until it is empty.
(Guide P1440)
Then the program starts to enter the main loop. In the main loop, the program first waits for receiving data Receive = UARTCharGet (SOC_UART_1_REGS); UARTCharGet function is as follows:
Copy code
1 intUARTCharGet(unsignedint baseAdd)
2 {
3 int data = 0;
4
5 /*
6 ** Busy check if data is available in receiver FIFO(RBR regsiter in non-FIFO
7 ** mode) so that data could be read from the RBR register.
8 */
9 while ((HWREG(baseAdd + UART_LSR) & UART_DATA_READY) == 0);
10
11 data = (int)HWREG(baseAdd + UART_RBR);
12
13 return data;
14 }
Copy code In
UART receive mode, when the DR bit of the LSR register is 0, it means that the data is ready and the data is received in the receive buffer register RBR (receiver buffer register (RBR)). Then the data in RBR is read out. Because it is non-fifo mode, one byte of data is read out.
(Guide P1442)
Then, the program sends the read data to UART intact and returns it, UARTCharPut(SOC_UART_1_REGS, Receive);.
|