本帖最后由 白丁 于 2014-8-14 23:09 编辑
1、在开关矩阵中给RXD和TXD分配引脚,设置系统时钟、主时钟、和UART时钟分频
- SystemCoreClockUpdate();
- Init_UART0_PinMux();
复制代码
2、全局定义
3、定义配置结构体,初始化UART API的指针
4、定义要发送的字符串
5、为UART API 初始化memory并创建句柄
6、初始化
UART,配置波特率。
- /* Set UART base rate */
- uint32_t Chip_Clock_SetUARTBaseClockRate(uint32_t rate, bool fEnable)
- {
- uint32_t div, inclk;
- /* Input clock into FRG block is the main system cloock */
- inclk = Chip_Clock_GetMainClockRate();
- /* Get integer divider for coarse rate */
- div = inclk / rate;
- if (div == 0) {
- div = 1;
- }
- /* Approximated rate with only integer divider */
- Chip_Clock_SetUARTFRGDivider((uint8_t) div);
- if (fEnable) {
- uint32_t err;
- uint64_t uart_fra_multiplier;
- err = inclk - (rate * div);
- uart_fra_multiplier = ((uint64_t) err * 256) / (uint64_t) (rate * div);
- /* Enable fractional divider and set multiplier */
- LPC_SYSCTL->FRGCTRL = 0xFF | ((uart_fra_multiplier & 0xFF) << 8);
- }
- else {
- /* Disable fractional generator and use integer divider only */
- LPC_SYSCTL->FRGCTRL = 0;
- }
- return Chip_Clock_GetUARTBaseClockRate();
- }
复制代码
7、使能UART中断
- NVIC_EnableIRQ(UART0_IRQn);
复制代码
8、配置
UART 常数结构体UART_PARAM_T
- /**
- * @brief UART ROM driver parameter structure
- */
- typedef struct {
- uint8_t *buffer; /*!< Pointer to data buffer */
- uint32_t size; /*!< Size of the buffer */
- uint16_t transfer_mode; /*!< Transfer mode settings */
- /*!< 0x00: uart_get_line: stop transfer when the buffer is full */
- /*!< 0x00: uart_put_line: stop transfer when the buffer is empty */
- /*!< 0x01: uart_get_line: stop transfer when CRLF are received */
- /*!< 0x01: uart_put_line: transfer stopped after reaching \0 and CRLF is sent out after that */
- /*!< 0x02: uart_get_line: stop transfer when LF are received */
- /*!< 0x02: uart_put_line: transfer stopped after reaching \0 and LF is sent out after that */
- /*!< 0x03: uart_get_line: RESERVED */
- /*!< 0x03: uart_put_line: transfer stopped after reaching \0 */
- uint8_t driver_mode; /*!< Driver mode */
- /*!< 0x00: Polling mode, function blocked until transfer completes */
- /*!< 0x01: Interrupt mode, function immediately returns, callback invoked when transfer completes */
- /*!< 0x02: DMA mode, in case DMA block is available, DMA req function is called for UART DMA channel setup, then callback function indicate that transfer completes */
- uint8_t dma_num; /*!< DMA channel number in case DMA mode is enabled */
- UART_CALLBK_T callback_func_pt;
- uint32_t dma; /* DMA handler */
- } UART_PARAM_T;
复制代码
9、定义传输结束时调用的接收和发送回调函数
10、发送字符串,遇到\0时结束发送,然后发送回车换行(CR:Carriage Return LF:Line Feed \r\n)
11、接收字符串直到填满缓冲区