Microprocessors Lab Manual Part A Software Programs

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 41

MICROPROCESSORS

LAB MANUAL

PART A
SOFTWARE PROGRAMS
1. Design and develop an assembly language program to search a key element “X” in a list
of ‘n’ 16-bit numbers. Adopt Binary search algorithm in your program for searching.

.data
array dw 1234h,2345h,3456h,4567h,5678h
len dw ($-array)/2
srchkey equ 0456h
msg1 db "Element Found ",'$'
msg2 db "Element Not Found ",'$'

.code
start: mov ax,@data
mov ds,ax
mov bx,01
mov cx,srchkey
mov dx,len

again: cmp bx,dx


ja failure
mov ax,dx
add ax,bx
shr ax,01
mov si,ax
dec si
add si,si
cmp cx,array[si]
jae bigger
dec ax
mov dx,ax
jmp again

bigger:je success
inc ax
mov bx,ax
jmp again

success:lea dx,msg1
jmp display
failure:lea dx,msg2
display:mov ah,09h
int 21h
mov ah,4ch
int 21h
end

Sri Venkateshwara College of Engineering, Bengaluru 2


Sri Venkateshwara College of Engineering, Bengaluru 3
2. Design and develop an assembly program to sort a given set of ‘n’ 16-bit numbers in
ascending order. Adopt Bubble Sort algorithm to sort given elements.

.data
array dw 1010h,2511h,1222h,1643h,5432h
len db $-array

code segment
assume cs:code,ds:data
start: mov ax,data
mov ds,ax
mov cl,len
dec cl

up2: lea si,array


mov ch,cl
mov dl,0

up1: mov ax,[si]


cmp ax,[si+2]
jbe nochange
xchg al,[si+2]
mov [si],ax
mov dl,1

nochange:inc si
inc si
dec ch
jnz up1
cmp dl,0
je last
dec cl
jnz up2

last: mov ah,4ch


int 21h

end

Sri Venkateshwara College of Engineering, Bengaluru 4


Sri Venkateshwara College of Engineering, Bengaluru 5
3. Develop an assembly language program to reverse a given string and verify whether it
is a palindrome or not. Display the appropriate message.

Printf macro msg


lea dx, msg
mov ah,09h
int 21h
enm

.data
str_read db "Enter the String : ",'$'
str1 db 255 dup('$')
str_rev db "Reveresed String is : ",'$'
str2 db "String is Palindrome",'$'
str3 db "String is not Palindrome",'$'
newline db 13,10,'$'

.code
start: mov ax,@data
mov ds,ax
mov si,0
Printf str_read

stdin: mov ah,01h


int 21h
cmp al,0dh
jz newline1
mov str1[si],al
inc si
jmp stdin

newline1: Printf newline


mov cx,si
mov di,cx
dec di
Printf str_rev

print: mov dl,str1[di]


mov ah,02h
int 21h
dec di
loop print
mov cx,si
mov di,cx
mov si,0
dec di

Sri Venkateshwara College of Engineering, Bengaluru 6


stdout: mov bl,str1[si]
mov bh,str1[di]
cmp bl,bh
jnz notpal
inc si
dec di
loop stdout

Printf newline
Printf str2
jmp stop

notpal: Printf newline


Printf str3

stop: mov ah,4ch


int 21h

end

Sri Venkateshwara College of Engineering, Bengaluru 7


4. Develop an assembly language program to compute nCr using recursive procedure.
Assume that ‘n’ and ‘r’ are non-negative integers.

.data
n dw 6
r dw 4
ncr dw 1
msg db "the ncr is :$"

.code segment
start:
mov ax,@data
mov ds,ax
mov bx,n
inc bx
mov cx,r
call ncp
lea dx,msg
mov ah,09h
int 21h
mov ax,ncr
call display
mov ah,4ch
int 21h
ncp proc near
cmp cx,00h
je l1
push cx
dec cx
call ncp
mov ax,bx
pop cx
sub ax,cx
mul ncr
div cx
mov ncr,ax
l1: ret
ncp endp

display proc
aam
add ax,3030h
mov bx,ax
mov dl,bh
mov ah,02h
int 21h

Sri Venkateshwara College of Engineering, Bengaluru 8


mov dl,bl
mov ah,02h
int 21h
ret
display endp

end

Sri Venkateshwara College of Engineering, Bengaluru 9


5. Design and develop an assembly language program to read the current time and
Datefrom the system and display it in the standard format on the screen.

.data
msg db 'the current time is : $'
msg1 db 10,13,'the current date is : $'

.code
start: mov ax,@data
mov ds,ax ;initialise ds
lea dx,msg ;display the message
mov ah,09h
int 21h
mov ah,2ch ;dos function to get the current system time
int 21h
mov al,ch ; CH stores the hours returned
call display
mov dl,':’ ; display the char ':'
mov ah,02h
int 21h
mov al,cl ; CL stores the minute returned
call display
mov dl,':' ; display the char ':'
mov ah,02h
int 21h
mov al,dh ; DH stores the second returned
call display

lea dx,msg1 ;display the message


mov ah,09h
int 21h
mov ah,2ah ;dos function to get the current system time
int 21h
mov bx,dx
mov al,bl ; BL stores the Date returned
call display
mov dl,'/’ ; display the char ':'
mov ah,02h
int 21h
mov al,bh ; BH stores the Month returned
call display
mov dl,'/' ; display the char ':'
mov ah,02h
int 21h

Sri Venkateshwara College of Engineering, Bengaluru 10


add cx,18F0h ; CX stores the Year returned
mov al,cl
call display

mov ah,4ch ;dos function to terminate


int 21h

display proc ;function to display a character on


aam ;bcd adjust after multiply
mov bx,ax
add bx,3030h
mov dl,bh ;the standard o/p device
mov ah,02h
int 21h
mov dl,bl
mov ah,02h
int 21h
ret
display endp

end

Sri Venkateshwara College of Engineering, Bengaluru 11


6. To write and simulate ARM assembly language programs for data transfer,
arithmetic and logical operations (Demonstrate with the help of a suitable program).

AREA PROGRAM,CODE,READONLY
START
LDR R1,VALUE1
LDR R2,VALUE2
LDR R3,VALUE3
LDR R4,VALUE4
LDR R5,VALUE5
MOV R6,R1
ADD R7,R1,R2
ADD R8,R3,#1
ADDS R9,R4,R5
ADC R10,R1,R2
SUB R11,R2,#1
MUL R12,R1,R2
AND R5,R1,R2
ORR R6,R1,R2
EOR R7,R1,R2
STR R1,RESULT
SWI &11
VALUE1 DCD &00000001
VALUE2 DCD &00000002
VALUE3 DCD &00000003
VALUE4 DCD &00000004
VALUE5 DCD &FFFFFFFF
RESULT DCD 0
END

Sri Venkateshwara College of Engineering, Bengaluru 12


7. To write and simulate C Programs for ARM microprocessor using KEIL
(Demonstrate with the help of a suitable program)

#include<lpc21xx.h>

int main(void)

{
int var1=10;
int var2=20;
int sum, diff, mu, div;
sum = var1 + var2;
diff = var1 - var2;
mu = var1*var2;
div=var1/var2;

Sri Venkateshwara College of Engineering, Bengaluru 13


CONTENT BEYOND SYLLABUS

Write two ALP modules stored in two different files; one module is to read a character
from the keyboard and the other one is to display a character. Use the above two modules
to read a string of characters from the keyboard terminated by the carriage return and
print the string on the display in the next line.

; Module 1 from which we are going to read the character from the standard output
; Name should became as by which we are going to include in the 3 module

readkb macro
mov ah,01h
int 21h
endm

; Module 2 from which we are going to write it onto the standard output
; Name should became as by which we are going to include in the 3 module
writeout macro
mov dl,al
mov ah,02h
int 21h
endm

; Module 3 from where we are going to read and write the Program from the Standard
output and Standard input same the file differently.

data segment
str1 db 255 dup(?)
newline db 13,10,'$'
data ends

code segment
assume cs:code,ds:data
start: include c:\masm\read.asm
include c:\masm\write.asm
mov ax,data
mov ds,ax
mov si,0
stdin:readkb
cmp al,0dh
jz newline1
mov str1[si],al

Sri Venkateshwara College of Engineering, Bengaluru 14


inc si
jmp stdin
newline1: lea dx,newline
mov ah,09h
int 21h
mov cx,si
mov si,0
stdout: mov al,str1[si]
writeout
inc si
loop stdout
mov ah,4ch
int 21h
code ends
end start

Sri Venkateshwara College of Engineering, Bengaluru 15


Read two strings, store them in locations STR1 and STR2. Check whether they are equal
or not and display appropriate messages. Also display the length of the stored strings.

printf macro msg


lea dx,msg
mov ah,09h
int 21h
endm

data segment
f_str db 255 dup('$')
s_str db 255 dup('$')
first_str_msg db " Enter the First String : ", '$'
second_str_msg db " Enter the Second String : ", '$'
str_equal db "Both String are Equal",'$'
str_not_equal db "Both String are not equal",'$'
first_str_length db "Length of First String : ",'$'
second_str_length db "Length of Second String : ",'$'
newline db 13,10,'$'
data ends

code segment
assume cs:code,ds:data
start: mov ax,data
mov ds,ax
mov si,0
printf first_str_msg

stdin1: mov ah,01h


int 21h
cmp al,0dh
jz second
mov f_str[si],al
inc si
jmp stdin1

second: printf newline


printf second_str_msg
mov di,0
stdin2: mov ah,01h
int 21h
cmp al,0dh
jz newline1
mov s_str[di],al
inc di
jmp stdin2

Sri Venkateshwara College of Engineering, Bengaluru 16


;Print Both the String

newline1: printf newline


printf first_str_length

mov ax,si
call display
printf newline
printf second_str_length
mov ax,di
call display

;Compare the length of the String


cmp di,si;
jnz exit
;Comparing the string character by character
mov cx,di
mov si,0

p_l_s: mov al,f_str[si]


mov ah,s_str[si]
cmp al,ah
jnz exit
loop p_l_s

printf newline
printf str_equal
jmp end1

exit : printf newline


printf str_not_equal

end1: mov ah,4ch


int 21h

;display proc

display proc
aam
add ax,3030h
mov bx,ax
mov dl,bh
mov ah,02h
int 21h
mov dl,bl

Sri Venkateshwara College of Engineering, Bengaluru 17


mov ah,02h
int 21h
ret
display endp

code ends
end start

Sri Venkateshwara College of Engineering, Bengaluru 18


MICROPROCESSORS
LAB MANUAL
HARDWARE PROGRAMS

PART B

Sri Venkateshwara College of Engineering, Bengaluru 19


8. a. Design and develop an assembly program to demonstrate BCD Up-Down
Counter (00-99) on the Logic Controller Interface.

PRINTF MACRO MSG


LEA DX, MSG ; to tell what function its doing print MSG
MOV AH, 09H
INT 21H
ENDM

DATA SEGMENT
MSG1 DB "STIMULATING BCD UP_DOWN COUNTER $"
NEWLINE DB 13,10,’$’
DATA ENDS

CODE SEGMENT
ASSUME CS:CODE, DS:DATA

START : MOV AX,DATA ; initialize the data segment


MOV DS,AX

PRINTF MSG1
MOV AL,82H ; move the control word into CWR
MOV DX,0D483H
OUT DX,AL
MOV DX,0D480H ; port A used as o/p port

MOV AL,0
UP_CNTR: OUT DX,AL ; o/p the count
CMP AL,100 ; if count >=9 start down counter
JZ NEXT1
INC AL ; else increment AL and loop back
CALL DELAY ; wait after each display
JMP UP_CNTR

NEXT1: MOV AL,99 ; start counter from 9


DOWN_CNTR: OUT DX,AL ; o/p the count
CMP AL,00H ; if count is <=0 stop down counter and exit
JZ NEXT2
DEC AL ; else dec al and loop back
CALL DELAY ; wait after each display
JMP DOWN_CNTR

NEXT2: MOV AH,4CH ; return to DOS


INT 21H

DELAY PROC
PUSH BX
PUSH CX
MOV CX,4FFFH
LOOP2:MOV BX,0FFFFH
LOOP1:DEC BX
JNZ LOOP1
LOOP LOOP2
POP CX
POP BX

Sri Venkateshwara College of Engineering, Bengaluru 20


RET
DELAY ENDP
CODE ENDS
END START

Sri Venkateshwara College of Engineering, Bengaluru 21


b. Design and develop an assembly program to read the status of two 8-bit inputs
(X & Y) from the Logic Controller Interface and display X*Y.
PRINTF MACRO MSG
LEA DX,MSG ; to tell what function its doing print MSG
MOV AH,09H
INT 21H
ENDM

DATA SEGMENT
PORTA EQU 0D480H
PORTB EQU 0D481H
PORTC EQU 0D482H
CWR EQU 0D483H
MSG1 DB "SET THE FIRST 8 BIT NUMBER AND PRESS ENTER",'$'
MSG2 DB "SET THE SECOND 8 BIT NUMBER AND PRESS ENTER",'$'
NEWLINE 13,10,'$'
DATA ENDS

CODE SEGMENT
ASSUME CS:CODE, DS:DATA
START: MOV AX,DATA ; initialize the data segment
MOV DS,AX
MOV DX, CWR
MOV AL, 82H ; move the control word into CWR
OUT DX,AL
PRINTF MSG1
MOV AH,01H
INT 21H

MOV DX,PORTB
IN AL,DX ; port B used as i/p port for 8-bit input
MOV BL,AL
PRINTF NEWLINE ; wait for sometime
PRINTF MSG2
MOV AH,01H
INT 21H

MOV DX,PORTB ;port B used as i/p port for 2nd 8-bit data
IN AL,DX

MUL BL ;multiply the contents of al and bl and result stored


MOV BX,AX ;in ax and temporarily move the data to bx register

MOV DX,PORTA ;Display the lower 8-bit of multiplication stored in


MOV AL,BL ;BX register
OUT DX,AL

MOV AH,01H
INT 21H

MOV DX,PORTA ;Display the upper 8-bit of multiplication


MOV AL,BH ;BX register

Sri Venkateshwara College of Engineering, Bengaluru 22


OUT DX,AL

MOV AH,4CH
INT 21H
CODE ENDS
END START

Sri Venkateshwara College of Engineering, Bengaluru 23


9. Design and develop an assembly program to display messages “FIRE” and
“HELP”a alternately with flickering effects on a 7-segment display interface for a
suitable period of time. Ensure a flashing rate that makes it easy to read both the
messages (Examiner does not specify these delay values nor is it necessary for
the student to compute these values).
PROGRAM

DATA SEGMENT

PORTA EQU 0D480H


PORTB EQU 0D481H
PORTC EQU 0D482H
CWR EQU 0D483H
FIRE DB 00H, 71H, 06H, 77H, 79H, 00H
HELP DB 00H, 76H, 79H, 38H, 73H, 00H
BLANK DB 00H, 00H, 00H, 00H, 00H, 00H
ADDR DW ?

DATA ENDS

CODE SEGMENT
ASSUME CS: CODE, DS: DATA
START: MOV AX, DATA
MOV DS, AX
MOV AL, 80H
MOV DX, CWR
OUT DX, AL

LP1: LEA DX, FIRE


MOV ADDR, DX
CALL DISPLAY

LEA DX, BLANK


MOV ADDR, DX
CALL DISPLAY

LEA DX, HELP


MOV ADDR, DX
CALL DISPLAY

LEA DX, BLANK


MOV ADDR, DX
CALL DISPLAY

MOV AH, 0BH


INT 21H
OR AL, AL
JZ LP1
MOV AH, 4CH
INT 21H

DISPLAY PROC

Sri Venkateshwara College of Engineering, Bengaluru 24


PUSH CX

MOV DI, 1500


LP2: MOV SI, ADDR
MOV BL, 5
LP3: MOV AL, BL
MOV DX, PORTC
OUT DX, AL
MOV DX, PORTA
MOV AL, [SI]
INC SI
OUT DX, AL
MOV CX, 0FFFFH
HERE: LOOP HERE
DEC BL
JNZ LP3
DEC DI
JNZ LP2

POP CX
RET
DISPLAY ENDP

CODE ENDS
END START

Sri Venkateshwara College of Engineering, Bengaluru 25


10. Design and develop an assembly program to drive a Stepper Motor interface
and rotate the motor in specified direction (clockwise or counter-clockwise) by N
steps (Direction and N are specified by the examiner). Introduce suitable delay
between successive steps. (Any arbitrary value for the delay may be assumed by
the student).

PROGRAM

DATA SEGMENT

PORTA EQU 0D480H


PORTB EQU 0D481H
PORTC EQU 0D482H
CWR EQU 0D483H

DATA ENDS

CODE SEGMENT
ASSUME CS: CODE, DS: DATA
START: MOV AX, DATA
MOV DS, AX
MOV AL, 80H
MOV DX, CWR
OUT DX, AL

MOV CX, 15 ; NUMBER OF ROTATION


MOV AL, 33H

L1: MOV DX, PORTC


OUT DX, AL
CALL DELAY
ROR AL, 1 ; FOR COUNTER CLOCKWISE USE ROTATE LEFT(ROL)
LOOP L1

MOV AH, 4CH


INT 21H

DELAY PROC
PUSH CX
PUSH BX

MOV CX, 7FFFH


UP2:MOV BX, 0FFFFH
UP1:DEX BX
JNZ UP1
LOOP UP2

POP BX
POP CX

RET
DELAY ENDP

Sri Venkateshwara College of Engineering, Bengaluru 26


CODE ENDS
END START
11. Design and develop an assembly language program to

a. Generate the Sine Wave using DAC interface (The output of the DAC is to
be displayed on the CRO).

Calculate table values using the formula x[i]=128+127 SinӨ ( Equation to generate sine
wave) Where Ө(Theta ranges from0 to 180 in steps of 5 degree)

I Ө 128+127 SinӨ I Ө 128+127 SinӨ


1 0 37 180
2 5 38 185
3 10 39 190
4 15 40 200
5 20 41 205
6 25 42 210
7 30 43 215
8 35 44 220
9 40 45 225
10 45 46 230
11 50 47 235
12 55 48 240
13 60 49 245
14 65 50 250
15 70 51 255
16 75 52 260
17 80 53 265
18 85 54 270
19 90 55 275
20 95 56 280
21 100 57 285
22 105 58 290
23 110 59 295
24 115 60 300
25 120 61 305
26 125 62 310
27 130 63 315
28 135 64 320
29 140 65 325
30 145 66 330
31 150 67 335
32 155 68 340
33 160 69 345
34 165 70 350

Sri Venkateshwara College of Engineering, Bengaluru 27


35 170 71 355
36 175 72 360
Program:

DATA SEGMENT
PORTA EQU 0D480H
PORTB EQU 0D481H
PORTC EQU 0D482H
CWR EQU 0D483H
X DB ___,___,___,___,___,___,___,___,___
DB ___,___,___,___,___,___,___,___,___
DB ___,___,___,___,___,___,___,___,___
DB ___,___,___,___,___,___,___,___,___
DB ___,___,___,___,___,___,___,___,___
DB ___,___,___,___,___,___,___,___,___
DB ___,___,___,___,___,___,___,___,___
DB ___,___,___,___,___,___,___,___,___
LEN DW $-X
DATA ENDS

CODE SEGMENT
ASSUME CS: CODE, DS: DATA
START: MOV AX, DATA ; initialize data segment
MOV DS, AX

MOV AL, 80H ; 80h is control word


MOV DX, CWR ; control register address
OUT DX, AL ; control word is moved to control register

BEGIN:MOV DX, PORTA ; Port A address


LEA SI, X ; SI points to 1st position of X
MOV CX, LEN ; total number of values is moved to CX

BACK: MOV AL,[SI]


OUT DX, AL
INC SI ; increment SI

MOV BX, 0FFFFH ; Delay for Sometime


UP: DEC BX
JNZ UP

LOOP BACK ; loop – decrements the CX register value

MOV AH, 0BH


INT 21H
OR AL,AL
JZ BEGIN ; if no key is pressed goes to begin

MOV AH, 4CH ; terminate the program


INT 21H
CODE ENDS ; end of program
END START

Sri Venkateshwara College of Engineering, Bengaluru 28


b) Generate a Half Rectified Sine wave form using the DAC interface. (The output
of the DAC is to be displayed on the CRO).

Algorithm: Calculate table values using the formula table[i]=128+127 SinӨ Where Ө ranges
from0 to 180. refer the same table given in program 9b.

Step1: Initialize the control word i.e 80H


Step2: Store the control word in the Control register and initialize the Port A address
Step3: Initialize the counter to X (Total number of elements in table)
Step4: For (i=0 to 35)
Outport(Port A, table[i]); /* will get +ve half cycle of sine wave
For(i=0 to 35)
Outport(Port A, 128); /* will get -ve half cycle of sine wave will be blank
Step 5: Stop.

Note: Connections to be done: same as 11a


PROGRAM

DATA SEGMENT
PORTA EQU 0D480H
PORTB EQU 0D481H
PORTC EQU 0D482H
CWR EQU 0D483H
X DB ___,___,___,___,___,___,___,___,___
DB ___,___,___,___,___,___,___,___,___
DB ___,___,___,___,___,___,___,___,___
DB ___,___,___,___,___,___,___,___,___
DB ___,___,___,___,___,___,___,___,___
DB ___,___,___,___,___,___,___,___,___
DB ___,___,___,___,___,___,___,___,___
DB ___,___,___,___,___,___,___,___,___
LEN DW $-X
DATA ENDS

CODE SEGMENT
ASSUME CS: CODE, DS: DATA
START: MOV AX, DATA ; initialize data segment
MOV DS, AX
MOV AL, 80H ; 80h is control word
MOV DX, CWR ; control register address
OUT DX, AL ; control word is moved to control register

BEGIN:MOV DX, PORTA ; Port A address


LEA SI, X ; SI points to 1st position of X
MOV CX, LEN ; total number of values is moved to CX
BACK: MOV AL,[SI]
OUT DX, AL

Sri Venkateshwara College of Engineering, Bengaluru 29


INC SI ; increment SI

MOV BX, 0FFFFH ; Delay for Sometime


UP: DEC BX
JNZ UP

LOOP BACK ; loop – decrements the CX register value

MOV AH, 0BH


INT 21H
OR AL, AL
JZ BEGIN ; if no key is pressed goes to begin

MOV AH, 4CH ; terminate the program


INT 21H

CODE ENDS ; end of program


END START

Sri Venkateshwara College of Engineering, Bengaluru 30


12. To interface LCD with ARM processor-- ARM7TDMI/LPC2148. Write and execute
programs in C language for displaying text messages and numbers on LCD

// Program to Display "LCD 16x2 DEMO" & "SILICON" on LCD Display


// PINOUT: LCD - LPC2148
// ----------------
// DB4 - P0.20
// DB5 - P0.21
// DB6 - P0.22
// DB7 - P0.23
// RS - P0.28
// E - P0.30
// R/W - GND
//***************************************************************************
//
**************************INCLUDES******************************************
**
#include "config.h"
//**********************End of
INCLUDES*****************************************

//************************Private Protoype**************************************
void _DelayMs(unsigned int delay);
//*******************End of Private Protoype************************************
//**************************************************************************
// Function: void main(void)
// Input: None
// Output: None
// Overview: Main program entry point.
// Note: None
//**************************************************************************

int main(void)
{
char silicon[] = "9964500991";
PINSEL0 = 0;
PINSEL1 = 0;
PINSEL2 &= 0x0000000C;
PINSEL2 |= 0x00000030;
ClcdInit();
ClcdCursorOff();
while(1)

Sri Venkateshwara College of Engineering, Bengaluru 31


{
ClcdClear();
ClcdGoto(1,1);
ClcdPutS_P(" Kirankumar B ");
ClcdGoto(1,2);
ClcdPutS(silicon);
_DelayMs(2000);
}
}
//
*****************************************************************************
// Function: void _DelayMs(unsigned int count)
// Input: count in milliseconds
// Output: None
// Overview: Delay in milliseconds.
// Note: None
//
*****************************************************************************
void _DelayMs(unsigned int count)
{
volatile unsigned int j,k;
for (j=0;j<count;j++)
{
for (k=0;k<6000;k++)
{
__asm
{
nop;
nop;
}
}
}
}

Sri Venkateshwara College of Engineering, Bengaluru 32


13. To interface Stepper motor with ARM processor-- ARM7TDMI/LPC2148. Write a
program to rotate stepper motor

//Connect J3 of SiMS-LPC2148 to J1 OF SiMS-COMBO-MOTRRLY


#include <LPC21xx.h>
/* Declare function prototypes */
void _DelayMs(unsigned int count);
/************************* MAIN *************************/
int main (void)
{
IO0DIR |= ((1<<16)|(1<<17)|(1<<18)|(1<<19)|(1<<20)|(1<<21)|(1<<22)|(1<<23)); //
Configuring
P0.16..P0.23 as Output
while (1) /* Loop forever */
{
IOSET0 = 0x00060000 ; /* Turn ON Windings A and B */
_DelayMs(10); /* Add some delay */
IOCLR0 = 0x00060000 ;

IOSET0 = 0x000a0000 ; /* Turn ON Windings B and C */


_DelayMs(10); /* Add some delay */
IOCLR0 = 0x000a0000 ;

IOSET0 = 0x00090000 ; /* Turn ON Windings C and D*/


_DelayMs(10); /* Add some delay */
IOCLR0 = 0x00090000 ;

IOSET0 = 0x00050000 ; /* Turn ON Windings A and D*/


_DelayMs(10); /* Add some delay */
IOCLR0 = 0x00050000 ;

}
}

/* delay function */
void _DelayMs(unsigned int count)
{
volatile unsigned int j,k;
for (j=0;j<count;j++)
{

Sri Venkateshwara College of Engineering, Bengaluru 33


for (k=0;k<6000;k++)
{
__asm
{
nop;
nop;
}
}
}
}

Sri Venkateshwara College of Engineering, Bengaluru 34


CONTENT BEYOND SYLLABUS
1. INTERFACING OF REAL TIME CLOCK AND SERIAL PORT

AIM:

To develop and verify the interfacing of real time clock and serial port with ARM

DEVELOPMENT KIT:

Microcontroller using embedded C program.

APPARATUS REQUIRED:

 ARM Development Kit LPC2148 1


 Keil μVision3 IDE 1

PROCEDURE:

Step 1: Give a double click on μvision 4 icon on the desk top, it will generate a window as shown
below
Step 2: To create new project go to project select new micro vision project.
Step 3: select a drive where you would like to create your project.
Step 4: Create a new folder and name it with your project name.
Step 5: Open that project folder and give a name of your project executable and save it.
Step 6: After saving it will show some window there you select your microcontroller company
i.e NXP from Phillips
Step 7: Select your chip as ARM DEVELOPMENT KIT
Step 8: After selecting chip click on OK then it will display some window asking to add
STARTUP . Select YES.
Step 9: A target is created and startup is added to your project target and is shown below.
Step 10: To write your project code select a new from menu bar.
Step 11: It will display some text editor, to save that select SAVE option from menu bar.
Step 12: By giving a name with extension .c and save it
Step 13: To add our c to target give a right click on Source Group, choose “ADD s to Group”
option.
Step 14:It will displays some window there select the you have to add and click on ADD option.

Sri Venkateshwara College of Engineering, Bengaluru 35


Step 15: It will be added to our target and it shows in the project window.
Step 16: Now give a right click on target in the project window and select “Options for Target”
Step 17: It will show some window, in that go to output option and choose Create Hex option by
selecting that box
Step 18: In the same window go to Linker option and choose Use Memory Layout from Target
Dialog by selecting the box, and click OK.
Step 19: Now to compile your project go to Project select Build Target option or press F7.
Step 20: Ensure the real time clock, displaying in output window.
PROGRAM:

RTC:

#include <LPC213x.h>
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include "UART.h"
#define BUZZ 7

//Buzzer Connected to P0.7

void UART1_ISR(void)__irq;
void RTC_ISR (void)__irq;
unsigned char Flag=0;
char Key;

void DelayMs(long ms) // delay 1 ms per count @ CCLK 60 MHz


{
long i,j;
for (i = 0; i < ms; i++ )
for (j = 0; j < 6659; j++ );
}

void Initialize(void)
{
PLLCFG = 0x24; // M : 4 | P = 2 -> Fosc = 12MHz &
CCLK = 60MHz
PLLFEED = 0xAA; // Feed Process
PLLFEED = 0x55;
PLLCON = 0x01;
PLLFEED = 0xAA; // Feed Process
PLLFEED = 0x55;
while (!(PLLSTAT & 0x400)); //Wait untill PLL is Locked!
PLLCON = 0x03; //Connect PLL as the Clock Source for Microcontroller
PLLFEED = 0xAA; // Feed Process
PLLFEED = 0x55;

Sri Venkateshwara College of Engineering, Bengaluru 36


MAMCR = 0x02; //Memory Accerleration Module Fully Enabled
MAMTIM = 0x04; //MAM fetch cycles are 4 CCLKs in duration
VPBDIV = 0x02; //Divide Clock for PCLK = 30MHz
}

void RTC_Setup(char *Buff)


{
unsigned char TimE;
char i=0;
for(i=0;i<2;i++)
{
while(!isdigit(Key)); //Wait till Key = 0 to 9
if (i==0)
{
TimE = 10 * atoi( &Key );
}
if (i==1)
{
TimE += atoi( &Key );
}
putchar(Key);
Key = 0;
}
*Buff = TimE; /Load Setup New Value
}

void Delay()
{
unsigned int i,j;
for(i=0;i<50;i++)
for(j=0;j<700;j++);
}

void Wait()
{
Delay();Delay();Delay();
Delay();Delay();Delay();
Delay();Delay(); Delay();
}
void Alarm(void)
{
IOSET0 = 1 << BUZZ;
Wait();Wait();
IOCLR0 = 1 << BUZZ;

Sri Venkateshwara College of Engineering, Bengaluru 37


Wait();
}
//void Clean(void)
//{
// unsigned char i;

// //for(i=0;i<250;i++)
// printf("[2M");
//}

void main(void)
{
Initialize();
UART1_Init(9600/*Baud Rate*/);
U1IER = 3;//Enable UART1 Recieve Interrupt
//PINSEL0 |= (1 << 18); //Select Pin as UART1
IO0DIR |= (1<<7); //Configure P0.7 as O/p (Buzzer)
VICVectAddr0 = (unsigned)UART1_ISR;
VICVectCntl0 = 0x20 | 7;
VICIntEnable |= (1 << 7);
VICVectAddr2 = (unsigned)RTC_ISR;
VICVectCntl2 = 0x20 | 13;
VICIntEnable |= (1 << 13);
AMR = 0xFF; //Mask all valued except hh:mm:ss for alarm comparision
PREINT = 0x00000392; // Set RTC Prescaler for PCLK 30 MHz
PREFRAC = 0x00004380;// printf("[2J\0");// Clear Screen
CCR = 0x01;
//CIIR = 0x01;
UART1_PutS("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
UART1_PutS(" ARM LPC2138 RTC Demo\n\r--------------------\n\n\n");
UART1_PutS("> Press * to Set Time\n");
UART1_PutS("> Press ! to Set Alarm\n");
UART1_PutS("> Press $ to Snooze Alarm 5 Minutes\n");
UART1_PutS("> Press . to Stop Alarm\n");
UART1_PutS("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n\n");
while(1)
{
printf("CTC : %d\t",CTC);
printf(">> TIME: %02d:%02d:%02d \r",HOUR,MIN,SEC); //
Display time format hh:mm:ss
DelayMs(100); // Delay for display
if (Key == '*')
{
Key = 0;
UART1_PutS(">> Set Time: ");
RTC_Setup(&HOUR);

Sri Venkateshwara College of Engineering, Bengaluru 38


UART1_PutC(':');
RTC_Setup (&MIN);
UART1_PutC (':');
RTC_Setup (&SEC);
//printf("\r\tTIME: %02d:%02d:%02d \r",HOUR,MIN,SEC);
// Display time format hh:mm:ss
//printf("^[2J");
U1THR = 0x1B; //Escape
UART1_PutS("[2J\0"); // Clear Screen
}
if (Key == '!')
{
AMR = 0xF8;
Key = 0;
UART1_PutS(">>\tSet Alarm: ");
RTC_Setup(&ALHOUR);
UART1_PutC(':');
RTC_Setup(&ALMIN);
UART1_PutC(':');
RTC_Setup(&ALSEC)
}
if (Key == '$' && Flag == 1)
{
if (MIN >= 55)
{
ALHOUR = HOUR + 1;
ALMIN = 5 - (60 - MIN);
}
else
{
ALMIN = MIN + 5;
}
Key = 0;
Flag = 0;
}

if (Key == '.')
{
Key = 0;
Flag = 0;
}
if (Flag == 1)
{
Alarm (); Wait (); Alarm ();
}
}

Sri Venkateshwara College of Engineering, Bengaluru 39


}

void UART1_ISR(void)__irq
{

char Msg;
if(((Msg = U1IIR) & 0x01) == 0)//Check Flag Status of Recieve Interrupt
{
switch(Msg & 0x0E) //Filter Msg
{
case 0x04: while (!(U1LSR & 0x20));
//Recieve Data
Key = U1RBR;
case 0x02: break; // Interrupt
default : break;
}
}

VICVectAddr = 0; //Acknowledge Interrupt


}

void RTC_ISR (void)__irq


{
if ((ILR & 0x02) == 0x02)
{
Flag = 1;
UART1_PutS("\nALARM\n");
ILR = 0x02; //Clear Current Interrupt
}
VICVectAddr = 0; //Ack Interrupt
}

SERIAL PORT:

#define CR 0x0D
#include <LPC21xx.H>
void init_serial (void);
int putchar (int ch);
int getchar (void);
unsigned char test;

Sri Venkateshwara College of Engineering, Bengaluru 40


//<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Code Begins Here
int main(void)
{
char *Ptr = "*** UART0 Demo ***\n\n\rType Characters to be echoed!!\n\n\r";
VPBDIV = 0x02; //Divide Pclk by two
init_serial()
while(1)
{
while (*Ptr)
{
putchar(*Ptr++);
}
putchar(getchar()); //Echo terminal
}
}

void init_serial (void) /* Initialize Serial Interface */


{
PINSEL0 = 0x00000005; /* Enable RxD0 and TxD0*/
U0LCR = 0x00000083; /* 8 bits, no Parity, 1 Stop bit */
U0DLL = 0x000000C3; /*9600 Baud Rate @ 30MHz VPB Clock */
U0LCR = 0x00000003; /* DLAB = 0 */
}
int putchar (int ch) /* Write character to Serial Port */
{
if (ch == '\n') {
while (!(U0LSR & 0x20));
U0THR = CR; /* output CR */
}
while (!(U0LSR & 0x20));
return (U0THR = ch);
}

int getchar (void) /* Read character from Serial Port */


{

while (!(U0LSR & 0x01));


return (U0RBR);
}

Sri Venkateshwara College of Engineering, Bengaluru 41

You might also like