You are on page 1of 4

Laboratory workbook for Advanced Microprocessors (As per Pune University Syllabus)

LAB 2: Using UART of LPC2148 for serial reception and transmission from/to
computer

Objective: Write a program to display string on hyper terminal and receive back
character.

Part List:
 Educational practice board for ARM7 LPC2148
 All in One General Purpose Board (ASK25)
 +9V Power supply
 USB A to B type cable
 20 pin flat cable
 PC
 Eclipse IDE
 Flash Magic Utility

Hardware Connection:
 Connect USB cable between PL3 connector of EPBARM7 and PC.

Included Files:

HEADER FILES SOURCE FILES


LPC214x.h -

____________________________________________________________________________
Edutech Learning Solutions Pvt. Ltd. info@edutechlearning.com
Laboratory workbook for Advanced Microprocessors (As per Pune University Syllabus)

Program Listing:

/** This Program is used to display a character and string.


* This program also echo the character typed from the
HyperTerminal/Teraterm
*/

#include "lpc214x.h"

#define FOSC 12000000

/* Function Declaration */
void Init_Uart0(unsigned long BaudRate);
void Uart0_Tx(char Data);
void Uart0_String(char *Data);
char Uart0_Rx(void);

/* Main Program */
int main(void)
{
char rdata;
Init_Uart0(9600); // UART0
initialized with Baudrate=9600

Uart0_Tx('A'); //
Transmit single character using UART0
Uart0_String(" UART0 Demo Program \n\r");// Transmit String
using UART0
Uart0_String(" \n\r"); // Transmit
String using UART0
Uart0_String(" Type any Character on HyperTerminal or
Teraterm \n\r");// Transmit String using UART0

while(1)
{
rdata = Uart0_Rx(); //
Receive character from UART0 input from HyperTerminal or Teraterm
Uart0_Tx(rdata); // Transmit
the same character received from HyperTerminal or Teraterm
}
}

/******************************************************************
*********
UART0 Functions
*******************************************************************
***********/
/** This Program initialize the UART0 **/
void Init_Uart0(unsigned long BaudRate)
{
unsigned int CountVal;
VPBDIV = 0X01;
// to choose pclk=cclk

PINSEL0 &= ~0x0F; // Clearing GPIO Values for Pin0.0


and P0.1 to config it for UART0
____________________________________________________________________________
Edutech Learning Solutions Pvt. Ltd. info@edutechlearning.com
Laboratory workbook for Advanced Microprocessors (As per Pune University Syllabus)

PINSEL0 |= 0x05; // Configure P0.0 and P0.1 for UART0


TxD and RxD
IO0DIR &= ~0x02; // Direction: P0.1 RX as input
IO0DIR |= 0x01; // Direction: P0.0 TX as Output

U0FCR = 0X07; // Reset FIFO and Enable it

U0LCR |= 0x03; // databits: 8, parity: No, stopbit:


1

CountVal = FOSC /(16 * BaudRate); // Pg.150 LPC2148 User


Manual (Rev. 4.0 2012 Edition)
U0LCR |= (0x01<<7); // Divisor latch
enable for setting of Fractoinal divisor value

U0DLL = (CountVal & 0XFF); // Store DLL


U0DLM = ((CountVal >> 8) & 0XFF); // Store DLM

if(BaudRate >=115200 && BaudRate < 460800 )


{
U0FDR |= (1<<0); // DIVADDVAL = 1
U0FDR |= (12<<4); // MULVAL = 12
}

U0LCR &= ~(0x01<<7);


U0TER |= (1<<7); // UART0 Enable
}

/** This Function is used to transmit data using UART0 **/


void Uart0_Tx(char Data)
{
while(!(U0LSR & 0x20)); // Wait until UART0 FIFO gets empty
U0THR = Data; // Transmit data using Transmit
holding register
}

/** This Function is used to transmit string using UART0 **/


void Uart0_String(char *Data)
{
while(*Data) // While until string gets
complete
Uart0_Tx(*Data++); // UART0 Transmit character function
}

/** This Function is used to Receive data using UART0 **/


char Uart0_Rx(void)
{
char Data;

while(!(U0LSR & 0x01)); // Wait for Single character in


Receive FIFO
Data = U0RBR; // Receive character from
Receive holding register to local variable
return(Data); // Return Received character
}

____________________________________________________________________________
Edutech Learning Solutions Pvt. Ltd. info@edutechlearning.com
Laboratory workbook for Advanced Microprocessors (As per Pune University Syllabus)

Steps to create project and program compilation:


Steps:
 Open Eclipse.exe from “C:\eclipse” folder.
 Now browse to the ARM7 Workspace under E_Workspace (in this case
C:\E_Workspace\ARM7). Click OK to continue.
 Click File > Import…, to import template.
 Click General > Existing Projects into Workspace and click Next.
 First Select Root Directory of your Eclipse Template for ARM7. Then select “Copy
projects into workspace” check box and Click Finish.
 Every time you import a project make sure to rename it. So Right Click Project >
Rename or press F2 while selecting project to rename it.
 Go to File> New > Source File and you will see New Source File Wizard. Enter Source
File name (For example main.c) then Click Finish.
 Write your code and then save your Files.
 Copy necessary .c and .h files to your local project folder. They will be added to your
project in Eclipse IDE

Steps to use hardware:

 Connect 9V DC Power supply to the educational practice board for ARM7


 Connect the board with the USB port of the PC using the USB A to B type cable.
 Using the RUN/PROGRAM mode selection switch, set the board in the program
mode. This will be indicated by the red LED.
 Apply Reset condition by pressing the RESET switch to ensure proper
communication.
 Using download tool (Flash Magic) downloads the .HEX file to the target board.
 Connect the PL3 connector of EPB ARM7 board to PC.
 Open hyper terminal and set the baudrate.
 Using the RUN/PROGRAM mode selection switch, set the board in the run mode.
This will be indicated by the green LED and apply reset to execute the program.

Result:
String would be displayed on hyper terminal and character would be received from key board.

____________________________________________________________________________
Edutech Learning Solutions Pvt. Ltd. info@edutechlearning.com

You might also like