MC Lab Manual
MC Lab Manual
PART - A
Program 1: Write a program to multiply two 16 bit binary numbers.
AREA BINMUL,CODE,READONLY
ENTRY
LDR R2,[R1]; Load the Data which is stored in Register R1 to the Register R2
LDR R4,[R3]; Load the Data which is stored in Register R3 to the Register R4
MUL R5,R2,R4; Multiply the data which is stored in R2 and R4. Store it in R5
NOP
END
Input:
Output:
Result can be viewed in Register R5 or on the memory location 0x40000008 (2ED8267D) in hex
decimal values.
ENTRY
LOOP
ADD R3,R3,R1; Add the content of R1 with R3 and store the result back to R3
ADD R1,R1,#01; Add one to the data of R1 and store the result back to R1
MOV R5,#0X40000000; Move the address to the Register R5 to store the result
NOP
END
Output:
Result can be viewed in Register R4 and on the memory location 0X40000000 (37) in hex
decimal value.
ENTRY
LOOP
BEQ STOP;
MOV R3,#0X40000000; Move the address to the register R3 to store the result
BNE LOOP;
STOP
NOP
END
Output:
The result can be viewed in register R0 and on the memory location 0X40000000 (78) in hex
decimal value.
Program 4: Write a program to add an array of 16 bit numbers and store the
32 bit result in internal RAM
AREA ARRAY_SUM,CODE,READONLY
ENTRY
MOV R1,#0X00000003; Initialize the counter to 3 (i.e N=4) and move that to R1
LOOP
ADD R3,R3,#4; Add 4 to the address in R3 to point out to the next address
LDR R5,[R3]; Load data which is stored on the address of R3 to the register R5
CMP R1,#00; Compare the counter with 0 to come out of the loop
BNE LOOP
NOP
END
INPUT:
0X40000000 = 1111
0X40000004 = 2222
0X40000008 = 3333
0X4000000C = 4444
OUTPUT:
The result can be viewed in register R4 and on the memory location 0X4000001C (AAAA) in
hex decimal value.
OUTPUT:
The result can be viewed in register R3 and on the memory location 0X40000000 in hex decimal
value. (eg: In this program 2 is loaded onto the register R1, so the output at 0X40000000 memory
location is 4)
LOOP1
SUB R1,R1,#01 ; Decrement the counter
CMP R1,#00 ; Compare counter to 0 to exit from LOOP
BNE LOOP
STR R4,[R3] ; Store the resultant to the address of register R3
NOP
NOP
NOP
END
INPUT:
0X40000000 = 11111111
0X40000004 = 22222222
0X40000008 = 33333333
0X4000000C = 44444444
0X40000010 = 55555555
OUTPUT:
The result can be viewed in register R4 and on the memory location 0X4000001C (55555555) in
hex decimal value.
LOOP1
SUB R1,R1,#01 ; Decrement the counter
CMP R1,#00 ; Compare counter to 0 to exit from LOOP
BNE LOOP
STR R4,[R3] ; Store the resultant to the address of register R3
NOP
NOP
NOP
END
INPUT:
0X40000000 = 11111111
0X40000004 = 22222222
0X40000008 = 33333333
0X4000000C = 44444444
0X40000010 = 55555555
OUTPUT:
The result can be viewed in register R4 and on the memory location 0X4000001C (11111111) in
hex decimal value.
INPUT:
0X40000000 = 22222222
0X40000004 = 11111111
0X40000008 = 55555555
0X4000000C = 44444444
0X40000010 = 33333333
OUTPUT:
The result can be viewed in the five different memory location starting from 0X40000000 to
0x40000010
0X40000000 = 11111111
0X40000004 = 22222222
0X40000008 = 33333333
0X4000000C = 44444444
0X40000010 = 55555555
INPUT:
0X40000000 = 22222222
0X40000004 = 11111111
0X40000008 = 55555555
0X4000000C = 44444444
0X40000010 = 33333333
OUTPUT:
The result can be viewed in the five different memory location starting from 0X40000000 to
0x40000010
0X40000000 = 55555555
0X40000004 = 44444444
0X40000008 = 33333333
0X4000000C = 22222222
0X40000010 = 11111111
Program 8: Write a program to count the number of ones and zeros in two
consecutive memory locations.
START
LOOP0 MOVS R0, R0, ROR #1 ; RIGHT SHIFT TO CHECK CARRY BIT (1's/0's)
BHI ONES
; IF CARRY BIT IS 1 GOTO ONES BRANCH OTHERWISE NEXT
Output:
PART – B
Creating a C project in Keil
Building
1. Now give a right click on target in the project window and select “Options for Target”.
2. It will show a window, in that go to output option and choose Create Hex file option by
selecting that box. In the same window go to Linker option and choose Use Memory Layout
from Target Dialog by selecting the box, and click OK.
3. To compile your project go to Project select Build Target option or press F7.
Dr. Archana B, Department of CSE, VVIET, Page 18
Mysuru
Microcontroller and Embedded Systems Laboratory (21CS43)
4. In the build Output window you can see the errors and warnings in your code. And here
Your project Hex file will be created.
LPC2148 C Programming
• LPC2148 has 2 ports viz. Port 0 and Port 1. Each port can be used as I/P or O/P. Port 0 has
0-31 pins and Port1 has 16-31 pins.
• Port 0 is a 32 bit wide I/O port (i.e. it can be used for max 32 pins where each pin refers
to a corresponding bit) and has dedicated direction bits for each of the pins present in the
port. 28 out of the 32 pins can be used as bi-directional I/O (digital) pins. Pins P0.24, P0.26
& P0.27 are unavailable for use and Pin P0.30 can be used as output pin only. In port0,
P0.24, P0.26, P0.27 are not physically available means you can‟t find them on chip but still
you can write values to them. & P0.31 is available for digital output only.
• Port 1 is also a 32 bit wide I/0 port but Pins 0 to 15 i.e P1.0 – P1.15 are unavailable for use
and this port too has a dedicated direction bit for each of the usable pins. In port1, P1.0 to
P1.15 is not available physically. Only P1.16 to P1.31 is available. So out of 64 pins 45
pins are covered by port0 & port1.
• Each Port can be used as GPIO (general purpose I/p and O/p) or As SFR (Special Function).
1. IOPINx (x=port number): To read the status of the PIN. GPIO Port Pin value registers. This
register can be used to Read or Write values directly to the pins. Regardless of the direction set for
the particular pins it gives the current start of the GPIO pin when read. The current state of the
GPIO configured port pins can always be read from this register, regardless of pin direction.
2. IODIRx: To set the direction of the PIN (INPUT or OUTPUT). This is the GPIO direction
control register. Setting a bit to 0 in this register will configure the corresponding pin to be used
as an Input while setting it to 1 will configure it as Output. To set the direction of any Port as I/P
or O/P we have to SET and CLR a Register named as IODIR. This register individually controls
the direction of each port pin.
When we are giving INPUT to the pin then some output should be produced from the
microcontroller, so the pin should be OUTPUT pin.
When we are accepting request from the pin then it should be INPUT.
Example:
3. IOSETx: GPIO Port Output Set registers. This register can be used to drive an „output‟
configured pin to Logic 1 i.e HIGH. Writing Zero does NOT have any effect and hence it cannot
be used to drive a pin to Logic 0 i.e LOW. This register controls the state of output pins in
conjunction with the IOCLR register. Writing ones produces highs at the corresponding port pins.
Writing zeroes has no effect.
Example:
4. IOCLR0: This register can be used to drive an „output‟ configured pin to Logic 0 i.e LOW.
Writing Zero does NOT have any effect and hence it cannot be used to drive a pin to Logic 1.
GPIO Port Output Clear registers. This Register controls the state of output pins. Writing ones
produces lows at the corresponding port pins and clears the corresponding bits in the IOSET
register.
Note:- When IOSET = 1 it will set the particular pin, but it doesn't means that to clear the pin you
will use IOSET = 0 or IOCLR = 0, this is different in case of ARM, to set the pin we have to use
IOSET=1 and to clear we have to use IOCLR=1.
int main(void)
U0LCR = 0X00000003;
rx_flag = 0x00;
tx_flag = 0x00;
while(1)
// Do this forever
temp = U0IIR;
if(temp == 0x02)
tx_flag = 0xff;
VICVectAddr=0;
U0THR = U0RBR;
rx_flag = 0xff;
VICVectAddr=0;
PROGRAM:
#include <LPC214x.H>
void delay(unsigned int x)
{
unsigned int i,j;
for(i=0;i<x;i++)
for(j=0;j<1275;j++); //1275 * int * times
}
int main()
{
IODIR0| = 0x00000900; //setting both the pins p0.8 and p0.11 to high making them as o/p pins
Result: When connecting DC Motor to the microcontroller interface then motor head will first
rotate anti-clockwise and then it will rotate in clockwise.
PROGRAM 11: Interface a Stepper motor and rotate it in clockwise and anti-
clockwise direction.
A stepper motor is a brush less, synchronous electric motor that converts digital pulses into
mechanical shaft rotation. Every revolution of the stepper motor is divided into a discretenumber
of steps, and the motor must be sent a separate pulse for each step. It demonstrates the principle of
stepper motor interfacing with ARM LPC2148 microcontroller. It finds great application in field
of microcontrollers such as robotics. Unipolar Motor is the most popular stepper motor among
electronics hobbyist because of its ease of operation and availability.
The Stepper motor can be interfaced to the board by connecting it into the Power Mate PM1. The
rotating direction of the stepper motor can be changed through software. Port lines used for Stepper
motor are P0.12 to P0.15. (4 Pins)
PROGRAM:
#include<LPC214x.h>
void delay(unsigned int x)
{
unsigned int i,j;
for(i=0;i<x;i++)
for(j=0;j<1275;j++); //1275 * int * times
}
void main()
{
int s;
IODIR0 |= 0x0000f000; //setting all four pins p0.12 to p0.15 to high making them as o/p pins
IOCLR0 = 0x0000f000;
for(s=0;s<60;s++) // clockwise rotation (For each iteration motor will rotate 1 cycle clockwise)
{
IOCLR0=0x0000f000; // for anticlockwise send 8,4,2,1 instead of 1,2,4,8
Result: When connecting Stepper Motor to the microcontroller interface then motor head will
first rotate clockwise and then it will rotate in anti-clockwise.
PROGRAM 12: Determine Digital output for a given Analog input using
Internal ADC of ARM controller.
#include <lpc214x.h>
#include <Stdio.h>
void lcd_init(void);
void wr_cn(void);
void clr_disp(void);
void delay(unsigned int);
void lcd_com(void);
void wr_dn(void);
void lcd_data(void);
int main()
{
PINSEL1 = 0X00040000; //AD0.4 pin is selected(P0.25)
IO0DIR = 0x000000FC; //configure o/p lines for lcd
delay(3200);
lcd_init(); //LCD initialization
delay(3200);
clr_disp(); //clear display
delay(3200); //delay
ptr = dis;
temp1 = 0x80; //Display starting address of first line 1 th pos
lcd_com();
delay(800);
while(*ptr!='\0')
{
temp1 = *ptr;
lcd_data();
ptr ++;
ptr1 = arr;
temp1 = 0xC0; //Display starting address of second line 4 th
pos
lcd_com();
delay(800);
while(*ptr1!='\0')
{
temp1 = *ptr1;
lcd_data();
ptr1 ++;
}
//infinite loop
while(1)
{
//CONTROL register for ADC
AD0CR = 0x01200010; //command register for ADC-AD0.4
temp1 = 0x89;
lcd_com();
delay(1200);
ptr = var1;
while(*ptr!='\0')
{
temp1=*ptr;
lcd_data();
ptr++;
temp1 = 0xc9;
lcd_com();
delay(1200);
ptr1 = var;
while(*ptr1!='\0')
{
temp1=*ptr1;
lcd_data();
ptr1++;
}
} // end of while(1)
} //end of main()
//lcd initialization
void lcd_init()
{
temp2=0x30;
wr_cn();
delay(800);
temp2=0x30;
wr_cn();
delay(800);
temp2=0x30;
wr_cn();
delay(800);
temp2=0x20;
wr_cn();
delay(800);
temp1 = 0x28;
lcd_com();
delay(800);
temp1 = 0x0c;
lcd_com();
delay(800);
temp1 = 0x06;
lcd_com();
delay(800);
temp1 = 0x80;
lcd_com();
delay(800);
void lcd_com(void)
{
temp2= temp1 & 0xf0;
wr_cn();
temp2 = temp1 & 0x0f;
temp2 = temp2 << 4;
wr_cn();
delay(500);
}
{
unsigned int r;
for(r=0;r<r1;r++);
}
void clr_disp(void)
{
temp1 = 0x01;
lcd_com();
delay(500);
}
Triangular Wave:
#include <lpc214x.h>
#include <Stdio.h>
int main ()
{
unsigned long int temp=0x00000000;
unsigned int i=0;
while(1)
{
// output 0 to FFC
for(i=0; i!=0x3ff; i++)
{
temp = i;
temp = temp << 6;
DACR = temp;
}
// output FF to 1
for(i=0x3ff; i!=0; i--)
{
temp=i;
temp = temp << 6;
DACR = temp;
}
}//End of while(1)
}//End of main()
Square Wave:
#include <lpc214x.h>
#include <Stdio.h>
int main()
{
while(1)
{
DACR = 0x00000000;
for(count=0;count<1000;count++);
DACR = 0x0000ffc0;
for(count=0;count<1000;count++);
}
} //end of main()
PROGRAM 14: Interface a 4x4 keyboard and display the key code on an
LCD.
#include<lpc21xx.h>
#include<stdio.h>
void lcd_init(void);
void clr_disp(void);
void lcd_com(void);
void lcd_data(void);
void wr_cn(void);
void wr_dn(void);
void scan(void);
void get_key(void);
void display(void);
void delay(unsigned int);
void init_port(void);
delay(3200); //delay
clr_disp(); //clear display
delay(500); //delay
while(*ptr!='\0')
{
temp1 = *ptr;
lcd_data();
ptr ++;
}
} //end of main()
}
else if(row == 0X03)
{
temp3=0x00E00000;
}
var1 = temp3;
IO1PIN = var1; // each time var1 value is put to port1
IO1CLR =~var1; // Once again Conforming (clearing all other bits)
scan();
delay(100); //delay
if(flag == 0xff)
break;
} // end of for
if(flag == 0xff)
break;
} // end of while
for(i=0;i<16;i++)
{
if(scan_code[i] == res1) //equate the scan_code with res1
{
result = ASCII_CODE[i]; //same position value of ascii code
break; //is assigned to result
}
}
}// end of get_key();
void scan(void)
{
unsigned long int t;
temp2 = IO1PIN; // status of port1
temp2 = temp2 & 0x000F0000; // Verifying column key
if(temp2 != 0x000F0000) // Check for Key Press or Not
{
delay(1000); //delay(100)//give debounce delay check again
temp2 = IO1PIN;
temp2 = temp2 & 0x000F0000; //changed condition is same
{
flag = 0x00;
}
}
} // end of scan()
void display(void)
{
ptr = disp0;
temp1 = 0x80; // Display starting address of first line
lcd_com();
while(*ptr!='\0')
{
temp1 = *ptr;
lcd_data();
ptr ++;
}
ptr = disp1;
temp1 = 0xC0; // Display starting address of second line
lcd_com();
while(*ptr!='\0')
{
temp1 = *ptr;
lcd_data();
ptr ++;
}
temp1 = 0xC6; //display address for key value
lcd_com();
temp1 = result;
lcd_data();
}
temp = 0x30;
wr_cn();
delay(3200);
temp = 0x30;
wr_cn();
delay(3200);
temp = 0x20;
wr_cn();
delay(3200);
// load command for lcd function setting with lcd in 4 bit mode,
// 2 line and 5x7 matrix display
temp = 0x28;
lcd_com();
delay(3200);
temp1 = 0x80;
lcd_com();
delay(800);
}
void lcd_data(void)
{
temp = temp1 & 0xf0;
wr_dn();
temp= temp1 & 0x0f;
temp= temp << 4;
wr_dn();
delay(100);
}
IO0CLR = 0x00000008;
}
void lcd_com(void)
{
temp = temp1 & 0xf0;
wr_cn();
temp = temp1 & 0x0f;
temp = temp << 4;
wr_cn();
delay(500);
}
void clr_disp(void)
{
// command to clear lcd display
temp1 = 0x01;
lcd_com();
delay(500);
}
void init_port()
{
IO0DIR = 0x000000FC; //configure o/p lines for lcd
IO1DIR = 0XFFF0FFFF;
}
The external interrupts are the interrupts received from the (external) devices interfaced with the
microcontroller. By using P0.3 port line we are generating external interrupt (EINT1).Short JP12,
when we press the switch SW12 the port line goes low & the external interrupt occurs at port line
P0.16. The microcontroller gets interrupts in whatever it is doing and jumps to the vector table to perform
the interrupt service routine.
#include<lpc214x.h>
void Extint1_Isr(void) irq; //declaration of ISR
unsigned char int_flag, flag;
void main()
{
IODIR1 |= 0X02000000; //P1.25 int led
IOSET1 = 0X02000000;
PINSEL0=0X000000c0; //P0.3 EINT1
EXTMODE =0x01; //edge i.e falling edge trigger and active low
EXTPOLAR= 0X00;
VICVectAddr0 = (unsigned long) Extint1_Isr; //Assign the EINT0 ISR function
VICVectCntl0 = 0x20 | 15; //Assign the VIC channel EINT1 to interrupt priority 0
VICIntEnable |= 0x00008000; //Enable the EINT1 interrupt
flag = 1;
}
else if(flag == 1)
{
IO1SET = 0x02000000;
flag = 0;
}
int_flag = 0x00;
}
}
}
Result: After dumping the code to the microcontroller when we press the interrupt switch the
LED will go off and again if we press the same switch the LED will gets on, which indicates that
the interrupt is servicing.
There are four multiplexed 7-segment displays TL543 (U8 - U11) on the board. Each display has
8-inputs SEG_A, SEG_B, SEG_C, SEG_D, SEG_E, SEG_F, SEG_G and DP. The port lines
P0.28 to P0.31 are used to select one of the 4 digits as shown in the table below. The port lines
P0.16 to P0.23 are used as segment lines for the EIGHT digits.
PROGRAM:
#include<LPC214x.h>
void delay(unsigned int x)
{
unsigned int i,j;
for(i=0;i<x;i++)
for(j=0;j<1275;j++);
}
void main()
{
IODIR0 |= 0xF0FF0000;
while(1)
{
IOCLR0=0xF0FF0000;
IOSET0=0xF03F0000; // Code to print 0
delay(5000);
IOCLR0=0xF0FF0000;
IOSET0=0xF0060000; // Code to print 1
delay(5000);
IOCLR0=0xF0FF0000;
IOSET0=0xF05B0000; // Code to print 2
delay(5000);
IOCLR0=0xF0FF0000;
IOSET0=0xF04F0000; // Code to print 3
delay(5000);
IOCLR0=0xF0FF0000;
IOSET0=0xF0660000; // Code to print 4
delay(5000);
IOCLR0=0xF0FF0000;
IOSET0=0xF06D0000; // Code to print 5
delay(5000);
IOCLR0=0xF0FF0000;
IOSET0=0xF07D0000; // Code to print 6
delay(5000);
IOCLR0=0xF0FF0000;
IOSET0=0xF0070000; // Code to print 7
delay(5000);
IOCLR0=0xF0FF0000;
IOSET0=0xF07F0000; // Code to print 8
delay(5000);
IOCLR0=0xF0FF0000;
IOSET0=0xF0670000; // Code to print 9
delay(5000);
IOCLR0=0xF0FF0000;
IOSET0=0xF0770000; // Code to print A
delay(5000);
IOCLR0=0xF0FF0000;
IOSET0=0xF07C0000; // Code to print b
delay(5000);
IOCLR0=0xF0FF0000;
IOSET0=0xF0390000; // Code to print C
delay(5000);
IOCLR0=0xF0FF0000;
IOSET0=0xF05E0000; // Code to print d
delay(5000);
IOCLR0=0xF0FF0000;
IOSET0=0xF0790000; // Code to print E
delay(5000);
IOCLR0=0xF0FF0000;
IOSET0=0xF0710000; // Code to print F
delay(5000);
}
}
Result: After dumping the code to the microcontroller when we switch on ALL four seven
segments the HEX digits from 0 to F will be displayed.
VIVA QUESTIONS
1. What do you mean by Embedded System? Give examples.
An embedded system is a computer system, made from a combination of hardware and
software, that is used to perform a specific task. It may or not be programmable, depending
on the application. Examples of embedded systems include washing machines, printers,
automobiles, cameras, industrial machines and more.
2. Why are embedded Systems useful?
Since the embedded system is dedicated to specific tasks, design engineers can optimize
it to reduce the size and cost of the product and increase the reliability and performance.
Some embedded systems are mass-produced, benefiting from economies of scale.
3. What are the segments of Embedded System?
Memory Segment and Data Segment.
4. What is Embedded Controller?
An embedded controller is a microcontroller in computers that handles various system
tasks that the operating system does not handle.
5. What is Microcontroller?
A microcontroller is a computer present in a single integrated circuit which is dedicated
to perform one task and execute one specific application.
Microprocessor is an IC which has only the CPU inside them i.e. only the processing
powers such as Intel’s Pentium 1,2,3,4, core 2 duo, i3, i5 etc. These microprocessors don’t
have RAM, ROM, and other peripheral on the chip. A system designer has to add them
externally to make them functional. Application of microprocessor includes Desktop PC’s,
Laptops, notepads etc.
But this is not the case with Microcontrollers. Microcontroller has a CPU, in addition with
a fixed amount of RAM, ROM and other peripherals all embedded on a single chip. At
times it is also termed as a mini computer or a computer on a single chip. Today different
manufacturers produce microcontrollers with a wide range of features available in different
versions. Some manufacturers are ATMEL, Microchip, TI, Freescale, Philips, Motorola etc.
7. How are Microcontrollers more suitable than Microprocessor for Real Time
Applications?
Microcontroller is a mini computer it contains processor, memory , io ports etc, all are
integrated in a single chip. Microcontrollers are used in specific task applications as it has
limited memory and peripherals. Examples: pic, Atmel, avr etc. Microcontroller is cheaper
than microprocessor.
8. Differentiate between Program Memory and Data Memory.
Data memory = where you place your variables. You can read and write values.
Program memory = where the application is stored. Some chips allows parts of the program
memory to be modified in blocks (segments), but you can't store variables in the program
memory. It is normally possible to store constants - i.e. initialized variables that you do not
change - in the program memory.
A register is a small place in a CPU that can store small amounts of the data used for
performing various operations such as addition and multiplication and loads the resulting
data on the main memory.
The program counter, commonly called the instruction pointer in Intel x86 and Itanium
microprocessors, and sometimes called the instruction address register, the instruction
counter, or just part of the instruction sequencer, is a processor register that indicates where
a computer is in its program sequence.
A stack pointer is a small register that stores the address of the last program request in
a stack. A stack is a specialized buffer which stores data from the top down. As new
requests come in, they "push down" the older ones.
In this mode data is present in address field of instruction .Designed like one address
instruction format.
In register addressing the operand is placed in one of 8 bit or 16 bit or 32 bit general purpose
registers. The data is in the register that is specified by the instruction.
memory simultaneously. Typically, code (or program) memory is read-only and data
memory is read-write. Therefore, it is impossible for program contents to be modified by
the program itself.
The von Neumann Architecture is named after the mathematician and early computer
scientist John von Neumann. Von Neumann machines have shared signals and memory for
code and data. Thus, the program can be easily modified by itself since it is stored in read-
write memory.
18. List some of the 8051 microcontroller manufacturers?
Intel
Philips
Infineon
Maxim/Dallas semiconductor
Atmel
19. What are the various types of memories used in microcontroller/microprocessor?
ROM - Read Only Memory
RAM - Random Access Memory
PROM - Programmable Read Only Memory
EPROM - Erasable Programmable Read Only Memory
EEROM - Electrically Erasable Programmable Read Only Memory
20. What is Firmware?
Firmware: The software that is stored permanently in ROM, PROM or EPROM is called
firmware.
21. What is cache memory?
Cache memory is small high speed memory. It is used for temporary storage of data &
information between main memory and CPU (central processing unit).
What is the processor used by ARM7?
ARM7 is a group 32-bit RISC ARM processor cores licensed by ARM Holdings for
microcontroller use.