UART initialization.
1. We can see through the schematic diagram that the serial port rxd and txd are connected to ok6410 gpa0 and gpa1 respectively .. So first set the input and output status of gpio: 0010 = UART rxd [0] 0010 = UART txd [0]
At the same time, set the corresponding up/down resistance relationship.
rGPACON = (rGPACON & ~(0xff<<0)) | (0x22<<0);
rGPAPUD = (rGPAPUD & ~(0xf<<0)) | (0x1<<0);
2 UART initialization:
2.1 configure UART 0-channel row control register. Shows the ulcon0 format.
Rulcon0 = rulcon0 & (0 xffffffff) | (0 <6) | (0 <3) | (0 <2) | (3 <0 ); // In normal operation mode, no one-bit stop-bit 8-Bit Data bit is verified.
2.2 configure the ucon0 register.
rUCON0=rUCON0&(0xFFFFFFFF)|(0<<10)|(1<<9)|(1<<8)|(0<<7)|(0<<6)|(0<<5)|(0<<4)|(1<<2)|(1<<0);
·
2.3 configure ufcon0 register
rUFCON0=rUFCON0&(0xFFFFFFFF)|(0<<6)|(0<<4)|(0<<2)|(0<<1)|(0<<1);
2.4 configure rumcon0 register
rUMCON0 = (0<<4)|(0<<0);
2.5 set the baud rate ubrdiv0 and udivslot0 registers
Because this mode is selected when ucon0 registers are configured above, the following formula is used.
Div_val = (pclk/(B/S × 16)-1
Pclk = 66 MHz = 66000000Hz
Div_val = 66000000/(115200x16)-1
= 35.8-1
= 34.8
Ubrdivn = 20 (integer part of div_val)
(Number of 1 in udivslotn)/16 = 0.8
At this time, (Number of 1 in udivslotn) = 12
Therefore, udivslotn is 16 'b1101 _ 1101_1101_1101 or 16 'b0111 _ 0111_0111_0101.
Num of 1's udivslotn num of 1's udivslotn
0 0x0000 (1__1__1_ B) 8 0x5555 (0101_0101_0101b)
1 0x0080 (1__1__1000b) 9 0xd555 (1101_0101_0101_0101b)
2 0x0808 (1__1000_1__1000b) 10 0xd5d5 (1101_0101_1101_0101b)
3 0x0888 (1__1000_1000_1000b) 11 0xddd5 (1101_1101_1101_0101b)
4 0x2222 (0010_0010_0010_0010b) 12 0 xdddd (1101_1101_1101b)
5 0x4924 (0100_1001_0010_0100b) 13 0 xdfdd (1101_11_1101_1101b)
6 0x4a52 (0100_1010_01000000010b) 14 0 xdfdf (110000011_11000001111b)
7 0x54aa (0101_0100_1010_1010b) 15 0 xffdf (1111_1111_1101_1111b)
Based on the above analysis:
rUBRDIV0=34;rUDIVSLOT0=0xDDDD;
3 UART Transmission
The main function calls the uart_printf () function to implement the printing function.
When UART is sent, it determines whether the FIFO buffer (the second digit of utrstat0) is null. If it is null, it is sent. (This is set in the sixth position in the ufcon0 register)
void Uart_SendByte(int data) { while(!(rUTRSTAT0&0x02)); WrUTXH0(data);
}
Final project file: http://pan.baidu.com/share/link? Consumer id = 64455 & UK = 3037598361