分享到plurk 分享到twitter 分享到facebook

版本 313e8940b0d358cf1e3675c5d1c62bcb642988b2

embedded/USART

INTRODUCTION ………………. The universal synchronous asynchronous receiver transmitter (USART) offers a flexible means of full-duplex data exchange with external equipment requiring an industry standard NRZ asynchronous serial data format. The USART offers a very wide range of baud rates using a fractional baud rate generator.

It supports synchronous one-way communication and half-duplex single wire communication. It also supports the LIN (local interconnection network), Smartcard Protocol and IrDA (infrared data association) SIR ENDEC specifications, and modem operations (CTS/RTS). It allows multiprocessor communication.

High speed data communication is possible by using the DMA for multibuffer configuration.

MAIN FEATURES ……………….

● Full duplex, asynchronous communications

● NRZ standard format (Mark/Space)

● Configurable oversampling method by 16 or by 8 to give flexibility between speed and clock tolerance

● Fractional baud rate generator systems

– Common programmable transmit and receive baud rate (refer to the datasheets for the value of the baud rate at the maximum APB frequency.

● Programmable data word length (8 or 9 bits)

● Configurable stop bits - support for 1 or 2 stop bits

● LIN Master Synchronous Break send capability and LIN slave break detection capability

– 13-bit break generation and 10/11 bit break detection when USART is hardware configured for LIN

● Transmitter clock output for synchronous transmission

● IrDA SIR encoder decoder

– Support for 3/16 bit duration for normal mode

● Smartcard emulation capability

– The Smartcard interface supports the asynchronous protocol Smartcards as defined in the ISO 7816-3 standards

– 0.5, 1.5 stop bits for Smartcard operation

● Single-wire half-duplex communication

● Configurable multibuffer communication using DMA (direct memory access)

– Buffering of received/transmitted bytes in reserved SRAM using centralized DMA

● Separate enable bits for transmitter and receiver

● Transfer detection flags:

– Receive buffer full

– Transmit buffer empty

– End of transmission flags

● Parity control:

– Transmits parity bit

– Checks parity of received data byte

● Four error detection flags: – Overrun error – Noise detection – Frame error – Parity error

● Ten interrupt sources with flags:

– CTS changes

– LIN break detection

– Transmit data register empty

– Transmission complete

– Receive data register full

– Idle line received

– Overrun error

– Framing error

– Noise error

– Parity error

● Multiprocessor communication - enter into mute mode if address match does not occur

● Wake up from mute mode (by idle line detection or address mark detection)

● Two receiver wakeup modes: Address bit (MSB, 9th bit), Idle line

CODE SECTION ………………. main() { //初始化GPIO GPIO_InitTypeDef GPIO_InitStructure;

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOB, &GPIO_InitStructure);

GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_USART1); GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_USART1);

//初始化UART SART_InitStructure.USART_BaudRate = 115200;//設定baudrate USART_InitStructure.USART_WordLength = USART_WordLength_8b;//設定word長度為8位 USART_InitStructure.USART_StopBits = USART_StopBits_1;//1位停止字節 USART_InitStructure.USART_Parity = USART_Parity_No;//無奇偶檢驗 USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//無流量控制 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//開啟Rx接收和Tx發送功能

USART_Init(USART1, &USART_InitStructure); USART_Cmd(USART1, ENABLE);//啟動usart1

while(1) { while(i < 100){ send_byte(‘G’); i++; } while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) != RESET)//等到有data才變為RESET b = (USART_ReceiveData(USART1) & 0x7F);

}

} void send_byte(uint8_t b) {

USART_SendData(USART1, b);

while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);//直到USART1是空

}

……………….