0% found this document useful (0 votes)
3 views16 pages

Microcontroller LAB Programs

The document provides a series of assembly language programs for ARM microcontrollers using Keil software, demonstrating various operations such as multiplication, addition, logical operations, and finding the largest/smallest number in an array. It also includes C programs for sorting numbers, calculating factorials, converting character cases, and handling interrupts and exceptions. Each program is designed to illustrate specific functionalities and operations relevant to ARM architecture.

Uploaded by

carolinraja1875
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views16 pages

Microcontroller LAB Programs

The document provides a series of assembly language programs for ARM microcontrollers using Keil software, demonstrating various operations such as multiplication, addition, logical operations, and finding the largest/smallest number in an array. It also includes C programs for sorting numbers, calculating factorials, converting character cases, and handling interrupts and exceptions. Each program is designed to illustrate specific functionalities and operations relevant to ARM architecture.

Uploaded by

carolinraja1875
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1.

Using Keil software, observe the various registers, dump, CPSR, with a simple ALP programs

AREA MULTIPLY, CODE, READONLY


ENTRY
START
MOV R1, #6400
MOV R2, #3200
MUL R3, R2, R1
NOP
NOP
END

Output: The result of multiplication, addition is stored in register R3


2. Develop and simulate ARM ALP for Data Transfer, Arithmetic and Logical operations(Demonstrate with the
help of a suitable program)

AREA ADDITION, CODE, READONLY


ENTRY
START
MOV R1, #6
MOV R2, #3
ADD R3, R2, R1
NOP
NOP
END

AREA MINUS, CODE, READONLY


ENTRY
START
MOV R1, #6
MOV R2, #3
ADD R3, R2, R1
NOP
NOP
END

AREA LOGICAL, CODE, READONLY


ENTRY
START
MOV R1, #6
MOV R2, #3
AND R3, R2, R1
NOP
NOP
END
3. Develop an ALP to multiply two 16-bit binary numbers
AREA ADDITION, CODE, READONLY
ENTRY
START
MOV R1, #0016
MOV R2, #0034
ADD R3, R2, R1
NOP
NOP
END

4. Write an ALP to find sum of first 10 integer numbers

AREA SUMMATION, CODE, READONLY


ENTRY
START
MOV R5,#10
MOV R0,#0

MOV R1,#1
LOOP ADD R0, R0, R1
ADD R1, R1,#1
SUBS R5,R5,#1
CMP R5,#0
BNE LOOP
LDR R4,=RESULT
STR R0,[R4]
NOP
NOP
NOP

AREA DATA2,DATA, READWRITE


RESULT DCD 0X0
END

Output: The sum of first 10 integer numbers is stored in register R3


5. Write an ALP to find the largest/smallest number in an array of 32 bit numbers.

[Link]

AREA LARGEST, CODE, READONLY


ENTRY
START
MOV R5,#5

LDR R1,=VALUE1
LDR R2,[R1],#4
LOOP LDR R4,[R1],#4
CMP R2,R4
BHI LOOP1
MOV R2,R4
LOOP1 SUBS R5,R5,#1
CMP R5,#0
BNE LOOP
LDR R4,=RESULT
STR R2,[R4]
BX LR

VALUE1 DCD 0X44444444;


DCD 0X22222222;
DCD 0X11111111;
DCD 0X33333333;
DCD 0XAAAAAAAA;
DCD 0X99999999;

AREA DATA6,DATA, READWRITE


RESULT DCD 0X00000000
END

Output: The result is stored in memory location RESULT


[Link]

AREA LARGEST,CODE, READONLY


ENTRY
START
MOV R5,#5

LDR R1,=VALUE1
LDR R2,[R1],#4
LOOP LDR R4,[R1],#4
CMP R2,R4
BLS LOOP1
MOV R2,R4
LOOP1 SUBS R5,R5,#1
CMP R5,#0
BNE LOOP
LDR R4,=RESULT
STR R2,[R4]
BX LR

VALUE1 DCD 0X44444444;


DCD 0X22222222;
DCD 0X11111111;
DCD 0X33333333;
DCD 0XAAAAAAAA;
DCD 0X99999999;

AREA DATA6, DATA, READWRITE


RESULT DCD 0X00000000
END

Output: The result is stored in memory location RESULT


6. Write an ALP to count number of ones and zeros in two consecutive memory locations.

AREA ONEZERO,CODE, READONLY


ENTRY
START
MOV R2,#0
MOV R3,#0
MOV R7,#2
LDR R6,=VALUE
LOOP MOV R1,#32
LDR R0,[R6],#4
LOOP0 MOVS R0,R0,ROR #1
BHI ONES
ZEROS ADD R3,R3,#1
B LOOP1
ONES ADD R2,R2,#1
LOOP1 SUBS R1,R1,#1
BNE LOOP0
SUBS R7,R7,#1
CMP R7,#0
BNE LOOP
BX LR

VALUE DCD 0X00000001, 0X000000AA


END
Output: The result is stored in memory location RESULT

7. Simulate a programin C for ARM microcontroller using KEIL to sort the numbers in
ascending/descending
order using bubble sort.

#include <lpc214x.h>
#include <stdio.h>

void UART0_Init(void) {
PINSEL0 |= 0x00000005; // Enable UART0 TXD and RXD
U0LCR = 0x83; // 8-bit data, 1 stop bit, no parity, enable DLAB
U0DLL = 97; // Baud rate 9600 for 15MHz PCLK
U0DLM = 0;
U0LCR = 0x03; // Disable DLAB
}

void UART0_SendChar(char ch) {


while (!(U0LSR & 0x20)); // Wait until THR is empty
U0THR = ch;
}

void UART0_SendString(char *str) {


while (*str) {
UART0_SendChar(*str++);
}
}

void delay(void) {
volatile int i; // Declare 'i' outside the loop
for (i = 0; i < 100000; i++);
}

void bubbleSort(int arr[], int n, int order) {


int i, j, temp;
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; j++) {
if ((order == 1 && arr[j] > arr[j + 1]) || (order == 0 && arr[j] < arr[j + 1]))
{
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

int main(void) {
int numbers[] = {34, 7, 23, 32, 5, 62};
int n = sizeof(numbers) / sizeof(numbers[0]);
int order = 1; // 1 for ascending, 0 for descending
int i; // Declare 'i' at the start of the function

UART0_Init(); // Initialize UART


UART0_SendString(" Before Sorting Numbers:\r\n");
for (i = 0; i < n; i++) {
char buffer[10];
sprintf(buffer, "%d\r\n", numbers[i]);
UART0_SendString(buffer);
}

UART0_SendString(" After Sorting Numbers:\r\n");

// Sorting the numbers


bubbleSort(numbers, n, order);

// Send sorted numbers via UART


for (i = 0; i < n; i++) {
char buffer[10];
sprintf(buffer, "%d\r\n", numbers[i]);
UART0_SendString(buffer);
}

while (1) {

delay();
}
}
LAB MANUAL CSE DEPT

8. Simulate a program in C for ARM microcontroller to find factorial of a number

#include <lpc214x.h>

void UART0_Init(void);
void UART0_TxChar(char ch);
char UART0_RxChar(void);
void UART0_SendString(char *str);
unsigned int factorial(unsigned int n);

int main()
{
char num;
unsigned int fact;
char buffer[20];
UART0_Init();
UART0_SendString("Enter a number: ");
num = UART0_RxChar() - '0'; // Receive single-digit number
UART0_TxChar(num + '0'); // Echo back
UART0_SendString("\r\n");
fact = factorial(num);
sprintf(buffer, "Factorial: %u\r\n", fact);
UART0_SendString(buffer);
while(1);
}

void UART0_Init(void)
{
LAB MANUAL CSE DEPT

PINSEL0 |= 0x00000005; // Enable TxD0 and RxD0


U0LCR = 0x83; // 8-bit, no parity, 1 stop bit, DLAB=1
U0DLL = 97; // Baud rate 9600 for 15MHz PCLK
U0LCR = 0x03; // DLAB=0
}

void UART0_TxChar(char ch)


{
while (!(U0LSR & 0x20)); // Wait until THR is empty
U0THR = ch;
}

char UART0_RxChar(void)
{
while (!(U0LSR & 0x01)); // Wait until RBR has data
return U0RBR;
}

void UART0_SendString(char *str)


{
while (*str)
{
UART0_TxChar(*str++);
}
}

unsigned int factorial(unsigned int n)


{
unsigned int fact = 1, i;
for (i = 1; i <= n; i++)
{
fact *= i;
}
return fact;
}
LAB MANUAL CSE DEPT

9. Simulate a program in C for ARM microcontroller to demonstrate case conversion of characters


from upper to lowercase and lower to uppercase.

#include <lpc214x.h>
void UART0_Init(void);
void UART0_TxChar(char ch);
char UART0_RxChar(void);
void UART0_SendString(char *str);
char convertCase(char ch);

int main()
{
char ch, convertedCh;
UART0_Init();
UART0_SendString("Enter a character: ");

while (1)
{
ch = UART0_RxChar(); // Receive character
UART0_TxChar(ch); // Echo received character
UART0_SendString("\r\n");

convertedCh = convertCase(ch);
UART0_SendString("Converted character: ");
UART0_TxChar(convertedCh);
UART0_SendString("\r\n");
}
}

void UART0_Init(void)
{
PINSEL0 |= 0x00000005; // Enable TxD0 and RxD0
U0LCR = 0x83; // 8-bit, no parity, 1 stop bit, DLAB=1
U0DLL = 97; // Baud rate 9600 for 15MHz PCLK
U0LCR = 0x03; // DLAB=0
}

void UART0_TxChar(char ch)


{
while (!(U0LSR & 0x20)); // Wait until THR is empty
U0THR = ch;
}

char UART0_RxChar(void)
{
LAB MANUAL CSE DEPT

while (!(U0LSR & 0x01)); // Wait until RBR has data


return U0RBR;
}

void UART0_SendString(char *str)


{
while (*str)
{
UART0_TxChar(*str++);
}
}

char convertCase(char ch)


{
if (ch >= 'A' && ch <= 'Z')
return ch + 32; // Convert uppercase to lowercase
else if (ch >= 'a' && ch <= 'z')
return ch - 32; // Convert lowercase to uppercase
else
return ch; // Return unchanged if not a letter
}

10. Demonstrate enabling and disabling of Interrupts in ARM.

#include <LPC214x.h>

void UART0_Init(void);
void UART0_SendString(char *str);
void delayMs(unsigned int ms);
void disableIRQ(void);
void enableIRQ(void);
void init_timer0(void);
__irq void Timer0_ISR(void);

int main(void)
{
UART0_Init();
UART0_SendString("Starting Program...\r\n");

init_timer0();

disableIRQ(); // Disable interrupts initially


UART0_SendString("IRQs disabled. Waiting 5 seconds...\r\n");
delayMs(5000); // 5-second delay
LAB MANUAL CSE DEPT

UART0_SendString("Enabling IRQs...\r\n");
enableIRQ(); // Enable interrupts

while (1);
}

void init_timer0(void)
{
T0PR = 15000 - 1; // 1ms tick (for 15MHz PCLK)
T0MR0 = 1000; // 1 second
T0MCR = 3; // Interrupt + Reset on MR0
T0TCR = 1; // Timer0 Enable

VICVectAddr0 = (unsigned long)Timer0_ISR;


VICVectCntl0 = 0x20 | 4; // Enable Slot 0 for Timer0
VICIntEnable |= (1 << 4); // Enable Timer0 interrupt
}

__irq void Timer0_ISR(void)


{
UART0_SendString("Timer0 Interrupt Triggered!\r\n");
T0IR = 1; // Clear interrupt flag
VICVectAddr = 0; // Acknowledge
}

void UART0_Init(void)
{
PINSEL0 |= 0x00000005; // Enable TxD0 and RxD0
U0LCR = 0x83; // 8-bit, no parity, 1 stop bit, DLAB = 1
U0DLL = 97; // Baud = 9600 (for 15MHz PCLK)
U0LCR = 0x03; // DLAB = 0
}

void UART0_SendString(char *str)


{
while (*str)
{
while (!(U0LSR & 0x20)); // Wait until THR is empty
U0THR = *str++;
}
}
LAB MANUAL CSE DEPT

void delayMs(unsigned int ms)


{
unsigned int i, j;
for (i = 0; i < ms; i++)
for (j = 0; j < 6000; j++);
}

void disableIRQ(void)
{
__asm {
MRS R0, CPSR
ORR R0, R0, #0x80
MSR CPSR_c, R0
}
}

void enableIRQ(void)
{
__asm {
MRS R0, CPSR
BIC R0, R0, #0x80
MSR CPSR_c, R0
}
}
LAB MANUAL CSE DEPT

11. Demonstrate the handling of divide by zero, Invalid Operation and Overflow
exceptions in ARM.
#include <lpc214x.h>
#include <stdio.h>
#include <limits.h>

void UART0_Init(void);
void UART0_TxChar(char ch);
void UART0_SendString(char *str);
int divide(int a, int b);
void checkOverflow(int a, int b);

int main()
{
int a = 10, b = 0, result;
char buffer[50];
volatile int *invalid_ptr = (int *)0xFFFFFFFF; // Move declaration to the start

UART0_Init();
UART0_SendString("ARM Exception Handling Demo\r\n");

// Handling Divide by Zero Exception


if (b == 0)
{
UART0_SendString("Error: Divide by zero exception!\r\n");
}
else
{
result = divide(a, b);
sprintf(buffer, "Result: %d\r\n", result);
UART0_SendString(buffer);
}

// Handling Invalid Operation (e.g., accessing invalid memory)


if (invalid_ptr == (int *)0xFFFFFFFF)
{
UART0_SendString("Error: Invalid Operation Exception!\r\n");
}

// Handling Overflow Exception


checkOverflow(INT_MAX, 1);
LAB MANUAL CSE DEPT

while (1);
}

int divide(int a, int b)


{
return a / b;
}

void checkOverflow(int a, int b)


{
int result;
char buffer[50];

if ((b > 0 && a > (INT_MAX - b)) || (b < 0 && a < (INT_MIN - b)))
{
UART0_SendString("Error: Overflow Exception!\r\n");
}
else
{
result = a + b;
sprintf(buffer, "Sum: %d\r\n", result);
UART0_SendString(buffer);
}
}

void UART0_Init(void)
{
PINSEL0 |= 0x00000005; // Enable TxD0 and RxD0
U0LCR = 0x83; // 8-bit, no parity, 1 stop bit, DLAB=1
U0DLL = 97; // Baud rate 9600 for 15MHz PCLK
U0LCR = 0x03; // DLAB=0
}

void UART0_TxChar(char ch)


{
while (!(U0LSR & 0x20)); // Wait for THR empty
U0THR = ch;
}

void UART0_SendString(char *str)


{
while (*str)
{
UART0_TxChar(*str++);
}
}

You might also like