⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uart.c

📁 LPC-H2214-test_mem.zip 这是一款LPC2214开发板的示例代码
💻 C
字号:
//uart.c
#include "uart.h"

unsigned int processorClockFrequency(void)
{
  //return real processor clock speed
  return OSCILLATOR_CLOCK_FREQUENCY * (PLLCON & 1 ? (PLLCFG & 0xF) + 1 : 1);
}

unsigned int peripheralClockFrequency(void)
{
  //VPBDIV - determines the relationship between the processor clock (cclk)
  //and the clock used by peripheral devices (pclk).
  unsigned int divider;
  switch (VPBDIV & 3)
    {
      case 0: divider = 4;  break;
      case 1: divider = 1;  break;
      case 2: divider = 2;  break;
    }
  return processorClockFrequency() / divider;
}


/**** UART0 SETTINGS ****/
void UART0Initialize(unsigned int baud)
{
  unsigned int divisor = peripheralClockFrequency() / (16 * baud);

  //set Line Control Register (8 bit, 1 stop bit, no parity, enable DLAB)
  U0LCR_bit.WLS   = 0x3;    //8 bit
  U0LCR_bit.SBS   = 0x0;    //1 stop bit
  U0LCR_bit.PE    = 0x0;    //no parity
  U0LCR_bit.DLAB  = 0x1;    //enable DLAB
  //with one row -> U0LCR = 0x83;

  //devisor
  U0DLL = divisor & 0xFF;
  U0DLM = (divisor >> 8) & 0xFF;
  U0LCR &= ~0x80;

  //U0FCR_bit.FCRFE = 1;

  //set functionalite to pins
  PINSEL0_bit.P0_0 = 0x1;     //port0.0 -> TX0
  PINSEL0_bit.P0_1 = 0x1;     //port0.1 -> RXD0
  //with one row -> PINSEL0 = PINSEL0 & ~0xF | 0x5;

  //Enable UART1 interrupts
  //U0IER_bit.RDAIE  = 1;     //Enable byte received interrupt
  //U0IER_bit.THREIE = 1;     //Enable tx buf empty interrupt
}

void UART0WriteChar(unsigned char ch0)
{
  //when U0LSR_bit.THRE is 0 - U0THR contains valid data.
  while (U0LSR_bit.THRE == 0);
  U0THR = ch0;
}

unsigned char UART0ReadChar(void)
{
  //when U0LSR_bit.DR is 1 - U0RBR contains valid data
  while (U0LSR_bit.DR == 0);
  return U0RBR;
}

void UART0WriteTxt(unsigned char* txt)
{
  for(int i=0; i<strlen(txt); i++)
  {
    UART0WriteChar(txt[i]);
  }
}

unsigned char UART0ReadChar_nostop(void)
{
  //when U0LSR_bit.DR is 1 - U0RBR contains valid data
  if(U0LSR_bit.DR == 1)
    return U0RBR;
  else
    return 0;
}

void UART0WriteChar_nostop(unsigned char ch0)
{
  //when U0LSR_bit.THRE is 0 - U0THR contains valid data.
  if(U0LSR_bit.THRE == 1) U0THR = ch0;
}


/**** UART1 SETTINGS ****/
void UART1Initialize(unsigned int baud)
{
  unsigned int divisor = peripheralClockFrequency() / (16 * baud);

  //set Line Control Register (8 bit, 1 stop bit, no parity, enable DLAB)
  U1LCR_bit.WLS   = 0x3;    //8 bit
  U1LCR_bit.SBS   = 0x0;    //1 stop bit
  U1LCR_bit.PE    = 0x0;    //no parity
  U1LCR_bit.DLAB  = 0x1;    //enable DLAB
  //with one row -> U0LCR = 0x83;

  //devisor
  U1DLL = divisor & 0xFF;
  U1DLM = (divisor >> 8) & 0xFF;
  U1LCR &= ~0x80;

  //U1FCR_bit.FCRFE = 1;

  //set functionalite to pins
  PINSEL0_bit.P0_8 = 0x1;     //port0.0 -> TXD1
  PINSEL0_bit.P0_9 = 0x1;     //port0.1 -> RXD1
  //with one row -> PINSEL0 = PINSEL0 & ~0xF | 0x5;

  //Enable UART1 interrupts
  //U1IER_bit.RDAIE  = 1;     //Enable byte received interrupt
  //U1IER_bit.THREIE = 1;     //Enable tx buf empty interrupt
}

void UART1WriteChar(unsigned char ch0)
{
  //when U0LSR_bit.THRE is 0 - U0THR contains valid data.
  while (U1LSR_bit.THRE == 0);
  U1THR = ch0;
}

unsigned char UART1ReadChar(void)
{
  //when U0LSR_bit.DR is 1 - U0RBR contains valid data
  while (U1LSR_bit.DR == 0);
  return U1RBR;
}

void UART1WriteTxt(unsigned char* txt)
{
  for(int i=0; i<strlen(txt); i++)
  {
    UART1WriteChar(txt[i]);
  }
}


unsigned char UART1ReadChar_nostop(void)
{
  //when U0LSR_bit.DR is 1 - U0RBR contains valid data
  if(U1LSR_bit.DR == 1)
    return U1RBR;
  else
    return 0;
}

void UART1WriteChar_nostop(unsigned char ch0)
{
  //when U0LSR_bit.THRE is 0 - U0THR contains valid data.
  if(U1LSR_bit.THRE == 1) U1THR = ch0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -