Sie sind auf Seite 1von 5

//*****************************************************************************

// +--+
// | ++----+
// +-++
|
//
|
|
// +-+--+ |
// | +--+--+
// +----+
Copyright (c) 2009 Code Red Technologies Ltd.
//
// UART example project for RDB1768 development board
//
// Software License Agreement
//
// The software is owned by Code Red Technologies and/or its suppliers, and is
// protected under applicable copyright laws. All rights are reserved. Any
// use in violation of the foregoing restrictions may subject the user to crimin
al
// sanctions under applicable laws, as well as to civil liability for the breach
// of the terms and conditions of this license.
//
// THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED
// OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
// USE OF THIS SOFTWARE FOR COMMERCIAL DEVELOPMENT AND/OR EDUCATION IS SUBJECT
// TO A CURRENT END USER LICENSE AGREEMENT (COMMERCIAL OR EDUCATIONAL) WITH
// CODE RED TECHNOLOGIES LTD.
//
//*****************************************************************************
#include "LPC17xx.h"
// PCUART0
#define PCUART0_POWERON
#define PCUART1_POWERON
#define PCUART2_POWERON
#define PCUART3_POWERON

(1
(1
(1
(1

<<
<<
<<
<<

3)
4)
24)
25)

#define
#define
#define
#define
#define
#define
#define
#define

(3
(3
(3
(3

<<
<<
<<
<<

6)
8)
16)
18)

PCLK_UART0 6
PCLK_UART1 8
PCLK_UART2 16
PCLK_UART3 18
PCLK_UART0_MASK
PCLK_UART1_MASK
PCLK_UART2_MASK
PCLK_UART3_MASK

#define IER_RBR
#define IER_THRE
#define IER_RLS

0x01
0x02
0x04

#define
#define
#define
#define
#define

0x01
0x03
0x02
0x06
0x01

IIR_PEND
IIR_RLS
IIR_RDA
IIR_CTI
IIR_THRE

#define LSR_RDR
#define LSR_OE

0x01
0x02

#define
#define
#define
#define
#define
#define

LSR_PE
LSR_FE
LSR_BI
LSR_THRE
LSR_TEMT
LSR_RXFE

0x04
0x08
0x10
0x20
0x40
0x80

// ***********************
// Function to set up UART
void UART0_Init(int baudrate)
{
int pclk;
unsigned long int Fdiv;
// PCLK_UART0 is being set to 1/4 of SystemCoreClock
pclk = SystemCoreClock / 4;
// Turn on power to UART0
LPC_SC->PCONP |= PCUART0_POWERON;
// Turn on UART0 peripheral clock
LPC_SC->PCLKSEL0 &= ~(PCLK_UART0_MASK);
LPC_SC->PCLKSEL0 |= (0 << PCLK_UART0);

// PCLK_periph = CCLK/4

// Set PINSEL0 so that P0.2 = TXD0, P0.3 = RXD0


LPC_PINCON->PINSEL0 &= ~0xf0;
LPC_PINCON->PINSEL0 |= ((1 << 4) | (1 << 6));
LPC_UART0->LCR = 0x83;
Fdiv = ( pclk / 16 ) / baudrate ;
LPC_UART0->DLM = Fdiv / 256;
LPC_UART0->DLL = Fdiv % 256;
LPC_UART0->LCR = 0x03;

// 8 bits, no Parity, 1 Stop bit, DLAB=1


// Set baud rate

LPC_UART0->FCR = 0x07;

// Enable and reset TX and RX FIFO

// 8 bits, no Parity, 1 Stop bit DLAB =

0
}
void UART1_Init(int baudrate)
{
int pclk;
unsigned long int Fdiv;
// PCLK_UART0 is being set to 1/4 of SystemCoreClock
pclk = SystemCoreClock / 4;
// Turn on power to UART0
LPC_SC->PCONP |= PCUART1_POWERON;
// Turn on UART0 peripheral clock
LPC_SC->PCLKSEL0 &= ~(PCLK_UART1_MASK);
LPC_SC->PCLKSEL0 |= (0 << PCLK_UART1);
// Set PINSEL0 so that
LPC_PINCON->PINSEL0 &=
LPC_PINCON->PINSEL0 |=
LPC_PINCON->PINSEL1 &=
LPC_PINCON->PINSEL1 |=

// PCLK_periph = CCLK/4

P0.2 = TXD0, P0.3 = RXD0


~0xc0000000;
(1 << 30);
~0xc;
(1 << 0);

LPC_UART1->LCR = 0x83;
Fdiv = ( pclk / 16 ) / baudrate ;

// 8 bits, no Parity, 1 Stop bit, DLAB=1


// Set baud rate

LPC_UART1->DLM = Fdiv / 256;


LPC_UART1->DLL = Fdiv % 256;
LPC_UART1->LCR = 0x03;

// 8 bits, no Parity, 1 Stop bit DLAB =

LPC_UART1->FCR = 0x07;

// Enable and reset TX and RX FIFO

0
}
void UART2_Init(int baudrate)
{
int pclk;
unsigned long int Fdiv;
// PCLK_UART2 is being set to 1/4 of SystemCoreClock
pclk = SystemCoreClock / 4;
// Turn on power to UART2
LPC_SC->PCONP |= PCUART0_POWERON;
// Turn on UART2 peripheral clock
LPC_SC->PCLKSEL1 &= ~(PCLK_UART2_MASK);
LPC_SC->PCLKSEL1 |= (0 << PCLK_UART2);

// PCLK_periph = CCLK/4

// Set PINSEL0 so that P0.10 = TXD2, P0.11 = RXD2


LPC_PINCON->PINSEL0 &= ~0xf00000;
LPC_PINCON->PINSEL0 |= ((1 << 20) | (1 << 22));
LPC_UART2->LCR = 0x83;
Fdiv = ( pclk / 16 ) / baudrate ;
LPC_UART2->DLM = Fdiv / 256;
LPC_UART2->DLL = Fdiv % 256;
LPC_UART2->LCR = 0x03;

// 8 bits, no Parity, 1 Stop bit, DLAB=1


// Set baud rate

LPC_UART2->FCR = 0x07;

// Enable and reset TX and RX FIFO

// 8 bits, no Parity, 1 Stop bit DLAB =

0
}
void UART3_Init(int baudrate)
{
int pclk;
unsigned long int Fdiv;
// PCLK_UART2 is being set to 1/4 of SystemCoreClock
pclk = SystemCoreClock / 4;
// Turn on power to UART2
LPC_SC->PCONP |= PCUART3_POWERON;
// Turn on UART2 peripheral clock
LPC_SC->PCLKSEL1 &= ~(PCLK_UART3_MASK);
LPC_SC->PCLKSEL1 |= (0 << PCLK_UART3);

// PCLK_periph = CCLK/4

// Set PINSEL0 so that P0.0 = TXD3, P0.1 = RXD3


LPC_PINCON->PINSEL0 &= ~0xf;
LPC_PINCON->PINSEL0 |= ((1 << 1) | (1 << 3));
LPC_UART3->LCR = 0x83;
Fdiv = ( pclk / 16 ) / baudrate ;
LPC_UART3->DLM = Fdiv / 256;
LPC_UART3->DLL = Fdiv % 256;
LPC_UART3->LCR = 0x03;
0

// 8 bits, no Parity, 1 Stop bit, DLAB=1


// Set baud rate
// 8 bits, no Parity, 1 Stop bit DLAB =

LPC_UART3->FCR = 0x07;
// Enable and reset TX and RX FIFO
}
// ***********************
// Function to send character over UART
void UART0_Sendchar(char c)
{
while( (LPC_UART0->LSR & LSR_THRE) == 0 );
// Block until tx empty
LPC_UART0->THR = c;
}
void UART1_Sendchar(char c)
{
while( (LPC_UART1->LSR & LSR_THRE) == 0 );

// Block until tx empty

LPC_UART1->THR = c;
}
void UART2_Sendchar(char c)
{
while( (LPC_UART2->LSR & LSR_THRE) == 0 );

// Block until tx empty

LPC_UART2->THR = c;
}
void UART3_Sendchar(char c)
{
while( (LPC_UART3->LSR & LSR_THRE) == 0 );

// Block until tx empty

LPC_UART3->THR = c;
}
// ***********************
// Function to get character from UART
char UART0_Getchar()
{
char c;
while( (LPC_UART0->LSR & LSR_RDR) == 0 ); // Nothing received so just b
lock
c = LPC_UART0->RBR; // Read Receiver buffer register
return c;
}
char UART1_Getchar()
{
char c;
while( (LPC_UART1->LSR & LSR_RDR) == 0 ); // Nothing received so just b
lock
c = LPC_UART1->RBR; // Read Receiver buffer register
return c;
}
char UART2_Getchar()
{
char c;
while( (LPC_UART2->LSR & LSR_RDR) == 0 ); // Nothing received so just b
lock
c = LPC_UART2->RBR; // Read Receiver buffer register
return c;
}

char UART3_Getchar()
{
char c;
while( (LPC_UART3->LSR & LSR_RDR) == 0 ); // Nothing received so just b
lock
c = LPC_UART3->RBR; // Read Receiver buffer register
return c;
}
// ***********************
// Function to prints the string out over the UART
void UART0_PrintString(char *pcString)
{
int i = 0;
// loop through until reach string's zero terminator
while (pcString[i] != 0)
{
UART0_Sendchar(pcString[i]); // print each character
i++;
}
}
void UART1_PrintString(char *pcString)
{
int i = 0;
// loop through until reach string's zero terminator
while (pcString[i] != 0)
{
UART1_Sendchar(pcString[i]); // print each character
i++;
}
}
void UART2_PrintString(char *pcString)
{
int i = 0;
// loop through until reach string's zero terminator
while (pcString[i] != 0)
{
UART2_Sendchar(pcString[i]); // print each character
i++;
}
}
void UART3_PrintString(char *pcString)
{
int i = 0;
// loop through until reach string's zero terminator
while (pcString[i] != 0)
{
UART3_Sendchar(pcString[i]); // print each character
i++;
}
}

Das könnte Ihnen auch gefallen