Knowledge about serial ports
Three independent serial ports, each of which can use DMA
And interrupt operation. Each contains two 64-byte FIFO, one connection and one sending.
The non-FIFO mode is equivalent to a register buffer mode in the FIFO mode.
Each UART has seven statuses: overrun error, verification error, frame error, breakpoint, and receiving buffer. The sending buffer is empty and the sending shift register is empty.
When receiving data from the shift register to the FIFO, the received data triggers the RXFIFO, RXInterrupted.
When the first-in-first-out data in the transmitter reaches the Tx FIFO threshold, the Tx interruption occurs. (I think it should be understood:The sending of a FIFO packet in the sender ends, that is, an interruption occurs when it is null .)
Function: Enter 16 bytes on the serial port, including carriage return, and then display the entered characters back on the serial port terminal.
Program code:
// Function declaration
Void uart_init (void );
Void int_init (void );
Void _ IRQ uart0 (void );
Void main (void );
// Cache
Static unsigned char mywords [100] = {0, 0 };
Static int data_is_ready = 0;
// Initialize UART
Void uart_init (void)
{
// Initialization pin
Rgphcon = 0x00faaa; // set the pin to txd0 and rxd0.
Rgphup= 0x7ff;// No pulling resistance is added.
// Initialize UART
Rulcon0 = 0x03; // send 8-bit data each time, one stop bit, no verification, Normal Mode
Rucon0 = (0x05) | (1 <9); // you can specify that the receiving and sending methods are interrupted.
Rufcon0 = (0x3 <6) | (0x2 <4) | (0x01 <0); // enable FIFO, send 48 bytes, receive 16 bytes
Rumcon0 = 0;
// Set the clock frequency
Rubrdiv0 = 26;
}
// Initialization interruption
Void int_init (void)
{
Rintmod = 0x0; // interrupt mode register
Pisr_uart0 = (INT) uart0; // set the interrupt service function address
Rintsubmsk = ~ (0x3 );// Enable uart0 sending and receiving interruption shielding
Rintmsk = ~ (0x1 <28 );// Enable uart0 interrupt shielding
// Rsubsrcpnd = (bit_sub_txd0 );
}
Void main (void)
{
Mmu_init ();
Int_init ();
Uart_init ();
While (1 );
}
// Interrupt handling function
Void _ IRQ uart0 (void)
{
Unsigned char * PS = mywords;
Int I;
If (rsubsrcpnd & bit_sub_rxd0)// Receive interrupted
{
Rintsubmsk | = bit_sub_rxd0;
While (rufstat0 & 0x1f)> 0 ))
{
* PS ++ = rurxh0;
}
Data_is_ready = 1;
Rsubsrcpnd = bit_sub_rxd0;
}
// Sending interrupted
Else if (rsubsrcpnd & bit_sub_txd0)
{
Rintsubmsk | = bit_sub_txd0;
While ((! (Rufstat0 & (1 <14) & (* PS! = '\ R '))
{
Rutxh0 = * PS ++;
For (I = 0; I <100; I ++ );
}
Data_is_ready = 0;
Rsubsrcpnd = bit_sub_txd0;
}
Rsrcpnd = bit_uart0;
Rintpnd = bit_uart0;
If (data_is_ready)
Rintsubmsk & = ~ (Bit_sub_txd0 );
Else
Rintsubmsk & = ~ (Bit_sub_rxd0 );
// Rintsubmsk & = ~ (Bit_sub_txd0 );
}
The function can be completed after testing.