You are on page 1of 6

Circuit Analysis-II

Lab Report # 1
Introduction to KEIL & ARM Assembly

Submitted To: Mr. Umair & Mr. Asad Ullah Ghilani

Submitted By:
Riyan Ali Alvi
Abdul Rehman Al Nasir
Munad e Ali

Department: Electrical Engineering


Program and Session: BS-EE-20-24

DATED: 9-MAR-2022

PAKISTAN INSTITUTE OF ENGINEERING AND APPLIED SCIENCES


NILORE, ISLAMABAD
LAB #1: Introduction to KEIL IDE and ARM Assembly
1. Objective
 Understanding and setting up Keil IDE environment for ARM microcontrollers
 Compiling and executing ARM controller based program using assembly
 On-Board LED blinking program.
2. Tools
 Computer / laptop
 Keil IDE V5 software with ARM library
3. Block diagram of STM32F103C8:
4. Procedure:

1. Create new project in µVision5 and setup STM32F103C8 board CMSIS core and device
startup libraries in runtime environment.
2. Create new Assembler source file (.s).
3. On-board RTC-LED is connected with Port C13, PC13 is connected to Advanced Peripheral
Bus2 (ABP2). Clock on PC13 is enabled by changing bit 4 of ABP2 enable register which is
at 0x18 offset from RCC register located at 0x4002100.So existing value of register is
LOADED into stack register and OR operation is applied with new information and again
STORED back into the memory.
4. Configuration of Pin 13 is set to output push pull and Mode is set to 2MHz clock in
GPIOC_CRH register which is located at 0x04 offset from GPIOC base address at
0x4001100 (from memory map in user_manual).Memory register is altered by using same
technique used in previous step.
5. BSRR is used for atomic control over a single pin of a port, so Pin is set and reset repeatedly
by changing corresponding bits of pin 13 to turn LED on and off. BSRR is located at and
offset of 0x10 from GPIOC base address. BSRR is WRITE-ONLY register so we cannot
change information present in it , we just LOAD new information into the register
6. After turning ON of OFF LED program enters into a loop which last for 1 second and then
performs the next operation.
5. Code
; ******* Constants *******
;Delay interval= 2M clock units. = 1sec
;due to 8 machine cycle execution.
;Overheads are ignored

DELAY_INTERVAL EQU 0x42400 ;for 1sec delay.

; **************************

RCC_BASE EQU 0x40021000 ;From memory map Reset and clock control RCC
APB2ENR_OFFSET EQU 0x18 ;Peripheral bus 2 Clock Enable Register
RCC_APB2ENR EQU RCC_BASE + APB2ENR_OFFSET

GPIOC_BASE EQU 0x40011000 ;GPIOC port starting mamory address from MM


GPIOC_MODER_OFFSET EQU 0x04 ;for Mode and configuration setting register
GPIOC_MODER EQU GPIOC_BASE + GPIOC_MODER_OFFSET

GPIOC_BSRR_OFFSET EQU 0x10 ;GPIOC Bit Set/Reset register offset for GPIO C start value
GPIOC_BSRR EQU GPIOC_BASE + GPIOC_BSRR_OFFSET

GPIOC_EN EQU 0x10 ;For enabling GPIOC


MODER13_OUT EQU 0x00200000 ; For Setting the CLK 2MHZ and CNG the GPIOC 13 to GP PP
OUTPUT
LED_ON EQU 0x2000 ;0b'0000 0000 0000 0000 0010 0000 0000 0000 for GPIOx_BSRR
LED_OFF EQU 0x20000000 ;0b'0010 0000 0000 0000 0000 0000 0000 0000 for GPIOx_BSRR

AREA |.text|, CODE, READONLY, ALIGN=2


THUMB
ENTRY
EXPORT __main
__main
BL GPIOC_Init

;***************LOOPSTART HERE*******************
;******************** Repetition To ON OFF LED
Loop ;LABEL

;**************TO ON LED***********************
LDR R0, =GPIOC_BSRR
LDR R1, =LED_ON
STR R1, [R0]
;***********************************************
LDR R7, =DELAY_INTERVAL
MOV R3, R7
BL delay
B turnOFF
;**********************DELAY********************
;*Assume 8 Machine cylce for execute delay func*
delay
CBZ R3, delayend
SUBS R3, #1
B delay1
delayend
BX LR

;***********************************************
;*****************TO OFF LED********************
turnOFF
; Set output low
LDR R0, =GPIOC_BSRR ; R0=0x40011010
LDR R1, =LED_OFF
STR R1, [R0]

LDR R7, =DELAY_INTERVAL


MOV R3, R7
BL delay

B Loop ;Call to label for Repetition

;******************** Repetition To ON OFF LED


;***************LOOPEND HERE*******************

GPIOC_Init

;****************To Enable Clock On GPIOC PORT**********

LDR R0, =RCC_APB2ENR ;R0=0x40021018


LDR R1, [R0]
ORR R1, #GPIOC_EN
STR R1, [R0]
;********************************************************

;*****************To Set the Mode and Configuration*****


;******************** of Port for specific pin**********
LDR R0, =GPIOC_MODER;
LDR R1, [R0]
ORR R1, #MODER13_OUT
STR R1, [R0]
;*******************************************************
BX LR

NOP
ALIGN
END
6. Simulation Results:
Simulations were performed in Proteus Professional 8 with BLUEPILL libraries
LED is connected to Pin C13which is acting as sink and oscilloscope is connected to pin13
to visualize change in state

Figure 1 : Simulation Results

7. Conclusion
The code executed perfectly on STM32 board and computer simulation. Proper workflow was
followed to achieve this objective. The fact that microcontroller takes some time to execute
single instruction was used to construct a loop which repeatedly ran a single instruction for a
specified number of times to achieve a perceivable delay by human eye.

You might also like