Experiment No 1: 1. Study of Arm Evaluation System

Download as pdf or txt
Download as pdf or txt
You are on page 1of 30

Syllabus Programs.

Experiment No 1

1. STUDY OF ARM EVALUATION SYSTEM.


Aim of the Experiment:
To study the ARM7TDMI-S CPU based 32 bit microcontroller (LPC2148) features, Block diagram, Pin
diagram and Program used Register description.
Refer”LPC214x User Manual”

Experiment No 2

2. INTERFACING ADC AND DAC.


Aim of the Experiment:
To write the program for onchip ADC.

Connection Details:
 Variable Pot output(Analog) 0 to 3.3 Volt connect to P0.28 burg pin by burg wire.

2.1.ADC Program:

#include <LPC214x.H>
#include <stdio.h>

unsigned int ADC_Val;


char adcreading[16] ;
char Newline[]=" \r";
void Delay(unsigned int Time)
{
unsigned int i,j;

for(i=0;i<=Time;i++)
for(j=0;j<110;j++);
}
/*********************************************************************************************
* UART0 Initialization
*
*********************************************************************************************/
void UART0_Init()
{
PINSEL0 = 0x05; /* Enable RxD0 and TxD0 */
U0LCR = 0x83; /* DLAB=1, 8 bits, no Parity, 1 Stop bit */
U0DLL = 0x86; /* 9600 Baud Rate @60MHz VPB Clock */
U0DLM = 0x01;
U0LCR = 0x03; /* DLAB = 0 */
VPBDIV = 0x01; /*VLSI Pheripheral Bus clock as Processor clock*/
}

IES-ARM7, Rev: 1. Page 23 of 52


Syllabus Programs.

/*********************************************************************************************
* ADC initialization
*
*********************************************************************************************/
void ADC_Init()
{
PINSEL1 = 0x01000000; /* P0.28 configure to channel AD0.1 */
AD0CR = 0x00230602; /* Setup A/D: 10-bit AIN0 @ 3MHz */
}

void U0Write(char data)


{
while (!(U0LSR & (1<<5))); // wait till the THR is empty
// now we can write to the Tx FIFO
U0THR = data;
}

void Send_String(char* StringPtr)


{
while(*StringPtr != 0x00){
U0Write(*StringPtr);
StringPtr++;}
}
/*********************************************************************************************
* Main
* Description : This function Initialize the UART0and ADC
*********************************************************************************************/
int main (void) /* program exec. starts here */
{
UART0_Init(); /* Init UART 0 */
Delay(3000);
ADC_Init(); /* ADC Initialize */
while(1)
{
AD0CR |= 0x01000000; /* start of ADC conversion*/
do
{
ADC_Val = AD0GDR; /* 10 bit value */
}
while ((ADC_Val & 0x80000000) == 0); /* Wait ADC Conversion Complete */
AD0CR &= ~0x01000000; /* Again start ADC */
ADC_Val = (ADC_Val >> 6) & 0x03FF;
sprintf(adcreading,"ADC Value= %u",ADC_Val);
Send_String(adcreading);
Send_String(Newline);
Delay(4000);
}
}

IES-ARM7, Rev: 1. Page 24 of 52


Syllabus Programs.

Used Formula:
Digital O/P = Analag I/P X 2n /Vref

Where,
N = No of bits per ADC(1024).
Vref =Refernce Voltage(3.3V)

Result:
Thus the analog input converted to digital form and display the digtal output on Hyper
Teriminal.Output was verified by formula.

Aim of the Experiment:


To write the program for square wave,saw tooth wave,Tri angular and sine wave form genaration
using onchip DAC.

Connection Details:
 P0.25 connect to +ve(Red) Probe teriminal of CRO/DSO.
 Gnd connect to -ve(Block) Probe teriminal of CRO/DSO.

2.2.1.DAC Program for Square Wave:

#include<LPC214x.H>

void msdelay (unsigned long a) // DELAY ROUTINE


{
while(--a!=0); //waiting statement
}

int main(void)
{
unsigned int DAC_Val;
VPBDIV = 0x01;
PINSEL1 = 0x00080000; //ENABLE DAC PIN (P0.25)
while (1)
{
DAC_Val = 0x03FF; // For +ve Peak
DAC_Val=DAC_Val<<6;
DACR=DAC_Val;
msdelay(100);

DAC_Val = 0x00; // For -ve Peak


DAC_Val=DAC_Val<<6;
DACR=DAC_Val;
msdelay(100);
}
}

IES-ARM7, Rev: 1. Page 25 of 52


Syllabus Programs.

2.2.2.DAC Program for Saw Tooth Wave:

#include<LPC214x.H>
int main(void)
{ unsigned int DAC_Val,i;
VPBDIV = 0x01;
PINSEL1 = 0x00080000; //ENABLE DAC PIN (P0.25)
DAC_Val = 0x0;
while (1)
{
for(i=0;i<=0x3ff;i++)
{
DAC_Val=i<<6;
DACR=DAC_Val;
}
}
}

2.2.3.DAC Program for Tri Angular Wave:

#include<LPC214x.H>

int main(void)
{
unsigned int DAC_Val,i,j;
VPBDIV = 0x01;
PINSEL1 = 0x00080000; //ENABLE DAC PIN (P0.25)
DAC_Val = 0x0;
while (1)
{
for(i=0;i<=0x3ff;i++) // for rising slope
{
DAC_Val=i<<6;
DACR=DAC_Val;
}
for(i=0x3ff;i>0;i--) // for falling slope
{
DAC_Val=i<<6;
DACR=DAC_Val;
}
}
}

IES-ARM7, Rev: 1. Page 26 of 52


Syllabus Programs.

2.2.4.DAC Program for Sine Wave:

#include<LPC214x.H>
Unsigned int sample[]={512,528,544,560,576,592,608,624,639,655,670,685,700,715,730,
744,759,773,786,800,813,826,838,850,862,874,885,896,906,916,926,935,944,953,961,968,975,
982,988,994,999,1004,1008,1012,1015,1018,1020,1022,1022,1023,1023,1023,1022,1022,1020,
1018,1015,1012,1008,1004,999,994,988,982,975,968,961,953,945,936,927,917,907,896,886,87
4,863,851,839,826,813,800,787,773,759,745,731,716,701,686,671,656,640,624,609,593,577,56
1,545,529,513,497,481,465,449,433,417,401,386,370,355,339,324,310,295,280,266,252,238,22
5,212,199,186,174,162,151,139,129,118,108,98,89,80,72,64,56,49,43,36,31,25,21,16,13,9,6,4,2,1
,0,0,0,1,2,4,6,9,12,16,20,25,30,35,42,48,55,63,71,79,88,97,107,117,127,138,149,160,172,185,197
,210,223,236,250,264,278,293,307,322,337,352,368,383,399,415,430,446,462,478,494};
int main(void)
{
unsigned int DAC_Val,i;
VPBDIV = 0x01;
PINSEL1 = 0x00080000; //ENABLE DAC PIN (P0.25)
DAC_Val = 0x0;
while (1)
{
for(i=0;i<200;i++)
{
DAC_Val=sample[i]<<6;
DACR=DAC_Val;
}

}
}

Result:
Thus the all waveforms obtained by on chip DAC and verified by DSO.

Experiment No 3

3. INTERFACING LED AND PWM.


Aim of the Experiment:
To write the program for LED(Toggling & Shift) and PWM.

Connection Details for LED:


 P0.16-P0.23 connect to LED I/P 1-8 burg pins by burg wires.

3.1.1/Program for Toggling LED:

#include<LPC214x.h> // Define LPC2148 Header File


#define led IO0PIN // Define LED to Port0
#define tled IO0DIR
void delay(int x);

IES-ARM7, Rev: 1. Page 27 of 52


Syllabus Programs.

int main()
{
tled = 0x00FF0000; // Define P0.16 – P0.23 as O/P
while(1) // Loop forever
{
led = 0x00AA0000; // Turn ON P0.16 – P0.23
delay(2000);
led = 0x00550000; // Turn OFF P0.16 – P0.23
delay(2000);
}
}
void delay(int x)
{
unsigned int k,l;
for(k = x;k > 0;k--)
for(l = 0;l < x;l++);
}

3.1.2.Program for shifting LED:

#include<LPC214x.h> // Define LPC2148 Header File


#define led IO0PIN // Define LED to Port1
#define tled IO0DIR // Define Port1 as output
void delay(int x);
int main()
{
long int i,j=0x1;
tled = 0x00FF0000; // Define P0.16 – P0.23 as O/P
while(1) // Loop forever
{
for(i=16;i<24;i++)
{
led= j <<i;
delay(2000);
}
}
}
void delay(int x)
{
unsigned int k,l;
for(k = x;k > 0;k--)
for(l = 0;l < x;l++);
}

Connection Details for PWM :


 P0.0 connect to +ve(Red) Probe teriminal of CRO/DSO.
 Gnd connect to -ve(Block) Probe teriminal of CRO/DSO.

IES-ARM7, Rev: 1. Page 28 of 52


Syllabus Programs.

3.2.Program for 75% PWM Wave form:

#include <lpc214x.h>

void PWM_Init()
{
PINSEL0 = 0x00000002; /* set GPIOs for PWM1*/
PWMTCR = 0x00000002; /* Counter Reset */
PWMPR = 0x00000000; /* count frequency:Fpclk */
PWMLER = 0x0000007F; /* all PWM latch enabled */
}

void PWM_Set()
{
PWMMR0 = 2000; // Total count.
PWMMR1 = 1500; // On time count.
}

void PWM_Start( void )


{
PWMPCR = 0x00000200; /* Selects single edge controlled mode for PWM1 output
enabled.*/
PWMTCR = 0x09; /* counter enable, PWM enable */
}

int main(void)
{
PWM_Init(); /* PWM Init */
PWM_Set(); /* Set % PWM Cycle */
PWM_Start(); /* Start PWM genration */
while(1);
}

Result:
Thus the Toggling LED, Shifting LED and PWM output was verified.

Experiment No 4

4. INTERFACING REAL TIME CLOCK AND SERIAL PORT.


Aim of the Experiment:
To write the program for onchip Real Time Clock or Calendar.

Connection Details for RTC:


 P1.16-P1.23 connect to the LCD datalines of D0-D7 burg pins by burg wires.
 P0.8 connect to the LCD RS(Register Select) burg pin by burg wire.
 P0.9 connect to the LCD R/W(Read/Write) burg pin by burg wire.
 P0.10 connect to the LCD ENA(Enable) burg pin by burg wire.

IES-ARM7, Rev: 1. Page 29 of 52


Syllabus Programs.

4.1.Program for Onchip RTC:


#include <lpc214x.h>
unsigned char arr1[16]="REAL TIME CLOCK";
unsigned char h1,h2,m1,m2,s1,s2;
#define lcd_data IO1PIN
#define EN 0x00000400
#define RW 0x00000200
#define RS 0x00000100
void DelayMs(unsigned int ms);
void lcd_cmd(unsigned char value);
void lcd_dat(unsigned char item);
void lcd_data_string(unsigned char *str);
void init_rtc(void);

void rtc_int(void)__irq
{
ILR = 0x01;
}
int main(void)
{
VPBDIV = 0x02;
DelayMs(1000);
IO0DIR=0x00000700;
IO1DIR=0x00FF0000;
lcd_cmd(0x38);
lcd_cmd(0x0C);
lcd_cmd(0x01);
lcd_cmd(0x06);
lcd_cmd(0x80);
lcd_data_string(arr1);
init_rtc();

while(1)
{ lcd_cmd(0xC4);
lcd_dat(HOUR/10 + '0');
lcd_dat(HOUR%10 + '0');
lcd_dat(':') ;
lcd_dat(MIN/10 + '0');
lcd_dat(MIN%10 + '0');
lcd_dat(':');
lcd_dat(SEC/10 + '0');
lcd_dat(SEC%10 + '0');
}
}
void DelayMs(unsigned int ms)
{
long i,j;
for (i = 0; i < ms; i++ )
for (j = 0; j < 6659; j++ );
}

IES-ARM7, Rev: 1. Page 30 of 52


Syllabus Programs.

void busy_check()
{
DelayMs(5);
}
void lcd_cmd(unsigned char value)
{
busy_check();
IO0CLR=RS;
IO0CLR=RW;
lcd_data=(value<<16);
IO0SET=EN;
IO0CLR=EN;
return;
}
void lcd_dat(unsigned char item)
{
busy_check();
IO0SET=RS;
IO0CLR=RW;
lcd_data=(item<<16);
IO0SET=EN;
IO0CLR=EN;
return;
}
void lcd_data_string(unsigned char *str) // Program to send string to LCD
{
int i=0;
while(str[i]!='\0')
{
lcd_dat(str[i]);
i++;
DelayMs(5);
}
return;
}

void init_rtc(void)
{
ILR = 0x01;
CCR = 0x13;
CCR = 0x11;
CIIR = 0x01;
SEC = 0x00;
MIN = 0x00;
HOUR = 0x00;
VICIntEnable |= (1<<13); //RTC Interrupt 13(0x0D)
VICVectCntl0 = 0x0000002D; //0x20+13=0x2D
VICVectAddr0 = (unsigned)rtc_int;
}
Result:
Thus the RTC operation was verified by LCD.

IES-ARM7, Rev: 1. Page 31 of 52


Syllabus Programs.

Aim of the Experiment:


To write the program for write the strings to UART-0 serial port.

Connection Details for Serial:


 No need external connection except from serial cable.

4.2.Program for Onchip UART-0:

#include <LPC214x.H> /* LPC214x definitions */

/***********************************************************************************************
* UART0 Initialization
* Description : This function Initializes the UART0.
* Note : This function should be called once before any of the other functions of UART0.
***********************************************************************************************/
void UART0_Init()
{
PINSEL0 = 0x00000005; /* lower 4bit selected for UART0 and remaining all bits selected for
GPIO's.*/
/* frist LSB 2bit(0 and 1 bits) selected 01b for UART0 Tx.*/
/* LSB bit(2 and 3 bits) selected 01b for UART0 Rx.*/
U0LCR = 0x83; /* 0x83: enable Divisor Latch access, set 8-bit word length,no Parity,1 Stop
bit*/
VPBDIV = 0x01; /* VPBDIV: VPB bus clock divider 0x01: PCLK = processor clock*/
/*-----------------------------------------------------------------------------------------------*/
// BAUD RATE CALCULATION
// DIVISOR = (PCLK/(16*DESIRED_BAUDRATE))
// For Example
// Peripheral Clock Frequency (PCLK) = 60 Mhz
// Desired Baud Rate = 9600 bps
// Divisor = (60000000/(16*9600)) = 390(Decimal) =186(Hex)
//-----------------------------------------------------------------------------------------------
U0DLL = 0x86;
U0DLM = 0x01;
U0LCR = 0x03; /* 0x03: same as above, but disable Divisor Latch access.*/
}

/***********************************************************************************************
* Send_Char
* Description : Write a character to Serial Port
***********************************************************************************************/
unsigned char Send_Char(unsigned char ch)
{
if (ch == '\n')
{
while(!(U0LSR & 0x20));
U0THR='\r'; /* output CR */
}
while(!(U0LSR & 0x20));
return (U0THR = ch);
}

IES-ARM7, Rev: 1. Page 32 of 52


Syllabus Programs.

void Send_String(unsigned char *ch)


{
while(*ch != '\0')
{
Send_Char(*ch++);
}
}

/***********************************************************************************************
**
* Main Routine
***********************************************************************************************/
int main (void)
{
int i;
UART0_Init(); /* Init UART 0 */
while (1) /* Infinite Loop */
{
Send_String("IElec Systems.,Nellai\n");
}
}

Result:
Thus the strings write to serial uart-0 and verified by Hyper Terminal.

Experiment No 5

4. INTERFACING KEYBOARD AND LCD.


Aim of the Experiment:
To write the program for 4X4 matrix Keypad.

Connection Details for RTC:


 P0.16-P1.19 connect to the ROW1-ROW4 burg pins by burg wires.
 P0.20-P0.23 connect to the COL1-COL4 burg pins by burg wires.

5.1.Program for 4X4 Matrix Keypad:


#include <LPC214x.H> /* LPC214x definitions */
#include <stdio.h>

char Newline[]="\n\r";
char TAB[]="\t";
void Delay(unsigned int Time)
{
unsigned int i,j;
for(i=0;i<=Time;i++)
for(j=0;j<110;j++);
}

IES-ARM7, Rev: 1. Page 33 of 52


Syllabus Programs.

void UART0_Init()
{
PINSEL0 = 0x05; /* Enable RxD0 and TxD0 */
U0LCR = 0x83; /* 8 bits, no Parity, 1 Stop bit */
U0DLL = 0x86; /* 9600 Baud Rate @60MHz VPB Clock */
U0DLM = 0x01;
U0LCR = 0x03; /* DLAB = 0 */
VPBDIV = 0x01;
}

void U0Write(char data)


{
while (!(U0LSR & (1<<5))); // wait till the THR is empty
// now we can write to the Tx FIFO
U0THR = data;
}

void Send_String(char* StringPtr)


{
while(*StringPtr != 0x00){
U0Write(*StringPtr);
StringPtr++;}
}
/***********************************************************************************************
* Main
* Description : This function Initialize the UART0and ADC
***********************************************************************************************/
int main (void) /* program exec. starts here */
{
UART0_Init(); /* Init UART 0 */
PINSEL1 = 0x00000000;
IO0DIR = 0X00F00000;//P0.20 TO P0.23 (O/P)(COL 1 2 3 4), P0.16 TO P0.19(I/P)(ROW 1 2 3 4)
Send_String("4 x 4 Matrix Key Pad Interface");
Send_String(Newline);
Send_String("Press any Key");
Send_String(Newline);
while(1)
{
IO0PIN=0x00E00000; // First Scan Line
if(( IO0PIN & 0x000F0000 )!= 0x000F0000)
{
switch(IO0PIN & 0x000F0000)
{
case 0x00070000 : Send_String("0");Send_String(TAB);break;
case 0x000B0000 : Send_String("4");Send_String(TAB);break;
case 0x000D0000 : Send_String("8");Send_String(TAB);break;
case 0x000E0000 : Send_String("C");Send_String(TAB);break;
}
}

IES-ARM7, Rev: 1. Page 34 of 52


Syllabus Programs.

IO0PIN=0x00D00000; // Second Scan Line


if(( IO0PIN & 0x000F0000 )!= 0x000F0000)
{
switch(IO0PIN & 0x000F0000)
{
case 0x00070000 : Send_String("1");Send_String(TAB);break;
case 0x000B0000 : Send_String("5");Send_String(TAB);break;
case 0x000D0000 : Send_String("9");Send_String(TAB);break;
case 0x000E0000 : Send_String("D");Send_String(TAB);break;
}
}

IO0PIN=0x00B00000; // Third Scan Line


if(( IO0PIN & 0x000F0000 )!= 0x000F0000)
{
switch(IO0PIN & 0x000F0000)
{
case 0x00070000 : Send_String("2");Send_String(TAB);break;
case 0x000B0000 : Send_String("6");Send_String(TAB);break;
case 0x000D0000 : Send_String("A");Send_String(TAB);break;
case 0x000E0000 : Send_String("E");Send_String(TAB);break;
}
}

IO0PIN=0x00700000; // Fourth Scan Line


if(( IO0PIN & 0x000F0000 )!= 0x000F0000)
{
switch(IO0PIN & 0x000F0000)
{
case 0x00070000 : Send_String("3");Send_String(TAB);break;
case 0x000B0000 : Send_String("7");Send_String(TAB);break;
case 0x000D0000 : Send_String("B");Send_String(TAB);break;
case 0x000E0000 : Send_String("F");Send_String(TAB);break;
}
}
Delay(4000);
}
}

Result:
Thus the 4X4 Matrix keypad interface was verified by Hyper Terminal.

Aim of the Experiment:


To write the program for wite the strings to LCD.

Connection Details for LCD:


 P1.16-P1.23 connect to the LCD datalines of D0-D7 burg pins by burg wires.
 P0.8 connect to the LCD RS(Register Select) burg pin by burg wire.
 P0.9 connect to the LCD R/W(Read/Write) burg pin by burg wire.
 P0.10 connect to the LCD ENA(Enable) burg pin by burg wire.

IES-ARM7, Rev: 1. Page 35 of 52


Syllabus Programs.

5.2.Program for write the strings to 16X2 LCD:


#include<LPC214x.h>
#define lcd_data IO1PIN
#define EN 0x00000400
#define RW 0x00000200
#define RS 0x00000100
unsigned char arr1[16]=" IElec Systems ";
unsigned char arr2[16]=" Nellai.. ";
void delay_ms()
{
unsigned int i,j;
for(i=0;i<0xf;i++)
for(j=0;j<0xff;j++);
}
void busy_check()
{
delay_ms();
}
void lcd_cmd(unsigned char value)
{
busy_check();
IO0CLR=RS;
IO0CLR=RW;
lcd_data=(value<<16);
IO0SET=EN;
IO0CLR=EN;
return;
}
void lcd_dat(unsigned char item)
{
busy_check();
IO0SET=RS;
IO0CLR=RW;
lcd_data=(item<<16);
IO0SET=EN;
IO0CLR=EN;
return;
}
void lcd_data_string(unsigned char *str) // Program to send string to LCD
{
int i=0;
while(str[i]!='\0')
{
lcd_dat(str[i]);
i++;
delay_ms();
}
return;
}

void main()

IES-ARM7, Rev: 1. Page 36 of 52


Syllabus Programs.

{
IO0DIR=0x00000F00;
IO1DIR=0x00FF0000;
PINSEL0 = 0x00000000;
PINSEL1 = 0x00000000;
PINSEL2 = 0x00000000;
lcd_cmd(0x38);
lcd_cmd(0x0C);
lcd_cmd(0x01);
lcd_cmd(0x06);
lcd_cmd(0x80);
lcd_data_string(arr1);
lcd_cmd(0xc0);
lcd_data_string(arr2);
while(1);
}

Result:
Thus the strings write to 16X2 LCD and Display.

Experiment No 6

6. INTERFACING EPROM AND INTERRUPT.


Aim of the Experiment:
To write the program for write the strings to I2C EPROM(AT24c04).

Connection Details for I2C EPROM:


 P0.3 connect to the EPSDA burg pin by burg wire.
 P0.2 connect to the EPSCL burg pin by burg wire.
 P0.16 connect to the switch interfacing o/p 1 burg pin by burg wire for WRITE operation.
 P0.17 connect to the switch interfacing o/p 2 burg pin by burg wire for READ operation.
 P0.18 connect to the switch interfacing o/p 3 burg pin by burg wire for ERASE operation.

6.1.Program for Erase,Write and read to EPROM:


/******************************************************
WHAT PROGRAM DO:-This Program write and read data from EEPROM 24LC04 by I2C Protocol
with arm ( lpc2148 ) and send data to through UART.
******************************************************/
#include <lpc214x.h> //Includes LPC2148 register definitions

#define SW1 0X00010000


#define SW2 0X00020000
#define SW3 0X00040000
#define MAX_BUFFER_SIZE 16
#define DEVICE_ADDR 0xA0
#define BLK_0 0x00
#define BLK_1 0x02
#define MAX_BLOCK_SIZE 256

IES-ARM7, Rev: 1. Page 37 of 52


Syllabus Programs.

#define Fosc 12000000


#define Fcclk (Fosc * 5)
#define Fcco (Fcclk * 4)
#define Fpclk (Fcclk / 4)

void Delay(unsigned char j);


void Send_Start(void);
void Send_Stop(void);
unsigned char Send_I2C(unsigned char *Data,unsigned char Len);
unsigned char Read_I2C(unsigned char *Data,unsigned char Len);
unsigned char Page_Write(unsigned char BLOCK_NUMBER,unsigned char
BLOCK_ADDR,unsigned char *str);
unsigned char Page_Read(unsigned int BLOCK_NUMBER,unsigned char BLOCK_ADDR);
unsigned char I2C_Status(unsigned char status_code);

unsigned char I2C_WR_Buf[MAX_BUFFER_SIZE]={"01 02 03 04 05 06 07 09 0A 0B"};


unsigned char I2C_RD_Buf[MAX_BUFFER_SIZE];
unsigned char I2C_ER_Buf[MAX_BUFFER_SIZE]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
unsigned char Status=0;
unsigned char Status_Flag=0;

void Delay(unsigned char j)


{
unsigned int i;
for(;j>0;j--)
{
for(i=0; i<60000; i++);
}
}

void Send_Start()
{
I2C0CONSET=0x20;
}

void Send_Stop()
{
I2C0CONSET=0x10;
}
// This function sends sequential data to the EEPROM 24LC04
// The buffer size for EEPROM 24LC04 is 16 bytes
// The Len parameter should not exceed this value
unsigned char Send_I2C(unsigned char *Data,unsigned char Len)
{
while(Len)
{
I2C0DAT=*Data;
if(I2C_Status(0x28))
{
return 1;
}
Len--;

IES-ARM7, Rev: 1. Page 38 of 52


Syllabus Programs.

Data++;
}
return 0;
}

// This function reads random data from the EEPROM 24LC04


unsigned char Read_I2C(unsigned char *Data,unsigned char Len)
{
while(Len)
{
if(Len==1) //Last byte
{
I2C0CONCLR=0x04; //set hardware to send nack
if(I2C_Status(0x58)) //last byte has been received and NACK has been returned
{
return 1;
}
*Data=I2C0DAT;
}
else
{
I2C0CONSET=0x04; //set hardware to send ack
if(I2C_Status(0x50)) //Byte has been received ACK has been returned
{
return 1;
}
*Data=I2C0DAT;
}
Data++;
Len--;
}
return 0;
}

unsigned char I2C_Status(unsigned char status_code)


{
while(Status_Flag==0);
Status_Flag=0;
if(Status!=status_code)
{
return 1;
}
else
{
return 0;
}
}

unsigned char Page_Write(unsigned char BLOCK_NUMBER,unsigned char BLOCK_ADDR,


unsigned char *str)
{
Send_Start();

IES-ARM7, Rev: 1. Page 39 of 52


Syllabus Programs.

if(I2C_Status(0x08)) //Start has been transmitted


{
return 1;
}

I2C0DAT=DEVICE_ADDR | BLOCK_NUMBER; // Send Address


if(I2C_Status(0x18)) //Device address, block num and write has
been transmitted
{
return 1;
}

I2C0DAT=BLOCK_ADDR; // Send block address


if(I2C_Status(0x28)) //Block address has been transmitted
{
return 1;
}

if(Send_I2C(str,MAX_BUFFER_SIZE)) //Send Data


{
Send_Stop();
return 1;
}
Send_Stop();
return 0;
}
unsigned char Page_Read(unsigned int BLOCK_NUMBER,unsigned char BLOCK_ADDR)
{
Send_Start();
if(I2C_Status(0x08)) //Start has been transmitted
{
return 1;
}

I2C0DAT=DEVICE_ADDR | BLOCK_NUMBER; // Send Address


if(I2C_Status(0x18)) //Device address, block num and write has been transmitted
{
return 1;
}

I2C0DAT=BLOCK_ADDR;
if(I2C_Status(0x28)) //Block address has been transmitted
{
return 1;
}

Send_Start(); // Repeat Start


if(I2C_Status(0x10)) //Repeated Start has been transmitted
{
return 1;
}

IES-ARM7, Rev: 1. Page 40 of 52


Syllabus Programs.

I2C0DAT=DEVICE_ADDR | BLOCK_NUMBER | 0x01; //Device address, block num


and read has been transmitted
if(I2C_Status(0x40)) //
{
return 1;
}
if(Read_I2C(I2C_RD_Buf,MAX_BUFFER_SIZE)) //Receive 16bytes of Data from
EEPROM
{
Send_Stop();
return 1;
}
Send_Stop();
return 0;
}

void __irq I2C0_Status(void)


{
Status_Flag=0xFF; //update status flag
Status=I2C0STAT; //Read Status byte
I2C0CONCLR=0x28;
VICVectAddr = 0x00; //Acknowledge Interrupt
}

void I2C_Init()
{
PINSEL0&=0xFFFFFF0F;
PINSEL0|=0x00000050;
PINSEL1 = 0;
I2C0CONCLR=0x6C;
I2C0CONSET=0x40;
I2C0SCLH =80;
I2C0SCLL =70;

/* Init VIC for I2C0 */


VICIntSelect = 0x00000000; // Setting all interrupts as IRQ(Vectored)
VICVectCntl0 = 0x20 | 9; // Assigning Highest Priority Slot to I2C0 and enabling this slot
VICVectAddr0 = (unsigned long)I2C0_Status; // Storing vector address of I2C0
VICIntEnable = (1<<9);
}

void UART0_Init(unsigned int baudrate)


{
unsigned int BAUD_VAL;
BAUD_VAL = (Fpclk/(16*baudrate));

PINSEL0 |= 0x00000005; // Enable rx,tx


U0LCR = 0x00000083; // 8 bit data,1 stop bit,no
parity bit
U0DLL = BAUD_VAL & 0xFF; // U0DL for low byte
U0DLM = (BAUD_VAL>>8); // U0DL for high byte
U0LCR = 0x00000003; // DLAB =0

IES-ARM7, Rev: 1. Page 41 of 52


Syllabus Programs.

void UART0_Txmt(unsigned char Chr) /* Write character to Serial Port */


{
while (!(U0LSR & 0x20));
U0THR = Chr;
}

unsigned char UART0_Recv(void) /* Write character to Serial Port */


{
while (!(U0LSR & 0x01));
return (U0RBR);
}

void UART0_puts(unsigned char *string)


{
while(*string)
UART0_Txmt(*string++);
}

int main(void)
{
IO1DIR &= 0xfC7fffff;
IO0DIR &= 0xfff8ffff;

UART0_Init(9600);
I2C_Init();
UART0_puts ("********* ARM7/LPC-2148 I2C EEPROM Demo **********\n\r");
UART0_puts (">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n\r");
UART0_puts ("[~] Turn SW 1 ON to Write default data to EEPROM! \n\r");
UART0_puts ("[~] Turn SW 2 ON to Read and Display data from EEPROM! \n\r");
UART0_puts ("[~] Turn SW 3 ON to Erase data from EEPROM \n\r");

while(1)
{
if ((IOPIN0 & SW1) == 0) /*...To Load the Default Data to the EEPROM ...*/
{
if(Page_Write(BLK_1,0x00,I2C_WR_Buf))
{
UART0_puts("Write Failed");
}
Delay(50);
UART0_puts("Data Written Successfully\r\n");
while (!(IOPIN0 & SW1));
}
else if ((IOPIN0 & SW2) == 0) /*..To Read the Data Stored in the EEPROM...*/
{
if(Page_Read(BLK_1,0x00))
{
UART0_puts("Read Failed");
}
else

IES-ARM7, Rev: 1. Page 42 of 52


Syllabus Programs.

{
UART0_puts("Data Read:");
UART0_puts(I2C_RD_Buf);
UART0_puts("\r\n");
}
Delay(50);
while (!(IOPIN0 & SW2));
}
else if ((IOPIN0 & SW3) == 0)
{
if(Page_Write(BLK_1,0x00,I2C_ER_Buf))
{
UART0_puts("Write Failed");
}
Delay(50);
UART0_puts("Data Erase Successfully\r\n");
while (!(IOPIN0 & SW3));
}
}
}

Result:
Thus the strings Erase, write and Read to I2C EPROM.

Aim of the Experiment:


Assume that the EINT0 pin is connected to a switch that is normally high. Whenever it goes low, it
should turn on an LED. The LED is connected to P1.16-P1.23 and is normally off. When it is turned on it
should stay on for a fraction of a second. As long as the switch is pressed low, the LED should stay on.

Connection Details for INTERRUPT:


 P0.1 connect to the Level triggered Interrupt switch INT1 burg pin by burg wire.
 P1.16-P1.23 connect to LED I/P 1-8 burg pins by burg wires.

6.2.Program for EINT-0:


#include <LPC214x.H>
__irq void ExtInt0_ISR (void)
{
EXTINT = 0x00000001;
IO1SET = 0x00FF0000; /* All LED Glow */
VICVectAddr = 0x00000000;
}

void InitExtInt0(void)
{
VICVectCntl0 = (1<<5) | 14; /* use it for External Interrupt 0 */
VICIntEnable = (1<<14); /* Enable External Interrupt0 */
VICVectAddr0 = (unsigned long)ExtInt0_ISR; /* set interrupt vector in 0 */
}

int main(void)

IES-ARM7, Rev: 1. Page 43 of 52


Syllabus Programs.

{
PINSEL0 = 0x0000000C; /*Enable the EXTINT0 interrupt */
PINSEL2 = 0x00000000; /* Select pin for LED I/F */
IO1DIR = 0x00FF0000; /* Set direction for Pin P1.16 to Pin P1.23 as output */
VPBDIV = 0x00000001;
InitExtInt0(); /* Init Extenal Interrupt 0 */
while(1)
{
IO1CLR = 0x00FF0000; /* Clear All LED */
}
}

Result:
Thus the External Interrupt0 activated by level triggered switch and Interrupt Service Routine (ISR) was
executed and output was observed by LED.

Experiment No 7

7. MAILBOX.
Aim of the Experiment:
To write the program for Mailbox using Keil-RTX kernal.

Connection Details for I2C EPROM:


 No need external connection except from serial cable.

7.Program for MAILBOX :

/*----------------------------------------------------------------------------
* RL-ARM - RTX
*----------------------------------------------------------------------------
* Name: MAILBOX.C
* Purpose: RTX example program
*----------------------------------------------------------------------------
* This code is part of the RealView Run-Time Library.
* Copyright (c) 2004-2013 KEIL - An ARM Company. All rights reserved.
*---------------------------------------------------------------------------*/

#include <RTL.h> /* RTX kernel functions & defines */


#include <LPC21xx.H> /* LPC21xx definitions */
#include <stdio.h>

OS_TID tsk1; /* assigned identification for task 1 */


OS_TID tsk2; /* assigned identification for task 2 */

typedef struct { /* Message object structure */


float voltage; /* AD result of measured voltage */
float current; /* AD result of measured current */
U32 counter; /* A counter value */

IES-ARM7, Rev: 1. Page 44 of 52


Syllabus Programs.

} T_MEAS;

os_mbx_declare (MsgBox,16); /* Declare an RTX mailbox */


_declare_box (mpool,sizeof(T_MEAS),16);/* Dynamic memory pool */

__task void send_task (void);


__task void rec_task (void);

/*----------------------------------------------------------------------------
* Initialize serial interface
*---------------------------------------------------------------------------*/
void init_serial () {
PINSEL0 = 0x00000005; /* Enable RxD0 and TxD0 */
U0LCR = 0x83; /* 8 bits, no Parity, 1 Stop bit */
U0DLL = 97; /* 9600 Baud Rate @ 15MHz VPB Clock */
U0LCR = 0x03; /* DLAB = 0 */
}
/*----------------------------------------------------------------------------
* Task 1: RTX Kernel starts this task with os_sys_init (send_task)
*---------------------------------------------------------------------------*/
__task void send_task (void) {
T_MEAS *mptr;

tsk1 = os_tsk_self (); /* get own task identification number */


tsk2 = os_tsk_create (rec_task, 0); /* start task 2 */
os_mbx_init (MsgBox, sizeof(MsgBox));/* initialize the mailbox */
os_dly_wait (5); /* Startup delay for MCB21xx */

mptr = _alloc_box (mpool); /* Allocate a memory for the message */


mptr->voltage = 223.72; /* Set the message content */
mptr->current = 17.54;
mptr->counter = 120786;
os_mbx_send (MsgBox, mptr, 0xffff); /* Send the message to the mailbox */
os_dly_wait (100);

mptr = _alloc_box (mpool);


mptr->voltage = 227.23; /* Prepare a 2nd message */
mptr->current = 12.41;
mptr->counter = 170823;
os_mbx_send (MsgBox, mptr, 0xffff); /* And send it. */
os_tsk_pass (); /* Cooperative multitasking */
os_dly_wait (100);

mptr = _alloc_box (mpool);


mptr->voltage = 229.44; /* Prepare a 3rd message */
mptr->current = 11.89;
mptr->counter = 237178;
os_mbx_send (MsgBox, mptr, 0xffff); /* And send it. */
os_dly_wait (100);
os_tsk_delete_self (); /* We are done here, delete this task */
}
/*----------------------------------------------------------------------------

IES-ARM7, Rev: 1. Page 45 of 52


Syllabus Programs.

* Task 2: RTX Kernel starts this task with os_tsk_create (rec_task, 0)


*---------------------------------------------------------------------------*/
__task void rec_task (void) {
T_MEAS *rptr;

for (;;)
{
os_mbx_wait (MsgBox, (void **)&rptr, 0xffff); /* wait for the message */
printf ("\nVoltage: %.2f V\n",rptr->voltage);
printf ("Current: %.2f A\n",rptr->current);
printf ("Number of cycles: %d\n",rptr->counter);
_free_box (mpool, rptr); /* free memory allocated for message */
}
}

/*----------------------------------------------------------------------------
* Main: Initialize and start RTX Kernel
*---------------------------------------------------------------------------*/
int main (void) { /* program execution starts here */
init_serial (); /* initialize the serial interface */
_init_box (mpool, sizeof(mpool), /* initialize the 'mpool' memory for */
sizeof(T_MEAS)); /* the membox dynamic allocation */
os_sys_init (send_task); /* initialize and start task 1 */
}

Result:
Thus the mail massage has been sent and received by Hyper Terminal using Keil-RTX Kernel.

Experiment No 9

9. FLASHING OF LEDS.
Aim of the Experiment:
To write the program for flashing of LEDS.

Connection Details for LED:


 P0.16-P0.23 connect to LED I/P 1-8 burg pins by burg wires.

9.Program for Flashing of LEDS:

#include<LPC214x.h> // Define LPC2148 Header File


#define led IO0PIN // Define LED to Port1
#define tled IO0DIR // Define Port1 as output
void delay(int x);

int main(void)
{
tled = 0x00FF0000; // Define P0.16 – P0.23 as O/P

IES-ARM7, Rev: 1. Page 46 of 52


Syllabus Programs.

while(1) // Loop forever

{
led = 0x00FF0000; // Turn ON P0.16 – P0.23
delay(2000);
led = 0x00000000; // Turn OFF P0.16 – P0.23
delay(2000);
}
}
void delay(int x)
{
unsigned int k,l;
for(k = x;k > 0;k--)
for(l = 0;l < x;l++);
}

Result:
Thus the Flashing LEDs program was written and executed. Output was verified.

Experiment No 10

10. INTERFACING STEPPER MOTOR AND TEMPERATURE SENSOR.


Aim of the Experiment:
To write the program for rotate stepper motor on clock wise.

Connection Details for Stepper Motor:


 P0.4-P0.7 connect to MOT1SEQ burg pins by burg wires.

10.1.1.Program for To run the Stepper Motor in Clockwise :

#include<LPC214x.h> // Define LPC2148 Header File


#define mot IO0PIN // Define mot to Port1
#define tmot IO0DIR
void delay(int x);
int main()
{
PINSEL0 = 0x00000000;
tmot = 0x000000F0; // Define P0.4 – P0.7 as O/P
while(1) // Loop forever
{
mot = 0x00000010; // Phase Scheme Clock-wise First Sequence.
delay(200); // Call Delay with data
mot = 0x00000020; // Phase Scheme Clock-wise Second Sequence.
delay(200);
mot = 0x00000040; // Phase Scheme Clock-wise Third Sequence.
delay(200);
mot = 0x00000080; // Phase Scheme Clock-wise Fourth Sequence.
delay(200);
}

IES-ARM7, Rev: 1. Page 47 of 52


Syllabus Programs.

}
void delay(int x)
{
unsigned int k,l;
for(k = x;k > 0;k--)
for(l = 0;l < x;l++);
}

10.1.2.Program for To run the Stepper Motor in Unti-Clockwise:

#include<LPC214x.h> // Define LPC2148 Header File


#define mot IO0PIN // Define mot to Port1
#define tmot IO0DIR
void delay(int x);
int main()
{
PINSEL0 = 0x00000000;
tmot = 0x000000F0; // Define P0.4 – P0.7 as O/P
while(1) // Loop forever
{
mot = 0x00000080; // Phase Scheme Clock-wise First Sequence.
delay(200); // Call Delay with data
mot = 0x00000040; // Phase Scheme Clock-wise Second Sequence.
delay(200);
mot = 0x00000020; // Phase Scheme Clock-wise Third Sequence.
delay(200);
mot = 0x00000010; // Phase Scheme Clock-wise Fourth Sequence.
delay(200);
}
}
void delay(int x)
{
unsigned int k,l;
for(k = x;k > 0;k--)
for(l = 0;l < x;l++);
}

10.1.3.Program for To run the Stepper Motor in Required Angel 360 :


#include<LPC214x.h> // Define LPC2148 Header File
#define mot IO0PIN // Define mot to Port1
#define tmot IO0DIR
unsigned int i;
void delay(int x);
int main()
{
PINSEL0 = 0x00000000;
tmot = 0x000000F0; // Define P0.4 – P0.7 as O/P
for(i=0;i<=50;i++) // 50 = 360 angel //Required angel count=Required angel/Step Size*4
{
mot = 0x00000010; // Phase Scheme Clock-wise First Sequence.

IES-ARM7, Rev: 1. Page 48 of 52


Syllabus Programs.

delay(200); // Call Delay with data


mot = 0x00000020; // Phase Scheme Clock-wise Second Sequence.
delay(200);
mot = 0x00000040; // Phase Scheme Clock-wise Third Sequence.
delay(200);
mot = 0x00000080; // Phase Scheme Clock-wise Fourth Sequence.
delay(200);
}
}
void delay(int x)
{
unsigned int k,l;
for(k = x;k > 0;k--)
for(l = 0;l < x;l++);
}

Result:
Thus the following Stepper Motor programs was written and executed. Output was verified.

Aim of the Experiment:


To write the program for Find the Room Temparature using LM35 sensor.

Connection Details for Stepper Motor:


 Sensor o/p (Analog) mv connect to P0.28 burg pin by burg wire.

10.2.Program for Interfacing TI-LM35 Sensor :

#include <LPC214x.H> /* LPC214x definitions */


#include <stdio.h>

unsigned int ADC_Val;


char adcreading[16] ;
char Newline[]=" \r";
void Delay(unsigned int Time)
{
unsigned int i,j;
for(i=0;i<=Time;i++)
for(j=0;j<110;j++);
}
/***********************************************************************************************
* UART0 Initialization *
***********************************************************************************************/
void UART0_Init()
{
PINSEL0 = 0x05; /* Enable RxD0 and TxD0 */
U0LCR = 0x83; /* 8 bits, no Parity, 1 Stop bit */
U0DLL = 0x86; /* 9600 Baud Rate @60MHz VPB Clock */
U0DLM = 0x01;
U0LCR = 0x03; /* DLAB = 0 */
VPBDIV = 0x01;
}

IES-ARM7, Rev: 1. Page 49 of 52


Syllabus Programs.

/***********************************************************************************************
* ADC initialization *
***********************************************************************************************/
void ADC_Init()
{
PINSEL1 = 0x01000000; /* channel AD0.1 (P0.28) */
AD0CR = 0x00230602; /* Setup A/D: 10-bit AIN0 @ 3MHz */
}

void U0Write(char data)


{
while (!(U0LSR & (1<<5))); // wait till the THR is empty
// now we can write to the Tx FIFO
U0THR = data;
}

void Send_String(char* StringPtr)


{
while(*StringPtr != 0x00){
U0Write(*StringPtr);
StringPtr++;}
}
/***********************************************************************************************
* Main
* Description : This function Initialize the UART0and ADC
***********************************************************************************************/
int main (void) /* program exec. starts here */
{
UART0_Init(); /* Init UART 0 */
Delay(3000);
ADC_Init(); /* ADC Initialize */
while(1)
{
AD0CR |= 0x01000000; /* start of ADC conversion*/
do
{
ADC_Val = AD0GDR; /* 10 bit value */
}
while ((ADC_Val & 0x80000000) == 0); /* Wait ADC Conversion Complete */
AD0CR &= ~0x01000000; /* Again start ADC */
ADC_Val = (ADC_Val >> 6) & 0x03FF;
sprintf(adcreading,"Temperature Value= %f",ADC_Val*0.322);
Send_String(adcreading);
Send_String(Newline);
Delay(4000);
}
}

IES-ARM7, Rev: 1. Page 50 of 52


Syllabus Programs.

Used Formula:
Digital O/P = Analag I/P X 2n /Vref
Temperature =Digital O/P x 0.322
Where,
N = No of bits per ADC(1024).
Vref =Refernce Voltage(3.3V)

Result:
Thus the Temp program was written and find the room temparature and display by Hyper Teriminal.

Experiment No 11

11. IMPLEMENTING ZIGBEE PROTOCOL WITH ARM.


Aim of the Experiment:
To write the program for Find the Room Temparature using LM35 sensor and send to PC by wireless
using ZigBee Protocol .
Connection Details for Stepper Motor:
 Sensor o/p (Analog) mv connect to P0.28 burg pin by burg wire.

11Program for Interfacing TI-LM35 Sensor :


#include <LPC214x.H> /* LPC214x definitions */
#include <stdio.h>
unsigned int ADC_Val;
char adcreading[16] ;
char Newline[]=" \r";
void Delay(unsigned int Time)
{
unsigned int i,j;
for(i=0;i<=Time;i++)
for(j=0;j<110;j++);
}
/***********************************************************************************************
* UART0 Initialization *
***********************************************************************************************/
void UART0_Init()
{
PINSEL0 = 0x05; /* Enable RxD0 and TxD0 */
U0LCR = 0x83; /* 8 bits, no Parity, 1 Stop bit */
U0DLL = 0x86; /* 9600 Baud Rate @60MHz VPB Clock */
U0DLM = 0x01;
U0LCR = 0x03; /* DLAB = 0 */
VPBDIV = 0x01;
}
/***********************************************************************************************
* ADC initialization *
***********************************************************************************************/
void ADC_Init()
{
PINSEL1 = 0x01000000; /* channel AD0.1 (P0.28) */

IES-ARM7, Rev: 1. Page 51 of 52


Syllabus Programs.

AD0CR = 0x00230602; /* Setup A/D: 10-bit AIN0 @ 3MHz */


}

void U0Write(char data)


{
while (!(U0LSR & (1<<5))); // wait till the THR is empty
// now we can write to the Tx FIFO
U0THR = data;
}

void Send_String(char* StringPtr)


{
while(*StringPtr != 0x00){
U0Write(*StringPtr);
StringPtr++;}
}
/***********************************************************************************************
* Main
* Description : This function Initialize the UART0and ADC
***********************************************************************************************/
int main (void) /* program exec. starts here */
{
UART0_Init(); /* Init UART 0 */
Delay(3000);
ADC_Init(); /* ADC Initialize */
while(1)
{
AD0CR |= 0x01000000; /* start of ADC conversion*/
do
{
ADC_Val = AD0GDR; /* 10 bit value */
}
while ((ADC_Val & 0x80000000) == 0); /* Wait ADC Conversion Complete */
AD0CR &= ~0x01000000; /* Again start ADC */
ADC_Val = (ADC_Val >> 6) & 0x03FF;
sprintf(adcreading,"Temperature Value= %f",ADC_Val*0.322);
Send_String(adcreading);
Send_String(Newline);
Delay(4000);
}
}

Used Formula:
Digital O/P = Analag I/P X 2n /Vref
Temperature =Digital O/P x 0.322
Where,
N = No of bits per ADC(1024).
Vref =Refernce Voltage(3.3V)

Result:
Thus the Temp program was written and find the room temparature and sent to PC by wireless ZigBee
Co-ordinator & Router. Output verified by Hyper Teriminal.

IES-ARM7, Rev: 1. Page 52 of 52

You might also like