CortexM0开发 —— LPC11C14的UART使用方法

来源:互联网 发布:最新网络流行的英文词 编辑:程序博客网 时间:2024/05/21 22:29

LPC1100系列微控制器UART  LPC1100系列Cortex-M0微控制器具有一个符合16C550工业标准的异步串行口(UART)。此口同时增加了调制解调器(Modem)接口,DSR、DCD和RI Modem信号是只用于LQFP48和PLCC44封装的管脚配置。 

 特性  

 16字节收发FIFO; 

 寄存器位置符合16C550工业标准;  

 接收器FIFO触发点可为1、4、8和14字节; 

 内置波特率发生器;  

  用于精确控制波特率的小数分频器,并拥有赖以实现软件流控制的自动波特率检测能力和机制;  

 支持软件或硬件流控制执行;  

 包含标准Modem接口信号(CTS、DCD、DTS、DTR、RI、RTS); 

 支持RS-458/EIA-485的9位模式和输出使能。


【实验步骤】:

先看一下板子上UART的原理图

PL-2303HX是一款UART-USB芯片,这里先不管其原理,我们只学习如何将数据从CPU发送到这个TXD RXD处。


一、LPC11C14 UART 寄存器描述



这里只贴出部分



具体寄存器分析,这里不再阐述,先看一下在头文件中我们这样定义

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /*------------- Universal Asynchronous Receiver Transmitter (UART) -----------*/  
  2. /** @addtogroup LPC11xx_UART LPC11xx Universal Asynchronous Receiver/Transmitter  
  3.   @{ 
  4. */  
  5. typedef struct  
  6. {  
  7.   union {  
  8.   __I  uint32_t  RBR;                   /*!< Offset: 0x000 Receiver Buffer  Register (R/ ) */  
  9.   __O  uint32_t  THR;                   /*!< Offset: 0x000 Transmit Holding Register ( /W) */  
  10.   __IO uint32_t  DLL;                   /*!< Offset: 0x000 Divisor Latch LSB (R/W) */  
  11.   };  
  12.   union {  
  13.   __IO uint32_t  DLM;                   /*!< Offset: 0x004 Divisor Latch MSB (R/W) */  
  14.   __IO uint32_t  IER;                   /*!< Offset: 0x000 Interrupt Enable Register (R/W) */  
  15.   };  
  16.   union {  
  17.   __I  uint32_t  IIR;                   /*!< Offset: 0x008 Interrupt ID Register (R/ ) */  
  18.   __O  uint32_t  FCR;                   /*!< Offset: 0x008 FIFO Control Register ( /W) */  
  19.   };  
  20.   __IO uint32_t  LCR;                   /*!< Offset: 0x00C Line Control Register (R/W) */  
  21.   __IO uint32_t  MCR;                   /*!< Offset: 0x010 Modem control Register (R/W) */  
  22.   __I  uint32_t  LSR;                   /*!< Offset: 0x014 Line Status Register (R/ ) */  
  23.   __I  uint32_t  MSR;                   /*!< Offset: 0x018 Modem status Register (R/ ) */  
  24.   __IO uint32_t  SCR;                   /*!< Offset: 0x01C Scratch Pad Register (R/W) */  
  25.   __IO uint32_t  ACR;                   /*!< Offset: 0x020 Auto-baud Control Register (R/W) */  
  26.        uint32_t  RESERVED0;  
  27.   __IO uint32_t  FDR;                   /*!< Offset: 0x028 Fractional Divider Register (R/W) */  
  28.        uint32_t  RESERVED1;  
  29.   __IO uint32_t  TER;                   /*!< Offset: 0x030 Transmit Enable Register (R/W) */  
  30.        uint32_t  RESERVED2[6];  
  31.   __IO uint32_t  RS485CTRL;             /*!< Offset: 0x04C RS-485/EIA-485 Control Register (R/W) */  
  32.   __IO uint32_t  ADRMATCH;              /*!< Offset: 0x050 RS-485/EIA-485 address match Register (R/W) */  
  33.   __IO uint32_t  RS485DLY;              /*!< Offset: 0x054 RS-485/EIA-485 direction control delay Register (R/W) */  
  34.   __I  uint32_t  FIFOLVL;               /*!< Offset: 0x058 FIFO Level Register (R) */  
  35. } LPC_UART_TypeDef;  
  36. /*@}*/ /* end of group LPC11xx_UART */  

相关宏定义(部分)

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. ****************************************************************************/  
  2. #ifndef __UART_H   
  3. #define __UART_H  
  4.   
  5. #define RS485_ENABLED   0  
  6. #define TX_INTERRUPT    0       /* 0 if TX uses polling, 1 interrupt driven. */  
  7. #define MODEM_TEST      0  
  8.   
  9. #define IER_RBR         (0x01<<0)  
  10. #define IER_THRE        (0x01<<1)  
  11. #define IER_RLS         (0x01<<2)  
  12.   
  13. #define IIR_PEND        0x01  
  14. #define IIR_RLS         0x03  
  15. #define IIR_RDA         0x02  
  16. #define IIR_CTI         0x06  
  17. #define IIR_THRE        0x01  
  18.   
  19. #define LSR_RDR         (0x01<<0)  
  20. #define LSR_OE          (0x01<<1)  
  21. #define LSR_PE          (0x01<<2)  
  22. #define LSR_FE          (0x01<<3)  
  23. #define LSR_BI          (0x01<<4)  
  24. #define LSR_THRE        (0x01<<5)  
  25. #define LSR_TEMT        (0x01<<6)  
  26. #define LSR_RXFE        (0x01<<7)  
  27.   
  28. #define UART0_RBUF_SIZE 64  

二、UART的初始化

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /***************************************************************************** 
  2. ** Function name:       UARTInit 
  3. ** 
  4. ** Descriptions:        Initialize UART0 port, setup pin select, 
  5. **                      clock, parity, stop bits, FIFO, etc. 
  6. ** 
  7. ** parameters:          UART baudrate 
  8. ** Returned value:      None 
  9. **  
  10. *****************************************************************************/  
  11. void UARTInit(uint32_t baudrate)  
  12. {  
  13.   uint32_t Fdiv;  
  14.   uint32_t regVal;  
  15.   
  16.   UARTTxEmpty = 1;  
  17.   UARTCount = 0;  
  18.     
  19.   NVIC_DisableIRQ(UART_IRQn);  
  20.   
  21.   LPC_IOCON->PIO1_6 &= ~0x07;    /*  UART I/O config */  
  22.   LPC_IOCON->PIO1_6 |= 0x01;     /* UART RXD */  
  23.   LPC_IOCON->PIO1_7 &= ~0x07;      
  24.   LPC_IOCON->PIO1_7 |= 0x01;     /* UART TXD */  
  25.   
  26.   /* Enable UART clock */  
  27.   LPC_SYSCON->SYSAHBCLKCTRL |= (1<<12);  
  28.   LPC_SYSCON->UARTCLKDIV = 0x1;     /* divided by 1 */  
  29.   
  30.   LPC_UART->LCR = 0x83;             /* 8 bits, no Parity, 1 Stop bit */  
  31.   regVal = LPC_SYSCON->UARTCLKDIV;  
  32.   Fdiv = ((SystemAHBFrequency/regVal)/16)/baudrate ;    /*baud rate */  
  33.   
  34.   LPC_UART->DLM = Fdiv / 256;                              
  35.   LPC_UART->DLL = Fdiv % 256;  
  36.   LPC_UART->LCR = 0x03;      /* DLAB = 0 */  
  37.   LPC_UART->FCR = 0x07;      /* Enable and reset TX and RX FIFO. */  
  38.   
  39.   /* Read to clear the line status. */  
  40.   regVal = LPC_UART->LSR;  
  41.   
  42.   /* Ensure a clean start, no data in either TX or RX FIFO. */  
  43.   while (( LPC_UART->LSR & (LSR_THRE|LSR_TEMT)) != (LSR_THRE|LSR_TEMT) );  
  44.   while ( LPC_UART->LSR & LSR_RDR )  
  45.   {  
  46.     regVal = LPC_UART->RBR;  /* Dump data from RX FIFO */  
  47.   }  
  48.    
  49.   /* Enable the UART Interrupt */  
  50.   NVIC_EnableIRQ(UART_IRQn);  
  51.   
  52. #if TX_INTERRUPT  
  53.   LPC_UART->IER = IER_RBR | IER_THRE | IER_RLS;  /* Enable UART interrupt */  
  54. #else  
  55.   LPC_UART->IER = IER_RBR | IER_RLS; /* Enable UART interrupt */  
  56. #endif  
  57.   return;  
  58. }  


CortexM0 中UART与CPIO口复用,这里看到用到了PIO1_6 与PIO1_7


1、对IO口进行设置

以PIO1_7寄存器为例

可以看到低3位用于配置管脚功能 001为TXD,PIO1_6配置也相同

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. LPC_IOCON->PIO1_6 &= ~0x07;    /*  UART I/O config */  
  2. LPC_IOCON->PIO1_6 |= 0x01;     /* UART RXD */  
  3. LPC_IOCON->PIO1_7 &= ~0x07;    
  4. LPC_IOCON->PIO1_7 |= 0x01;     /* UART TXD */  

2、时钟设置

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /* Enable UART clock */  
  2.   LPC_SYSCON->SYSAHBCLKCTRL |= (1<<12);  
  3.   LPC_SYSCON->UARTCLKDIV = 0x1;     /* divided by 1 */  

3、设置波特率、数据位

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. LPC_UART->LCR = 0x83;             /* 8 bits, no Parity, 1 Stop bit */  
  2. regVal = LPC_SYSCON->UARTCLKDIV;  
  3. Fdiv = ((SystemAHBFrequency/regVal)/16)/baudrate ;  /*baud rate */  

4、UART相应配置

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. LPC_UART->DLM = Fdiv / 256;                            
  2. LPC_UART->DLL = Fdiv % 256;  
  3. LPC_UART->LCR = 0x03;        /* DLAB = 0 */  
  4. LPC_UART->FCR = 0x07;        /* Enable and reset TX and RX FIFO. */  
第4行 FCR为 FIFO控制寄存器。控制UART  FIFO的使用和模式 


5、使能中断等操作

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1.   /* Read to clear the line status. */  
  2.   regVal = LPC_UART->LSR;  
  3.   
  4.   /* Ensure a clean start, no data in either TX or RX FIFO. */  
  5.   while (( LPC_UART->LSR & (LSR_THRE|LSR_TEMT)) != (LSR_THRE|LSR_TEMT) );  
  6.   while ( LPC_UART->LSR & LSR_RDR )  
  7.   {  
  8.     regVal = LPC_UART->RBR;  /* Dump data from RX FIFO */  
  9.   }  
  10.    
  11.   /* Enable the UART Interrupt */  
  12.   NVIC_EnableIRQ(UART_IRQn);  
  13.   
  14. #if TX_INTERRUPT  
  15.   LPC_UART->IER = IER_RBR | IER_THRE | IER_RLS;  /* Enable UART interrupt */  
  16. #else  
  17.   LPC_UART->IER = IER_RBR | IER_RLS; /* Enable UART interrupt */  
  18. #endif  
LCR寄存器作用



三、发送数据

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /***************************************************************************** 
  2. ** Function name:       UARTSend 
  3. ** 
  4. ** Descriptions:        Send a block of data to the UART 0 port based 
  5. **                      on the data length 
  6. ** 
  7. ** parameters:          buffer pointer, and data length 
  8. ** Returned value:      None 
  9. **  
  10. *****************************************************************************/  
  11. void UARTSend(uint8_t *BufferPtr, uint32_t Length)  
  12. {  
  13.     
  14.   while ( Length != 0 )  
  15.   {  
  16.       /* THRE status, contain valid data */  
  17. #if !TX_INTERRUPT  
  18.       while ( !(LPC_UART->LSR & LSR_THRE) );  
  19.       LPC_UART->THR = *BufferPtr;  
  20. #else  
  21.       /* Below flag is set inside the interrupt handler when THRE occurs. */  
  22.       while ( !(UARTTxEmpty & 0x01) );  
  23.       LPC_UART->THR = *BufferPtr;  
  24.       UARTTxEmpty = 0;  /* not empty in the THR until it shifts out */  
  25. #endif  
  26.       BufferPtr++;  
  27.       Length--;  
  28.   }  
  29.   return;  
  30. }  



四、接收数据

       这里利用中断

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /***************************************************************************** 
  2. ** Function name:       UART_IRQHandler 
  3. ** 
  4. ** Descriptions:        UART interrupt handler 
  5. ** 
  6. ** parameters:          None 
  7. ** Returned value:      None 
  8. **  
  9. *****************************************************************************/  
  10. void UART_IRQHandler(void)  
  11. {  
  12.   uint8_t IIRValue, LSRValue;  
  13.   uint8_t Dummy = Dummy;  
  14.   
  15.   IIRValue = LPC_UART->IIR;  
  16.       
  17.   IIRValue >>= 1;         /* skip pending bit in IIR */  
  18.   IIRValue &= 0x07;         /* check bit 1~3, interrupt identification */  
  19.   if (IIRValue == IIR_RLS)      /* Receive Line Status */  
  20.   {  
  21.     LSRValue = LPC_UART->LSR;  
  22.     /* Receive Line Status */  
  23.     if (LSRValue & (LSR_OE | LSR_PE | LSR_FE | LSR_RXFE | LSR_BI))  
  24.     {  
  25.       /* There are errors or break interrupt */  
  26.       /* Read LSR will clear the interrupt */  
  27.       UARTStatus = LSRValue;  
  28.       Dummy = LPC_UART->RBR; /* Dummy read on RX to clear  
  29.                                 interrupt, then bail out */  
  30.       return;  
  31.     }  
  32.     if (LSRValue & LSR_RDR) /* Receive Data Ready */              
  33.     {  
  34.       /* If no error on RLS, normal ready, save into the data buffer. */  
  35.       /* Note: read RBR will clear the interrupt */  
  36.       UARTBuffer[UARTCount++] = LPC_UART->RBR;  
  37.       if (UARTCount >= UART0_RBUF_SIZE)  
  38.       {  
  39.         UARTCount = 0;      /* buffer overflow */  
  40.       }   
  41.     }  
  42.   }  
  43.   else if (IIRValue == IIR_RDA) /* Receive Data Available */  
  44.   {  
  45.     /* Receive Data Available */  
  46.     UARTBuffer[UARTCount++] = LPC_UART->RBR;  
  47.     if (UARTCount >= UART0_RBUF_SIZE)  
  48.     {  
  49.       UARTCount = 0;        /* buffer overflow */  
  50.     }  
  51.   }  
  52.   else if (IIRValue == IIR_CTI) /* Character timeout indicator */  
  53.   {  
  54.     /* Character Time-out indicator */  
  55.     UARTStatus |= 0x100;        /* Bit 9 as the CTI error */  
  56.   }  
  57.   else if (IIRValue == IIR_THRE)    /* THRE, transmit holding register empty */  
  58.   {  
  59.     /* THRE interrupt */  
  60.     LSRValue = LPC_UART->LSR;        /* Check status in the LSR to see if 
  61.                                 valid data in U0THR or not */  
  62.     if (LSRValue & LSR_THRE)  
  63.     {  
  64.       UARTTxEmpty = 1;  
  65.     }  
  66.     else  
  67.     {  
  68.       UARTTxEmpty = 0;  
  69.     }  
  70.   }  
  71.   return;  
  72. }  

下面学习一下UART中断


对于UART接口来说,有两种情况可以触发UART接收中断:接收字节数达到接收FIFO的触发点(RDA)、接收超时(CTI)。

(1) 接收字节数达到接收FIFO中的触发点(RDA)

     LPC1100系列Cortex-M0微控制器UART接口具有16字节的接收FIFO,接收触发点可以设置为1、4、8、14字节,当接收到的字节数达到接收触发点时,便会触发中断。

     通过UART FIFO控制寄存器U0FCR,将接收触发点设置为“8字节触发”。那么当UART接收8个字节时,便会触发RDA中断(注:在接收中断使能的前提下)。


下面看一下IIR



五、其他操作补充

[cpp] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /******************************************************************************* 
  2. * Function Name  : UART0_PutChar 
  3. * Description    : Send a char to uart0 channel. 
  4. * Input          : c 
  5. * Output         : None 
  6. * Return         : None 
  7. *******************************************************************************/  
  8. void UART0_PutChar(char ch)  
  9. {  
  10.   while(!(LPC_UART->LSR & LSR_THRE));  
  11.   LPC_UART->THR = ch;  
  12. }  
  13.   
  14. /******************************************************************************* 
  15. * Function Name  : uart0_sendstring 
  16. * Description    : Send string to uart0 channel. 
  17. * Input          : pString  --  string 
  18. * Output         : None 
  19. * Return         : None 
  20. *******************************************************************************/  
  21. void UART0_PutString(char *pString)  
  22. {  
  23.   while(*pString)  
  24.   {  
  25.     UART0_PutChar(*pString++);  
  26.   }  
  27. }  
  28.   
  29. /******************************************************************************* 
  30. * Function Name  : UART0_printf 
  31. * Description    : print format string. 
  32. * Input          : fmt 
  33. * Output         : None 
  34. * Return         : None 
  35. *******************************************************************************/  
  36. void UART0_printf(char *fmt, ...)  
  37. {  
  38.   char      uart0_pString[101];  
  39.   va_list   uart0_ap;  
  40.   
  41.   va_start(uart0_ap, fmt);  
  42.   vsnprintf(uart0_pString, 100, fmt, uart0_ap);  
  43.   UART0_PutString(uart0_pString);  
  44.   va_end(uart0_ap);  
  45. }  
  46.   
  47. /******************************************************************************* 
  48. * Function Name  : UART0_GetChar 
  49. * Description    : print format string. 
  50. * Input          : fmt 
  51. * Output         : None 
  52. * Return         : None 
  53. *******************************************************************************/  
  54. uint8_t UART0_GetChar(uint8_t *ch)  
  55. {  
  56.   if(UART_op != UARTCount)  
  57.   {  
  58.     *ch = UARTBuffer[UART_op];  
  59.     UART_op ++;  
  60.     if(UART_op >= UART0_RBUF_SIZE)  
  61.       UART_op = 0;  
  62.   
  63.     return 1;  
  64.   }  
  65.   
  66.   return 0;  
  67. }  
0 0
原创粉丝点击