首页资源分类嵌入式系统 > LPC4357学习笔记-UART

LPC4357学习笔记-UART

已有 445023个资源

下载专区

文档信息举报收藏

标    签:LPC4357UART

分    享:

文档简介

LPC4357学习笔记-UART

文档预览

LPC4357学习笔记-UART3 作者:BLUEFLAME 20140403 见手册UM10503见手册UM10503-Rev. 1.8 — 28 January 2014 P1102 寄存器如下 RBR------------接收缓冲寄存器 THR------------发送保持寄存器 DLL------------除数锁存 LSB用于设置波特率 DLM------------除数锁存 MSB用于设置波特率 IER------------中断使能寄存器 IIR------------中断ID 寄存器,识别要挂起的中断 FCR------------FIFO 控制寄存器,控制USART FIFO 的使用和模式 LCR------------线路控制寄存器 LSR------------线路状态寄存器 SCR------------暂存寄存器,软件的八位临时存储器。 ACR------------自动波特率控制寄存器 ICR------------IrDA 控制寄存器(仅限USART3) FDR------------小数分频寄存器,为波特率分频器生成时钟输入 OSR------------过采样寄存器。控制每个位时间内的过采样度 HDEN-----------半双工使能寄存器 SCICTRL--------智能卡接口控制寄存器 RS485CTRL------RS-485/EIA-485 控制 RS485ADRMATCH--RS-485/EIA-485 地址匹配 RS485DLY-------RS-485/EIA-485 方向控制延迟 SYNCCTRL-------同步模式控制寄存器 TER------------发送使能寄存器 具体应用如下 void UART3Init(void)//使用寄存器初始化 { // Enable GPIO register interface clock LPC_CCU1->CLK_M4_GPIO_CFG |= 1; while (!(LPC_CCU1->CLK_M4_GPIO_STAT & 1)); // Enable USART3 peripheral clock 见手册UM10503-P199 LPC_CCU2->CLK_APB2_USART3_CFG |= 1; while (!(LPC_CCU2->CLK_APB2_USART3_STAT & 1)); //Enable USART3 register interface clock 见手册UM10503-P198 LPC_CCU1->CLK_M4_USART3_CFG |= 1; while (!(LPC_CCU1->CLK_M4_USART3_STAT & 1)); // Init GPIO pins 见手册UM10503-P389和P403 LPC_SCU->SFSP2_3 = (1 << 6) | //Input buffer enabled (1 << 4) | // Pull-up disabled (2 << 0) ; // Pin P2_3 used as U3_TXD LPC_SCU->SFSP2_4 = (1 << 6) | // Input buffer enabled (1 << 4) | // Pull-up disabled (2 << 0) ; // Pin P2_4 used as U3_RXD // Init USART3 见UM10503手册P1099 LPC_USART3->LCR = 0x83; // 8 bits, no Parity, 1 Stop bit LPC_USART3->DLL = 0x06; // 115200 Baudrate @ 12 MHz IRC LPC_USART3->DLM = 0x00; LPC_USART3->FDR = 0xC1; LPC_USART3->LCR = 0x03; // DLAB = 0 LPC_USART3->IER |= (1<<0);//使能接收中断 //优先级设置preemption = 1, sub-priority = 1 NVIC_SetPriority(USART3_IRQn, ((0x01<<3)|0x01)); // Enable Interrupt for UART3 channel NVIC_EnableIRQ(USART3_IRQn); } #include "lpc43xx_uart.h" #include "lpc43xx_cgu.h" #include "lpc43xx_scu.h" void UART3LibInit(uint32_t rate)//用库的方式初始化UART3 {// UART Configuration structure variable UART_CFG_Type UARTConfigStruct; // UART FIFO configuration Struct variable UART_FIFO_CFG_Type UARTFIFOConfigStruct; CGU_Init(); /* * Initialize UART3 pin connect */ LPC_SCU->SFSP2_3 = 2|MD_PDN;//U3_TXD LPC_SCU->SFSP2_4 = 2|MD_PLN|MD_EZI|MD_ZI;//U3_RXD /* Initialize UART Configuration parameter structure to default state: * Baudrate = 9600 * 8 data bit * 1 Stop bit * None parity */ UART_ConfigStructInit(&UARTConfigStruct); UARTConfigStruct.Baud_rate=rate; UARTConfigStruct.Clock_Speed=12000000; // Initialize UART3 peripheral with given to corresponding parameter UART_Init((LPC_USARTn_Type *)LPC_USART3, &UARTConfigStruct); /* Initialize FIFOConfigStruct to default state: * - FIFO_DMAMode = DISABLE * - FIFO_Level = UART_FIFO_TRGLEV0 * - FIFO_ResetRxBuf = ENABLE * - FIFO_ResetTxBuf = ENABLE * - FIFO_State = ENABLE */ UART_FIFOConfigStructInit(&UARTFIFOConfigStruct); // Initialize FIFO for UART1 peripheral UART_FIFOConfig((LPC_USARTn_Type *)LPC_USART3, &UARTFIFOConfigStruct); // Enable UART Transmit UART_TxCmd((LPC_USARTn_Type *)LPC_USART3, ENABLE); /* Enable UART Rx interrupt */ UART_IntConfig((LPC_USARTn_Type *)LPC_USART3, UART_INTCFG_RBR, ENABLE); /* Enable UART line status interrupt */ UART_IntConfig((LPC_USARTn_Type *)LPC_USART3, UART_INTCFG_RLS, ENABLE); /* preemption = 1, sub-priority = 1 */ NVIC_SetPriority(USART3_IRQn, ((0x01<<3)|0x01)); /* Enable Interrupt for UART1 channel */ NVIC_EnableIRQ(USART3_IRQn); } void UART3_IRQHandler(void) { unsigned short Value; if (LPC_USART3->LSR & 0x01) { Value = LPC_USART3->RBR; USART3_Putc(Value); } } unsigned char USART3_GetChar(void) { while(!(LPC_USART3->LSR&0x01)); return (LPC_USART3->RBR); } void USART3_SendByte(unsigned short Data) { while (!(LPC_USART3->LSR & 0x20)); LPC_USART3->THR = (Data & (uint16_t)0x01FF); } void USART3_Putc(unsigned char data) { USART3_SendByte(data); } void USART3_Puts(unsigned char * str) { while(*str) { while (!(LPC_USART3->LSR & 0x20)); LPC_USART3->THR = (*str & (uint16_t)0x01FF); *str++; } } //----------------------------------------------------------------------------//

Top_arrow
回到顶部
EEWORLD下载中心所有资源均来自网友分享,如有侵权,请发送举报邮件到客服邮箱bbs_service@eeworld.com.cn 或通过站内短信息或QQ:273568022联系管理员 高员外,我们会尽快处理。