UART学习笔记

串口(UART)

DIV_VAL = (PCLK / (bps x 16 ) ) 

        35 =  115200/66.5/16-1

 

 

查看芯片手册:

GPACON  0x7F008000  R/W  Port A Configuration Register  0x0000 

 

 

GPA0  [3:0]  0000 = Input   0001 = Output 

0010 = UART RXD[0]  0011 = Reserved 

0100 = Reserved  0101 = Reserved 

0110 = Reserved  0111 = External Interrupt Group 1 [0] 

0000 

GPA1  [7:4]  0000 = Input   0001 = Output 

0010 = UART TXD[0]  0011 = Reserved 

0100 = Reserved  0101 = Reserved 

0110 = Reserved  0111 = External Interrupt Group 1 [1] 

0000 

 

 

ULCON0 0x7F005000 R/W UART channel 0 line control register  0x00 

 

 

Reserved [7]  0 

Infra-Red Mode  [6]  Determine whether ornot to use the Infra-Red mode. 

0 = Normal mode operation 

1 = Infra-Red Tx/Rx mode 

Parity Mode  [5:3]  Specify the type of parity generation and checking during 

UART transmit and receive operation. 

0xx = No parity 

100 = Odd parity 

101 = Even parity 

110 = Parity forced/checked as 1 

111 = Parity forced/checked as 0 

000 

Number of Stop Bit  [2]  Specify how many stop bits are to be used for end-of-frame 

signal. 

0 = One stop bit per frame 

1 = Two stop bit per frame 

Word Length  [1:0]  Indicate the number ofdata bits to be transmitted or received 

per frame. 

00 = 5-bit 01 = 6-bit 

10 = 7-bit 11 = 8-bit

UCON0 0x7F005004 R/W UART channel 0 control register  0x00

Transmit Mode  [3:2]  Determine which function is currently able to write Tx data to the 

UART transmit buffer register. 

00 = Disable 

01 = Interrupt request or polling mode 

10 = DMA request (DMA_UART0) 

11 = DMA request (DMA_UART1) 

00 

Receive Mode  [1:0]  Determine which function is currently able to read data from UART 

receive buffer register. 

00 = Disable 

01 = Interrupt request or polling mode 

10 = DMA request (DMA_UART0) 

11 = DMA request (DMA_UART1) 

 

 

UFCON0 0x7F005008 R/W UART channel 0 FIFO control register  0x0 

FIFO Enable  [0]  0 = Disable 1 = Enable  0 

 

UMCON0 0x7F00500C R/W UART channel 0 Modem control register  0x0 

 

DIV_VAL = UBRDIVn + (num of 1s in UDIVSLOTn)/16 

DIV_VAL = (PCLK / (bps x 16 ) ) 1        

For example, if the baud-rate is 115200 bps and EXT_UCLK0 is UART baud-rate clock and 40 MHz, UBRDIVn 

and UDIVSLOTn are: 

 

 DIV_VAL  = (40000000 / (115200 x 16) ) -1  = 21.7 -1 

= 20.7 

UBRDIVn = 20 (integer part of DIV_VAL ) 

 (num of 1s in UDIVSLOTn)/16 = 0.7 

 then, (num of 1s in UDIVSLOTn) = 11 

 

66.5mhz*10(6)=66500000

 

UBRDIV0  0x7F005028  R/W  Baud rate divisior register 0  0x0000 

UDIVSLOT0  0x7F00502C  R/W  Baud rate divisior register 0  0x0000 

 

UFSTAT0 0x7F005018 R UART channel 0 FIFO status register  0x00 

 

Tx FIFO Full  [14]  Set to 1 automatically whenever transmit FIFO is full 

during transmit operation 

0 = 0-byte Tx FIFO data 63-byte 

1 = Full 

Tx FIFO Count  [13:8]  Number of data in Tx FIFO  0 

Reserved [7]  0 

Rx FIFO Full  [6]  Set to 1 automaticallywhenever receive FIFO is full during 

receive operation 

0 = 0-byte Rx FIFO data 63-byte 

1 = Full 

Rx FIFO Count  [5:0]  Number of data in Rx FIFO  0 

URXH0  0x7F005024  R  UART channel 0 receive buffer register  0x00

UTXH0  0x7F005020  W  UART channel 0 transmit buffer register  -

 

Tx FIFO Full  [14]  Set to 1 automatically whenever transmit FIFO is full 

during transmit operation 

0 = 0-byte Tx FIFO data 63-byte 

1 = Full 

示例代码如下:

 

#define ULCON0     (*((volatile unsigned long *)0x7F005000))

#define UCON0      (*((volatile unsigned long *)0x7F005004))

#define UFCON0     (*((volatile unsigned long *)0x7F005008))

#define UMCON0     (*((volatile unsigned long *)0x7F00500C))

#define UTRSTAT0   (*((volatile unsigned long *)0x7F005010))

#define UFSTAT0    (*((volatile unsigned long *)0x7F005018))

#define UTXH0      (*((volatile unsigned char *)0x7F005020))

#define URXH0      (*((volatile unsigned char *)0x7F005024))

#define UBRDIV0    (*((volatile unsigned short *)0x7F005028))

#define UDIVSLOT0  (*((volatile unsigned short *)0x7F00502C))

 

#define GPACON     (*((volatile unsigned long*)0x7F008000))

 

 

void init_uart(void)

{

/*GPACON的前八位清零*/

/*1010 1010*/

/*0000 0000 ~0xff*/

/*--------------*/

/*0000 0000*/

 

GPACON &= ~0xff;

/*GPACON初值*/

GPACON |= 0x22;

 

/* ULCON0 */

ULCON0 = 0x3;  /* 数据位:8, 无较验停止位: 1, 8n1 */

UCON0  = 0x5;  /* 使能UART发送、接收 */

UFCON0 = 0x01; /* FIFO ENABLE */

 

UMCON0 = 0;

 

/* 波特率 */

/* DIV_VAL = (PCLK / (bps x 16 ) ) - 1 

 * bps = 57600

 * DIV_VAL = (66500000 / (115200 x 16 ) ) - 1 

 *         = 35.08

 */

UBRDIV0   = 35;

 

/* x/16 = 0.08

 * x = 1

 */

UDIVSLOT0 = 0x1;

 

}

 

char getchar(void)

{

while ((UFSTAT0 & (1<<6)) == 0 && (UFSTAT0 & 0x3f) == 0);

return URXH0;

}

 

void putchar(char c)

{

while ((UFSTAT0 & (1<<14));

UTXH0 = c;

}

posted @ 2013-05-04 22:00  retacn_yue  阅读(838)  评论(0编辑  收藏  举报