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

Introduction

Chapter 1
Introduction

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 10 / 74
Introduction 1.3 Binary digital system

Two’s complement representation


Using 8-bit two’s complement representation:

Carry (C) 1 1 1 1 1 0 1 0

A = 1 1 1 1 1 0 1 1 = -610
+
B = 1 1 1 1 1 0 1 0 = -510
Y = 1 1 1 1 0 1 0 1 = -1110
Y = 0 0 0 0 1 0 1 0
+ 1
Y+1 = 0 0 0 0 1 0 1 1 = +1110

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 56 / 74
Introduction 1.8 Logical and arithmetic operations

Logical operations

A B NOT A A OR B A NOR B A AND B A NAND B A XOR B


0 0 1 0 1 0 1 0
0 1 1 1 0 0 1 1
1 0 0 1 0 0 1 1
1 1 0 1 0 1 0 0

NOT: Logical complement.


OR, NOR: Logical disjunction.
AND, NAND: Logical conjunction.
AND, NAND: Exclusive OR, A OR B, but not both.
Bitwise: 0 → 1, 1 → 0.

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 71 / 74
Assembly programming language

Chapter 3
Assembly programming language

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 1 / 81
Assembly programming language 3.3 Move instructions

MOV instruction

Solution:

Code 3.1 (MOV instruction)

Stack_Size EQU 0x00000400

AREA STACK,NOINIT,READWRITE,ALIGN=3

Stack_Mem SPACE Stack_Size

AREA RESET,DATA, READONLY


EXPORT __Vectors
__Vectors
DCD Stack_Mem+Stack_Size
DCD Reset_Handler
ALIGN

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 17 / 81
Assembly programming language 3.3 Move instructions

MOV instruction

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


THUMB
ENTRY
EXPORT Reset_Handler
Reset_Handler
MOV R0,#0xF9C0
NOP

ALIGN
END

This leads to R0 = 0x0000F9C0.

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 18 / 81
Assembly programming language 3.3 Move instructions

MOVT instruction
Syntax
MOVT{cond} Rd, #imm16
where:
cond is an optional condition code.
Rd is the destination register.
imm16 is a 16-bit immediate value.
Operation
Rd ← imm16
Description
MOVT writes imm16 to Rd[31:16], without affecting Rd[15:0]
Status bits
This instruction does not change the flags.

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 19 / 81
Assembly programming language 3.3 Move instructions

MOVT instruction

Solution:

Code 3.2 (MOVT instruction)

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


THUMB
ENTRY
EXPORT Reset_Handler
Reset_Handler
MOVT R0,#0xF9C0
NOP

ALIGN
END

This leads to R0 = 0xF9C00000.

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 21 / 81
Assembly programming language 3.3 Move instructions

MOV32 pseudo-instruction

Solution:

Code 3.3 (MOV32 instruction)

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


THUMB
ENTRY
EXPORT Reset_Handler
Reset_Handler
MOV32 R0,#150000
NOP

ALIGN
END

This leads to R0 = 0x000249F0 (150000).

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 24 / 81
Assembly programming language 3.4 Load/Store Instructions

LDR instruction

Solution:

Code 3.4 (LDR instruction)

Stack_Size EQU 0x00000400

AREA STACK,NOINIT,READWRITE,ALIGN=3

Stack_Mem SPACE Stack_Size

AREA RESET,DATA, READONLY


EXPORT __Vectors
__Vectors
DCD Stack_Mem+Stack_Size
DCD Reset_Handler
ALIGN

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 29 / 81
Assembly programming language 3.4 Load/Store Instructions

LDR instruction

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


THUMB
ENTRY
EXPORT Reset_Handler
Reset_Handler
MOV32 R2,#0xC0F9A4B0
LDR R3,[R2]
NOP

ALIGN
END

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 30 / 81
Assembly programming language 3.4 Load/Store Instructions

LDR pseudo-instruction

Solution:

Code 3.5 (LDR pseudo instruction)

Stack_Size EQU 0x00000400

AREA STACK,NOINIT,READWRITE,ALIGN=3

Stack_Mem SPACE Stack_Size

AREA RESET,DATA, READONLY


EXPORT __Vectors
__Vectors
DCD Stack_Mem+Stack_Size
DCD Reset_Handler
ALIGN

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 34 / 81
Assembly programming language 3.4 Load/Store Instructions

LDR pseudo-instruction

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


THUMB
ENTRY
EXPORT Reset_Handler
Reset_Handler
MOV R6,#2
LDR R2,=LED_codes
LDR R3,[R2,R6]
NOP

LED_codes DCB 0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8

ALIGN
END

The value of register R3 will be 0x9299B0A4.

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 35 / 81
Assembly programming language 3.4 Load/Store Instructions

LDR pseudo-instruction

Solution:

Code 3.6 (LDR pseudo instruction)

GPIOA_BASE EQU 0x08000000


MODER EQU 0x00

Stack_Size EQU 0x00000400

AREA STACK,NOINIT,READWRITE,ALIGN=3

Stack_Mem SPACE Stack_Size

AREA RESET,DATA, READONLY


EXPORT __Vectors

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 38 / 81
Assembly programming language 3.4 Load/Store Instructions

LDR pseudo-instruction

__Vectors
DCD Stack_Mem+Stack_Size
DCD Reset_Handler
ALIGN

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


THUMB
ENTRY
EXPORT Reset_Handler
Reset_Handler
LDR R0,=GPIOA_BASE+MODER
LDR R1,[R0]
NOP

ALIGN
END

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 39 / 81
Assembly programming language 3.4 Load/Store Instructions

STR instruction

Solution:

Code 3.7 (STR instruction)

Stack_Size EQU 0x00000400

AREA STACK,NOINIT,READWRITE,ALIGN=3

Stack_Mem SPACE Stack_Size

AREA RESET,DATA, READONLY


EXPORT __Vectors
__Vectors
DCD Stack_Mem+Stack_Size
DCD Reset_Handler
ALIGN

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 43 / 81
Assembly programming language 3.4 Load/Store Instructions

STR instruction

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


THUMB
ENTRY
EXPORT Reset_Handler
Reset_Handler
MOV R6,#2
LDR R2,=StoredData
MOV32 R3,#0xF0C2E7A5
STR R3,[R2]
NOP

AREA MyData,DATA, READWRITE, ALIGN=2

StoredData DCB 0x00

ALIGN
END

This Nguyen
leads Tiento R0(TNUT)
Hung = 0x000249F0 (150000).
Microcomputer principles and applications Academic year 2022-2023 44 / 81
Assembly programming language 3.4 Load/Store Instructions

STR instruction

Solution:

Code 3.8 (STR instruction offset)

Reset_Handler
MOV R1,#2
LDR R2,=StoredData
MOV32 R3,#0xF0C2E7A5
STR R3,[R2,R1]
NOP

AREA MyData,DATA, READWRITE, ALIGN=2

StoredData DCB 0x00

ALIGN
END

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 47 / 81
Assembly programming language 3.4 Load/Store Instructions

Pre-indexed addressing

Solution:

Code 3.9 (STR pre indexed)

Reset_Handler
LDR R2,=StoredData
MOV32 R3,#0xF0C2E7A5
STR R3,[R2,#2]!
NOP

AREA MyData,DATA, READWRITE, ALIGN=2

StoredData DCB 0x00

ALIGN
END

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 50 / 81
Assembly programming language 3.4 Load/Store Instructions

Post-indexed addressing

Solution:

Code 3.10 (STR post indexed)

Reset_Handler
LDR R2,=StoredData
MOV32 R3,#0xF0C2E7A5
STR R3,[R2],#2]
NOP

AREA MyData,DATA, READWRITE, ALIGN=2

StoredData DCB 0x00

ALIGN
END

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 53 / 81
Assembly programming language 3.5 Arithmetic instructions

ADD instruction

Solution:

Code 3.11 (ADD instruction)

Reset_Handler
MOVT R1,#0xE0F5
MOVT R2,#0x60D3
MOVT R3,#0x79A7
ADD R1,R2
ADD R1,R3
NOP

The obtained result is R1=0xCC6F0000.

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 59 / 81
Assembly programming language 3.5 Arithmetic instructions

ADD instruction

Solution:

Code 3.12 (ADDS instruction)

Reset_Handler
Reset_Handler
MOVT R1,#0xE0F5
MOVT R2,#0x60D3
MOVT R3,#0x79A7
ADDS R1,R2
ADDS R1,R3
NOP

The obtained result is R1=0xCC6F0000.

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 61 / 81
Assembly programming language 3.5 Arithmetic instructions

ADC instruction

Solution:

Code 3.13 (ADCS instruction)

Reset_Handler
MOVT R1,#0xE0F5
MOVT R2,#0x60D3
MOVT R3,#0x79A7
ADCS R1,R2
ADCS R1,R3
NOP

The obtained result is R1=0xCC6F0001.

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 64 / 81
Assembly programming language 3.5 Arithmetic instructions

SUB instruction

Solution:

Code 3.14 (SUB instruction)

Reset_Handler
MOV R1,#0xE0F5
SUBS R1,R1,#1 ; R1--
NOP

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 67 / 81
Assembly programming language 3.5 Arithmetic instructions

MUL instruction

Solution:

Code 3.15 (SUB instruction)

Reset_Handler
MOV R1,#0xE0F5
MOV R2,#0x765C
MUL R1,R1,R2 ; R1 = R1 * R2
NOP

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 72 / 81
Assembly programming language 3.6 Logical instructions

AND instruction

Solution:

Code 3.18 (AND instruction)

Reset_Handler
MOV R1,#0xE0F5
MOV R2,#0x765C
AND R3,R1,R2 ; R3 = R1 AND R2
NOP

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 91 / 186
Assembly programming language 3.6 Logical instructions

AND instruction
We have

R1 = 1110 0000 1111 0101 = 0xE0F5


R2 = 0111 0110 0101 1100 = 0x765C
---------------------
R1 AND R2 = 0110 0000 0101 0100 = 0x6054

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 92 / 186
Assembly programming language 3.6 Logical instructions

AND instruction

Solution:

Code 3.19 (AND instruction one bit)

Reset_Handler
MOV R1,#0xE0F5
MOV R2,#0xFFDF
AND R1,R1,R2
NOP

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 94 / 186
Assembly programming language 3.6 Logical instructions

AND instruction
We have

R1 = 1110 0000 1111 0101 = 0xE0F5


R2 = 1111 1111 1101 1111 = 0xFFDF
---------------------
R1 AND R2 = 1110 0000 1101 0101 = 0xE0D5

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 95 / 186
Assembly programming language 3.6 Logical instructions

ORR instruction

Solution:

Code 3.20 (ORR instruction)

Reset_Handler
MOV R1,#0xE0F5
MOV R2,#0x765C
ORR R3,R1,R2 ; R3 = R1 OR R2
NOP

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 100 / 186
Assembly programming language 3.6 Logical instructions

ORR instruction
We have

R1 = 1110 0000 1111 0101 = 0xE0F5


R2 = 0111 0110 0101 1100 = 0x765C
---------------------
R1 ORR R2 = 1111 0110 1111 1101 = 0xF6FD

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 101 / 186
Assembly programming language 3.6 Logical instructions

ORR instruction

Solution:

Code 3.21 (ORR instruction one bit)

Reset_Handler
MOV R1,#0xE0F5
MOV R2,#0x0400
ORR R3,R1,R2
NOP

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 103 / 186
Assembly programming language 3.6 Logical instructions

ORR instruction
We have

R1 = 1110 0000 1111 0101 = 0xE0F5


R2 = 0000 0100 0000 0000 = 0x0400
---------------------
R1 ORR R2 = 1110 0100 1111 0101 = 0xE4F5

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 104 / 186
Assembly programming language 3.6 Logical instructions

BIC instruction

Solution:

Code 3.22 (BIC instruction)

Reset_Handler
MOV R1,#0xE0F5
MOV R2,#0x8040
BIC R1,R2
NOP

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 109 / 186
Assembly programming language 3.6 Logical instructions

BIC instruction
We have

R1 = 1110 0000 1111 0101 = 0xE0F5


R2 = 1000 0000 0100 0000 = 0x8040
---------------------
R1 BIC R2 = 0110 0000 1011 0101 = 0x60B5

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 110 / 186
Assembly programming language 3.6 Logical instructions

EOR instruction

Solution:

Code 3.23 (EOR instruction)

Reset_Handler
MOV R1,#0xE0F5
MOV R2,#0x765C
ORR R3,R1,R2 ; R3 = R1 OR R2
NOP

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 115 / 186
Assembly programming language 3.6 Logical instructions

EOR instruction
We have

R1 = 1110 0000 1111 0101 = 0xE0F5


R2 = 0111 0110 0101 1100 = 0x765C
---------------------
R1 EOR R2 = 1001 0110 1010 1001 = 0x96A9

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 116 / 186
Assembly programming language 3.6 Logical instructions

EOR instruction

Solution:

Code 3.24 (EOR instruction toggle bits)

Reset_Handler
MOV R1,#0xE0F5
MOV R2,#0x0210
EOR R3,R1,R2
NOP

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 118 / 186
Assembly programming language 3.6 Logical instructions

EOR instruction
We have

R1 = 1110 0000 1111 0101 = 0xE0F5


R2 = 0000 0010 0001 0000 = 0x0210
---------------------
R1 EOR R2 = 1110 0010 1110 0101 = 0xE2E5

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 119 / 186
Assembly programming language 3.7 Shift and rotating instructions

LSL instructions

Solution:

Code 3.25 (LSL instruction)

Reset_Handler
MOV32 R1,#0x617EC3A8
LSLS R1,R1,#3
NOP

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 126 / 186
Assembly programming language 3.7 Shift and rotating instructions

LSR instructions

Solution:

Code 3.26 (LSR instruction)

Reset_Handler
MOV32 R1,#0x617EC3A8
LSRS R1,R1,#6
NOP

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 131 / 186
Assembly programming language 3.7 Shift and rotating instructions

ASR instruction

Solution:

Code 3.27 (ASR instruction)

Reset_Handler
MOV32 R1,#0xB17EC3A8
ASRS R1,R1,#4
NOP

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 136 / 186
Assembly programming language 3.7 Shift and rotating instructions

ROR instruction

Solution:

Code 3.28 (ROR instruction)

Reset_Handler
MOV32 R1,#0xB17EC3A8
RORS R1,R1,#16
NOP

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 141 / 186
Assembly programming language 3.8 Compare instructions

CMP and CMN instructions

Solution:

Code 3.29 (CMP CMN instructions)

Reset_Handler
MOV R1,#0xE0F5
MOV R2,#0x765C
CMP R1,R2
CMN R1,R2
NOP

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 147 / 186
Assembly programming language 3.10 Flow control instructions

B instruction

Solution:

Code 3.30 (B instructions)

Reset_Handler
MOV R1,#1
MOV R2,#0
Loop
ADD R2,R2,R1
ADD R1,R1,#1
CMP R1,#5
BLE Loop
Stop
B Stop

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 157 / 186
Assembly programming language 3.10 Flow control instructions

BL and BX instructions

Solution:

Code 3.31 (FindMaxSubroutine)

Reset_Handler
MOV32 R1,#0xB17EC3A7
MOV32 R2,#0xB17EC3A8
MOV32 R3,#0x706892AF
MOV32 R4,#0x706892AE

MOV R5,R1
MOV R6,R2
BL FindMax
MOV R1,R5

MOV R5,R3
MOV R6,R4

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 170 / 186
Assembly programming language 3.10 Flow control instructions

BL and BX instructions

BL FindMax
MOV R3,R5
Stop
B Stop
FindMax
CMP R5,R6
BLT MaxR6
BX LR
MaxR6
MOV R5,R6
BX LR

ALIGN
END

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 171 / 186
Assembly programming language 3.11 If-Then block

IT block

Solution:
Not using IT block

Code 3.32 (B instruction)

Reset_Handler
MOV R1,#5
MOV R2,#12
CMP R1,R2

BLT Result
SUB R3,R1,R2
B Stop
Result
ADD R3,R1,R2
Stop
B Stop

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 183 / 186
Assembly programming language 3.11 If-Then block

IT block
Using IT block

Code 3.33 (IT instruction)

Reset_Handler
MOV R1,#5
MOV R2,#12
CMP R1,R2

ITE GT
SUBGT R3,R1,R2
ADDLE R3,R1,R2
Stop
B Stop

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 184 / 186
Assembly programming language 3.14 While loop and Do-while loops

The while loop

Solution:

Code 3.47 (Non zero sum)

Reset_Handler
MOV R0,#0 ; Array index
MOV R1,#0 ; Sum
LDR R2,=Array
CalcSum
LDR R3,[R2,R0] ; Array elements
BIC R3,R3,#0xFFFFFF00

CMP R3,#0
BEQ Done
ADD R1,R1,R3
ADD R0,#1
B CalcSum

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 205 / 231
Assembly programming language 3.14 While loop and Do-while loops

The while loop

Done
B Done

Array DCB 0xC0,0xF9,0xA4,0xB0,0x99,0x00,0x82,0xF8,0x80

ALIGN
END

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 206 / 231
Assembly programming language 3.14 While loop and Do-while loops

The Do-while loop

Solution:

Code 3.48 (Non zero sum)

Reset_Handler
MOV R0,#0 ; Array index
MOV R1,#0 ; Sum
LDR R2,=Array
CalcSum
LDR R3,[R2,R0]
BIC R3,R3,#0xFFFFFF00
ADD R1,R1,R3
ADD R0,#1
CMP R3,#0
BEQ Done
B CalcSum

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 210 / 231
Assembly programming language 3.14 While loop and Do-while loops

The Do-while loop

Done
B Done

Array DCB 0xC0,0xF9,0xA4,0xB0,0x99,0x00,0x82,0xF8,0x80

ALIGN
END

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 211 / 231
Assembly programming language 3.16 Floating-point instruction set

Frequently used FPU instructions

Solution:

Code 3.50 (Circle area)

Reset_Handler
LDR R0,=0xE000ED88
LDR R1,[R0]
ORR R1,R1,#(0xF<<20)
STR r1,[R0] ; Enable FPU

LDR R0,=pi
VLDR.F S0,[R0]
LDR R0,=Radius
VLDR.F S1,[R0]

VMUL.F S2,S1,S1
VMUL.F S2,S2,S0

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 230 / 231
Assembly programming language 3.16 Floating-point instruction set

Frequently used FPU instructions

Stop
B Stop

pi DCFS 3.14159
Radius DCFS 20.0

ALIGN
END

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 231 / 231
Interfacing methods

Chapter 4
Interfacing methods

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 1 / 82
Interfacing methods 4.17 GPIO port initialization procedure

GPIO port initialization procedure

Solution:

Code 4.1 (Init PortA)

INCLUDE ../inc/stm32G431xx.s

LED EQU BIT5

Stack_Size EQU 0x00000400


AREA STACK,NOINIT,READWRITE,ALIGN=3
Stack_Mem SPACE Stack_Size
AREA RESET,DATA, READONLY
EXPORT __Vectors
__Vectors
DCD Stack_Mem+Stack_Size
DCD Reset_Handler
ALIGN

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 75 / 82
Interfacing methods 4.17 GPIO port initialization procedure

GPIO port initialization procedure

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


THUMB
ENTRY
EXPORT Reset_Handler
Reset_Handler
; Step 1. Enabled the clock to the GPIOA Port
LDR R0,=RCC_AHB2ENR
LDR R1,[R0]
ORR R1,#RCC_AHB2ENR_GPIOAEN
STR R1,[R0]
NOP

; Step 2. Enable output mode


LDR R0,=GPIOA_MODER
LDR R1,[R0]
MOV R2,#GPIO_MODER_MODE5_0

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 76 / 82
Interfacing methods 4.17 GPIO port initialization procedure

GPIO port initialization procedure

ORR R1,R2
LSL R2,R2,#1
BIC R1,R2
STR R1,[R0]

; Step 3. It is not necessary


; Step 4. Set pin PA5 to 1
LDR R0,=GPIOA_ODR
LDR R1,=LED
STR R1,[R0]
Stop
B Stop

ALIGN
END

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 77 / 82
Interfacing methods 4.18 GPIO output ports

GPIO output ports

 Example 4.2 (Blinky)


Consider a circuit with the STM32G431RBT6 microcontroller and an LED as
shown in the figure. Write Assembly codes to blink the LED.
STM32G431RBT6
12 24
PA0 PB0
13 25
PA1 PB1
14 26
PA2 PB2
17 56
PA3 PB3
18 57
PA4 PB4
19 58
PA5 PB5
20 59
PA6 PB6
21 60
PA7 PB7
42
R1 PA8
43 62
PA9 PB9
44 30
PA10 PB10
45 33
PA11 PB11
N1 46 34
PA12 PB12
49 35
PA13 PB13
50 36
PA14 PB14
51 37
PA15 PB15

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 79 / 82
Interfacing methods 4.18 GPIO output ports

GPIO output ports

Solution:

Code 4.2 (Blinking LED)

INCLUDE ../inc/stm32G431xx.s
LED EQU BIT5
DLVAL EQU 5000000

Stack_Size EQU 0x00000400


AREA STACK,NOINIT,READWRITE,ALIGN=3
Stack_Mem SPACE Stack_Size

AREA RESET,DATA, READONLY


EXPORT __Vectors
__Vectors
DCD Stack_Mem+Stack_Size
DCD Reset_Handler
ALIGN

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 80 / 82
Interfacing methods 4.18 GPIO output ports

GPIO output ports

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


THUMB
ENTRY
EXPORT Reset_Handler

Reset_Handler
BL GPIO_Init

LDR R2,=DLVAL ; R2 for countdown


Loop
SUBS R2,#1 ; Decrement R2
BNE Loop ; if zero yet
EOR R1,R1,#LED ; Exclusive OR R3
STR R1,[R0]
LDR R2,=DLVAL ; R2 for countdown
B Loop ; unconditional branch

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 81 / 82
Interfacing methods 4.18 GPIO output ports

GPIO output ports

GPIO_Init
; Step 1. Enabled the clock to the GPIOA Port
LDR R0,=RCC_AHB2ENR
LDR R1,[R0]
ORR R1,#RCC_AHB2ENR_GPIOAEN
STR R1,[R0]
NOP

; Step 2. Enable output mode


LDR R0,=GPIOA_MODER
LDR R1,[R0]
MOV R2,#GPIO_MODER_MODE5_0
ORR R1,R2
LSL R2,R2,#1
BIC R1,R2
STR R1,[R0]

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 82 / 82
Interfacing methods 4.18 GPIO output ports

GPIO output ports

; Step 3. It is not necessary


; Step 4. Set pin PA5 to 1
LDR R0,=GPIOA_ODR
LDR R1,=LED
STR R1,[R0]

BX LR

ALIGN
END

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 83 / 82
Interfacing methods 4.5 General purpose input output ports

GPIO output ports

 Example 4.3

There are eight LEDs connected to eight output pins of the STM32G431RBT6 as
shown in the figure. Program to turn the LEDs on and off one after another.
U1
R1 12 24
PA0 PB0
R2 13 25
PA1 PB1
14 26
PA2 PB2
17 56
PA3 PB3
R3 18 57
PA4 PB4
R4 19 58
PA5 PB5
R5 20 59
PA6 PB6
R6 21 60
PA7 PB7
R7 42
PA8
R8 43 62
PA9 PB9
44 30
PA10 PB10
45 33
PA11 PB11
46 34
N0 N1 N2 N3 N4 N5 N6 N7 PA12 PB12
49 35
PA13 PB13
50 36
PA14 PB14
51 37
VDD PA15 PB15

STM32G431RBT6

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 79 / 178
Interfacing methods 4.5 General purpose input output ports

GPIO output ports

NUCLEO-G431RB

PC9
PC10 1 2 PC11 1 2 PC8
D15 PB8 10
PC12 3 4 PD2 3 4 PC6
D14 PB9 9
VDD 5 6 E5V 5 6 PC5
AVDD AVDD 8
5V_USB_CHGR
PB8-BOOT0 7 8 GND 7 8
GND GND 7
NC 9 10 1 NC NC 9 10 NC
D13 PA5 6
NC 11 12 2 IOREF IOREF 11 12 PA12
D12 PA6 5
PA13 13 14 3 NRST NRST 13 14 PA11
D11 PA7 4
PA14 15 16 4 +3V3 +3V3 15 16 PB12
D10 PB6 3
PA15 17 18 5 +5V +5V 17 18 PB11
D9 PC7 2
GND 19 20 6 GND GND 19 20 GND
D8 PA9 1
PB7 21 22 7 GND GND 21 22 PB2
PC13 23 24 8 VIN VIN D7 PA8 8 23 24 PB1
PC14 25 26 D6 PB10 7 25 26 PB15
PC15 27 28 1 PA0 A0 D5 PB4 6 27 28 PB14
PF0 29 30 2 PA1 A1 D4 PB5 5 29 30 PB13
PF1 31 32 3 PA4 A2 D3 PB3 4 31 32 AGND
VBAT 33 34 4 PB0 A3 D2 PA10 3 33 34 PC4
PC2 35 36 5 PC1/PB9 A4 D1 PC4/PA2 2 35 36 NC
PC3 37 38 6 PC0/PA15 A5 D0 PC5/PA3 1 37 38 NC

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 80 / 178
Interfacing methods 4.5 General purpose input output ports

GPIO output ports

Solution:

Code 4.3 (GPIO Eight LEDs)

MODER0 EQU 1<<0


MODER1 EQU 1<<2
MODER4 EQU 1<<8
MODER5 EQU 1<<10
MODER6 EQU 1<<12
MODER7 EQU 1<<14
MODER8 EQU 1<<16
MODER9 EQU 1<<18

BIT0 EQU 1<<0


BIT1 EQU 1<<1
BIT4 EQU 1<<4
BIT5 EQU 1<<5

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 81 / 178
Interfacing methods 4.5 General purpose input output ports

GPIO output ports

BIT6 EQU 1<<6


BIT7 EQU 1<<7
BIT8 EQU 1<<8
BIT9 EQU 1<<9

RCC_BASE EQU 0x40021000


AHB2ENR_OFFSET EQU 0x4C

GPIOA_BASE EQU 0x48000000


GPIOA_EN EQU 1<<0

MODER_OFFSET EQU 0x00


ODR_OFFSET EQU 0x14

N0 EQU BIT0
N1 EQU BIT1
N2 EQU BIT4

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 82 / 178
Interfacing methods 4.5 General purpose input output ports

GPIO output ports

N3 EQU BIT5
N4 EQU BIT6
N5 EQU BIT7
N6 EQU BIT8
N7 EQU BIT9

DLVAL EQU 2000000

Stack_Size EQU 0x00000400

AREA STACK,NOINIT,READWRITE,ALIGN=3

Stack_Mem SPACE Stack_Size

AREA RESET,DATA, READONLY


EXPORT __Vectors

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 83 / 178
Interfacing methods 4.5 General purpose input output ports

GPIO output ports

__Vectors
DCD Stack_Mem+Stack_Size
DCD Reset_Handler
ALIGN

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


THUMB
ENTRY
EXPORT Reset_Handler
Reset_Handler
BL GPIO_Init
Start
LDR R0,=GPIOA_BASE+ODR_OFFSET
LDR R1,[R0]
Loop
ORN R1,#0 ;
BIC R1,#N0 ;

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 84 / 178
Interfacing methods 4.5 General purpose input output ports

GPIO output ports

STR R1,[R0] ; N0 ON
LDR R2,=DLVAL ;
BL Delay

ORN R1,#0 ;
BIC R1,#N1 ;
STR R1,[R0] ; N1 ON
LDR R2,=DLVAL ;
BL Delay

ORN R1,#0 ;
BIC R1,#N2 ;
STR R1,[R0] ; N2 ON
LDR R2,=DLVAL ;
BL Delay

ORN R1,#0 ;

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 85 / 178
Interfacing methods 4.5 General purpose input output ports

GPIO output ports

BIC R1,#N3 ;
STR R1,[R0] ; N3 ON
LDR R2,=DLVAL ;
BL Delay

ORN R1,#0 ;
BIC R1,#N4 ;
STR R1,[R0] ; N4 ON
LDR R2,=DLVAL ;
BL Delay

ORN R1,#0 ;
BIC R1,#N5 ;
STR R1,[R0] ; N5 ON
LDR R2,=DLVAL ;
BL Delay

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 86 / 178
Interfacing methods 4.5 General purpose input output ports

GPIO output ports

ORN R1,#0 ;
BIC R1,#N6 ;
STR R1,[R0] ; N6 ON
LDR R2,=DLVAL ;
BL Delay

ORN R1,#0 ;
BIC R1,#N7 ;
STR R1,[R0] ; N7 ON
LDR R2,=DLVAL ;
BL Delay

B Loop ;
GPIO_Init
; Step 1. Enable the clock to the GPIOA Port
LDR R0,=RCC_BASE+AHB2ENR_OFFSET
LDR R1,[R0]

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 87 / 178
Interfacing methods 4.5 General purpose input output ports

GPIO output ports

ORR R1,#GPIOA_EN
STR R1,[R0]
NOP

; Step 2. Enable output mode


LDR R0,=GPIOA_BASE+MODER_OFFSET
LDR R1,[R0]

;MOV R2,#0
MOV32 R2,#MODER0+MODER1+MODER4+MODER5+MODER6
... +MODER7+MODER8+MODER9
ORR R1,R2
LSL R2,R2,#1
BIC R1,R2
STR R1,[R0]

BX LR

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 88 / 178
Interfacing methods 4.5 General purpose input output ports

GPIO output ports

Delay
SUBS R2,#1 ; Decrement R2
BNE Delay ; if zero yet
BX LR ; Return

ALIGN
END

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 89 / 178
Interfacing methods 4.5 General purpose input output ports

GPIO output ports

 Example 4.6 (7-segment LED)


Consider a circuit of the STM32G431RBT6 and an 7-Seg common cathode LED
as shown in the figure. Program to display number 5 on the LED.

U1
Q1 R1 12 24
PA0 PB0
R2 13 25
PA1 PB1
A 14 26
PA2 PB2
B 17 56
PA3 PB3
C R3 18 57
PA4 PB4
D R4 19 58
PA5 PB5
E R5 20 59
PA6 PB6
F R6 21 60
PA7 PB7
G R7 42
PA8
H R8 43 62
PA9 PB9
44 30
PA10 PB10
45 33
PA11 PB11
YY2841AH-33 46 34
PA12 PB12
49 35
PA13 PB13
50 36
PA14 PB14
51 37
PA15 PB15

STM32G431RBT6

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 94 / 178
Interfacing methods 4.5 General purpose input output ports

GPIO output ports


The 7-Seg common anode and cathode LEDs

Common Anode
a

f b
g h g f e d c b a

e c Common Cathode

h
d
h g f e d c b a

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 95 / 178
Interfacing methods 4.5 General purpose input output ports

GPIO output ports


The bit values for displaying numbers from 0 to 4

h g f e d c b a
0 0 1 1 1 1 x x 1 1 = 0x0F3

h g f e d c b a
0 0 0 0 0 1 x x 1 0 = 0x012

h g f e d c b a
0 1 0 1 1 0 x x 1 1 = 0x163

h g f e d c b a
0 1 0 0 1 1 x x 1 1 = 0x133

h g f e d c b a
0 1 1 0 0 1 x x 1 0 = 0x192

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 96 / 178
Interfacing methods 4.5 General purpose input output ports

GPIO output ports


The bit values for displaying numbers from 5 to 9

h g f e d c b a
0 1 1 0 1 1 x x 0 1 = 0x1B1

h g f e d c b a
0 1 1 1 1 1 x x 0 1 = 0x1F1

h g f e d c b a
0 0 0 0 0 1 x x 1 1 = 0x013

h g f e d c b a
0 1 1 1 1 1 1 1 = 0x1F3

h g f e d c b a
0 1 1 0 1 1 1 1 = 0x1B3

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 97 / 178
Interfacing methods 4.5 General purpose input output ports

GPIO output ports

Solution:

Code 4.4 (7Seg LED)

MODER0 EQU 1<<0


MODER1 EQU 1<<2
MODER2 EQU 1<<4
MODER3 EQU 1<<6
MODER4 EQU 1<<8
MODER5 EQU 1<<10
MODER6 EQU 1<<12
MODER7 EQU 1<<14
MODER8 EQU 1<<16
MODER9 EQU 1<<18
MODER10 EQU 1<<20

BIT0 EQU 1<<0

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 98 / 178
Interfacing methods 4.5 General purpose input output ports

GPIO output ports

BIT1 EQU 1<<1


BIT3 EQU 1<<3
BIT4 EQU 1<<4
BIT5 EQU 1<<5
BIT6 EQU 1<<6
BIT7 EQU 1<<7
BIT8 EQU 1<<8
BIT9 EQU 1<<9
BIT10 EQU 1<<10

RCC_BASE EQU 0x40021000


AHB2ENR_OFFSET EQU 0x4C

GPIOA_BASE EQU 0x48000000


GPIOA_EN EQU 1<<0

GPIOB_BASE EQU 0x48000400


GPIOB_EN EQU 1<<1
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 99 / 178
Interfacing methods 4.5 General purpose input output ports

GPIO output ports

MODER_OFFSET EQU 0x00


ODR_OFFSET EQU 0x14

Stack_Size EQU 0x00000400

AREA STACK,NOINIT,READWRITE,ALIGN=3

Stack_Mem SPACE Stack_Size

AREA RESET,DATA, READONLY


EXPORT __Vectors
__Vectors
DCD Stack_Mem+Stack_Size
DCD Reset_Handler
ALIGN

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

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 100 / 178
Interfacing methods 4.5 General purpose input output ports

GPIO output ports

THUMB
ENTRY
EXPORT Reset_Handler
Reset_Handler
BL GPIO_Init
Start
LDR R0,=GPIOA_BASE+ODR_OFFSET
MOV R1,#0x1B1 ;
STR R1,[R0] ;

LDR R0,=GPIOB_BASE+ODR_OFFSET
MVN R1,#BIT3 ;
STR R1,[R0] ;
Loop
B Loop ;
GPIO_Init
; Step 1. Enable the clock to the GPIOA Port

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 101 / 178
Interfacing methods 4.5 General purpose input output ports

GPIO output ports

LDR R0,=RCC_BASE+AHB2ENR_OFFSET
LDR R1,[R0]
ORR R1,#GPIOA_EN
STR R1,[R0]

; Step 2. Enable output mode


LDR R0,=GPIOA_BASE+MODER_OFFSET
LDR R1,[R0]
MOV32 R2,#MODER0+MODER1+MODER4+MODER5+MODER6
... +MODER7+MODER8+MODER9
ORR R1,R2
LSL R2,R2,#1
BIC R1,R2
STR R1,[R0]

; Step 3. Enable the clock to the GPIOB Port


LDR R0,=RCC_BASE+AHB2ENR_OFFSET

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 102 / 178
Interfacing methods 4.5 General purpose input output ports

GPIO output ports

LDR R1,[R0]
ORR R1,#GPIOB_EN
STR R1,[R0]

; Step 4. Enable output mode


LDR R0,=GPIOB_BASE+MODER_OFFSET
LDR R1,[R0]
MOV32 R2,#MODER3
ORR R1,R2
LSL R2,R2,#1
BIC R1,R2
STR R1,[R0]

BX LR

ALIGN
END

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 103 / 178
Interfacing methods 4.5 General purpose input output ports

GPIO output ports

 Example 4.7
Consider a circuit of the STM32G431RBT6 and four 7-Seg common cathode LED
as shown in the figure. Program to display number 1234 on the LED.

U1
Q1 R1 12 24
PA0 PB0
R2 13 25
PA1 PB1
A 14 26
PA2 PB2
B 17 56
PA3 PB3
C R3 18 57
PA4 PB4
D R4 19 58
PA5 PB5
E R5 20 59
PA6 PB6
F R6 21 60
PA7 PB7
G R7 42
PA8
H R8 43 62
PA9 PB9
44 30
PA10 PB10
45 33
PA11 PB11
YY2841AH-33 46 34
PA12 PB12
49 35
PA13 PB13
50 36
PA14 PB14
51 37
PA15 PB15

STM32G431RBT6

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 104 / 178
Interfacing methods 4.5 General purpose input output ports

GPIO output ports

Solution:

Code 4.5 (7Seg Four LEDs)

MODER0 EQU 1<<0


MODER1 EQU 1<<2
MODER2 EQU 1<<4
MODER3 EQU 1<<6
MODER4 EQU 1<<8
MODER5 EQU 1<<10
MODER6 EQU 1<<12
MODER7 EQU 1<<14
MODER8 EQU 1<<16
MODER9 EQU 1<<18
MODER10 EQU 1<<20

BIT0 EQU 1<<0

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 105 / 178
Interfacing methods 4.5 General purpose input output ports

GPIO output ports

BIT1 EQU 1<<1


BIT4 EQU 1<<4
BIT5 EQU 1<<5
BIT6 EQU 1<<6
BIT7 EQU 1<<7
BIT8 EQU 1<<8
BIT9 EQU 1<<9
BIT10 EQU 1<<10

RCC_BASE EQU 0x40021000


AHB2ENR_OFFSET EQU 0x4C

GPIOA_BASE EQU 0x48000000


GPIOA_EN EQU 1<<0

GPIOB_BASE EQU 0x48000400


GPIOB_EN EQU 1<<1

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 106 / 178
Interfacing methods 4.5 General purpose input output ports

GPIO output ports

MODER_OFFSET EQU 0x00


ODR_OFFSET EQU 0x14

N0 EQU BIT0
N1 EQU BIT1
N2 EQU BIT4
N3 EQU BIT5
N4 EQU BIT6
N5 EQU BIT7
N6 EQU BIT8
N7 EQU BIT9

DLVAL EQU 25000

Stack_Size EQU 0x00000400

AREA STACK,NOINIT,READWRITE,ALIGN=3

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 107 / 178
Interfacing methods 4.5 General purpose input output ports

GPIO output ports

Stack_Mem SPACE Stack_Size

AREA RESET,DATA, READONLY


EXPORT __Vectors
__Vectors
DCD Stack_Mem+Stack_Size
DCD Reset_Handler
ALIGN

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


THUMB
ENTRY
EXPORT Reset_Handler
Reset_Handler
BL GPIO_Init
Loop
LDR R0,=GPIOA_BASE+ODR_OFFSET

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 108 / 178
Interfacing methods 4.5 General purpose input output ports

GPIO output ports

MOV R1,#0x012 ;
STR R1,[R0] ;

LDR R0,=GPIOB_BASE+ODR_OFFSET
MVN R1,#0x400 ;
STR R1,[R0] ;

LDR R2,=DLVAL ;
BL Delay

LDR R0,=GPIOA_BASE+ODR_OFFSET
MOV R1,#0x163 ;
STR R1,[R0] ;

LDR R0,=GPIOB_BASE+ODR_OFFSET
MVN R1,#0x20 ;
STR R1,[R0] ;

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 109 / 178
Interfacing methods 4.5 General purpose input output ports

GPIO output ports

LDR R2,=DLVAL ;
BL Delay

LDR R0,=GPIOA_BASE+ODR_OFFSET
MOV R1,#0x133 ;
STR R1,[R0] ;

LDR R0,=GPIOB_BASE+ODR_OFFSET
MVN R1,#0x10 ;
STR R1,[R0] ;

LDR R2,=DLVAL ;
BL Delay

LDR R0,=GPIOA_BASE+ODR_OFFSET
MOV R1,#0x192 ;
STR R1,[R0] ;

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 110 / 178
Interfacing methods 4.5 General purpose input output ports

GPIO output ports

LDR R0,=GPIOB_BASE+ODR_OFFSET
MVN R1,#0x8 ;
STR R1,[R0] ;

LDR R2,=DLVAL ;
BL Delay

B Loop ;
GPIO_Init
; Step 1. Enable the clock to the GPIOA Port
LDR R0,=RCC_BASE+AHB2ENR_OFFSET
LDR R1,[R0]
ORR R1,#GPIOA_EN
STR R1,[R0]

; Step 2. Enable output mode

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 111 / 178
Interfacing methods 4.5 General purpose input output ports

GPIO output ports

LDR R0,=GPIOA_BASE+MODER_OFFSET
LDR R1,[R0]
MOV32 R2,#MODER0+MODER1+MODER4+MODER5+MODER6
... +MODER7+MODER8+MODER9
ORR R1,R2
LSL R2,R2,#1
BIC R1,R2
STR R1,[R0]

; Step 3. Enable the clock to the GPIOB Port


LDR R0,=RCC_BASE+AHB2ENR_OFFSET
LDR R1,[R0]
ORR R1,#GPIOB_EN
STR R1,[R0]

; Step 4. Enable output mode


LDR R0,=GPIOB_BASE+MODER_OFFSET

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 112 / 178
Interfacing methods 4.5 General purpose input output ports

GPIO output ports

LDR R1,[R0]
MOV32 R2,#MODER3+MODER4+MODER5+MODER10
ORR R1,R2
LSL R2,R2,#1
BIC R1,R2
STR R1,[R0]

BX LR

Delay
SUBS R2,#1 ; Decrement R2
BNE Delay ; if zero yet
BX LR ; Return

ALIGN
END

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 113 / 178
Interfacing methods 4.5 General purpose input output ports

GPIO output ports

 Example 4.9 (Full-step motor control)


Consider a step motor control circuit as follows
STM32G431RBT6 ULN2003
12 24 PA7 B2 1 16 Blue (1) A1
PA0 PB0 IN1 OUT1
13 25 PA8 A1 2 15 Pink (2)
PA1 PB1 IN2 OUT2

N
14 26 PA9 B1 3 14 Yellow (3) A0
PA2 PB2 IN3 OUT3
17 56 PA10 A2 4 13 Orange (4)
PA3 PB3 IN4 OUT4

S
18 57 5 12 A2
PA4 PB4 IN5 OUT5
19 58 6 11
PA5 PB5 IN6 OUT6
20 59 7 10
PA6 PB6 IN7 OUT7
PA7 21 60 8 9
PA7 PB7 GND COM B1 B0 B2
PA8 42
PA8
PA9 43 62
PA9 PB9
PA10 44 30 Red (5)
PA10 PB10 +VM
45 33
PA11 PB11
46 34
PA12 PB12
49 35
PA13 PB13
50 36
PA14 PB14
51 37
PA15 PB15

55
PD2

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 117 / 178
Interfacing methods 4.5 General purpose input output ports

GPIO output ports

Solution:

Code 4.6 (Stepper Motor Full Step)

MODER7 EQU 1<<14


MODER8 EQU 1<<16
MODER9 EQU 1<<18
MODER10 EQU 1<<20

BIT7 EQU 1<<7


BIT8 EQU 1<<8
BIT9 EQU 1<<9
BIT10 EQU 1<<10

RCC_BASE EQU 0x40021000


AHB2ENR_OFFSET EQU 0x4C

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 120 / 178
Interfacing methods 4.5 General purpose input output ports

GPIO output ports

GPIOA_BASE EQU 0x48000000


GPIOA_EN EQU 1<<0

GPIOB_BASE EQU 0x48000400


GPIOB_EN EQU 1<<1

GPIOC_BASE EQU 0x48000800


GPIOC_EN EQU 1<<2

MODER_OFFSET EQU 0x00


IDR_OFFSET EQU 0x10
ODR_OFFSET EQU 0x14

A1 EQU BIT8 ; Pink


A2 EQU BIT10 ; Orange
B1 EQU BIT9 ; Yellow
B2 EQU BIT7 ; Blue

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 121 / 178
Interfacing methods 4.5 General purpose input output ports

GPIO output ports

DLVAL EQU 100000 ;

Stack_Size EQU 0x00000400

AREA STACK,NOINIT,READWRITE,ALIGN=3

Stack_Mem SPACE Stack_Size

AREA RESET,DATA, READONLY


EXPORT __Vectors
__Vectors
DCD Stack_Mem+Stack_Size
DCD Reset_Handler
ALIGN

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


THUMB

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 122 / 178
Interfacing methods 4.5 General purpose input output ports

GPIO output ports

ENTRY
EXPORT Reset_Handler
GPIO_Init
; Step 1. Enabled the clock to the GPIOA Port
LDR R0,=RCC_BASE+AHB2ENR_OFFSET
LDR R1,[R0]
ORR R1,#GPIOA_EN
STR R1,[R0]
NOP
NOP

; Step 2. Enable output mode


LDR R0,=GPIOA_BASE+MODER_OFFSET
LDR R1,[R0]
MOV R2,#MODER7+MODER8+MODER9+MODER10
ORR R1,R2
LSL R2,R2,#1

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 123 / 178
Interfacing methods 4.5 General purpose input output ports

GPIO output ports

BIC R1,R2
STR R1,[R0]

BX LR
Reset_Handler
BL GPIO_Init
Start
LDR R0,=GPIOA_BASE+ODR_OFFSET
LDR R1,[R0]
Loop
AND R1,#A2+B1+B2 ;
ORR R1,#A2+B1+B2 ;
STR R1,[R0] ;
LDR R2,=DLVAL ;
BL Delay

AND R1,#A1+A2+B2 ;

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 124 / 178
Interfacing methods 4.5 General purpose input output ports

GPIO output ports

ORR R1,#A1+A2+B2 ;
STR R1,[R0] ;
LDR R2,=DLVAL ;
BL Delay

AND R1,#A1+B1+B2 ;
ORR R1,#A1+B1+B2 ;
STR R1,[R0] ;
LDR R2,=DLVAL ;
BL Delay

AND R1,#A1+A2+B1 ;
ORR R1,#A1+A2+B1 ;
STR R1,[R0] ;
LDR R2,=DLVAL ;
BL Delay

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 125 / 178
Interfacing methods 4.5 General purpose input output ports

GPIO output ports

B Loop ;
Delay
SUBS R2,#1 ; Decrement R2
BNE Delay ; if zero yet
BX LR ; Return

ALIGN
END

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 126 / 178
Interfacing methods 4.6 GPIO Port interfaces

74HC595 shift register

 Example 4.11
Consider a circuit of the STM32G431RBT6 shown in the figure. Write a program
to send bits 10100100 to IC 74HC595.
U1 STM32G431RBT6 U2
12 24 CLK 11 15
PA0 PB0 SH_CP Q0
13 25 DAT 14 1
PA1 PB1 DS Q1
14 26 2
PA2 PB2 Q2
17 56 LAT LAT 12 3
PA3 PB3 ST_CP Q3
18 57 DAT 4
PA4 PB4 Q4
19 58 SCK 5
PA5 PB5 Q5
59 6
PB6 VCC Q6
60 10 7
PB7 MR Q7
13 9
8 OE Q7'
PC0 62
9 PB9
PC1 30
10 PB10
PC2 33
11 PB11
PC3 34
22 PB12
PC4 35
23 PB13
PC5 36
38 PB14
PC6 37
39 PB15
PC7
40
PC8
41 55
PC9 PD2
52
PC10
53
PC11
54
PC12
2
PC13

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 137 / 178
Interfacing methods 4.6 GPIO Port interfaces

74HC595 shift register

Solution:

Code 4.7 (HC595 Send Bits)

INCLUDE ../inc/stm32G431xx.s

HC595_SCK EQU BIT13


HC595_LAT EQU BIT12
HC595_DAT EQU BIT15

Stack_Size EQU 0x00000400

AREA STACK,NOINIT,READWRITE,ALIGN=3

Stack_Mem SPACE Stack_Size

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 138 / 178
Interfacing methods 4.6 GPIO Port interfaces

74HC595 shift register

AREA RESET,DATA, READONLY


EXPORT __Vectors
__Vectors
DCD Stack_Mem+Stack_Size
DCD Reset_Handler
ALIGN

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


THUMB
ENTRY
EXPORT Reset_Handler
Reset_Handler
BL GPIO_Init
Start
LDR R0,=GPIOB_ODR ;
BIC R1,#HC595_LAT ; LAT = 0
STR R1,[R0] ;

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 139 / 178
Interfacing methods 4.6 GPIO Port interfaces

74HC595 shift register

; Bit 7
ORR R1,#HC595_DAT ;
STR R1,[R0] ; DAT = 1

BIC R1,#HC595_SCK ;
STR R1,[R0] ; CLK = 0

ORR R1,#HC595_SCK ;
STR R1,[R0] ; CLK = 1

; Bit 6
BIC R1,#HC595_DAT ;
STR R1,[R0] ; DAT = 0

BIC R1,#HC595_SCK ;
STR R1,[R0] ; CLK = 0

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 140 / 178
Interfacing methods 4.6 GPIO Port interfaces

74HC595 shift register

ORR R1,#HC595_SCK ;
STR R1,[R0] ; CLK = 1

; Bit 5
ORR R1,#HC595_DAT ;
STR R1,[R0] ; DAT = 1

BIC R1,#HC595_SCK ;
STR R1,[R0] ; CLK = 0

ORR R1,#HC595_SCK ;
STR R1,[R0] ; CLK = 1

; Bit 4
BIC R1,#HC595_DAT ;
STR R1,[R0] ; DAT = 0

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 141 / 178
Interfacing methods 4.6 GPIO Port interfaces

74HC595 shift register

BIC R1,#HC595_SCK ;
STR R1,[R0] ; CLK = 0

ORR R1,#HC595_SCK ;
STR R1,[R0] ; CLK = 1

; Bit 3
BIC R1,#HC595_DAT ;
STR R1,[R0] ; DAT = 0

BIC R1,#HC595_SCK ;
STR R1,[R0] ; CLK = 0

ORR R1,#HC595_SCK ;
STR R1,[R0] ; CLK = 1

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 142 / 178
Interfacing methods 4.6 GPIO Port interfaces

74HC595 shift register

; Bit 2
ORR R1,#HC595_DAT ;
STR R1,[R0] ; DAT = 1

BIC R1,#HC595_SCK ;
STR R1,[R0] ; CLK = 0

ORR R1,#HC595_SCK ;
STR R1,[R0] ; CLK = 1

; Bit 1
BIC R1,#HC595_DAT ;
STR R1,[R0] ; DAT = 0

BIC R1,#HC595_SCK ;
STR R1,[R0] ; CLK = 0

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 143 / 178
Interfacing methods 4.6 GPIO Port interfaces

74HC595 shift register

ORR R1,#HC595_SCK ;
STR R1,[R0] ; CLK = 1

; Bit 0
BIC R1,#HC595_DAT ;
STR R1,[R0] ; DAT = 0

BIC R1,#HC595_SCK ;
STR R1,[R0] ; CLK = 0

ORR R1,#HC595_SCK ;
STR R1,[R0] ; CLK = 1

ORR R1,#HC595_LAT ;
STR R1,[R0] ; LAT = 1
Loop
B Loop

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 144 / 178
Interfacing methods 4.6 GPIO Port interfaces

74HC595 shift register

GPIO_Init
; Step 1. Enabled the clock to the GPIOB Port
LDR R0,=RCC_AHB2ENR
LDR R1,[R0]
ORR R1,#RCC_AHB2ENR_GPIOBEN
STR R1,[R0]

; Step 2. Enable output mode


LDR R0,=GPIOB_MODER
LDR R1,[R0]
MOV R2,#GPIO_MODER_MODE15_0+GPIO_MODER_MODE13_0
+ GPIO_MODER_MODE12_0
ORR R1,R2
LSL R2,R2,#1
BIC R1,R2
STR R1,[R0]

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 145 / 178
Interfacing methods 4.6 GPIO Port interfaces

74HC595 shift register

GPIO_Init
BX LR

ALIGN
END

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 146 / 178
Interfacing methods 4.6 GPIO Port interfaces

74HC595 shift register

 Example 4.12
Consider a circuit of the STM32G431RBT6 and IC 74HC595 as shown in the
figure. Write a program to display number 2 on the LED.
U1 STM32G431RBT6 U2 Q1
R1 - R8
12 24 CLK 11 15 A
PA0 PB0 SH_CP Q0
13 25 DAT 14 1 B
PA1 PB1 DS Q1
14 26 2 C
PA2 PB2 Q2
17 56 LAT LAT 12 3 D VDD
PA3 PB3 ST_CP Q3
18 57 DAT 4 E
PA4 PB4 Q4
19 58 CLK 5 F
PA5 PB5 Q5
59 6 G
PB6 VCC Q6
60 10 7 H
PB7 MR Q7
13 9
8 OE Q7'
PC0 62
9 PB9
PC1 30
10 PB10
PC2 33
11 PB11
PC3 34
22 PB12
PC4 35
23 PB13
PC5 36
38 PB14
PC6 37
39 PB15
PC7
40
PC8
41 55
PC9 PD2
52
PC10
53
PC11
54
PC12
2
PC13

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 147 / 178
Interfacing methods 4.6 GPIO Port interfaces

74HC595 shift register

Solution:

Code 4.8 (HC595 7Seg LED 1D)

INCLUDE ../inc/stm32G431xx.s

HC595_SCK EQU BIT5


HC595_LAT EQU BIT3
HC595_DAT EQU BIT4

Stack_Size EQU 0x00000400

AREA STACK,NOINIT,READWRITE,ALIGN=3

Stack_Mem SPACE Stack_Size

AREA RESET,DATA, READONLY

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 148 / 178
Interfacing methods 4.6 GPIO Port interfaces

74HC595 shift register

EXPORT __Vectors
__Vectors
DCD Stack_Mem+Stack_Size
DCD Reset_Handler
ALIGN

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


THUMB
ENTRY
EXPORT Reset_Handler
Reset_Handler
BL GPIO_Init
Start
MOV R3,#0xA4
LSL R3,R3,#24

LDR R0,=GPIOB_ODR ;

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 149 / 178
Interfacing methods 4.6 GPIO Port interfaces

74HC595 shift register

BIC R1,#HC595_LAT ; LAT = 0


STR R1,[R0] ;
NOP

MOV R4,#1 ; Rotating operator


shiftout
MOV R5,#7 ; R5 = Counter
shiftcheck
CMP R5,#0x00 ; R5 = 0?
BGE shift ;
B shiftend ;
shift
SUBS R5, R5, #1 ; R5--
ROR R4, #1 ;
TST R3, R4 ;
BNE setbit ;

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 150 / 178
Interfacing methods 4.6 GPIO Port interfaces

74HC595 shift register

BIC R1,#HC595_DAT ;
STR R1,[R0] ; DAT = 0
NOP

BIC R1,#HC595_SCK ;
STR R1,[R0] ; CLK = 0
NOP

ORR R1,#HC595_SCK ;
STR R1,[R0] ; CLK = 1
NOP

B shiftcheck ;
setbit
ORR R1,#HC595_DAT ;
STR R1,[R0] ; DAT = 1
NOP

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 151 / 178
Interfacing methods 4.6 GPIO Port interfaces

74HC595 shift register

BIC R1,#HC595_SCK ;
STR R1,[R0] ; CLK = 0
NOP

ORR R1,#HC595_SCK ;
STR R1,[R0] ; CLK = 1
NOP

B shiftcheck ;
shiftend
ORR R1,#HC595_LAT ;
STR R1,[R0] ; LAT = 1
NOP
Loop
B Loop

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 152 / 178
Interfacing methods 4.6 GPIO Port interfaces

74HC595 shift register

GPIO_Init
; Step 1. Enabled the clock to the GPIOB Port
LDR R0,=RCC_AHB2ENR
LDR R1,[R0]
ORR R1,#RCC_AHB2ENR_GPIOBEN
STR R1,[R0]
NOP

; Step 2. Enable output mode


LDR R0,=GPIOB_MODER
LDR R1,[R0]
MOV R2,#GPIO_MODER_MODE15_0+GPIO_MODER_MODE13_0
... +GPIO_MODER_MODE12_0
ORR R1,R2
LSL R2,R2,#1
BIC R1,R2
STR R1,[R0]

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 153 / 178
Interfacing methods 4.6 GPIO Port interfaces

74HC595 shift register

BX LR

ALIGN
END

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 154 / 178
Interfacing methods 4.6 GPIO Port interfaces

MAX7219 driver control

 Example 4.17 (MAX7219)


Consider a circuit of the STM32G431RBT6 with IC MAX7219 and eight seven-
segment LEDs as shown in the figure. Write a program to display numbers 12345678
on the LEDs.
STM32G431RBT6
12 24
PA0 PB0
13 25
PA1 PB1
14 26
PA2 PB2
17 56
PA3 PB3
18 57
PA4 PB4
19 58
PA5 PB5
59
PB6
60
PB7
8 62 VCC
PC0 PB9
9 30
PC1 PB10
10 33
PC2 PB11
11 34 LAT
PC3 PB12
22 35 SCK 10K
PC4 PB13
23 36
PC5 PB14
38 37 DIN 18 DIGITS
PC6 PB15 ISET DIG0 - DIG7
39
PC7 U2
40
PC8 DIN 1
41 55 DIN
PC9 PD2
52 LAT 12
PC10 LOAD (CS)
53
PC11 SCK 13
54 CLK SEGMENTS
PC12 SEG A-G
2
PC13 SEG DP
9
GND
GND
MAX7219

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 162 / 229
Interfacing methods 4.6 GPIO Port interfaces

MAX7219 driver control

Solution:

Code 4.8

MODER12 EQU 1<<24


MODER13 EQU 1<<26
MODER15 EQU 1<<30

BIT12 EQU 1<<12


BIT13 EQU 1<<13
BIT15 EQU 1<<15

RCC_BASE EQU 0x40021000


AHB2ENR_OFFSET EQU 0x4C

GPIOA_BASE EQU 0x48000000


GPIOA_EN EQU 1<<0

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 163 / 229
Interfacing methods 4.6 GPIO Port interfaces

MAX7219 driver control

GPIOB_BASE EQU 0x48000400


GPIOB_EN EQU 1<<1

GPIOC_BASE EQU 0x48000800


GPIOC_EN EQU 1<<2

MODER_OFFSET EQU 0x00


IDR_OFFSET EQU 0x10
ODR_OFFSET EQU 0x14
BSRR_OFFSET EQU 0x18

MAX7219_LED EQU BIT12


MAX7219_CLK EQU BIT13
MAX7219_DIN EQU BIT15

Stack_Size EQU 0x00000400

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 164 / 229
Interfacing methods 4.6 GPIO Port interfaces

MAX7219 driver control

AREA STACK,NOINIT,READWRITE,ALIGN=3
Stack_Mem SPACE Stack_Size

AREA RESET,DATA, READONLY


EXPORT __Vectors
__Vectors
DCD Stack_Mem+Stack_Size
DCD Reset_Handler
ALIGN

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


THUMB
ENTRY
EXPORT Reset_Handler
GPIO_Init
; Step 1. Enabled the clock to the GPIOB Port

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 165 / 229
Interfacing methods 4.6 GPIO Port interfaces

MAX7219 driver control

LDR R0,=RCC_BASE+AHB2ENR_OFFSET
LDR R1,[R0]
ORR R1,#GPIOB_EN
STR R1,[R0]

; Step 2. Enable output mode


LDR R0,=GPIOB_BASE+MODER_OFFSET
LDR R1,[R0]
MOV R2,#MODER12+MODER13+MODER15
ORR R1,R2
LSL R2,R2,#1
BIC R1,R2
STR R1,[R0]

BX LR

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 166 / 229
Interfacing methods 4.6 GPIO Port interfaces

MAX7219 driver control

shiftout
MOV R4,#1 ; Rotating operator
; digitalWrite(MAX7219_LED, LOW)
LDR R0,=GPIOB_BASE+ODR_OFFSET
LDR R1,[R0]
BIC R1,#MAX7219_LED ; LAT = 0
STR R1,[R0] ;

MOV R5,#15 ; R5 = Counter


shiftcheck
CMP R5,#0x00 ; R5 = 0?
BGE shift ;
B shiftend ;
shift
SUBS R5, R5, #1 ; R5--

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 167 / 229
Interfacing methods 4.6 GPIO Port interfaces

MAX7219 driver control

ROR R4, #1 ;
TST R3, R4 ;
BNE setbit ;

BIC R1,#MAX7219_DIN ;
STR R1,[R0] ; DAT = 0

ORR R1,#MAX7219_CLK ;
STR R1,[R0] ; CLK = 1

BIC R1,#MAX7219_CLK ;
STR R1,[R0] ; CLK = 0

B shiftcheck ;
setbit
ORR R1,#MAX7219_DIN ;

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 168 / 229
Interfacing methods 4.6 GPIO Port interfaces

MAX7219 driver control

STR R1,[R0] ; DAT = 1

ORR R1,#MAX7219_CLK ;
STR R1,[R0] ; CLK = 1

BIC R1,#MAX7219_CLK ;
STR R1,[R0] ; CLK = 0

B shiftcheck ;
shiftend
;digitalWrite(MAX7219_LED, HIGH);
ORR R1,#MAX7219_LED
STR R1,[R0] ; LAT = 1

BX LR

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 169 / 229
Interfacing methods 4.6 GPIO Port interfaces

MAX7219 driver control

Reset_Handler
BL GPIO_Init
MAX7219_Init
LDR R0,=GPIOB_BASE+ODR_OFFSET
LDR R1,[R0]

;digitalWrite(MAX7219_LED, HIGH);
ORR R1,#MAX7219_LED
STR R1,[R0] ; LAT = 1
NOP
NOP

; sendWord(LED0, 0x0F, 0x00)


MOV R3,#0x0F00
LSL R3,R3,#16
BL shiftout

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 170 / 229
Interfacing methods 4.6 GPIO Port interfaces

MAX7219 driver control

; sendWord(LED0, 0x0C, 0x00);


MOV R3,#0x0C00
LSL R3,R3,#16
BL shiftout

; sendWord(LED0, 0x0A, 0x03);


MOV R3,#0x0A03
LSL R3,R3,#16
BL shiftout

; sendWord(LED0, 0x09, 0xFF);


MOV R3,#0x09FF
LSL R3,R3,#16
BL shiftout

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 171 / 229
Interfacing methods 4.6 GPIO Port interfaces

MAX7219 driver control

; sendWord(LED0, 0x01, 0x0F);


MOV R3,#0x010F
LSL R3,R3,#16
BL shiftout

; sendWord(LED0, 0x02, 0x0F);


MOV R3,#0x020F
LSL R3,R3,#16
BL shiftout

; sendWord(LED0, 0x03, 0x0F);


MOV R3,#0x030F
LSL R3,R3,#16
BL shiftout

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 172 / 229
Interfacing methods 4.6 GPIO Port interfaces

MAX7219 driver control

; sendWord(LED0, 0x04, 0x0F);


MOV R3,#0x040F
LSL R3,R3,#16
BL shiftout

; sendWord(LED0, 0x05, 0x0F);


MOV R3,#0x050F
LSL R3,R3,#16
BL shiftout

; sendWord(LED0, 0x06, 0x0F);


MOV R3,#0x060F
LSL R3,R3,#16
BL shiftout

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 173 / 229
Interfacing methods 4.6 GPIO Port interfaces

MAX7219 driver control

; sendWord(LED0, 0x07, 0x0F);


MOV R3,#0x070F
LSL R3,R3,#16
BL shiftout

; sendWord(LED0, 0x08, 0x0F);


MOV R3,#0x080F
LSL R3,R3,#16
BL shiftout

; sendWord(LED0, 0x0B, 0x07);


MOV R3,#0x0B07
LSL R3,R3,#16
BL shiftout

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 174 / 229
Interfacing methods 4.6 GPIO Port interfaces

MAX7219 driver control

; sendWord(LED0, 0x0C, 0x01);


MOV R3,#0x0C01
LSL R3,R3,#16
BL shiftout
Start
; sendWord(LED0, 0x05, 0x01);
MOV R3,#0x0801
LSL R3,R3,#16
BL shiftout

MOV R3,#0x0702
LSL R3,R3,#16
BL shiftout

MOV R3,#0x0603
LSL R3,R3,#16

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 175 / 229
Interfacing methods 4.6 GPIO Port interfaces

MAX7219 driver control

BL shiftout

MOV R3,#0x0504
LSL R3,R3,#16
BL shiftout

MOV R3,#0x0405
LSL R3,R3,#16
BL shiftout

MOV R3,#0x0306
LSL R3,R3,#16
BL shiftout

MOV R3,#0x0207
LSL R3,R3,#16

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 176 / 229
Interfacing methods 4.6 GPIO Port interfaces

MAX7219 driver control

BL shiftout

MOV R3,#0x0108
LSL R3,R3,#16
BL shiftout
Loop
B Loop

ALIGN
END

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 177 / 229
Interfacing methods 4.7 GPIO input port

Input button

 Example 4.18
An LED is connected to pin PA5 along with a button connected to PC13 as shown
in the figure. Write a program to light up the LED whenever the button is pressed.
STM32G431RBT6
12 24
PA0 PB0
13 25
PA1 PB1
14 26
PA2 PB2
17 56
PA3 PB3
N1 18 57
R1 PA4 PB4
19 58
PA5 PB5
59
PB6
60
PB7
VDD 8
PC0 62
9 PB9
PC1 30
10 PB10
PC2 33
11 PB11
B1 PC3 34
22 PB12
PC4 35
23 PB13
PC5 36
38 PB14
PC6 37
39 PB15
PC7
40
PC8
41 55
PC9 PD2
52
PC10
53
PC11
54
PC12
2
PC13

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 179 / 229
Interfacing methods 4.7 GPIO input port

Input button and LED

Code 4.9 (Input button)

MODER5 EQU 1<<10


MODER6 EQU 1<<12

PUPDR6 EQU 1<<12

BIT5 EQU 1<<5


BIT6 EQU 1<<6

RCC_BASE EQU 0x40021000


AHB2ENR_OFFSET EQU 0x4C

GPIOA_BASE EQU 0x48000000


GPIOA_EN EQU 1<<0

GPIOC_BASE EQU 0x48000800


GPIOC_EN EQU 1<<2

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 182 / 229
Interfacing methods 4.7 GPIO input port

Input button and LED

MODER_OFFSET EQU 0x00


PUPDR_OFFSET EQU 0x0C
IDR_OFFSET EQU 0x10
ODR_OFFSET EQU 0x14
BSRR_OFFSET EQU 0x18

LED EQU BIT5


BUTTON EQU BIT6

Stack_Size EQU 0x00000400

AREA STACK,NOINIT,READWRITE,ALIGN=3
Stack_Mem SPACE Stack_Size

AREA RESET,DATA, READONLY


EXPORT __Vectors

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 183 / 229
Interfacing methods 4.7 GPIO input port

Input button and LED

__Vectors
DCD Stack_Mem+Stack_Size
DCD Reset_Handler
ALIGN

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


THUMB
ENTRY
EXPORT Reset_Handler
GPIO_Init
; Step 1. Enabled the clock to the GPIOA Port
LDR R0,=RCC_BASE+AHB2ENR_OFFSET
LDR R1,[R0]
ORR R1,#GPIOA_EN
STR R1,[R0]
NOP

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 184 / 229
Interfacing methods 4.7 GPIO input port

Input button and LED

; Step 2. Enable output mode


LDR R0,=GPIOA_BASE+MODER_OFFSET
LDR R1,[R0]
MOV R2,#MODER5
ORR R1,R2
LSL R2,R2,#1
BIC R1,R2
STR R1,[R0]

; Step 3. Enabled the clock to the GPIOC Port


LDR R0,=RCC_BASE+AHB2ENR_OFFSET
LDR R1,[R0]
ORR R1,#GPIOC_EN
STR R1,[R0]
NOP
NOP

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 185 / 229
Interfacing methods 4.7 GPIO input port

Input button and LED

; Step 4. Enable input mode


LDR R0,=GPIOC_BASE+MODER_OFFSET
LDR R1,[R0]
MOV R2,#MODER6
BIC R1,R2
LSL R2,R2,#1
BIC R1,R2
STR R1,[R0]

; Step 7. Enable pull-down resistor


LDR R0,=GPIOC_BASE+PUPDR_OFFSET
LDR R1,[R0]
MOV32 R2,#PUPDR6
BIC R1,R2
LSL R2,R2,#1
ORR R1,R2

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 186 / 229
Interfacing methods 4.7 GPIO input port

Input button and LED

STR R1,[R0]

BX LR
Reset_Handler
BL GPIO_Init
Loop
LDR R1,=GPIOC_BASE+IDR_OFFSET
LDR R0,[R1]
AND R0,R0,#BUTTON
CMP R0,#BUTTON
BEQ LED_ON
CMP R0,#0x0000
BEQ LED_OFF

B Loop
LED_OFF

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 187 / 229
Interfacing methods 4.7 GPIO input port

Input button and LED

LDR R0,=GPIOA_BASE+ODR_OFFSET
LDR R1,=LED
BIC R1,#LED
STR R1,[R0]

B Loop
LED_ON
LDR R0,=GPIOA_BASE+ODR_OFFSET
LDR R1,=LED
STR R1,[R0]

B Loop

ALIGN
END

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 188 / 229
Interfacing methods 4.7 GPIO input port

Input button toggling an LED

 Example 4.19
An LED is connected to pin PA5 along with a button connected to PC13 as shown
in the figure. Write a program to toggle the LED whenever the button is pressed.
STM32G431RBT6
12 24
PA0 PB0
13 25
PA1 PB1
14 26
PA2 PB2
17 56
PA3 PB3
N1 18 57
R1 PA4 PB4
19 58
PA5 PB5
59
PB6
60
PB7
VDD 8
PC0 62
9 PB9
PC1 30
10 PB10
PC2 33
11 PB11
B1 PC3 34
22 PB12
PC4 35
23 PB13
PC5 36
38 PB14
PC6 37
39 PB15
PC7
40
PC8
41 55
PC9 PD2
52
PC10
53
PC11
54
PC12
2
PC13

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 189 / 229
Interfacing methods 4.7 GPIO input port

Input button toggling an LED

Solution:

Code 4.10 (Input button)

Loop
LDR R1,=GPIOC_BASE+IDR_OFFSET
LDR R0,[R1]
AND R0,R0,#BUTTON
CMP R0,#BUTTON
BNE Loop
LDR R0,=GPIOA_BASE+ODR_OFFSET
EOR R2,R2,#LED
STR R2,[R0]

B Loop

ALIGN
END

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 190 / 229
Interfacing methods 4.7 GPIO input port

Anti-bouncing phenomenon

 Example 4.20
Employing the algorithm shown in the below flowchart in order to eliminate the
bouncing phenomenon.
Start

Count = 0
Num = 0

Count = 0

Key pressed?
-

+
Count++

- Count >= 100?


+ Toggle LED

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 192 / 229
Interfacing methods 4.7 GPIO input port

Anti-bouncing phenomenon

Solution:

Code 4.11

MODER5 EQU 1<<10


MODER6 EQU 1<<12
PUPDR6 EQU 1<<12

BIT5 EQU 1<<5


BIT6 EQU 1<<6

RCC_BASE EQU 0x40021000


AHB2ENR_OFFSET EQU 0x4C

GPIOA_BASE EQU 0x48000000


GPIOA_EN EQU 1<<0
GPIOC_BASE EQU 0x48000800
GPIOC_EN EQU 1<<2

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 193 / 229
Interfacing methods 4.7 GPIO input port

Anti-bouncing phenomenon

MODER_OFFSET EQU 0x00


PUPDR_OFFSET EQU 0x0C
IDR_OFFSET EQU 0x10
ODR_OFFSET EQU 0x14
BSRR_OFFSET EQU 0x18

LED EQU BIT5

BUTTON EQU BIT6

Stack_Size EQU 0x00000400


AREA STACK,NOINIT,READWRITE,ALIGN=3
Stack_Mem SPACE Stack_Size

AREA RESET,DATA, READONLY


EXPORT __Vectors

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 194 / 229
Interfacing methods 4.7 GPIO input port

Anti-bouncing phenomenon

__Vectors
DCD Stack_Mem+Stack_Size
DCD Reset_Handler
ALIGN

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


THUMB
ENTRY
EXPORT Reset_Handler
GPIO_Init
; Step 1. Enabled the clock to the GPIOA Port
LDR R0,=RCC_BASE+AHB2ENR_OFFSET
LDR R1,[R0]
ORR R1,#GPIOA_EN
STR R1,[R0]
NOP

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 195 / 229
Interfacing methods 4.7 GPIO input port

Anti-bouncing phenomenon

; Step 2. Enable output mode


LDR R0,=GPIOA_BASE+MODER_OFFSET
LDR R1,[R0]
MOV R2,#MODER5
ORR R1,R2
LSL R2,R2,#1
BIC R1,R2
STR R1,[R0]

; Step 3. Enabled the clock to the GPIOC Port


LDR R0,=RCC_BASE+AHB2ENR_OFFSET
LDR R1,[R0]
ORR R1,#GPIOC_EN
STR R1,[R0]
NOP
NOP

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 196 / 229
Interfacing methods 4.7 GPIO input port

Anti-bouncing phenomenon

; Step 4. Enable input mode


LDR R0,=GPIOC_BASE+MODER_OFFSET
LDR R1,[R0]
MOV32 R2,#MODER6
BIC R1,R2
LSL R2,R2,#1
BIC R1,R2
STR R1,[R0]

; Step 5. Enable pull-down resistor


LDR R0,=GPIOC_BASE+PUPDR_OFFSET
LDR R1,[R0]
MOV R2,#PUPDR6
BIC R1,R2
LSL R2,R2,#1
ORR R1,R2

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 197 / 229
Interfacing methods 4.7 GPIO input port

Anti-bouncing phenomenon

STR R1,[R0]

BX LR
Reset_Handler
BL GPIO_Init

LDR R2,=LED
MOV32 R4,#150000
Loop
LDR R1,=GPIOC_BASE+IDR_OFFSET
LDR R0,[R1]
AND R0,R0,#BUTTON

CMP R0,#BUTTON
BEQ KeyPressed

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 198 / 229
Interfacing methods 4.7 GPIO input port

Anti-bouncing phenomenon

CounterReset
MOV32 R3,#0x00000000
B Loop
KeyPressed
ADD R3,R3,#1
CMP R3,R4
BGE ToggleLED
B Loop
ToggleLED
LDR R0,=GPIOA_BASE+ODR_OFFSET
EOR R2,R2,#LED
STR R2,[R0]
B CounterReset

ALIGN
END

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 199 / 229
Interfacing methods 4.7 GPIO input port

Displaying input data

 Example 4.21
Consider a circuit of the STM32G431RBT6 microcontroller with the four digits
7-segment LED and two buttons as shown in the figure. Write Assembly codes to
implement a four-digit counter. When button B1 is pressed, the number of the
counter is increased by 1. Conversely, when button B2 is pressed, the number of
the counter is decreased by 1.
12 D1

D2

D3

D4
U1
Q1 R1 12 24
6
9

PA0 PB0
R2 13 25
PA1 PB1
11 A 14 26
PA2 PB2
7 B 17 56
PA3 PB3
4 C R3 18 57
PA4 PB4
2 D R4 19 58
PA5 PB5
1 E R5 20 59
PA6 PB6
10 F R6 21 60
PA7 PB7
5 G R7 42
PA8
3 H R8 43 62
PA9 PB9
30
PB10
33
PB11
YY2841AH-33 BUT1 38 34
PC6 PB12
B2 BUT2 39 35
BUT2 PC7 PB13
40 36
B1 PC8 PB14
BUT1 41 37
PC9 PB15
52

VCC
STM32G431RBT6

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 201 / 229
Interfacing methods 4.7 GPIO input port

Displaying input data

Solution:

Code 4.12 (7Seg LED and two buttons)

MODER0 EQU 1<<0


MODER1 EQU 1<<2
MODER3 EQU 1<<6
MODER4 EQU 1<<8
MODER5 EQU 1<<10
MODER6 EQU 1<<12
MODER7 EQU 1<<14
MODER8 EQU 1<<16
MODER9 EQU 1<<18
MODER10 EQU 1<<20

PUPDR6 EQU 1<<12


PUPDR7 EQU 1<<14

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 202 / 229
Interfacing methods 4.7 GPIO input port

Displaying input data

BIT0 EQU 1<<0


BIT1 EQU 1<<1
BIT4 EQU 1<<4
BIT5 EQU 1<<5
BIT6 EQU 1<<6
BIT7 EQU 1<<7
BIT8 EQU 1<<8
BIT9 EQU 1<<9

RCC_BASE EQU 0x40021000


AHB2ENR_OFFSET EQU 0x4C

GPIOA_BASE EQU 0x48000000


GPIOA_EN EQU 1<<0
GPIOB_BASE EQU 0x48000400
GPIOB_EN EQU 1<<1

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 203 / 229
Interfacing methods 4.7 GPIO input port

Displaying input data

GPIOC_BASE EQU 0x48000800


GPIOC_EN EQU 1<<2

MODER_OFFSET EQU 0x00


PUPDR_OFFSET EQU 0x0C
IDR_OFFSET EQU 0x10
ODR_OFFSET EQU 0x14

N0 EQU BIT0
N1 EQU BIT1
N2 EQU BIT4
N3 EQU BIT5
N4 EQU BIT6
N5 EQU BIT7
N6 EQU BIT8
N7 EQU BIT9

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 204 / 229
Interfacing methods 4.7 GPIO input port

Displaying input data

LED EQU 1<<5

BUTTON1 EQU BIT6


BUTTON2 EQU BIT7

DLVAL EQU 25000

Stack_Size EQU 0x00000400

AREA STACK,NOINIT,READWRITE,ALIGN=3

Stack_Mem SPACE Stack_Size

AREA RESET,DATA, READONLY


EXPORT __Vectors
__Vectors

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 205 / 229
Interfacing methods 4.7 GPIO input port

Displaying input data

DCD Stack_Mem+Stack_Size
DCD Reset_Handler
ALIGN

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


THUMB
ENTRY
EXPORT Reset_Handler
Reset_Handler
BL GPIO_Init
LDR R1,=LED_codes
MOV32 R9,#5
Start
MOV R4,#0 ; Counter number
MOV32 R5,#9999 ; Maximum number

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 206 / 229
Interfacing methods 4.7 GPIO input port

Displaying input data

Loop
CMP R4,R5
BGT Start

MOV R6,R4 ; R6 = dividend

MOV R0,#1000 ; divisor


UDIV R2,R6,R0 ; R2 = Thousand
MUL R3,R2,R0 ;
SUB R6,R6,R3 ; R6 = Redundant
MOV R0,#4
MUL R3,R2,R0 ; Number ofsset

LDR R0,=GPIOA_BASE+ODR_OFFSET
LDR R2,[R1,R3] ;
STR R2,[R0] ;

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 207 / 229
Interfacing methods 4.7 GPIO input port

Displaying input data

LDR R0,=GPIOB_BASE+ODR_OFFSET
MVN R3,#0x400 ;
STR R3,[R0] ;

LDR R2,=DLVAL ;
BL Delay

MOV R0,#100 ; divisor


UDIV R2,R6,R0 ; R2 = Thousand
MUL R3,R2,R0 ;
SUB R6,R6,R3 ; R6 = Redundant
MOV R0,#4
MUL R3,R2,R0 ; Number ofsset

LDR R0,=GPIOA_BASE+ODR_OFFSET
LDR R2,[R1,R3] ;

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 208 / 229
Interfacing methods 4.7 GPIO input port

Displaying input data

STR R2,[R0] ;
LDR R0,=GPIOB_BASE+ODR_OFFSET
MVN R3,#0x20 ;
STR R3,[R0] ;

LDR R2,=DLVAL ;
BL Delay ;

MOV R0,#10 ; divisor


UDIV R2,R6,R0 ; R2 = Thousand
MUL R3,R2,R0 ;
SUB R6,R6,R3 ; R7 = Redundant
MOV R0,#4
MUL R3,R2,R0 ; Number ofsset

LDR R0,=GPIOA_BASE+ODR_OFFSET

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 209 / 229
Interfacing methods 4.7 GPIO input port

Displaying input data

LDR R2,[R1,R3] ;
STR R2,[R0] ;
LDR R0,=GPIOB_BASE+ODR_OFFSET
MVN R3,#0x10 ;
STR R3,[R0] ;

LDR R2,=DLVAL ;
BL Delay ;

MOV R0,#4
MUL R3,R6,R0 ; Number ofsset

LDR R0,=GPIOA_BASE+ODR_OFFSET
LDR R2,[R1,R3] ;
STR R2,[R0] ;
LDR R0,=GPIOB_BASE+ODR_OFFSET

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 210 / 229
Interfacing methods 4.7 GPIO input port

Displaying input data

MVN R3,#0x8 ;
STR R3,[R0] ;

LDR R2,=DLVAL ;
BL Delay ;

; Check input button


LDR R0,=GPIOC_BASE+IDR_OFFSET
LDR R2,[R0]
AND R2,R2,#BUTTON1+BUTTON2

CMP R2,#BUTTON1
BEQ BUT1Pressed
CMP R2,#BUTTON2
BEQ BUT2Pressed
CounterReset

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 211 / 229
Interfacing methods 4.7 GPIO input port

Displaying input data

MOV32 R10,#0x00000000
B Loop
BUT1Pressed
ADD R10,R10,#1
CMP R10,R9
BGE CountDown
B Loop
CountDown
LDR R0,=GPIOA_BASE+ODR_OFFSET
EOR R2,R2,#LED
STR R2,[R0]
CMP R4,#1
BLT CounterReset
SUB R4,#1 ; Now increase the number by 1
B CounterReset
BUT2Pressed

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 212 / 229
Interfacing methods 4.7 GPIO input port

Displaying input data

ADD R10,R10,#1
CMP R10,R9
BGE CountUp
B Loop
CountUp
LDR R0,=GPIOA_BASE+ODR_OFFSET
EOR R2,R2,#LED
STR R2,[R0]
ADD R4,#1 ; Now increase the number by 1
B CounterReset
GPIO_Init
; Step 1. Enable the clock to the GPIOA Port
LDR R0,=RCC_BASE+AHB2ENR_OFFSET
LDR R1,[R0]
ORR R1,#GPIOA_EN
STR R1,[R0]

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 213 / 229
Interfacing methods 4.7 GPIO input port

Displaying input data

; Step 2. Enable output mode


LDR R0,=GPIOA_BASE+MODER_OFFSET
LDR R1,[R0]
MOV32 R2,#MODER0+MODER1+MODER4+MODER5+MODER6
... +MODER7+MODER8+MODER9
ORR R1,R2
LSL R2,R2,#1
BIC R1,R2
STR R1,[R0]

; Step 3. Enable the clock to the GPIOB Port


LDR R0,=RCC_BASE+AHB2ENR_OFFSET
LDR R1,[R0]
ORR R1,#GPIOB_EN
STR R1,[R0]
NOP

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 214 / 229
Interfacing methods 4.7 GPIO input port

Displaying input data

; Step 4. Enable output mode


LDR R0,=GPIOB_BASE+MODER_OFFSET
LDR R1,[R0]
MOV32 R2,#MODER3+MODER4+MODER5+MODER10
ORR R1,R2
LSL R2,R2,#1
BIC R1,R2
STR R1,[R0]

; Step 5. Enabled the clock to the GPIOC Port


LDR R0,=RCC_BASE+AHB2ENR_OFFSET
LDR R1,[R0]
ORR R1,#GPIOC_EN
STR R1,[R0]
NOP

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 215 / 229
Interfacing methods 4.7 GPIO input port

Displaying input data

; Step 6. Enable input mode


LDR R0,=GPIOC_BASE+MODER_OFFSET
LDR R1,[R0]
MOV32 R2,#MODER6+MODER7
BIC R1,R2
LSL R2,R2,#1
BIC R1,R2
STR R1,[R0]

; Step 7. Enable pull-down resistor


LDR R0,=GPIOC_BASE+PUPDR_OFFSET
LDR R1,[R0]
MOV32 R2,#PUPDR6+PUPDR7
BIC R1,R2
LSL R2,R2,#1
ORR R1,R2

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 216 / 229
Interfacing methods 4.7 GPIO input port

Displaying input data

STR R1,[R0]

BX LR
Delay
SUBS R2,#1 ; Decrement R2
BNE Delay ; if zero yet
BX LR ; Return

LED_codes DCD 0x0F3,0x012,0x163,0x133,0x192,0x1B1,


... 0x1F1,0x013,0x1F3,0x1B3

ALIGN
END

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 217 / 229
Interrupts

Chapter 5
Interrupts

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 1 / 97
Interrupts 5.3 Extended interrupts and events controller (EXTI)

Extended interrupts and events controller

 Example 5.1
Consider a circuit with the STM32G431RBT6 microcontroller and an LED as shown
in the figure. Write Assembly codes to toggle the LED using external interrupt on
pin PA1.
STM32G431RBT6
B1 12 24
PA0 PB0
13 25
VDD PA1 PB1
14 26
PA2 PB2
17 56
PA3 PB3
N1 18 57
R1 PA4 PB4
19 58
PA5 PB5
59
PB6
60
PB7
8 62
PC0 PB9
9 30
PC1 PB10
10 33
PC2 PB11
11 34
PC3 PB12

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 42 / 97
Interrupts 5.3 Extended interrupts and events controller (EXTI)

Extended interrupts and events controller


In order to initiate the input pin PA1 we will start as

; Enable clock for Port A


LDR R0,=RCC_AHB2ENR
LDR R1,[R0]
ORR R1,#RCC_AHB2ENR_GPIOAEN
STR R1,[R0]

; Enable input mode for PA1


LDR R0,=GPIOA_MODER
LDR R1,[R0]
MOV32 R2,#MODER1_0
BIC R1,R2
LSL R2,R2,#1
BIC R1,R2
STR R1,[R0]

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 43 / 97
Interrupts 5.3 Extended interrupts and events controller (EXTI)

Extended interrupts and events controller

; Enable pull-down resistor for PA1


LDR R0,=GPIOA_PUPDR
LDR R1,[R0]
MOV32 R2,#PUPDR1_0
BIC R1,R2
LSL R2,R2,#1
ORR R1,R2
STR R1,[R0]

Next, following Step 1 in Procedure 5.1, we define


RCC_APB2ENR_SYSCFGEN_Pos EQU 0
RCC_APB2ENR_SYSCFGEN_Msk EQU 0x1 << RCC_APB2ENR_SYSCFGEN_Pos
RCC_APB2ENR_SYSCFGEN EQU RCC_APB2ENR_SYSCFGEN_Msk

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 44 / 97
Interrupts 5.3 Extended interrupts and events controller (EXTI)

Extended interrupts and events controller


After that we enable clock for System Configuration Controller (SYSCFG) as follows

LDR R0,=RCC_APB2ENR
LDR R1,[R0]
ORR R1,#RCC_APB2ENR_SYSCFGEN
STR R1,[R0]

Since we will assign pin PA1 as an input interrupt source, we define

PERIPH_BASE EQU 0x40000000 ; Peripheral base address


APB2PERIPH_BASE EQU PERIPH_BASE + 0x00010000
SYSCFG_EXTIC1 EQU 0x08
SYSCFG_BASE EQU APB2PERIPH_BASE + 0x0000
SYSCFG_EXTICR1 EQU SYSCFG_BASE+SYSCFG_EXTIC1
SYSCFG_EXTICR1_EXTI1_Pos EQU 4
SYSCFG_EXTICR1_EXTI1_Msk EQU 0x7 << SYSCFG_EXTICR1_EXTI1_Pos
SYSCFG_EXTICR1_EXTI1 EQU SYSCFG_EXTICR1_EXTI1_Msk
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 45 / 97
Interrupts 5.3 Extended interrupts and events controller (EXTI)

Extended interrupts and events controller


We can now configure the EXTI1 as

LDR R0,=SYSCFG_EXTICR1
LDR R1,[R0]
BIC R1,#SYSCFG_EXTICR1_EXTI1 ; Line 1
STR R1,[R0]

Then the EXTI1 mask is disable by

LDR R0,=EXTI_IMR1
LDR R1,[R0]
ORR R1,#EXTI_IMR1_IM1
STR R1,[R0]

where
EXTI_IMR1_IM1_Pos EQU 1
EXTI_IMR1_IM1_Msk EQU 0x1 << EXTI_IMR1_IM1_Pos
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 46 / 97
Interrupts 5.3 Extended interrupts and events controller (EXTI)

Extended interrupts and events controller


EXTI_IMR1_IM1 EQU EXTI_IMR1_IM1_Msk
EXTI_BASE EQU APB2PERIPH_BASE + 0x0400
EXTI_IM1 EQU 0x00
EXTI_IMR1 EQU EXTI_BASE+EXTI_IM1

For Step 2, we define constants as


EXTI_RTS1 EQU 0x08
EXTI_RTSR1 EQU EXTI_BASE+EXTI_RTS1
EXTI_RTSR1_RT1_Pos EQU 1
EXTI_RTSR1_RT1_Msk EQU 0x1 << EXTI_RTSR1_RT1_Pos
EXTI_RTSR1_RT1 EQU EXTI_RTSR1_RT1_Msk

Next, we set the bit 1 of register EXTI RTSR1 as follows

LDR R0,=EXTI_RTSR1
LDR R1,[R0]
ORR R1,#EXTI_RTSR1_RT1
STR R1,[R0]

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 47 / 97
Interrupts 5.3 Extended interrupts and events controller (EXTI)

Extended interrupts and events controller


For Step 3, we define constants as

EXTI_FTS1 EQU 0x0C


EXTI_FTSR1 EQU EXTI_BASE+EXTI_FTS1
EXTI_FTSR1_FT1_Pos EQU 1
EXTI_FTSR1_FT1_Msk EQU 0x1 << EXTI_FTSR1_FT1_Pos
EXTI_FTSR1_FT1 EQU EXTI_FTSR1_FT1_Msk

Next, we clear the bit 1 of register EXTI RTSR1 as follows

LDR R0,=EXTI_FTSR1
LDR R1,[R0]
BIC R1,#EXTI_FTSR1_FT1
STR R1,[R0]

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 48 / 97
Interrupts 5.3 Extended interrupts and events controller (EXTI)

Extended interrupts and events controller


For Step 4, we define constants as

NVIC_ISER0 EQU 0xE000E100

EXTI1_IRQ_Pos EQU EXTI1_IRQn


EXTI1_IRQ_Msk EQU 0x1 << EXTI1_IRQ_Pos ;
EXTI1_IRQ_EN EQU EXTI1_IRQ_Msk

Note that the PA1 pin is in conjunction with the interrupt coming from the EXTI
line 1 and the EXT1 interrupt corresponds to number 7 in the NVIC vector table.
In addition, the NVIC interrupt set-enable register NVIC ISER0 is for interrupts 0
to 31. Thus, in order to enable interrupt number 7 we have to set bit 7 of register
NVIC ISER0. The following instructions can implement this

LDR R0,=NVIC_ISER0
LDR R1,=EXTI1_IRQ_EN
STR R1,[R0]

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 49 / 97
Interrupts 5.3 Extended interrupts and events controller (EXTI)

Extended interrupts and events controller


In the last step, we will change interrupt state EXTI1 to pending as follows

LDR R0,=NVIC_ISPR0
LDR R1,=EXTI1_IRQ_EN
STR R1,[r0]

The complete program codes are listed below.

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 50 / 97
Interrupts 5.3 Extended interrupts and events controller (EXTI)

Extended interrupts and events controller

Solution:

Code 5.1

INCLUDE ../inc/stm32G431xx.s

LED EQU BIT5


BUTTON EQU BIT1

TICK_LOAD_VAL EQU 0x00FFFFFF

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


THUMB
ENTRY
EXPORT Reset_Handler
EXPORT EXTI1_IRQHandler
Reset_Handler
BL GPIO_Init

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 51 / 97
Interrupts 5.3 Extended interrupts and events controller (EXTI)

Extended interrupts and events controller

LDR R5,=LED
Loop
B Loop
GPIO_Init
; Step 1. Enable clock for Port A
LDR R0,=RCC_AHB2ENR
LDR R1,[R0]
ORR R1,#RCC_AHB2ENR_GPIOAEN
STR R1,[R0]

; Step 2. Set PA5 as an output


LDR R0,=GPIOA_MODER
LDR R1,[R0]
BIC R1,R1,#MODER5
ORR R1,#MODER5_0

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 52 / 97
Interrupts 5.3 Extended interrupts and events controller (EXTI)

Extended interrupts and events controller

STR R1,[R0]
LDR R0,=GPIOA_ODR
LDR R1,=LED
STR R1,[R0]

; Step 3. Enable input mode for PA1


LDR R0,=GPIOA_MODER
LDR R1,[R0]
MOV32 R2,#MODER1_0
BIC R1,R2
LSL R2,R2,#1
BIC R1,R2
STR R1,[R0]

; Step 4. Enable pull-down resistor for PA1


LDR R0,=GPIOA_PUPDR

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 53 / 97
Interrupts 5.3 Extended interrupts and events controller (EXTI)

Extended interrupts and events controller

LDR R1,[R0]
MOV32 R2,#PUPDR1_0
BIC R1,R2
LSL R2,R2,#1
ORR R1,R2
STR R1,[R0]

; Step 5. Enable configuration controller clock


LDR R0,=RCC_AHB2ENR
LDR R1,[R0]
ORR R1,#RCC_APB2ENR_SYSCFGEN
STR R1,[R0]

; Step 6. Configure EXTI configuration Register


LDR R0,=SYSCFG_EXTICR1
LDR R1,[R0]

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 54 / 97
Interrupts 5.3 Extended interrupts and events controller (EXTI)

Extended interrupts and events controller

BIC R1,#SYSCFG_EXTICR1_EXTI1 ; Line 1


STR R1,[R0]

; Step 7. Disable the EXTI Mask


LDR R0,=EXTI_IMR1
LDR R1,[R0]
ORR R1,#EXTI_IMR1_IM1
STR R1,[R0]

; Step 8. Configure the Rising Edge Trigger


LDR R0,=EXTI_RTSR1
LDR R1,[R0]
ORR R1,#EXTI_RTSR1_RT1 ; Enable line 1
STR R1,[R0]

; Step 9. Configure the Falling Edge Trigger


LDR R0,=EXTI_FTSR1

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 55 / 97
Interrupts 5.3 Extended interrupts and events controller (EXTI)

Extended interrupts and events controller

LDR R1,[R0]
BIC R1,#EXTI_FTSR1_FT1 ; Disable line 1
STR R1,[R0]

; Step 10. Enable EXTI1


LDR R0,=NVIC_ISER0
LDR R1,=EXTI1_IRQ_EN
STR R1,[R0]

; Changes interrupt state EXTI1 to pending


LDR R0,=NVIC_ISPR0
LDR R1,=EXTI1_IRQ_EN
STR R1,[r0]

BX LR

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 56 / 97
Interrupts 5.3 Extended interrupts and events controller (EXTI)

Extended interrupts and events controller

EXTI1_IRQHandler
LDR R0,=EXTI_PR1
LDR R1,[R0]
ANDS R1,#EXTI_PR1_PIF1
BEQ End_EXTI1_IRQHandler
ORR R1,#EXTI_PR1_PIF1
STR R1,[R0]
LDR R0,=GPIOA_ODR
EOR R5,R5,#LED
STR R5,[R0]
End_EXTI1_IRQHandler
BX LR

ALIGN
END

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 57 / 97
Interrupts 5.3 Extended interrupts and events controller (EXTI)

Extended interrupts and events controller

 Example 5.3

Consider a circuit with the STM32G431RBT6 microcontroller and an LED as shown


in the figure. Write Assembly codes to toggle the LED using external interrupt on
pin PC6.
STM32G431RBT6
12 24
PA0 PB0
13 25
PA1 PB1
14 26
PA2 PB2
17 56
PA3 PB3
N1 18 57
R1 PA4 PB4
19 58
PA5 PB5
59
PB6
8 60
PC0 PB7
9
PC1 62
10 PB9
PC2 30
11 PB10
PC3 33
9 PB11
B1 PC4 34
10 PB12
PC5
11
VDD PC6

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 60 / 97
Interrupts 5.3 Extended interrupts and events controller (EXTI)

Extended interrupts and events controller

Solution:
Since pin PC6 belongs to line 6 corresponding to interrupt handler EXTI9 5 IRQHandle
the external interrupt assigned to pin PC6 and the interrupt handler should be con-
figured appropriately as follows
Code 5.2

INCLUDE ../../inc/stm32G431xx.s

LED EQU BIT5

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


THUMB
ENTRY
EXPORT Reset_Handler
EXPORT EXTI9_5_IRQHandler

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 61 / 97
Interrupts 5.3 Extended interrupts and events controller (EXTI)

Extended interrupts and events controller

Reset_Handler
BL GPIO_Init
LDR R5,=LED

Loop
B Loop
GPIO_Init
LDR R0,=RCC_AHB2ENR
LDR R1,[R0]
ORR R1,#RCC_AHB2ENR_GPIOAEN
STR R1,[R0]

LDR R0,=GPIOA_MODER
LDR R1,[R0]
BIC R1,R1,#GPIO_MODE5

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 62 / 97
Interrupts 5.3 Extended interrupts and events controller (EXTI)

Extended interrupts and events controller

ORR R1,#GPIO_MODE5_0
STR R1,[R0]

LDR R0,=GPIOA_ODR
LDR R1,=LED
STR R1,[R0]

LDR R0,=RCC_AHB2ENR
LDR R1,[R0]
ORR R1,#RCC_AHB2ENR_GPIOCEN
STR R1,[R0]

; Enable input mode for PC6


LDR R0,=GPIOC_MODER
LDR R1,[R0]
MOV32 R2,#GPIO_MODE6_0

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 63 / 97
Interrupts 5.3 Extended interrupts and events controller (EXTI)

Extended interrupts and events controller

BIC R1,R2
LSL R2,R2,#1
BIC R1,R2
STR R1,[R0]

; Enable pull-down resistor for PC6


LDR R0,=GPIOC_PUPDR
LDR R1,[R0]
MOV32 R2,#GPIO_PUPD6_0
BIC R1,R2
LSL R2,R2,#1
ORR R1,R2
STR R1,[R0]

; Enable the configuration controller clock


LDR R0,=RCC_APB2ENR

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 64 / 97
Interrupts 5.3 Extended interrupts and events controller (EXTI)

Extended interrupts and events controller

LDR R1,[R0]
ORR R1,#RCC_APB2ENR_SYSCFGEN
STR R1,[R0]

; Configure the EXTI configuration Register


LDR R0,=SYSCFG_EXTICR2
LDR R1,[R0]
MOV R1,#SYSCFG_EXTICR2_EXTI6_PC ; Line 6
STR R1,[R0]

; Disable the EXTI Mask


LDR R0,=EXTI_IMR1
LDR R1,[R0]
ORR R1,#EXTI_IMR1_IM6
STR R1,[R0]

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 65 / 97
Interrupts 5.3 Extended interrupts and events controller (EXTI)

Extended interrupts and events controller

; Configure the Rising Edge Trigger


LDR R0,=EXTI_RTSR1
LDR R1,[R0]
ORR R1,#EXTI_RTSR1_RT6
STR R1,[R0]

; Configure the Falling Edge Trigger


LDR R0,=EXTI_FTSR1
LDR R1,[R0]
BIC R1,#EXTI_FTSR1_FT6
STR R1,[R0]

; Enable Interrupt set-enable register EXTI6


LDR R0,=NVIC_ISER0
LDR R1,=EXTI9_5_IRQn_EN
STR R1,[R0]

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 66 / 97
Interrupts 5.3 Extended interrupts and events controller (EXTI)

Extended interrupts and events controller

; Enable Interrupt set-pending register EXTI6


LDR R0,=NVIC_ISPR0
LDR R1,=EXTI9_5_IRQn_EN
STR R1,[r0]

BX LR
EXTI9_5_IRQHandler
LDR R0,=EXTI_PR1
LDR R1,[R0]
ANDS R1,#EXTI_PR1_PIF6
BEQ End_EXTI_IRQHandler
ORR R1,#EXTI_PR1_PIF6
STR R1,[R0]
LDR R0,=GPIOA_ODR
EOR R5,R5,#LED
STR R5,[R0]

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 67 / 97
Interrupts 5.3 Extended interrupts and events controller (EXTI)

Extended interrupts and events controller

End_EXTI_IRQHandler
BX LR

ALIGN
END

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 68 / 97
Interrupts 5.4 SysTick timer (STK)

SysTick timer

 Example 5.4
Assume that the main clock fM CK = 170MHz. Write a program to toggle the
LED on pin PA5 every 1 second using SysTick timer.

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 77 / 97
Interrupts 5.4 SysTick timer (STK)

SysTick timer

Code 5.3 (SysTick LED Blink)

INCLUDE ../inc/stm32G431xx.s

LED EQU BIT5

SYST_RVR EQU 1700000-1

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


THUMB
ENTRY
EXPORT __main
__main
BL GPIO_Init
BL SysTick_Init

LDR R5,=LED

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 80 / 97
Interrupts 5.4 SysTick timer (STK)

SysTick timer

Loop
LDR R0,=GPIOA_ODR
EOR R5,R5,#LED
STR R5,[R0]

MOV R4,#100
DL_COUNT
LDR R1,=SYST_RVR
BL SysTick_Delay
SUBS R4,#1
BHI DL_COUNT
DL_END
B Loop

GPIO_Init
LDR R0,=RCC_AHB2ENR

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 81 / 97
Interrupts 5.4 SysTick timer (STK)

SysTick timer

LDR R1,[R0]
ORR R1,#RCC_AHB2ENR_GPIOAEN
STR R1,[R0]
NOP
NOP

LDR R0,=GPIOA_MODER
LDR R1,[R0]
BIC R1,R1,#GPIO_MODER_MODE5
ORR R1,#GPIO_MODER_MODE5_0
STR R1,[R0]

LDR R0,=GPIOA_ODR
LDR R1,=LED
STR R1,[R0]

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 82 / 97
Interrupts 5.4 SysTick timer (STK)

SysTick timer

BX LR
SysTick_Init
LDR R0,=STK_CTRLR
MOV R1,#0x0
STR R1,[R0]

LDR R0,=STK_VALR
MOV R1,#0x0
STR R1,[R0]

LDR R0,=STK_CTRLR
MOV R1,#(SYST_AHB_CLK+SYST_EN)
STR R1,[R0]

BX LR

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 83 / 97
Interrupts 5.4 SysTick timer (STK)

SysTick timer

SysTick_Delay
LDR R0,=STK_LOADR
STR R1,[R0]
LDR R0,=STK_CTRLR
ST_COUNT
LDR R3,[R0]
ANDS R3,R3,#SYST_COUNT_FLAG
BEQ ST_COUNT
BX LR

ALIGN
END

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 84 / 97
Interrupts 5.4 SysTick timer (STK)

SysTick timer

 Example 5.5

Assume that the main clock fM CK = 170MHz. Write a program to toggle the
LED on pin PA5 every 1 second using SysTick interrupt.

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 85 / 97
Interrupts 5.4 SysTick timer (STK)

SysTick timer
The code is similar to previous one except that the status of the LED will be
modified every 100 times of the interrupt instead of consuming time. Note that
GPIO and SysTick initialization codes are not given in this program.

Code 5.4 (SysTick Interrupt LED Blink)

INCLUDE ../inc/stm32G431xx.s

LED EQU BIT5

SYST_RVR EQU 1700000

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


THUMB
ENTRY
EXPORT __main
EXPORT SysTick_Handler

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 87 / 97
Interrupts 5.4 SysTick timer (STK)

SysTick timer

__main
BL GPIO_Init
BL SysTick_Init

LDR R5,=LED

Loop B Loop

GPIO_Init
...
SysTick_Init
...
SysTick_Handler
SUBS R4,#1
BHI STEND

LDR R0,=GPIOA_ODR

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 88 / 97
Interrupts 5.4 SysTick timer (STK)

SysTick timer

EOR R5,R5,#LED
STR R5,[R0]

MOV R4,#100
STEND
BX LR

ALIGN
END

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 89 / 97
Timing generation and measurements 6.3 Programming general purpose timers in counter modes

Programming general purpose timers in counter modes


We define some constants for timers in file stm32G431xx.s as follows

TIM3_BASE EQU APB1PERIPH_BASE + 0x0400


TIM_CR1 EQU 0x00 ; TIM control register 1
TIM_CR2 EQU 0x04 ; TIM control register 2
TIM_SMCR EQU 0x08 ; TIM slave mode control register
TIM_DIER EQU 0x0C ; TIM DMA/interrupt enable register
TIM_SR EQU 0x10 ; TIM status register
TIM_CNT EQU 0x24 ; TIM counter register
TIM_PSC EQU 0x28 ; TIM prescaler
TIM_ARR EQU 0x2C ; TIM auto-reload register

TIM3_PSC EQU TIM3_BASE+TIM_PSC


TIM3_DIER EQU TIM3_BASE+TIM_DIER
TIM3_CNT EQU TIM3_BASE+TIM_CNT

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 41 / 182
Timing generation and measurements 6.3 Programming general purpose timers in counter modes

Programming general purpose timers in counter modes

TIM3_ARR EQU TIM3_BASE+TIM_ARR


TIM3_CR1 EQU TIM3_BASE+TIM_CR1
TIM3_SR EQU TIM3_BASE+TIM_SR

TIM3_IRQ_Pos EQU TIM3_IRQn


TIM3_IRQ_Msk EQU 0x1 << TIM3_IRQ_Pos
TIM3_IRQ_EN EQU TIM3_IRQ_Msk

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 42 / 182
Timing generation and measurements 6.3 Programming general purpose timers in counter modes

Programming general purpose timers in counter modes

Solution:

Code 6.1

INCLUDE ../../../inc/stm32G431xx.s

TIM3_PSC_VALUE EQU 3999


TIM3_PERIOD_VALUE EQU 3999

LED EQU BIT5

Stack_Size EQU 0x00000400

AREA STACK,NOINIT,READWRITE,ALIGN=3

Stack_Mem SPACE Stack_Size

AREA RESET,DATA, READONLY

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 43 / 182
Timing generation and measurements 6.3 Programming general purpose timers in counter modes

Programming general purpose timers in counter modes

EXPORT __Vectors
__Vectors
DCD Stack_Mem+Stack_Size
DCD Reset_Handler
ALIGN

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


THUMB
ENTRY
EXPORT Reset_Handler
Reset_Handler
BL GPIO_Init
BL Timer3_Init

LDR R5,=LED

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 44 / 182
Timing generation and measurements 6.3 Programming general purpose timers in counter modes

Programming general purpose timers in counter modes

LED_Blink
;Toggle PIN A5
LDR R0,=GPIOA_ODR
EOR R5,R5,#LED
STR R5,[R0]

BL delay
BL LED_Blink
GPIO_Init
LDR R0,=RCC_AHB2ENR
LDR R1,[R0]
ORR R1,#RCC_GPIOA_EN
STR R1,[R0]

LDR R0,=GPIOA_MODER
LDR R1,[R0]

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 45 / 182
Timing generation and measurements 6.3 Programming general purpose timers in counter modes

Programming general purpose timers in counter modes

BIC R1,R1,#GPIO_MODE5
ORR R1,#GPIO_MODE5_0
STR R1,[R0]

LDR R0,=GPIOA_ODR
LDR R1,=LED
STR R1,[R0]

BX LR
Timer3_Init
; Timer 3 RCC enable
LDR R0,=RCC_APB1ENR1
LDR R1,[R0]
ORR R1,#RCC_APB1ENR1_TIM3EN
STR R1,[R0]
NOP

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 46 / 182
Timing generation and measurements 6.3 Programming general purpose timers in counter modes

Programming general purpose timers in counter modes

; Settings PSC value


LDR R0,=TIM3_PSC
MOV R1,#TIM3_PSC_VALUE
STR R1,[R0]

; Settings ARR value


LDR R0,=TIM3_ARR
MOV R1,#TIM3_PERIOD_VALUE
STR R1,[R0]

; Clear counter value


LDR R0,=TIM3_CNT
MOV R1,#0x00
STR R1,[R0]

; Enable TIM3

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 47 / 182
Timing generation and measurements 6.3 Programming general purpose timers in counter modes

Programming general purpose timers in counter modes

LDR R0,=TIM3_CR1
LDR R1,[R0]
ORR R1,#TIM_CR1_CEN
STR R1,[R0]

BX LR
delay
LDR R1,=TIM3_SR
flag_check
LDR R2,[R1]
AND R2,#TIM_SR_UIF
CMP R2,#0x00
BEQ flag_check

LDR R3,[R1]
BIC R3,R3,#0x01

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 48 / 182
Timing generation and measurements 6.3 Programming general purpose timers in counter modes

Programming general purpose timers in counter modes

STR R3,[R1]

BX LR

ALIGN
END

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 49 / 182
Timing generation and measurements 6.3 Programming general purpose timers in counter modes

Programming general purpose timers in counter modes

Solution:

Code 6.2

INCLUDE ../../inc/stm32G431xx.s

TIM3_PSC_VALUE EQU 3999


TIM3_PERIOD_VALUE EQU 3999

LED EQU BIT5

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


THUMB
ENTRY
EXPORT Reset_Handler
EXPORT TIM3_IRQHandler
Reset_Handler
BL GPIO_Init

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 51 / 182
Timing generation and measurements 6.3 Programming general purpose timers in counter modes

Programming general purpose timers in counter modes

BL Timer3_Init

LDR R5,=LED

Loop B Loop

GPIO_Init
...

BX LR
Timer3_Init
...

; Enable TIM3 interrupt


LDR R0,=TIM3_DIER
LDR R1,[R0]

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 52 / 182
Timing generation and measurements 6.3 Programming general purpose timers in counter modes

Programming general purpose timers in counter modes

ORR R1,#TIM_DIER_UIE
STR R1,[R0]

; Enable TIM3
LDR R0,=TIM3_CR1
LDR R1,[R0]
ORR R1,#TIM_CR1_CEN
STR R1,[R0]

; Timer 3 Interrupt Set-enable Register


LDR R0,=NVIC_ISER0
LDR R1,=TIM3_IRQ_EN
STR R1,[R0]

; Timer 3 Interrupt Set-pending Registers


LDR R0,=NVIC_ISPR0

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 53 / 182
Timing generation and measurements 6.3 Programming general purpose timers in counter modes

Programming general purpose timers in counter modes

LDR R1,=TIM3_IRQ_EN
STR R1,[r0]

BX LR
TIM3_IRQHandler
; Resets the interrupt of timer 3
LDR R0,=TIM3_SR
LDR R1,[R0]
BIC R1,#TIM_SR_UIF
STR R1,[R0]

LDR R0,=GPIOA_ODR
EOR R5,R5,#LED
STR R5,[R0]

BX LR

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 54 / 182
Timing generation and measurements 6.3 Programming general purpose timers in counter modes

Programming general purpose timers in counter modes

ALIGN
END

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 55 / 182
Timing generation and measurements 6.4 Timer in PWM mode

Programming timers in PWM mode

Solution:

Code 6.3

INCLUDE ../../inc/stm32G431xx.s

TIM2_PSC_VALUE EQU 159


TIM2_PERIOD_VALUE EQU 999
TIM2_DUTY_VALUE EQU 100

LED EQU BIT5

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


THUMB
ENTRY
EXPORT Reset_Handler
Reset_Handler
BL GPIO_Init
BL Timer2_Init
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 66 / 182
Timing generation and measurements 6.4 Timer in PWM mode

Programming timers in PWM mode

Loop B Loop

GPIO_Init
; Enable clock for GPIOA
LDR R0,=RCC_AHB2ENR
LDR R1,[R0]
ORR R1,#RCC_GPIOA_EN
STR R1,[R0]

; Set PA5 for TIM2


LDR R0,=GPIOA_AFRL
LDR R1,[R0]
BIC R1,R1,#GPIO_AFRL_AFSEL5 ; PA5 for TIM2
ORR R1,#GPIO_AFRL_AFSEL5_0 ;
STR R1,[R0]

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 67 / 182
Timing generation and measurements 6.4 Timer in PWM mode

Programming timers in PWM mode

; Select alternative function for PA5


LDR R0,=GPIOA_MODER
LDR R1,[R0]
BIC R1,R1,#GPIO_MODE5
ORR R1,#GPIO_MODE5_1
STR R1,[R0]

BX LR
Timer2_Init
; Timer 2 RCC enable
LDR R0,=RCC_APB1ENR1
LDR R1,[R0]
ORR R1,#RCC_APB1ENR1_TIM2EN
STR R1,[R0]

; Settings PSC value

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 68 / 182
Timing generation and measurements 6.4 Timer in PWM mode

Programming timers in PWM mode

LDR R0,=TIM2_PSC
MOV R1,#TIM2_PSC_VALUE
STR R1,[R0]

; Settings ARR value


LDR R0,=TIM2_ARR
MOV R1,#TIM2_PERIOD_VALUE
STR R1,[R0]

; Clear counter value


LDR R0,=TIM2_CNT
MOV R1,#0x00
STR R1,[R0]

; Select PWM Mode 1 on channel 1


LDR R0,=TIM2_CCMR1

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 69 / 182
Timing generation and measurements 6.4 Timer in PWM mode

Programming timers in PWM mode

LDR R1,[R0]
MOV32 R2,#TIM_CCMR1_OC1M
BIC R1,R2
MOV32 R2,#TIM_CCMR1_OC1M_1
ORR R1,R2
MOV32 R2,#TIM_CCMR1_OC1M_2
ORR R1,R2
STR R1,[R0]

; Enable PWM Ch1


LDR R0,=TIM2_CCER
LDR R1,[R0]
MOV32 R2,#TIM_CCER_CC1E
ORR R1,R2
STR R1,[R0]

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 70 / 182
Timing generation and measurements 6.4 Timer in PWM mode

Programming timers in PWM mode

; Duty cycle
LDR R0,=TIM2_CCR1
MOV R1,#TIM2_DUTY_VALUE
STR R1,[R0]

; Enable TIM2
LDR R0,=TIM2_CR1
LDR R1,[R0]
ORR R1,#TIM_CR1_CEN
STR R1,[R0]

BX LR

ALIGN
END

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 71 / 182
Timing generation and measurements 6.6 Advanced-control timer registers

Advanced-control timers programming

Solution:

Code 6.4

INCLUDE ../inc/stm32G431xx.s

TIM1_PSC_VALUE EQU 159


TIM1_PERIOD_VALUE EQU 999
PWM_DUTY_VALUE EQU 500

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


THUMB
ENTRY
EXPORT __main
__main
BL GPIO_Init
BL Timer1_Init

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 84 / 182
Timing generation and measurements 6.6 Advanced-control timer registers

Advanced-control timers programming

Loop B Loop

GPIO_Init
LDR R0,=RCC_AHB2ENR
LDR R1,[R0]
ORR R1,#RCC_GPIOA_EN
STR R1,[R0]
NOP
NOP

LDR R0,=GPIOA_MODER
LDR R1,[R0]
BIC R1,R1,#GPIO_MODE8
ORR R1,#GPIO_MODE8_1 ; Alternate func.
STR R1,[R0]

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 85 / 182
Timing generation and measurements 6.6 Advanced-control timer registers

Advanced-control timers programming

LDR R0,=GPIOA_AFRH
LDR R1,[R0]
BIC R1,R1,#GPIO_AFRH_AFSEL8 ; PA8 for TIM1
ORR R1,#GPIO_AFRH_AFSEL8_1 ;
ORR R1,#GPIO_AFRH_AFSEL8_2 ; AF6
STR R1,[R0]

BX LR
Timer1_Init
; Timer 1 RCC enable
LDR R0,=RCC_APB2ENR
LDR R1,[R0]
ORR R1,#RCC_APB2ENR_TIM1EN
STR R1,[R0]
NOP
NOP

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 86 / 182
Timing generation and measurements 6.6 Advanced-control timer registers

Advanced-control timers programming

; Settings PSC value


LDR R0,=TIM1_PSC
MOV R1,#TIM1_PSC_VALUE
STR R1,[R0]

; Settings ARR value


LDR R0,=TIM1_ARR
MOV R1,#TIM1_PERIOD_VALUE
STR R1,[R0]

; Clear counter value


LDR R0,=TIM1_CNT
MOV R1,#0x00
STR R1,[R0]

; Select PWM Mode 1 output on channel 1

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 87 / 182
Timing generation and measurements 6.6 Advanced-control timer registers

Advanced-control timers programming

LDR R0,=TIM1_CCMR1
LDR R1,[R0]
MOV32 R2,#TIM_CCMR1_OC1M
BIC R1,R2
MOV32 R2,#TIM_CCMR1_OC1M_1
ORR R1,R2
MOV32 R2,#TIM_CCMR1_OC1M_2
ORR R1,R2
STR R1,[R0]

; Enable main output


LDR R0,=TIM1_BDTR
LDR R1,[R0]
MOV32 R2,#TIM_BDTR_MOE
ORR R1,R2
STR R1,[R0]

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 88 / 182
Timing generation and measurements 6.6 Advanced-control timer registers

Advanced-control timers programming

; Enable PWM Ch1


LDR R0,=TIM1_CCER
LDR R1,[R0]
MOV32 R2,#TIM_CCER_CC1E
ORR R1,R2
STR R1,[R0]

; Duty cycle
LDR R0,=TIM1_CCR1
MOV R1,#PWM_DUTY_VALUE
STR R1,[R0]

; Enable TIM1
LDR R0,=TIM1_CR1
LDR R1,[R0]
ORR R1,#TIM_CR1_CEN

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 89 / 182
Timing generation and measurements 6.6 Advanced-control timer registers

Advanced-control timers programming

STR R1,[R0]

BX LR

ALIGN
END

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 90 / 182
Timing generation and measurements 6.6 Advanced-control timer registers

Dead time insertion

Solution:

Code 6.5

INCLUDE ../../inc/stm32G431xx.s

TIM1_PSC_VALUE EQU 159


TIM1_PERIOD_VALUE EQU 999
PWM_DUTY_VALUE EQU 100

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


THUMB
ENTRY
EXPORT Reset_Handler
Reset_Handler
BL GPIO_Init
BL Timer1_Init

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 97 / 182
Timing generation and measurements 6.6 Advanced-control timer registers

Dead time insertion

Loop B Loop

GPIO_Init
LDR R0,=RCC_AHB2ENR
LDR R1,[R0]
ORR R1,#RCC_GPIOA_EN
STR R1,[R0]

; PA7, PA8 as alternate function mode


LDR R0,=GPIOA_MODER
LDR R1,[R0]
BIC R1,R1,#GPIO_MODE7+GPIO_MODE8
ORR R1,#GPIO_MODE7_1+GPIO_MODE8_1
STR R1,[R0]

LDR R0,=GPIOA_AFRL

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 98 / 182
Timing generation and measurements 6.6 Advanced-control timer registers

Dead time insertion

LDR R1,[R0]
BIC R1,R1,#GPIO_AFRL_AFSEL7 ; PA7 for TIM1
ORR R1,#GPIO_AFRL_AFSEL7_1 ;
ORR R1,#GPIO_AFRL_AFSEL7_2 ; AF6
STR R1,[R0]

LDR R0,=GPIOA_AFRH
LDR R1,[R0]
BIC R1,R1,#GPIO_AFRH_AFSEL8 ; PA8 for TIM1
ORR R1,#GPIO_AFRH_AFSEL8_1 ;
ORR R1,#GPIO_AFRH_AFSEL8_2 ; AF6
STR R1,[R0]

BX LR
Timer1_Init
; Timer 1 RCC enable

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 99 / 182
Timing generation and measurements 6.6 Advanced-control timer registers

Dead time insertion

LDR R0,=RCC_APB2ENR
LDR R1,[R0]
ORR R1,#RCC_APB2ENR_TIM1EN
STR R1,[R0]

; Settings PSC value


LDR R0,=TIM1_PSC
MOV R1,#TIM1_PSC_VALUE
STR R1,[R0]

; Settings ARR value


LDR R0,=TIM1_ARR
MOV R1,#TIM1_PERIOD_VALUE
STR R1,[R0]

; Clear counter value

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 100 / 182
Timing generation and measurements 6.6 Advanced-control timer registers

Dead time insertion

LDR R0,=TIM1_CNT
MOV R1,#0x00
STR R1,[R0]

; Select PWM Mode 1 output on channel 1


LDR R0,=TIM1_CCMR1
LDR R1,[R0]
MOV32 R2,#TIM_CCMR1_OC1M
BIC R1,R2
MOV32 R2,#TIM_CCMR1_OC1M_1
ORR R1,R2
MOV32 R2,#TIM_CCMR1_OC1M_2
ORR R1,R2
STR R1,[R0]

; Enable complementary output (CHlN)

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 101 / 182
Timing generation and measurements 6.6 Advanced-control timer registers

Dead time insertion

LDR R0,=TIM1_CCER
LDR R1,[R0]
MOV32 R2,#TIM_CCER_CC1NE
ORR R1,R2
STR R1,[R0]

; Main output enable (MOE)


LDR R0,=TIM1_BDTR
LDR R1,[R0]
MOV32 R2,#TIM_BDTR_MOE
ORR R1,R2
STR R1,[R0]

; Enable PWM Ch1


LDR R0,=TIM1_CCER
LDR R1,[R0]

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 102 / 182
Timing generation and measurements 6.6 Advanced-control timer registers

Dead time insertion

MOV32 R2,#TIM_CCER_CC1E
ORR R1,R2
STR R1,[R0]

; Initial duty cycle


LDR R0,=TIM1_CCR1
MOV R1,#PWM_DUTY_VALUE
STR R1,[R0]

; Enable TIM1
LDR R0,=TIM1_CR1
LDR R1,[R0]
ORR R1,#TIM_CR1_CEN
STR R1,[R0]

BX LR

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 103 / 182
Timing generation and measurements 6.6 Advanced-control timer registers

Dead time insertion

ALIGN
END

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 104 / 182
Timing generation and measurements 6.6 Advanced-control timer registers

Dead time insertion

Remark 6.1 (Sign of complementary pulse)


If we need the pulses of the output channel and its complementary channel have
the same sign, we can do this by changing the code as

Timer1_Init
...

; Enable complementary output of channel 1 (CH1N)


LDR R0,=TIM1_CCER
LDR R1,[R0]
MOV32 R2,#TIM_CCER_CC1NE
ORR R1,R2
STR R1,[R0]

; Main output enable (MOE)


LDR R0,=TIM1_BDTR
LDR R1,[R0]

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 105 / 182
Timing generation and measurements 6.6 Advanced-control timer registers

Dead time insertion

MOV32 R2,#TIM_BDTR_MOE
ORR R1,R2
STR R1,[R0]

; Enable PWM CH1 and CH1N


LDR R0,=TIM1_CCER
LDR R1,[R0]
MOV32 R2,#TIM_CCER_CC1E
ORR R1,R2
MOV32 R2,#TIM_CCER_CC1P
ORR R1,R2
STR R1,[R0]

; Initial duty cycle


LDR R0,=TIM1_CCR1
MOV R1,#PWM_DUTY_VALUE
STR R1,[R0]

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 106 / 182
Timing generation and measurements 6.6 Advanced-control timer registers

Dead time insertion

; Enable TIM1
LDR R0,=TIM1_CR1
LDR R1,[R0]
ORR R1,#TIM_CR1_CEN
STR R1,[R0]

BX LR

Above code will generate two pulses on PA7 and PA8 with edges appeared at the
same time as shown in the figure.

OC1REF

CH1

CH1N

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 107 / 182
Timing generation and measurements 6.6 Advanced-control timer registers

Combined 3-phase PWM mode

 Example 6.12

Write an assembly program to generate center-aligned variable frequency PWM sig-


nal 4KHz output on multiple complementary channels (CH1, CH1N, CH2N, CH2N,
CH3, and CH3N) as shown in the figure.

CH1

CH1N

CH2

CH2N

CH3

CH3N

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 139 / 182
Timing generation and measurements 6.6 Advanced-control timer registers

Combined 3-phase PWM mode

Code 6.12 (Combined 3-phase PWM mode)

INCLUDE ../../../inc/stm32G431xx.s

TIM1_PSC_VALUE EQU 0
TIM1_PERIOD_VALUE EQU 2000

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


THUMB
ENTRY
EXPORT Reset_Handler
Reset_Handler
BL GPIO_Init
BL Timer1_Init

Loop B Loop

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 142 / 182
Timing generation and measurements 6.6 Advanced-control timer registers

Combined 3-phase PWM mode

GPIO_Init
LDR R0,=RCC_AHB2ENR
LDR R1,[R0]
ORR R1,#RCC_GPIOA_EN
STR R1,[R0]

; PA7,PA8,PA9,PA10 in alternate function mode


LDR R0,=GPIOA_MODER
LDR R1,[R0]
BIC R1,R1,#GPIO_MODE7+GPIO_MODE8+GPIO_MODE9
... +GPIO_MODE10
ORR R1,#GPIO_MODE7_1+GPIO_MODE8_1
... +GPIO_MODE9_1+GPIO_MODE10_1
STR R1,[R0]

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 143 / 182
Timing generation and measurements 6.6 Advanced-control timer registers

Combined 3-phase PWM mode

; PA7 for TIM1


LDR R0,=GPIOA_AFRL
LDR R1,[R0]
BIC R1,R1,#GPIO_AFSEL7
ORR R1,#GPIO_AFSEL7_1 ;
ORR R1,#GPIO_AFSEL7_2 ; AF6
STR R1,[R0]

; PA8, PA9, and PA10 for TIM1


LDR R0,=GPIOA_AFRH
LDR R1,[R0]
MOV32 R2,#GPIO_AFSEL8+GPIO_AFSEL9+GPIO_AFSEL10
BIC R1,R2
MOV32 R2,#GPIO_AFSEL8_1+GPIO_AFSEL9_1
... +GPIO_AFSEL10_1 ;
ORR R1,R2

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 144 / 182
Timing generation and measurements 6.6 Advanced-control timer registers

Combined 3-phase PWM mode

MOV32 R2,#GPIO_AFSEL8_2+GPIO_AFSEL9_2
... +GPIO_AFSEL10_2 ; AF6
ORR R1,R2
STR R1,[R0]

; Enable the clock to the GPIOB Port


LDR R0,=RCC_AHB2ENR
LDR R1,[R0]
ORR R1,#RCC_GPIOB_EN
STR R1,[R0]

LDR R0,=GPIOB_MODER
LDR R1,[R0]
BIC R1,R1,#GPIO_MODE0+GPIO_MODE1
ORR R1,#GPIO_MODE0_1+GPIO_MODE1_1

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 145 / 182
Timing generation and measurements 6.6 Advanced-control timer registers

Combined 3-phase PWM mode

STR R1,[R0]

; PB0, PB1 for TIM1


LDR R0,=GPIOB_AFRL
LDR R1,[R0]
BIC R1,R1,#GPIO_AFSEL0+GPIO_AFSEL1
ORR R1,#GPIO_AFSEL0_1+GPIO_AFSEL1_1 ;
ORR R1,#GPIO_AFSEL0_2+GPIO_AFSEL1_2 ; AF6
STR R1,[R0]

BX LR
Timer1_Init
; Timer 1 RCC enable
LDR R0,=RCC_APB2ENR
LDR R1,[R0]
ORR R1,#RCC_TIM1EN

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 146 / 182
Timing generation and measurements 6.6 Advanced-control timer registers

Combined 3-phase PWM mode

STR R1,[R0]

; Choose Center-aligned mode 1 for TIM1


LDR R0,=TIM1_CR1
LDR R1,[R0]
BIC R1,#TIM_CR1_CMS
ORR R1,#TIM_CR1_CMS_0
STR R1,[R0]

; Settings PSC value


LDR R0,=TIM1_PSC
MOV R1,#TIM1_PSC_VALUE
STR R1,[R0]

; Settings ARR value


LDR R0,=TIM1_ARR

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 147 / 182
Timing generation and measurements 6.6 Advanced-control timer registers

Combined 3-phase PWM mode

MOV R1,#TIM1_PERIOD_VALUE
STR R1,[R0]

; Clear counter value


LDR R0,=TIM1_CNT
MOV R1,#0x00
STR R1,[R0]

; Select PWM Mode 1 output on CH1 (OClM = 110)


; and CH2 (OC2M = 110)
LDR R0,=TIM1_CCMR1
LDR R1,[R0]
MOV32 R2,#TIM_CCMR1_OC1M+TIM_CCMR1_OC2M
BIC R1,R2
MOV32 R2,#TIM_CCMR1_OC1M_1+TIM_CCMR1_OC2M_1
ORR R1,R2

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 148 / 182
Timing generation and measurements 6.6 Advanced-control timer registers

Combined 3-phase PWM mode

MOV32 R2,#TIM_CCMR1_OC1M_2+TIM_CCMR1_OC2M_2
ORR R1,R2
STR R1,[R0]

; Select PWM Mode 1 output on channel 3


LDR R0,=TIM1_CCMR2
LDR R1,[R0]
MOV32 R2,#TIM_CCMR2_OC3M
BIC R1,R2
MOV32 R2,#TIM_CCMR2_OC3M_1
ORR R1,R2
MOV32 R2,#TIM_CCMR2_OC3M_2
ORR R1,R2
STR R1,[R0]

; Enable CHlN, CH2N, and CH3N

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 149 / 182
Timing generation and measurements 6.6 Advanced-control timer registers

Combined 3-phase PWM mode

LDR R0,=TIM1_CCER
LDR R1,[R0]
MOV32 R2,#TIM_CCER_CC1NE+TIM_CCER_CC2NE
... +TIM_CCER_CC3NE
ORR R1,R2
STR R1,[R0]

; Enable main output (MOE)


LDR R0,=TIM1_BDTR
LDR R1,[R0]
MOV32 R2,#TIM_BDTR_MOE
ORR R1,R2
STR R1,[R0]

; Enable PWM CH1, CH2, and CH3


LDR R0,=TIM1_CCER

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 150 / 182
Timing generation and measurements 6.6 Advanced-control timer registers

Combined 3-phase PWM mode

LDR R1,[R0]
MOV32 R2,#TIM_CCER_CC1E+TIM_CCER_CC2E
... +TIM_CCER_CC3E
ORR R1,R2
STR R1,[R0]

; Duty cycle for CH1


LDR R0,=TIM1_CCR1
MOV R1,#500
STR R1,[R0]

; Duty cycle for CH2


LDR R0,=TIM1_CCR2
MOV R1,#1000
STR R1,[R0]

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 151 / 182
Timing generation and measurements 6.6 Advanced-control timer registers

Combined 3-phase PWM mode

; Duty cycle for CH3


LDR R0,=TIM1_CCR3
MOV R1,#1500
STR R1,[R0]

; Enable TIM1
LDR R0,=TIM1_CR1
LDR R1,[R0]
ORR R1,#TIM_CR1_CEN
STR R1,[R0]

BX LR

ALIGN
END

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 152 / 182
Timing generation and measurements 6.7 Encoder interface mode

Encoder interface

 Example 6.13
Write an Assembly program to interface with an encoder using Timer 2.
STM32G431RBT6
12 8
PA0 PC0
13 9
PA1 PC1
14 10
PA2 PC2
17 11 VCC
PA3 PC3
18 22 R2 10K
PA4 PC4 CLK SW
19 23 A D
PA5 PC5
20 38 GND
PA6 PC6 C
21 39
PA7 PC7 DT GND
42 40 B E
PA8 PC8
41 R3 10K
PC9
CLK 51 52
PA15 PC10
53
PC11
24 54
PB0 PC12
25 2
PB1 PC13
26
PB2
DT 56
PB3
57
PB4
58
PB5 55
59 PD2
PB6

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 160 / 182
Timing generation and measurements 6.7 Encoder interface mode

Encoder interface

Solution:

Code 6.13 (Encoder interface)

INCLUDE ../../../inc/stm32G431xx.s

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


THUMB
ENTRY
EXPORT Reset_Handler
Reset_Handler
BL GPIO_Init
BL Timer2_Init
Loop
B Loop ; unconditional branch
GPIO_Init
LDR R0,=RCC_AHB2ENR
LDR R1,[R0]

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 161 / 182
Timing generation and measurements 6.7 Encoder interface mode

Encoder interface

ORR R1,#RCC_GPIOA_EN
STR R1,[R0]
NOP
NOP

; PA15 in alternate function mode


LDR R0,=GPIOA_MODER
LDR R1,[R0]
BIC R1,R1,#GPIO_MODE15
ORR R1,#GPIO_MODE15_1
STR R1,[R0]

; PA15 for TIM2 as CH1


LDR R0,=GPIOA_AFRH
LDR R1,[R0]
BIC R1,#GPIO_AFSEL15

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 162 / 182
Timing generation and measurements 6.7 Encoder interface mode

Encoder interface

ORR R1,#GPIO_AFSEL15_0 ; AF1


STR R1,[R0]

; Enabled the clock to the GPIOB Port


LDR R0,=RCC_AHB2ENR
LDR R1,[R0]
ORR R1,#RCC_AHB2ENR_GPIOBEN
STR R1,[R0]

; PB3 in alternate function mode


LDR R0,=GPIOB_MODER
LDR R1,[R0]
BIC R1,R1,#GPIO_MODE3
ORR R1,#GPIO_MODE3_1
STR R1,[R0]

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 163 / 182
Timing generation and measurements 6.7 Encoder interface mode

Encoder interface

; PB3 for TIM2 as CH2


LDR R0,=GPIOB_AFRL
LDR R1,[R0]
MOV32 R2,#GPIO_AFSEL3
BIC R1,R2
MOV32 R2,#GPIO_AFSEL3_0 ; AF1
ORR R1,R2
STR R1,[R0]

; Enable output mode for PB12, PB13, PB15


LDR R0,=GPIOB_MODER
LDR R1,[R0]
MOV R2,#GPIO_MODE12_0+GPIO_MODE13_0+GPIO_MODE15_0
ORR R1,R2
LSL R2,R2,#1
BIC R1,R2

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 164 / 182
Timing generation and measurements 6.7 Encoder interface mode

Encoder interface

STR R1,[R0]
BX LR
Timer2_Init
; Timer 2 RCC enable
LDR R0,=RCC_APB1ENR1
LDR R1,[R0]
ORR R1,#RCC_APB1ENR1_TIM2EN
STR R1,[R0]

; Settings ARR value


LDR R0,=TIM2_ARR
MOV32 R1,#0xFFFF
STR R1,[R0]

; tim_ti1fp1 mapped on tim_ti1,


; tim_ti1fp2 mapped on tim_ti2

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 165 / 182
Timing generation and measurements 6.7 Encoder interface mode

Encoder interface

LDR R0,=TIM2_CCMR1
LDR R1,[R0]
MOV32 R2,#TIM_CCMR1_CC1S+TIM_CCMR1_CC2S
BIC R1,R2
MOV32 R2,#TIM_CCMR1_CC1S_0+TIM_CCMR1_CC2S_0
ORR R1,R2
STR R1,[R0]

; tim_ti1fp1 non-inverted, tim_ti1fp1=tim_ti1


LDR R0,=TIM2_CCER
LDR R1,[R0]
BIC R1,#TIM_CCER_CC1P+TIM_CCER_CC1NP
STR R1,[R0]

; tim_ti1fp2 non-inverted, tim_ti1fp2=tim_ti2


LDR R0,=TIM2_CCER

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 166 / 182
Timing generation and measurements 6.7 Encoder interface mode

Encoder interface

LDR R1,[R0]
BIC R1,#TIM_CCER_CC2P+TIM_CCER_CC2NP
STR R1,[R0]

; Active on both rising and falling edges


LDR R0,=TIM2_SMCR
LDR R1,[R0]
MOV32 R2,#TIM_SMCR_SMS
BIC R1,R2
ORR R1,#TIM_SMCR_SMS_0+TIM_SMCR_SMS_1
STR R1,[R0]

; Enable TIM1
LDR R0,=TIM2_CR1
LDR R1,[R0]
ORR R1,#TIM_CR1_CEN

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 167 / 182
Timing generation and measurements 6.7 Encoder interface mode

Encoder interface

STR R1,[R0]

BX LR

ALIGN
END

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 168 / 182
Timing generation and measurements 6.7 Encoder interface mode

Encoder interface

 Example 6.14
Write an Assembly program to interface with an encoder using Timer 2 and a signal
filter.
STM32G431RBT6
12 8
PA0 PC0
13 9
PA1 PC1
14 10
PA2 PC2
17 11 VCC
PA3 PC3
18 22 R2 10K
PA4 PC4 CLK SW
19 23 A D
PA5 PC5
20 38 GND
PA6 PC6 C
21 39
PA7 PC7 DT GND
42 40 B E
PA8 PC8
41 R3 10K
PC9
CLK 51 52
PA15 PC10
53
PC11
24 54
PB0 PC12
25 2
PB1 PC13
26
PB2
DT 56
PB3
57
PB4
58
PB5 55
59 PD2
PB6

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 169 / 182
Timing generation and measurements 6.7 Encoder interface mode

Encoder interface

Solution:

Code 6.14 (Encoder filtered)

Timer2_Init
; Timer 2 RCC enable
LDR R0,=RCC_APB1ENR1
LDR R1,[R0]
ORR R1,#RCC_APB1ENR1_TIM2EN
STR R1,[R0]

; Settings ARR value


LDR R0,=TIM2_ARR
MOV32 R1,#0xFFFF
STR R1,[R0]

; tim_ti1fp1 mapped on tim_ti1,


; tim_ti1fp2 mapped on tim_ti2

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 170 / 182
Timing generation and measurements 6.7 Encoder interface mode

Encoder interface

LDR R0,=TIM2_CCMR1
LDR R1,[R0]
MOV32 R2,#TIM_CCMR1_CC1S+TIM_CCMR1_CC2S
BIC R1,R2
MOV32 R2,#TIM_CCMR1_CC1S_0+TIM_CCMR1_CC2S_0
ORR R1,R2
STR R1,[R0]

; CH1 and CH2 filters


LDR R0,=TIM2_CCMR1
LDR R1,[R0]
MOV32 R2,#TIM_CCMR1_IC1F+TIM_CCMR1_IC2F
BIC R1,R2
MOV32 R2,#TIM_CCMR1_IC1F_1+TIM_CCMR1_IC1F_2
... +TIM_CCMR1_IC2F_1+TIM_CCMR1_IC2F_2
ORR R1,R2

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 171 / 182
Timing generation and measurements 6.7 Encoder interface mode

Encoder interface

STR R1,[R0]

; tim_ti1fp1 non-inverted, tim_ti1fp1=tim_ti1


LDR R0,=TIM2_CCER
LDR R1,[R0]
BIC R1,#TIM_CCER_CC1P+TIM_CCER_CC1NP
STR R1,[R0]

; tim_ti1fp2 non-inverted, tim_ti1fp2=tim_ti2


LDR R0,=TIM2_CCER
LDR R1,[R0]
BIC R1,#TIM_CCER_CC2P+TIM_CCER_CC2NP
STR R1,[R0]

; Active on both rising and falling edges

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 172 / 182
Timing generation and measurements 6.7 Encoder interface mode

Encoder interface

LDR R0,=TIM2_SMCR
LDR R1,[R0]
MOV32 R2,#TIM_SMCR_SMS
BIC R1,R2
ORR R1,#TIM_SMCR_SMS_0+TIM_SMCR_SMS_1
STR R1,[R0]

; Enable TIM1
LDR R0,=TIM2_CR1
LDR R1,[R0]
ORR R1,#TIM_CR1_CEN
STR R1,[R0]

BX LR

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 173 / 182
Analog interfacing

Chapter 7
Analog interfacing

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 1 / 203
Analog interfacing 7.2 ADC registers

Single-channel continuous conversion mode

 Example 7.1
Consider a circuit with the STM32G431RBT6 microcontroller, an LED, and a po-
tentiometer connected to pin PB0 as an ADC IN15 input as shown in the figure.
Write Assembly codes to set LED if ADC15 > 0.5 VCC, else LED is off.
STM32G431RBT6
12 24
PA0 PB0
13 25
PA1 PB1
14 26
PA2 PB2
17 56
PA3 PB3
18 57
PA4 PB4
19 58
PA5 PB5
59 VCC
PB6
60
R1 PB7
IN6 8 62 IN6
PC0 PB9 R2
9 30
N1 PC1 PB10
10 33
PC2 PB11
11 34
PC3 PB12
VCC

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 73 / 203
Analog interfacing 7.2 ADC registers

Single-channel continuous conversion mode

Solution:

Code 7.1

INCLUDE ../inc/stm32G431xx.s

LED EQU BIT5


ADC1_THRH EQU 2048

Reset_Handler
BL GPIO_Init
BL ADC1_Init
LDR R5,=ADC1_THRH
Loop
; Step . ADC1 start conversion
LDR R0,=ADC1_CR
LDR R1,[R0]

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 74 / 203
Analog interfacing 7.2 ADC registers

Single-channel continuous conversion mode

ORR R1,#ADC_CR_ADSTART
STR R1,[R0]
NOP
Check_conversion
; Step .
LDR R0,=ADC1_ISR
LDR R1,[R0]
AND R1,#ADC_ISR_EOC
CMP R1,#0x00
BEQ Check_conversion

LDR R0,=ADC1_DR
LDR R1,[R0]

CMP R1,R5
BGE LED_ON

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 75 / 203
Analog interfacing 7.2 ADC registers

Single-channel continuous conversion mode

LED_OFF
LDR R0,=GPIOA_ODR
LDR R1,=LED
BIC R1,#LED
STR R1,[R0]

B Loop
LED_ON
LDR R0,=GPIOA_ODR
LDR R1,=LED
STR R1,[R0]

B Loop
GPIO_Init
; Step 1. Enabled the clock to the GPIOA Port
LDR R0,=RCC_AHB2ENR

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 76 / 203
Analog interfacing 7.2 ADC registers

Single-channel continuous conversion mode

LDR R1,[R0]
ORR R1,#RCC_GPIOA_EN
STR R1,[R0]
NOP

; Step 2. Enable output mode


LDR R0,=GPIOA_MODER
LDR R1,[R0]
MOV R2,#GPIO_MODE5_0
ORR R1,R2
LSL R2,R2,#1
BIC R1,R2
STR R1,[R0]

; Step 3. Enabled the clock to the GPIOB Port


LDR R0,=RCC_AHB2ENR

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 77 / 203
Analog interfacing 7.2 ADC registers

Single-channel continuous conversion mode

LDR R1,[R0]
ORR R1,#RCC_GPIOB_EN
STR R1,[R0]

; Step 4. Enable analog mode for PB0


LDR R0,=GPIOB_MODER
LDR R1,[R0]
ORR R1,GPIO_MODE0
STR R1,[R0]

; Step . No pull-down resistor


LDR R0,=GPIOB_PUPDR
LDR R1,[R0]
BIC R1,#GPIO_PUPD0
STR R1,[R0]

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 78 / 203
Analog interfacing 7.2 ADC registers

Single-channel continuous conversion mode

BX LR
ADC1_Init
; Step 1. Enable clock for ADC1, ADC2
LDR R0,=RCC_AHB2ENR
LDR R1,[R0]
ORR R1,#RCC_ADC12_EN
STR R1,[R0]
NOP

; Step 2. Enable ADC1/2 clock source selection


LDR R0,=RCC_CCIPR
LDR R1,[R0]
BIC R1,#RCC_ADC12_SEL
ORR R1,#RCC_ADC12_SEL_1
STR R1,[R0]
NOP

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 79 / 203
Analog interfacing 7.2 ADC registers

Single-channel continuous conversion mode

; Step 3. Enable continous mode


LDR R0,=ADC1_CFGR
LDR R1,[R0]
ORR R1,#ADC_CFGR_CONT
STR R1,[R0]

; Step 4. Select prescale for ADC1


LDR R0,=ADC1_CCR
LDR R1,[R0]
BIC R1,#ADC_CCR_PRESC
ORR R1,#ADC_CCR_PRESC_0+ADC_CCR_PRESC_2
STR R1,[R0]

; Step 5. Select ADC group sequencer scan length


LDR R0,=ADC1_SQR1
LDR R1,[R0]

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 80 / 203
Analog interfacing 7.2 ADC registers

Single-channel continuous conversion mode

BIC R1,#ADC_SQR1_L
STR R1,[R0]

; Step 6. Enable PB0 as ADC IN15


LDR R0,=ADC1_SQR1
LDR R1,[R0]
BIC R1,#ADC_SQ1
ORR R1, #ADC_SQ1_0+ADC_SQ1_1+ADC_SQ1_2+ADC_SQ1_3
STR R1,[R0]
NOP

; Step 7. Disable the Deep-power-down mode


LDR R0,=ADC1_CR
LDR R1,[R0]
BIC R1,#ADC_CR_DEEPPWD
STR R1,[R0]
NOP

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 81 / 203
Analog interfacing 7.2 ADC registers

Single-channel continuous conversion mode

; Step 8. Enable the ADC voltage regulator


LDR R0,=ADC1_CR
LDR R1,[R0]
ORR R1,#ADC_CR_ADVREGEN
STR R1,[R0]
NOP

; Step 9. Clear ADC ready bit by writing 1 to it


LDR R0,=ADC1_ISR
LDR R1,[R0]
ORR R1,#ADC_ISR_ADRDY
STR R1,[R0]
NOP

; Step 10. Enable ADC1


LDR R0,=ADC1_CR
LDR R1,[R0]

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 82 / 203
Analog interfacing 7.2 ADC registers

Single-channel continuous conversion mode

ORR R1,#ADC_CR_ADEN
STR R1,[R0]
NOP
Check_ADREADY
; Step 11. Wait until ADRDY = 1
LDR R0,=ADC1_ISR
LDR R1,[R0]
AND R1,#ADC_ISR_ADRDY
CMP R1,#0x01
BNE Check_ADREADY

BX LR

ALIGN
END

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 83 / 203
Analog interfacing 7.2 ADC registers

Single-channel continuous conversion mode

 Example 7.2
Consider a circuit of the STM32G431RBT6, a four 7-Seg common cathode LED,
and a potentiometer connected to pin PB0 as an ADC IN15 input as shown in the
figure. Write Assembly codes to show the ADC value on the LED.
12 D1

D2

D3

D4
U1 STM32G431RBT6
Q1 6 R1 12 8 IN6
9

PA0 PC0
R2 13 9
PA1 PC1
11 A 14 10
PA2 PC2
7 B 17 11
PA3 PC3
4 C R3 18 22
PA4 PC4
2 D R4 19 23
PA5 PC5
1 E R5 20 38
PA6 PC6
10 F R6 21 39
PA7 PC7
5 G R7 42 40
PA8 PC8
3 H R8 43 41
PA9 PC9
44 52
PA10 PC10
45 53
PA11 PC11
YY2841AH-33 VCC 46 54
PA12 PC12
49 2
PA13 PC13
50
PA14
IN15 51
R9 PA15

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 84 / 203
Analog interfacing 7.2 ADC registers

Single-channel continuous conversion mode

Solution:

Code 7.2

INCLUDE ../inc/stm32G431xx.s

DLVAL EQU 25000

...
Reset_Handler
BL GPIO_Init
BL ADC1_Init

LDR R1,=LED_codes
MOV R11,#0 ; Times to keep displaying number
Loop
; ADC1 start conversion
LDR R12,=ADC1_CR

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 85 / 203
Analog interfacing 7.2 ADC registers

Single-channel continuous conversion mode

LDR R6,[R12]
ORR R6,#ADC_CR_ADSTART
STR R6,[R12]
NOP
Check_conversion
LDR R12,=ADC1_ISR
LDR R6,[R12]
AND R6,#ADC_ISR_EOC
CMP R6,#0x00
BEQ Check_conversion

LDR R12,=ADC1_DR
LDR R6,[R12]

MOV R0,#1000 ; divisor


UDIV R2,R6,R0 ; R2 = Thousand

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 86 / 203
Analog interfacing 7.2 ADC registers

Single-channel continuous conversion mode

MUL R3,R2,R0 ;
SUB R6,R6,R3 ; R6 = Redundant
MOV R0,#4
MUL R3,R2,R0 ; Number ofsset

LDR R0,=GPIOA_ODR
LDR R2,[R1,R3] ;
STR R2,[R0] ;
LDR R0,=GPIOB_ODR
MVN R3,#BIT10 ;
STR R3,[R0] ;

LDR R2,=DLVAL ;
BL Delay

MOV R0,#100 ; divisor

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 87 / 203
Analog interfacing 7.2 ADC registers

Single-channel continuous conversion mode

UDIV R2,R6,R0 ; R2 = Thousand


MUL R3,R2,R0 ;
SUB R6,R6,R3 ; R6 = Redundant
MOV R0,#4
MUL R3,R2,R0 ; Number offset

LDR R0,=GPIOA_ODR
LDR R2,[R1,R3] ;
STR R2,[R0] ;
LDR R0,=GPIOB_ODR
MVN R3,#BIT5 ;
STR R3,[R0] ;

LDR R2,=DLVAL ;
BL Delay ;

MOV R0,#10 ; divisor

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 88 / 203
Analog interfacing 7.2 ADC registers

Single-channel continuous conversion mode

UDIV R2,R6,R0 ; R2 = Thousand


MUL R3,R2,R0 ;
SUB R6,R6,R3 ; R7 = Redundant
MOV R0,#4
MUL R3,R2,R0 ; Number ofsset

LDR R0,=GPIOA_ODR
LDR R2,[R1,R3] ;
STR R2,[R0] ;
LDR R0,=GPIOB_ODR
MVN R3,#BIT4 ;
STR R3,[R0] ;

LDR R2,=DLVAL ;
BL Delay ;
MOV R0,#4

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 89 / 203
Analog interfacing 7.2 ADC registers

Single-channel continuous conversion mode

MUL R3,R6,R0 ; Number offset

LDR R0,=GPIOA_ODR
LDR R2,[R1,R3] ;
STR R2,[R0] ;
LDR R0,=GPIOB_ODR
MVN R3,#BIT3 ;
STR R3,[R0] ;

LDR R2,=DLVAL ;
BL Delay ;

ADD R11,#1 ;
CMP R11,#50 ;
BLT Loop ;
MOV R11,#0 ;
B Loop ;

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 90 / 203
Analog interfacing 7.2 ADC registers

Single-channel continuous conversion mode

 Example 7.3
Consider a circuit of the STM32G431RBT6 with IC MAX7219, an eight-digit seven-
segment LEDs, and a potentiometer connected to pin PB0 as an ADC IN15 input
as shown in the figure. Write Assembly codes to show the ADC value on the LED.
STM32G431RBT6
12 8 IN6
PA0 PC0
13 9
PA1 PC1
14 10
PA2 PC2
17 11
PA3 PC3
18 22
PA4 PC4
19 23
PA5 PC5
20 38
PA6 PC6
21 39
PA7 PC7
42 40
PA8 PC8 VCC
43 41
PA9 PC9
44 52
PA10 PC10
53
PC11
24 54
PB0 PC12
25 2 R1
PB1 PC13
26 10K
PB2 18 DIGITS
56 ISET DIG0 - DIG7
PB3
57 U2
PB4
58 VCC DIN 1
PB5 55 DIN
59 PD2
PB6 LAT 12
60 LOAD (CS)
PB7 IN15
62 R2 SCK 13
PB9 CLK SEGMENTS
62 SEG A-G
PB9
30 SEG DP
PB10
33 9
PB11 GND
LAT 34
PB12 GND
SCK 35
PB13
36 MAX7219
PB14
DIN 37
PB15

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 91 / 203
Analog interfacing 7.2 ADC registers

Single-channel continuous conversion mode

Solution:

Code 7.3

INCLUDE ../inc/stm32G431xx.s

MAX7219_CS EQU BIT12


MAX7219_CLK EQU BIT13
MAX7219_DIN EQU BIT15

LED EQU BIT5


...
Reset_Handler
BL GPIO_Init
BL MAX7219_Init

BL ADC1_Init
LDR R5,=ADC1_THRH

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 92 / 203
Analog interfacing 7.2 ADC registers

Single-channel continuous conversion mode

Loop
; ADC1 start conversion
LDR R0,=ADC1_CR
LDR R1,[R0]
ORR R1,#ADC_CR_ADSTART
STR R1,[R0]
NOP
Check_conversion
LDR R0,=ADC1_ISR
LDR R1,[R0]
AND R1,#ADC_ISR_EOC
CMP R1,#0x00
BEQ Check_conversion

LDR R0,=ADC1_DR
LDR R6,[R0]

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 93 / 203
Analog interfacing 7.2 ADC registers

Single-channel continuous conversion mode

CMP R6,#500
BGE LED_ON
LED_OFF
LDR R0,=GPIOA_ODR
LDR R1,=LED
BIC R1,#LED
STR R1,[R0]

B MAX7219
LED_ON
LDR R0,=GPIOA_ODR
LDR R1,=LED
STR R1,[R0]
MAX7219
ADD R12,#1
CMP R12,#50

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 94 / 203
Analog interfacing 7.2 ADC registers

Single-channel continuous conversion mode

BLT Loop

MOV R12,#0

MOV32 R8,#10000000
UDIV R3,R6,R8 ;
MUL R7,R3,R8 ;
SUB R7,R6,R7 ; R7 = Redundant
ORR R3,#0x0800
LSL R3,R3,#16
BL shiftout

MOV32 R8,#1000000
UDIV R3,R7,R8 ;
MUL R8,R3,R8 ;
SUB R7,R7,R8 ; R7 = Redundant

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 95 / 203
Analog interfacing 7.2 ADC registers

Single-channel continuous conversion mode

ORR R3,#0x0700
LSL R3,R3,#16
BL shiftout

MOV32 R8,#100000
UDIV R3,R7,R8 ;
MUL R8,R3,R8 ;
SUB R7,R7,R8 ; R7 = Redundant
ORR R3,#0x0600
LSL R3,R3,#16
BL shiftout

MOV32 R8,#10000
UDIV R3,R7,R8 ;
MUL R8,R3,R8 ;
SUB R7,R7,R8 ; R7 = Redundant
ORR R3,#0x0500

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 96 / 203
Analog interfacing 7.2 ADC registers

Single-channel continuous conversion mode

LSL R3,R3,#16
BL shiftout

MOV32 R8,#1000
UDIV R3,R7,R8 ;
MUL R8,R3,R8 ;
SUB R7,R7,R8 ; R7 = Redundant
ORR R3,#0x0400
LSL R3,R3,#16
BL shiftout

MOV32 R8,#100
UDIV R3,R7,R8 ;
MUL R8,R3,R8 ;
SUB R7,R7,R8 ; R7 = Redundant
ORR R3,#0x0300

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 97 / 203
Analog interfacing 7.2 ADC registers

Single-channel continuous conversion mode

LSL R3,R3,#16
BL shiftout

MOV32 R8,#10
UDIV R3,R7,R8 ;
MUL R8,R3,R8 ;
SUB R7,R7,R8 ; R7 = Redundant
ORR R3,#0x0200
LSL R3,R3,#16
BL shiftout

MOV R3,R7
ORR R3,#0x0100
LSL R3,R3,#16
BL shiftout

B Loop

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 98 / 203
Analog interfacing 7.2 ADC registers

Exponential Moving Average Filter

 Example 7.4
Consider a circuit of the STM32G431RBT6 with IC MAX7219, an eight-digit seven-
segment LEDs, and a potentiometer connected to pin PB0 as an ADC IN15 input
as shown in the figure as shown in the figure. Write Assembly codes to implement
an Exponential Moving Average Filter and show the ADC value on the LED.
STM32G431RBT6
12 8 IN6
PA0 PC0
13 9
PA1 PC1
14 10
PA2 PC2
17 11
PA3 PC3
18 22
PA4 PC4
19 23
PA5 PC5
20 38
PA6 PC6
21 39
PA7 PC7
42 40
PA8 PC8 VCC
43 41
PA9 PC9
44 52
PA10 PC10
53
PC11
24 54
PB0 PC12
25 2 R1
PB1 PC13
26 10K
PB2 18 DIGITS
56 ISET DIG0 - DIG7
PB3
57 U2
PB4
58 VCC DIN 1
PB5 55 DIN
59 PD2
PB6 LAT 12
60 LOAD (CS)
PB7 IN15
62 R2 SCK 13
PB9 CLK SEGMENTS
62 SEG A-G
PB9
30 SEG DP
PB10
33 9
PB11 GND
LAT 34
PB12 GND
SCK 35
PB13
36 MAX7219
PB14
DIN 37
PB15

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 99 / 203
Analog interfacing 7.2 ADC registers

Exponential Moving Average Filter


The exponential moving average (EMA) filter is formed by a combination of the
current input value and the previous output one. It is also known as a low-pass
infinite-impulse response (IIR) filter.
The difference equation for an EMA filter can be expressed as:

y[n] = αx[n] + (1 − α)y[n − 1]

where y is the output, x is the input, and α is the filter weighted value.

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 100 / 203
Analog interfacing 7.2 ADC registers

Exponential Moving Average Filter

Solution:

Code 7.4
...
LDR R0,=ADC1_DR
LDR R6,[R0]

MOV R1,#49
MUL R2,R2,R1
ADD R2,R6
MOV R1,#50
UDIV R6,R2,R1
MOV R2,R6
...

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 101 / 203
Analog interfacing 7.2 ADC registers

Multiple-channel continuous conversion mode

 Example 7.5
Consider a circuit of the STM32G431RBT6 with IC MAX7219, an eight-digit seven-
segment LEDs, and two potentiometers connected to pins PA0 (ADC IN1) and PA1
(ADC IN2) as shown in the figure. Write Assembly codes to show the ADC values
of each channels on the the group of 4 LEDs. The ADC conversion is in the
continuous mode.

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 102 / 203
Analog interfacing 7.2 ADC registers

Multiple-channel continuous conversion mode

STM32G431RBT6
12 8 IN1
PA0 PC0
13 9 IN2
PA1 PC1
14 10
PA2 PC2
17 11
PA3 PC3
18 22
PA4 PC4
19 23
PA5 PC5
20 38
PA6 PC6
21 39
PA7 PC7
42 40
PA8 PC8 VCC
43 41
PA9 PC9
44 52 VCC
PA10 PC10
53
PC11
24 54
PB0 PC12
25 2 IN1 R1
PB1 PC13 R2
26 10K
PB2 18 DIGITS
56 ISET DIG0 - DIG7
PB3
57 U2
PB4
58 DIN 1
PB5 55 DIN
59 PD2
PB6 LAT 12
60 LOAD (CS)
PB7
62 VCC SCK 13
PB9 CLK SEGMENTS
62 SEG A-G
PB9
30 SEG DP
PB10 IN2
33 R3 9
PB11 GND
LAT 34
PB12 GND
SCK 35
PB13
36 MAX7219
PB14
DIN 37
PB15

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 103 / 203
Analog interfacing 7.2 ADC registers

Multiple-channel continuous conversion mode

Solution:

Code 7.5

INCLUDE ../../inc/stm32G431xx.s

MAX7219_CS EQU BIT12


MAX7219_CLK EQU BIT13
MAX7219_DIN EQU BIT15

Reset_Handler
BL GPIO_Init
BL MAX7219_Init
BL ADC1_Init
LDR R5,=ADC1_THRH

MOV R2,#0
MOV R10,#0

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 104 / 203
Analog interfacing 7.2 ADC registers

Multiple-channel continuous conversion mode

; ADC1 start conversion


LDR R0,=ADC1_CR
LDR R1,[R0]
ORR R1,#ADC_CR_ADSTART
STR R1,[R0]
NOP
Loop
MOV R1,#0

Check_conversion_CH1
LDR R0,=ADC1_ISR
LDR R1,[R0]
AND R1,#ADC_ISR_EOC
CMP R1,#0x00
BEQ Check_conversion_CH1

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 105 / 203
Analog interfacing 7.2 ADC registers

Multiple-channel continuous conversion mode

LDR R0,=ADC1_DR
LDR R6,[R0]
Check_conversion_CH2
LDR R0,=ADC1_ISR
LDR R1,[R0]
AND R1,#ADC_ISR_EOC
CMP R1,#0x00
BEQ Check_conversion_CH2

LDR R0,=ADC1_DR
LDR R9,[R0]

; EMA Filter CH1


MOV R1,#49
MUL R2,R2,R1
ADD R2,R6

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 106 / 203
Analog interfacing 7.2 ADC registers

Multiple-channel continuous conversion mode

MOV R1,#50
UDIV R6,R2,R1
MOV R2,R6

; EMA Filter CH2


MOV R1,#49
MUL R10,R10,R1
ADD R10,R9
MOV R1,#50
UDIV R9,R10,R1
MOV R10,R9
MAX7219
ADD R12,#1
CMP R12,#50
BLT Loop

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 107 / 203
Analog interfacing 7.2 ADC registers

Multiple-channel continuous conversion mode

MOV R12,#0

MOV32 R8,#1000
UDIV R3,R6,R8 ;
MUL R8,R3,R8 ;
SUB R7,R6,R8 ; R7 = Redundant
ORR R3,#0x0800
LSL R3,R3,#16
BL shiftout

MOV32 R8,#100
UDIV R3,R7,R8 ;
MUL R8,R3,R8 ;
SUB R7,R7,R8 ; R7 = Redundant
ORR R3,#0x0700
LSL R3,R3,#16

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 108 / 203
Analog interfacing 7.2 ADC registers

Multiple-channel continuous conversion mode

BL shiftout

MOV32 R8,#10
UDIV R3,R7,R8 ;
MUL R8,R3,R8 ;
SUB R7,R7,R8 ; R7 = Redundant
ORR R3,#0x0600
LSL R3,R3,#16
BL shiftout

MOV R3,R7
ORR R3,#0x0500
LSL R3,R3,#16
BL shiftout

MOV32 R8,#1000

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 109 / 203
Analog interfacing 7.2 ADC registers

Multiple-channel continuous conversion mode

UDIV R3,R9,R8 ;
MUL R8,R3,R8 ;
SUB R7,R9,R8 ; R7 = Redundant
ORR R3,#0x0400
LSL R3,R3,#16
BL shiftout

MOV32 R8,#100
UDIV R3,R7,R8 ;
MUL R8,R3,R8 ;
SUB R7,R7,R8 ; R7 = Redundant
ORR R3,#0x0300
LSL R3,R3,#16
BL shiftout

MOV32 R8,#10

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 110 / 203
Analog interfacing 7.2 ADC registers

Multiple-channel continuous conversion mode

UDIV R3,R7,R8 ;
MUL R8,R3,R8 ;
SUB R7,R7,R8 ; R7 = Redundant
ORR R3,#0x0200
LSL R3,R3,#16
BL shiftout

MOV R3,R7
ORR R3,#0x0100
LSL R3,R3,#16
BL shiftout

B Loop
GPIO_Init
; Enabled the clock to the GPIOA Port

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 111 / 203
Analog interfacing 7.2 ADC registers

Multiple-channel continuous conversion mode

LDR R0,=RCC_AHB2ENR
LDR R1,[R0]
ORR R1,#RCC_GPIOA_EN
STR R1,[R0]
NOP

; Enable output mode


LDR R0,=GPIOA_MODER
LDR R1,[R0]
MOV R2,#GPIO_MODE5_0
ORR R1,R2
LSL R2,R2,#1
BIC R1,R2
STR R1,[R0]

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 112 / 203
Analog interfacing 7.2 ADC registers

Multiple-channel continuous conversion mode

; Enable analog mode for PA0 and PA1


LDR R0,=GPIOA_MODER
LDR R1,[R0]
ORR R1,#GPIO_MODE0+GPIO_MODE1
STR R1,[R0]

; No pull-down resistor
LDR R0,=GPIOA_PUPDR
LDR R1,[R0]
BIC R1,#GPIO_PUPD0+GPIO_PUPD1
STR R1,[R0]

; Enabled the clock to the GPIOB Port


LDR R0,=RCC_AHB2ENR
LDR R1,[R0]
ORR R1,#RCC_GPIOB_EN

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 113 / 203
Analog interfacing 7.2 ADC registers

Multiple-channel continuous conversion mode

STR R1,[R0]

; Enable output mode for MAX7219


LDR R0,=GPIOB_MODER
LDR R1,[R0]
MOV R2,#GPIO_MODE12_0+GPIO_MODE13_0+GPIO_MODE15_0
ORR R1,R2
LSL R2,R2,#1
BIC R1,R2
STR R1,[R0]

BX LR
ADC1_Init
; Enable clock for ADC1, ADC2
LDR R0,=RCC_AHB2ENR
LDR R1,[R0]

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 114 / 203
Analog interfacing 7.2 ADC registers

Multiple-channel continuous conversion mode

ORR R1,#RCC_ADC12_EN
STR R1,[R0]

; Enable ADC1/2 clock source selection


LDR R0,=RCC_CCIPR
LDR R1,[R0]
BIC R1,#RCC_ADC12_SEL
ORR R1,#RCC_ADC12_SEL_1
STR R1,[R0]

; Enable continous mode


LDR R0,=ADC1_CFGR
LDR R1,[R0]
ORR R1,#ADC_CFGR_CONT
STR R1,[R0]

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 115 / 203
Analog interfacing 7.2 ADC registers

Multiple-channel continuous conversion mode

; Auto-delayed conversion mode


LDR R0,=ADC1_CFGR
LDR R1,[R0]
ORR R1,#ADC_CFGR_AUTDLY
STR R1,[R0]

; Select prescale for ADC1


LDR R0,=ADC1_CCR
LDR R1,[R0]
BIC R1,#ADC_CCR_PRESC
ORR R1,#ADC_CCR_PRESC_0+ADC_CCR_PRESC_1+ADC_CCR_PR
STR R1,[R0]

; Select sampling time for channel 1


LDR R0,=ADC1_SMPR1
LDR R1,[R0]

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 116 / 203
Analog interfacing 7.2 ADC registers

Multiple-channel continuous conversion mode

ORR R1,#ADC_SMPR1_SMP1
STR R1,[R0]

; Select scan length (2 conversion)


LDR R0,=ADC1_SQR1
LDR R1,[R0]
BIC R1,#ADC_SQR1_L
ORR R1,#ADC_SQR1_L_0
STR R1,[R0]

; Enable PA0 as ADC IN1


LDR R0,=ADC1_SQR1
LDR R1,[R0]
BIC R1,#ADC_SQ1
ORR R1,#ADC_SQ1_0
STR R1,[R0]

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 117 / 203
Analog interfacing 7.2 ADC registers

Multiple-channel continuous conversion mode

; Enable PA1 as ADC IN2


LDR R0,=ADC1_SQR1
LDR R1,[R0]
BIC R1,#ADC_SQ2
ORR R1,#ADC_SQ2_1
STR R1,[R0]

; Disable the Deep-power-down mode


LDR R0,=ADC1_CR
LDR R1,[R0]
BIC R1,#ADC_CR_DEEPPWD
STR R1,[R0]

; Enable the ADC voltage regulator


LDR R0,=ADC1_CR
LDR R1,[R0]

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 118 / 203
Analog interfacing 7.2 ADC registers

Multiple-channel continuous conversion mode

ORR R1,#ADC_CR_ADVREGEN
STR R1,[R0]

; Clear ADC ready bit


LDR R0,=ADC1_ISR
LDR R1,[R0]
ORR R1,#ADC_ISR_ADRDY
STR R1,[R0]

; Step 12. Enable ADC1


LDR R0,=ADC1_CR
LDR R1,[R0]
ORR R1,#ADC_CR_ADEN
STR R1,[R0]
Check_ADREADY
; Wait until ADRDY=1

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 119 / 203
Analog interfacing 7.2 ADC registers

Multiple-channel continuous conversion mode

LDR R0,=ADC1_ISR
LDR R1,[R0]
AND R1,#ADC_ISR_ADRDY
CMP R1,#0x01
BNE Check_ADREADY

BX LR
shiftout
...

BX LR
MAX7219_Init
...

BX LR

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 120 / 203
Analog interfacing 7.2 ADC registers

Multiple-channel continuous conversion mode

ALIGN
END

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 121 / 203
Analog interfacing 7.2 ADC registers

Multiple-channel single conversion mode

 Example 7.6

Consider a circuit of the STM32G431RBT6 with IC MAX7219, an eight-digit seven-


segment LEDs, and two potentiometers connected to pins PA0 (ADC IN1) and PA1
(ADC IN2) as shown in the figure. Write Assembly codes to show the ADC values
of each channels on the the group of 4 LEDs. The ADC conversion is in the single
mode.

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 122 / 203
Analog interfacing 7.2 ADC registers

Multiple-channel single conversion mode

STM32G431RBT6
12 8 IN1
PA0 PC0
13 9 IN2
PA1 PC1
14 10
PA2 PC2
17 11
PA3 PC3
18 22
PA4 PC4
19 23
PA5 PC5
20 38
PA6 PC6
21 39
PA7 PC7
42 40
PA8 PC8 VCC
43 41
PA9 PC9
44 52 VCC
PA10 PC10
53
PC11
24 54
PB0 PC12
25 2 IN1 R1
PB1 PC13 R2
26 10K
PB2 18 DIGITS
56 ISET DIG0 - DIG7
PB3
57 U2
PB4
58 DIN 1
PB5 55 DIN
59 PD2
PB6 LAT 12
60 LOAD (CS)
PB7
62 VCC SCK 13
PB9 CLK SEGMENTS
62 SEG A-G
PB9
30 SEG DP
PB10 IN2
33 R3 9
PB11 GND
LAT 34
PB12 GND
SCK 35
PB13
36 MAX7219
PB14
DIN 37
PB15

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 123 / 203
Analog interfacing 7.2 ADC registers

Multiple-channel single conversion mode

Solution:

Code 7.6
Reset_Handler
BL GPIO_Init
BL MAX7219_Init
BL ADC1_Init
LDR R5,=ADC1_THRH

MOV R2,#0
MOV R10,#0
Loop
; ADC1 start conversion CH1
LDR R0,=ADC1_CR
LDR R1,[R0]
ORR R1,#ADC_CR_ADSTART
STR R1,[R0]

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 124 / 203
Analog interfacing 7.2 ADC registers

Multiple-channel single conversion mode

Check_conversion_CH1
LDR R0,=ADC1_ISR
LDR R1,[R0]
AND R1,#ADC_ISR_EOC
CMP R1,#0x00
BEQ Check_conversion_CH1

LDR R0,=ADC1_DR
LDR R6,[R0]
Check_conversion_CH2
LDR R0,=ADC1_ISR
LDR R1,[R0]
AND R1,#ADC_ISR_EOC
CMP R1,#0x00
BEQ Check_conversion_CH2

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 125 / 203
Analog interfacing 7.2 ADC registers

Multiple-channel single conversion mode

LDR R0,=ADC1_DR
LDR R9,[R0]

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 126 / 203
Analog interfacing 7.4 DAC registers

DAC output

 Example 7.7
Consider again the circuit of the STM32G431RBT6 with IC MAX7219 and a poten-
tiometer connected to pin PB0 (ADC IN15) as shown in the figure. Write Assembly
codes to show the ADC value on the LED and send it to DAC output on pin PA4.
STM32G431RBT6
12 8 IN1
PA0 PC0
13 9
PA1 PC1
14 10
PA2 PC2
17 11
PA3 PC3
DAC 18 22
PA4 PC4
19 23
PA5 PC5
20 38
PA6 PC6
21 39
PA7 PC7
42 40
PA8 PC8 VCC
43 41
PA9 PC9
44 52 VCC
PA10 PC10
53
PC11
24 54
PB0 PC12
25 2 IN1 R1
PB1 PC13 R2
26 10K
PB2 18 DIGITS
56 ISET DIG0 - DIG7
PB3
57 U2
PB4
58 DIN 1
PB5 55 DIN
59 PD2
PB6 LAT 12
60 LOAD (CS)
PB7
62 SCK 13
PB9 CLK SEGMENTS
62 SEG A-G
PB9
30 SEG DP
PB10
33 9
PB11 GND
LAT 34
PB12 GND
SCK 35
PB13
36 MAX7219
PB14
DIN 37
PB15

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 165 / 203
Analog interfacing 7.4 DAC registers

DAC output

Solution:

Code 7.7
Reset_Handler
BL GPIO_Init
BL MAX7219_Init
BL ADC1_Init
BL DAC1_Init
Loop
; ADC1 start conversion
LDR R0,=ADC1_CR
LDR R1,[R0]
ORR R1,#ADC_CR_ADSTART
STR R1,[R0]
NOP
Check_conversion
LDR R0,=ADC1_ISR

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 166 / 203
Analog interfacing 7.4 DAC registers

DAC output

LDR R1,[R0]
AND R1,#ADC_ISR_EOC
CMP R1,#0x00
BEQ Check_conversion

LDR R0,=ADC1_DR
LDR R6,[R0]

LDR R0,=DAC1_DHR12R1
STR R6,[R0]
MAX7219
...

B Loop

GPIO_Init

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 167 / 203
Analog interfacing 7.4 DAC registers

DAC output

; Enabled the clock to the GPIOA Port


LDR R0,=RCC_AHB2ENR
LDR R1,[R0]
ORR R1,#RCC_GPIOA_EN
STR R1,[R0]

; Enable analog mode


LDR R0,=GPIOA_MODER
LDR R1,[R0]
ORR R1,#GPIO_MODE4_0+GPIO_MODE4_1
STR R1,[R0]

; Enable output mode


LDR R0,=GPIOA_MODER
LDR R1,[R0]
MOV R2,#GPIO_MODE5_0

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 168 / 203
Analog interfacing 7.4 DAC registers

DAC output

ORR R1,R2
LSL R2,R2,#1
BIC R1,R2
STR R1,[R0]

; Enabled the clock to the GPIOB Port


LDR R0,=RCC_AHB2ENR
LDR R1,[R0]
ORR R1,#RCC_GPIOB_EN
STR R1,[R0]

; Enable analog mode for PB0


LDR R0,=GPIOB_MODER
LDR R1,[R0]
ORR R1,#GPIO_MODE0
STR R1,[R0]

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 169 / 203
Analog interfacing 7.4 DAC registers

DAC output

; No pull-down resistor
LDR R0,=GPIOB_PUPDR
LDR R1,[R0]
BIC R1,#GPIO_PUPD0
STR R1,[R0]

; Enable output mode for MAX7219


LDR R0,=GPIOB_MODER
LDR R1,[R0]
MOV R2,#GPIO_MODE12_0+GPIO_MODE13_0+GPIO_MODE15_0
ORR R1,R2
LSL R2,R2,#1
BIC R1,R2
STR R1,[R0]

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 170 / 203
Analog interfacing 7.4 DAC registers

DAC output

BX LR
DAC1_Init
; Enable clock for DAC1
LDR R0,=RCC_AHB2ENR
LDR R1,[R0]
ORR R1,#RCC_DAC1_EN
STR R1,[R0]

; Trigger disable
LDR R0,=DAC1_CR
LDR R1,[R0]
BIC R1,#DAC_CR_TEN1
STR R1,[R0]

; Triangle wave disable


LDR R0,=DAC1_CR

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 171 / 203
Analog interfacing 7.4 DAC registers

DAC output

LDR R1,[R0]
BIC R1,#DAC_CR_WAVE1
STR R1,[R0]

; Use the Buffer


LDR R0,=DAC1_MCR
LDR R1,[R0]
BIC R1,#DAC_MCR_MODE1
STR R1,[R0]

LDR R0,=DAC1_CR
LDR R1,[R0]
ORR R1,#DAC_CR_EN1
STR R1,[R0]

LDR R0,=DAC1_DHR12R1

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 172 / 203
Analog interfacing 7.4 DAC registers

DAC output

MOV R1,#0
STR R1,[R0]

BX LR
ADC1_Init
...
BX LR
shiftout
...
BX LR
MAX7219_Init
...
BX LR

ALIGN
END

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 173 / 203
Analog interfacing 7.4 DAC registers

Discrete-time PID controller


Assume that register S1 contains the required material flux Qref , register S2 con-
tains the real weight w, and register S3 contains the real velocity v. The discrete-
time PID controller can be implemented as follows

; Calculate real material flux Q = w.v


VMUL.F S3,S3,S2 ; S3 stores real Flux

; Calculate the error e = Qref - Q


VSUB.F S3,S1,S3 ; S3 stores the error

; Calculate the propotional part P = kp.e


LDR R0,=kp
VLDR.F S0,[R0]
VMUL.F S4,S3,S0 ; S4 stores the P part

; Calculate the integral part I = ki.e + Iprev


LDR R0,=ki

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 185 / 203
Analog interfacing 7.4 DAC registers

Discrete-time PID controller

VLDR.F S0,[R0]
VMUL.F S5,S3,S0 ; ki.e
VADD.F S5,S5,S7 ; S5 stores the I part

; Calculate the derivative part D = kd(e-eprev)


LDR R0,=kd
VLDR.F S0,[R0]
VSUB.F S6,S3,S8 ; e - eprev
VMUL.F S6,S6,S0 ; S6 stores the D part

; Store previous values


VMOV.F S7,S5 ; The previous I part
VMOV.F S8,S3 ; The previous error

; Control signal
VADD.F S9,S4,S5 ;
VADD.F S9,S9,S6 ; The control value

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 186 / 203
Analog interfacing 7.4 DAC registers

Discrete-time PID controller

; Calculate the real control value


VCVTR.U32.F S9,S9 ; FP to integer
VMOV.F R1,S9 ; with the rounding mode

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 187 / 203
Serial communication interface

Chapter 8
Serial communication interface

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 1 / 135
Serial communication interface 8.2 USART registers

USART transmit and receive data

 Example 8.1
Suppose we have a computer interfacing with STM32G431RBT6 through an ST-
Link circuit and USART2 as shown in the figure. Write a program to receive a
character and transmit it back to the receiver (echo).
U1
12 24
PA0 PB0
13 25
PA1 PB1
TX 14 26
PA2 PB2
ST-Link RX 17 56
PA3 PB3
18 57
PC PA4 PB4
19 58
PA5 PB5
20 59
PA6 PB6
21 60
PA7 PB7
42
PA8
43 62
PA9 PB9
44 30
PA10 PB10
45 33
PA11 PB11
46 34
PA12 PB12
49 35
PA13 PB13
50 36
PA14 PB14
51 37
PA15 PB15

STM32G431RBT6

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 36 / 135
Serial communication interface 8.2 USART registers

USART transmit and receive data

Solution:

Code 8.1

INCLUDE ../inc/stm32G431xx.s

Stack_Size EQU 0x00000400

AREA STACK,NOINIT,READWRITE,ALIGN=3

Stack_Mem SPACE Stack_Size

AREA RESET,DATA, READONLY


EXPORT __Vectors
__Vectors
DCD Stack_Mem+Stack_Size
DCD Reset_Handler
ALIGN

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 37 / 135
Serial communication interface 8.2 USART registers

USART transmit and receive data

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


THUMB
ENTRY
EXPORT Reset_Handler
Reset_Handler
BL GPIO_Init
BL USART2_Init
Loop
LDR R0,=USART2_ISR
LDR R1,[R0]
AND R1,#USART_ISR_RXNE
CMP R1,#0x00
BEQ Loop
LDR R0,=USART2_RDR
LDR R1,[R0]

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 38 / 135
Serial communication interface 8.2 USART registers

USART transmit and receive data

LDR R0,=USART2_TDR
STR R1,[R0]
Check_TXC
LDR R0,=USART2_ISR
LDR R1,[R0]
AND R1,#USART_ISR_TC
CMP R1,#0x00
BEQ Check_TXC

B Loop
GPIO_Init
; Step 1. Enabled the clock to the GPIOA Port
LDR R0,=RCC_AHB2ENR
LDR R1,[R0]
ORR R1,#RCC_GPIOA_EN
STR R1,[R0]

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 39 / 135
Serial communication interface 8.2 USART registers

USART transmit and receive data

; Step 2. Enable output mode


LDR R0,=GPIOA_MODER
LDR R1,[R0]
MOV R2,#GPIO_MODE5_0
ORR R1,R2
LSL R2,R2,#1
BIC R1,R2
STR R1,[R0]

; Step 3. Enable alternative function


LDR R0,=GPIOA_MODER
LDR R1,[R0]
BIC R1,#GPIO_MODE2+GPIO_MODE3
ORR R1,#GPIO_MODE2_1+GPIO_MODE3_1
STR R1,[R0]

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 40 / 135
Serial communication interface 8.2 USART registers

USART transmit and receive data

; Step 4. Select UART mode (AF7) for PA2


LDR R0,=GPIOA_AFRL
LDR R1,[R0]
BIC R1,#GPIO_AFSEL2
ORR R1,#GPIO_AFSEL2_0+GPIO_AFSEL2_1+GPIO_AFSEL2_2
STR R1,[R0]

; Step 5. Select UART mode (AF7) for PA3


LDR R0,=GPIOA_AFRL
LDR R1,[R0]
BIC R1,#GPIO_AFSEL3
ORR R1,#GPIO_AFSEL3_0+GPIO_AFSEL3_1+GPIO_AFSEL3_2
STR R1,[R0]

BX LR

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 41 / 135
Serial communication interface 8.2 USART registers

USART transmit and receive data

USART2_Init
; Step 1. Enabled the clock to the USART
LDR R0,=RCC_APB1ENR1
LDR R1,[R0]
ORR R1,#RCC_USART2_EN
STR R1,[R0]
NOP

; Step 2. Disable the USART


; Step 3. Enable the RE and TE bits
; Step 4. Select Word length
; Step 5. Specify Oversampling mode
LDR R0,=USART2_CR1
LDR R1,[R0]
BIC R1,#USART_CR1_UE
ORR R1,#USART_CR1_RE+USART_CR1_TE

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 42 / 135
Serial communication interface 8.2 USART registers

USART transmit and receive data

BIC R1,#USART_CR1_M0+USART_CR1_M1
BIC R1,#USART_CR1_OVER8 ; Oversampling 16
STR R1,[R0]

; Step 6. Set UART Baud rate


LDR R0,=USART2_BRR
MOV R1,#138 ; 16.000.000/115.200 = 138
STR R1,[R0]

; Step 7. Enable the USART


LDR R0,=USART2_CR1
LDR R1,[R0]
ORR R1,#USART_CR1_UE
STR R1,[R0]

BX LR

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 43 / 135
Serial communication interface 8.2 USART registers

USART transmit and receive data

ALIGN
END

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 44 / 135
Serial communication interface 8.2 USART registers

USART transmit and receive data

 Example 8.2
Suppose we have a computer interfacing with STM32G431RBT6 through an ST-
Link circuit and LPUSART1 as shown in the figure. Write a program to turn the
LED on if the received data is larger than 0x65, else turn the LED off.
U1
12 24
PA0 PB0
13 25
PA1 PB1
TX 14 26
ST-Link PA2 PB2
RX 17 56
PA3 PB3
PC 18 57
PA4 PB4
19 58
PA5 PB5
R1 20 59
PA6 PB6
21 60
PA7 PB7
N1 42
PA8
43 62
PA9 PB9
44 30
PA10 PB10
VCC 45 33
PA11 PB11
46 34
PA12 PB12
49 35
PA13 PB13
50 36
PA14 PB14
51 37
PA15 PB15

STM32G431RBT6

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 46 / 135
Serial communication interface 8.2 USART registers

USART transmit and receive data

Solution:

Code 8.2
Reset_Handler
BL GPIO_Init
BL LPUSART1_Init
Loop
LDR R0,=LPUART1_ISR
LDR R1,[R0]
AND R1,#USART_RXNE
CMP R1,#0x00
BEQ Loop
LDR R0,=LPUART1_RDR
LDR R2,[R0]

CMP R2,#0x065
BGE LED_ON

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 47 / 135
Serial communication interface 8.2 USART registers

USART transmit and receive data

LED_OFF
LDR R0,=GPIOA_ODR
LDR R1,=LED
BIC R1,#LED
STR R1,[R0]

B Send_Back
LED_ON
LDR R0,=GPIOA_ODR
LDR R1,=LED
STR R1,[R0]

Send_Back
LDR R0,=LPUART1_TDR
STR R2,[R0]

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 48 / 135
Serial communication interface 8.2 USART registers

USART transmit and receive data

Check_TXC
LDR R0,=LPUART1_ISR
LDR R1,[R0]
AND R1,#USART_TC
CMP R1,#0x00
BEQ Check_TXC

B Loop

...
LPUSART1_Init
; Step 1. Enabled the clock to the LPUSART1
LDR R0,=RCC_APB1ENR2
LDR R1,[R0]
ORR R1,#RCC_LPUART1_EN
STR R1,[R0]

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 49 / 135
Serial communication interface 8.2 USART registers

USART transmit and receive data

; Step 2. Select clock source for LPUART1


LDR R0,=RCC_CCIPR
LDR R1,[R0]
BIC R1,#RCC_CCIPR_LPUART1SEL
ORR R1,#RCC_CCIPR_LPUART1SEL_0
STR R1,[R0]

; Step 3. Configure LPUART1


LDR R0,=LPUART1_CR1
LDR R1,[R0]
BIC R1,#USART_CR1_UE
ORR R1,#USART_CR1_RE+USART_CR1_TE
BIC R1,#USART_CR1_M0+USART_CR1_M1
STR R1,[R0]

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 50 / 135
Serial communication interface 8.2 USART registers

USART transmit and receive data

; Step 4. Set UART Baud rate


LDR R0,=LPUART1_BRR
MOV32 R1,#0x8AE4 ; 256x16.000.000/115.200=0x8AE4
STR R1,[R0]

; Step 5. Enable UART


LDR R0,=LPUART1_CR1
LDR R1,[R0]
ORR R1,#USART_CR1_UE
STR R1,[R0]
NOP

BX LR

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 51 / 135
Serial communication interface 8.3 Serial peripheral interface (SPI)

SPI communications

 Example 8.3 (SPI 74HC595)


Consider again the circuit of the STM32G431RBT6 CPU with ICs 74HC595 and
eight seven-segment LEDs as shown in the figure. Write a program to display
numbers 12345678 on the LEDs using SPI.

DP

DP
G

G
C
D

C
D
A
B

A
B

E
F

F
10

10
11

11
7
4
2
1

5
3

7
4
2
1

5
3
U1 a b c d e f g dp a b c d e f g dp
STM32G431RBT6
12 24
PA0 PB0
13 25
PA1 PB1
14 26
PA2 PB2
17 56
PA3 PB3
18 57
PA4 PB4
19 58

g1

g1
g2

g2
g3
g4

g3
g4
PA5 PB5
59
PB6
60

9
6

6
8

8
DIG7 12

DIG3 12
PB7
8 62
DIG6

DIG2
DIG4

DIG0
DIG5

DIG1
PC0 PB9
9 30
PC1 PB10
10 33
PC2 PB11
11 34 LAT U2 U3
PC3 PB12
22 35 SCK SCK 11 15 DIG0 SCK 11 15 A
PC4 PB13 CLK Q0 CLK Q0
23 36 DAT 14 1 DIG1 DAT 14 1 B
PC5 PB14 DAT Q1 DAT Q1
38 37 DAT 2 DIG2 2 C
PC6 PB15 Q2 Q2
39 LAT 12 3 DIG3 LAT 12 3 D
PC7 LAT Q3 LAT Q3
40 4 DIG4 4 E
PC8 Q4 Q4
41 55 5 DIG5 5 F
PC9 PD2 Q5 Q5
52 6 DIG6 6 G
PC10 Q6 Q6
53
VCC

VCC
PC11 10 7 DIG7 10 7 H
54 MR Q7 MR Q7
PC12 13 9 13 9 DP
2 OE Q7' OE Q7'
PC13
74HC595 74HC595

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 73 / 135
Serial communication interface 8.3 Serial peripheral interface (SPI)

SPI communications

Solution:

Code 8.3

INCLUDE ../inc/stm32G431xx.s

LED_codes DCB 0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,


0x80,0x90,0x8C,0xBF,0xC6,0xA1,0x86,0xFF,
0xBF

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


THUMB
ENTRY
EXPORT __main
__main
BL GPIO_Init
BL SPI2_Init
Start

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 74 / 135
Serial communication interface 8.3 Serial peripheral interface (SPI)

SPI communications

MOV R6,#1
MOV R7,#1
LSL R7,R7,#7
Loop
LDR R2,=LED_codes ;
LDR R3,[R2,R6]
LSL R3,R3,#8
ORR R3,R7

BL SPI2_Transmit

MOV32 R8,#0x00FFFFFF ;
Delay
SUBS R8,#1
BNE Delay

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 75 / 135
Serial communication interface 8.3 Serial peripheral interface (SPI)

SPI communications

ADDS R6,R6,#1
CMP R6,#0x09
BGE Start ;
LSR R7,R7,#1

B Loop

GPIO_Init
; Step 1. Enabled the clock to the GPIOB Port
LDR R0,=RCC_AHB2ENR
LDR R1,[R0]
ORR R1,#RCC_AHB2ENR_GPIOBEN
STR R1,[R0]

; PB13, PB14, PB15 as alternate function mode


LDR R0,=GPIOB_MODER

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 76 / 135
Serial communication interface 8.3 Serial peripheral interface (SPI)

SPI communications

LDR R1,[R0]
BIC R1,R1,#GPIO_MODE13+GPIO_MODE14
... +GPIO_MODE15
ORR R1,#GPIO_MODE13_1+GPIO_MODE14_1
... +GPIO_MODE15_1
STR R1,[R0]

LDR R0,=GPIOB_AFRH
LDR R1,[R0]
MOV32 R2,#GPIO_AFSEL12+GPIO_AFSEL13
... +GPIO_AFSEL14+GPIO_AFSEL15
BIC R1,R2
ORR R1,#GPIO_AFSEL12_0+GPIO_AFSEL12_2;
ORR R1,#GPIO_AFSEL13_0+GPIO_AFSEL13_2;
ORR R1,#GPIO_AFSEL14_0+GPIO_AFSEL14_2;

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 77 / 135
Serial communication interface 8.3 Serial peripheral interface (SPI)

SPI communications

ORR R1,#GPIO_AFSEL15_0+GPIO_AFSEL15_2;
STR R1,[R0]

BX LR
SPI2_Init
; Step 1. Enabled the clock to the SPI2
LDR R0,=RCC_APB1ENR1
LDR R1,[R0]
ORR R1,#RCC_APB1ENR1_SPI2EN
STR R1,[R0]

; Step 2. Configure the serial clock baud rate


; BR[2:0] = 111
LDR R0,=SPI2_CR1
LDR R1,[R0]
BIC R1,#SPI_CR1_BR

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 78 / 135
Serial communication interface 8.3 Serial peripheral interface (SPI)

SPI communications

ORR R1,#SPI_CR1_BR_0+SPI_CR1_BR_1+SPI_CR1_BR_2
STR R1,[R0]

; Step 3. Configure the CPOL and CPHA bits


LDR R0,=SPI2_CR1
LDR R1,[R0]
ORR R1,#SPI_CR1_CPOL+SPI_CR1_CPHA
STR R1,[R0]

; Step 4. Select simplex or half-duplex mode,


; RXONLY = 0
LDR R0,=SPI2_CR1
LDR R1,[R0]
BIC R1,#SPI_CR1_RXONLY
STR R1,[R0]

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 79 / 135
Serial communication interface 8.3 Serial peripheral interface (SPI)

SPI communications

; Step 5. Configure the LSBFIRST bit, MSB first


LDR R0,=SPI2_CR1
LDR R1,[R0]
BIC R1,#SPI_CR1_LSBFIRST
STR R1,[R0]

; Step 6. Configure the CRCL and CRCEN bits


; if CRC is needed
; Step 7. Configure SSM and SSI.
; Not required in TI and NSSP modes

; Step 8. Configure the MSTR bit, Master Mode


LDR R0,=SPI2_CR1
LDR R1,[R0]
ORR R1,#SPI_CR1_MSTR
STR R1,[R0]

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 80 / 135
Serial communication interface 8.3 Serial peripheral interface (SPI)

SPI communications

; Step 9
; SSM=0 -> Software Slave Management disable
LDR R0,=SPI2_CR1
LDR R1,[R0]
BIC R1,#SPI_CR1_SSM
STR R1,[R0]

; Step 10. Configure the DS[3:0] bits to select


; the data length for the transfer.
; DS[3:0] = 1111 -> 16 bit
LDR R0,=SPI2_CR2
LDR R1,[R0]
ORR R1,#SPI_CR2_DS
STR R1,[R0]

; Step 11. Configure SSOE

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 81 / 135
Serial communication interface 8.3 Serial peripheral interface (SPI)

SPI communications

LDR R0,=SPI2_CR2
LDR R1,[R0]
ORR R1,#SPI_CR2_SSOE
STR R1,[R0]

; Step 12. Set the FRF bit if the TI protocol


; is required
; Step 13. Set the NSSP bit if the NSS pulse
; mode between two data units is required

; Step 14. Enable SPI


LDR R0,=SPI2_CR1
LDR R1,[R0]
ORR R1,#SPI_CR1_SPE
STR R1,[R0]

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 82 / 135
Serial communication interface 8.3 Serial peripheral interface (SPI)

SPI communications

BX LR
SPI2_Transmit
LDR R0,=SPI2_SR
LDR R1,[R0]
ANDS R1,#SPI_SR_TXE
BEQ SPI2_Transmit ; Wait for TXE bit to set
SPI_TX_Ready
LDR R0,=SPI2_DR
STR R3,[R0]
SPI_TX_Transfer
LDR R0,=SPI2_SR
LDR R1,[R0]
ANDS R1,#SPI_SR_TXE
BEQ SPI_TX_Transfer ; Wait for buffer empty
SPI_Busy
LDR R0,=SPI2_SR

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 83 / 135
Serial communication interface 8.3 Serial peripheral interface (SPI)

SPI communications

LDR R1,[R0]
ANDS R1,#SPI_SR_BSY
BNE SPI_Busy ; Wait for Busy flag

LDR R0,=SPI2_DR
LDR R1,[R0]

BX LR

ALIGN
END

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 84 / 135
Serial communication interface 8.3 Serial peripheral interface (SPI)

SPI communications

 Example 8.4 (SPI MAX7219)


Consider again the circuit of the STM32G431RBT6 with IC MAX7219 and eight
seven-segment LEDs as shown in the figure. Write a program to display numbers
12345678 on the LEDs using SPI.
STM32G431RBT6
12 24
PA0 PB0
13 25
PA1 PB1
14 26
PA2 PB2
17 56
PA3 PB3
18 57
PA4 PB4
19 58
PA5 PB5
59
PB6
60
PB7
8 62 VCC
PC0 PB9
9 30
PC1 PB10
10 33
PC2 PB11
11 34 LAT
PC3 PB12
22 35 SCK 10K
PC4 PB13
23 36
PC5 PB14
38 37 DIN 18 DIGITS
PC6 PB15 ISET DIG0 - DIG7
39
PC7 U2
40
PC8 DIN 1
41 55 DIN
PC9 PD2
52 LAT 12
PC10 LOAD (CS)
53
PC11 SCK 13
54 CLK SEGMENTS
PC12 SEG A-G
2
PC13 SEG DP
9
GND
GND
MAX7219

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 85 / 135
Serial communication interface 8.3 Serial peripheral interface (SPI)

SPI communications

Solution:

Code 8.4

INCLUDE ../inc/stm32G431xx.s

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


THUMB
ENTRY
EXPORT __main
__main
BL GPIO_Init
BL SPI2_Init
Start
MOV R3,#0x0808
BL SPI2_Transmit

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 86 / 135
Serial communication interface 8.3 Serial peripheral interface (SPI)

SPI communications

MOV R3,#0x0707
BL SPI2_Transmit

MOV R3,#0x0606
BL SPI2_Transmit

MOV R3,#0x0505
BL SPI2_Transmit

MOV R3,#0x0404
BL SPI2_Transmit

MOV R3,#0x0303
BL SPI2_Transmit

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 87 / 135
Serial communication interface 8.3 Serial peripheral interface (SPI)

SPI communications

MOV R3,#0x0202
BL SPI2_Transmit

MOV R3,#0x0101
BL SPI2_Transmit
Loop
B Loop

GPIO_Init
...

BX LR
SPI2_Init
...

BX LR

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 88 / 135
Serial communication interface 8.3 Serial peripheral interface (SPI)

SPI communications

SPI2_Transmit
...
BX LR

ALIGN
END

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 89 / 135
Programming Microcontrollers in C Interface programming in C

GPIO programming

 Example 9.1 (Blinky)


Consider a circuit of the STM32G431RBT6 microcontroller and an LED as shown
in the figure. Write C codes to blink the LED.
STM32G431RBT6
12 24
PA0 PB0
13 25
PA1 PB1
14 26
PA2 PB2
17 56
PA3 PB3
18 57
PA4 PB4
19 58
PA5 PB5
20 59
PA6 PB6
21 60
PA7 PB7
R1 42
PA8
43 62
PA9 PB9
44 30
PA10 PB10
N1 45 33
PA11 PB11
46 34
PA12 PB12
49 35
PA13 PB13
50 36
VCC PA14 PB14
51 37
PA15 PB15

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 45 / 205
Programming Microcontrollers in C Interface programming in C

GPIO programming

Code 9.1 (Blinky)

#include "main.h"

void GPIO_Init(void);
void SystemClock_Init(void);

int main(void){
GPIO_Init();
for (;;) {
volatile unsigned int i;
GPIOA->ODR ˆ= 1<<5;
i = 5000000; // Delay
do (i--);
while (i != 0);
}

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 46 / 205
Programming Microcontrollers in C Interface programming in C

GPIO programming

void GPIO_Init(void){
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOAEN;
GPIOA->MODER &= ˜GPIO_MODER_MODE5;
GPIOA->MODER |= GPIO_MODER_MODE5_0;
GPIOA->ODR |= 1<<5;
}

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 47 / 205
Programming Microcontrollers in C Interface programming in C

GPIO programming

 Example 9.2
Consider a circuit with eight LEDs connected to eight output pins of the
STM32G431RBT6 as shown in the figure. Write C codes to turn the LEDs on
and off one after another.
U1 STM32G431RBT6
R30 12 8
PA0 PC0
R31 13 9
PA1 PC1
14 10
PA2 PC2
17 11
PA3 PC3
R32 18 22
PA4 PC4
R33 19 23
PA5 PC5
R34 20 38
PA6 PC6
R35 21 39
PA7 PC7
R36 42 40
PA8 PC8
R37 43 41
PA9 PC9
44 52
PA10 PC10
45 53
PA11 PC11
46 54
N0 N1 N2 N3 N4 N5 N6 N7 PA12 PC12
49 2
PA13 PC13
50
PA14
51
PA15
VCC

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 48 / 205
Programming Microcontrollers in C Interface programming in C

GPIO programming

Solution:

Code 9.2 (8 LEDs)

#include "main.h"

uint8_t Cnt=0;

void GPIO_Init(void);
void delay(volatile int i);

int main(void){
GPIO_Init();
for (;;) {
volatile unsigned int i,j;
for (Cnt=0; Cnt<8; Cnt++){
if (Cnt<2){
j=Cnt;
}else{
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 49 / 205
Programming Microcontrollers in C Interface programming in C

GPIO programming

j=Cnt+2;
}
GPIOA->ODR = (0xFFFF&˜(1<<j));
delay(5000000);
}
}
}

void GPIO_Init(void){
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOAEN;
delay(5);
GPIOA->MODER &= ˜(GPIO_MODER_MODE0 + GPIO_MODER_MODE1
...+GPIO_MODER_MODE4+GPIO_MODER_MODE5+GPIO_MODER_MODE6
...+GPIO_MODER_MODE7+GPIO_MODER_MODE8+GPIO_MODER_MODE9);
delay(5);
GPIOA->MODER |= (GPIO_MODER_MODE0_0+GPIO_MODER_MODE1_0
...+GPIO_MODER_MODE4_0+GPIO_MODER_MODE5_0+GPIO_MODER_MODE6_0

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 50 / 205
Programming Microcontrollers in C Interface programming in C

GPIO programming

...+GPIO_MODER_MODE7_0+GPIO_MODER_MODE8_0+GPIO_MODER_MODE9_0);
delay(5);
}

void delay(volatile int i){


while(i--) continue;
}

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 51 / 205
Programming Microcontrollers in C Interface programming in C

GPIO programming

 Example 9.4
Write C codes to control a stepper motor shown the following circuit using the
full-step method.
U1 STM32G431RBT6 ULN2003
12 8 A1 1 16 Pink (2) A1
PA0 PC0 IN1 OUT1
13 9 A2 2 15 Orange (4)
PA1 PC1 IN2 OUT2

N
14 10 B1 3 14 Yellow (3) A0
PA2 PC2 IN3 OUT3
17 11 B2 4 13 Blue (1)
PA3 PC3 IN4 OUT4

S
18 22 5 12 A2
PA4 PC4 IN5 OUT5
19 23 6 11
PA5 PC5 IN6 OUT6
20 38 7 10
PA6 PC6 IN7 OUT7
A2 21 39 8 9
PA7 PC7 GND COM B1 B0 B2
B1 42 40
PA8 PC8
B2 43 41
PA9 PC9
44 52 Red (2)
PA10 PC10 +VM
A1

A2

B1

B2
45 53
PA11 PC11
46 54
PA12 PC12
49 2
PA13 PC13
50
PA14
51
PA15
A1 24 N8 N5 N6 N7
PB0
VCC

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 54 / 205
Programming Microcontrollers in C Interface programming in C

GPIO programming

Solution:

Code 9.3
#include "main.h"

#define A1 1<<0 // Pink


#define A2 1<<7 // Orange
#define B1 1<<8 // Yellow
#define B2 1<<9 // Blue

void GPIO_Init(void);
void delay(volatile int i);

int main(void){

GPIO_Init();

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 55 / 205
Programming Microcontrollers in C Interface programming in C

GPIO programming

for (;;) {
GPIOA->ODR &= (A2)+(B1)+(B2);
GPIOA->ODR |= (A2)+(B1)+(B2);
delay(100000);

GPIOA->ODR &= (A1)+(A2)+(B2);


GPIOA->ODR |= (A1)+(A2)+(B2);
delay(100000);

GPIOA->ODR &= (A1)+(B1)+(B2);


GPIOA->ODR |= (A1)+(B1)+(B2);
delay(100000);

GPIOA->ODR &= (A1)+(A2)+(B1);


GPIOA->ODR |= (A1)+(A2)+(B1);
delay(100000);
}

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 56 / 205
Programming Microcontrollers in C Interface programming in C

GPIO programming

void GPIO_Init(void){
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOAEN;
delay(5);
GPIOA->MODER &= ˜(GPIO_MODER_MODE6+GPIO_MODER_MODE7
...+GPIO_MODER_MODE8+GPIO_MODER_MODE9);
delay(5);
GPIOA->MODER |= (GPIO_MODER_MODE6_0+GPIO_MODER_MODE7_0
...+GPIO_MODER_MODE8_0+GPIO_MODER_MODE9_0);
delay(5);
}

void delay(volatile int i){


while(i--) continue;
}

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 57 / 205
Programming Microcontrollers in C Interface programming in C

GPIO programming

 Example 9.5
Write C codes to control a stepper motor shown the following circuit using the
half-step method.
U1 STM32G431RBT6 ULN2003
12 8 A1 1 16 Pink (2) A1
PA0 PC0 IN1 OUT1
13 9 A2 2 15 Orange (4)
PA1 PC1 IN2 OUT2

N
14 10 B1 3 14 Yellow (3) A0
PA2 PC2 IN3 OUT3
17 11 B2 4 13 Blue (1)
PA3 PC3 IN4 OUT4

S
18 22 5 12 A2
PA4 PC4 IN5 OUT5
19 23 6 11
PA5 PC5 IN6 OUT6
20 38 7 10
PA6 PC6 IN7 OUT7
A2 21 39 8 9
PA7 PC7 GND COM B1 B0 B2
B1 42 40
PA8 PC8
B2 43 41
PA9 PC9
44 52 Red (2)
PA10 PC10 +VM
A1

A2

B1

B2
45 53
PA11 PC11
46 54
PA12 PC12
49 2
PA13 PC13
50
PA14
51
PA15
A1 24 N8 N5 N6 N7
PB0
VCC

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 58 / 205
Programming Microcontrollers in C Interface programming in C

GPIO programming

Solution:

Code 9.4
for (;;) {

GPIOA->ODR &= (A2)+(B1)+(B2);


GPIOA->ODR |= (A2)+(B1)+(B2);
delay(100000);

GPIOA->ODR &= (A2)+(B2);


GPIOA->ODR |= (A2)+(B2);
delay(100000);

GPIOA->ODR &= (A1)+(A2)+(B2);


GPIOA->ODR |= (A1)+(A2)+(B2);
delay(100000);

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 59 / 205
Programming Microcontrollers in C Interface programming in C

GPIO programming

GPIOA->ODR &= (A1)+(B2);


GPIOA->ODR |= (A1)+(B2);
delay(100000);

GPIOA->ODR &= (A1)+(B1)+(B2);


GPIOA->ODR |= (A1)+(B1)+(B2);
delay(100000);

GPIOA->ODR &= (A1)+(B1);


GPIOA->ODR |= (A1)+(B1);
delay(100000);

GPIOA->ODR &= (A1)+(A2)+(B1);


GPIOA->ODR |= (A1)+(A2)+(B1);
delay(100000);

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 60 / 205
Programming Microcontrollers in C Interface programming in C

GPIO programming

GPIOA->ODR &= (A2)+(B1);


GPIOA->ODR |= (A2)+(B1);
delay(100000);
}
}

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 61 / 205
Programming Microcontrollers in C 9.5 Interrupt programming in C

External Interrupts

 Example 9.10
Consider a circuit of the STM32G431RBT6 microcontroller and an LED as shown
in the figure. Write C codes to toggle the LED using external interrupt on pin PB6.
U1 STM32G431RBT6
12 24
PA0 PB0
13 25
PA1 PB1
14 26
PA2 PB2
R30 17 56
PA3 PB3
18 57
PA4 PB4
19 58
N0 PA5 PB5
59
PB6
60
PB7
8 62
VCC PC0 PB9 B0
9 30
PC1 PB10
10 33
PC2 PB11
11 34
PC3 PB12
22 35
PC4 PB13
23 36
PC5 PB14
37
PB15

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 76 / 205
Programming Microcontrollers in C 9.5 Interrupt programming in C

GPIO programming

Solution:

Code 9.5
#include "main.h"

#define EXTI9_5_IRQn_Pos EXTI9_5_IRQn


#define EXTI9_5_IRQn_Msk 0x1 << EXTI9_5_IRQn_Pos
#define EXTI9_5_IRQn_EN EXTI9_5_IRQn_Msk

void GPIO_Init(void);
void EXTI9_5_IRQHandler(void);
void delay(volatile int i);

int main(void){

GPIO_Init();

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 77 / 205
Programming Microcontrollers in C 9.5 Interrupt programming in C

GPIO programming

for (;;) {
}
}

void delay(volatile int i){


while(i--) continue;
}

void GPIO_Init(void){
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOAEN;
delay(5);
GPIOA->MODER &= ˜GPIO_MODER_MODE0;
delay(5);
GPIOA->MODER |= GPIO_MODER_MODE0_0;
delay(5);

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 78 / 205
Programming Microcontrollers in C 9.5 Interrupt programming in C

GPIO programming

RCC->AHB2ENR |= RCC_AHB2ENR_GPIOBEN;
delay(5);
GPIOB->MODER &= ˜GPIO_MODER_MODE6; // PB6 as an input
delay(5);
GPIOC->PUPDR &= ˜GPIO_PUPDR_PUPD6;
GPIOC->PUPDR |= GPIO_PUPDR_PUPD6_0; // PUPD6 = 10 -> Pull-up

RCC->APB2ENR |= RCC_APB2ENR_SYSCFGEN;
SYSCFG->EXTICR[1] = SYSCFG_EXTICR2_EXTI6_PB;

EXTI->IMR1 |= EXTI_IMR1_IM6;
EXTI->RTSR1 |= EXTI_RTSR1_RT6; //

EXTI->FTSR1 &= ˜EXTI_FTSR1_FT6;

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 79 / 205
Programming Microcontrollers in C 9.5 Interrupt programming in C

GPIO programming

NVIC->ISER[0] = EXTI9_5_IRQn_EN;
NVIC->ISPR[0] = EXTI9_5_IRQn_EN;
}

void EXTI9_5_IRQHandler(void){
if (((EXTI->PR1)&EXTI_PR1_PIF6) != RESET){
EXTI->PR1 |= EXTI_PR1_PIF6;
GPIOA->ODR ˆ= (1<<0);
}
}

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 80 / 205
Programming Microcontrollers in C 9.5 Interrupt programming in C

GPIO programming

Solution:

Code 9.6
#include "main.h"

#define DL_LED 100


#define SYST_RVR 1700000 // SysTick 10ms

volatile uint16_t delayCnt=0;

void GPIO_Init(void);
void SysTick_Init(void);
void SysTick_Handler(void);

int main(void){

GPIO_Init();

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 82 / 205
Programming Microcontrollers in C 9.5 Interrupt programming in C

GPIO programming

SysTick_Init();

for (;;) {
}
}

void delay(volatile int i){


while(i--) continue;
}

void GPIO_Init(void){
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOAEN;
delay(5);
GPIOA->MODER &= ˜GPIO_MODER_MODE5;
delay(5);
GPIOA->MODER |= GPIO_MODER_MODE5_0;

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 83 / 205
Programming Microcontrollers in C 9.5 Interrupt programming in C

GPIO programming

delay(5);
}

void SysTick_Init(void){
// Configure the SysTick to have interrupt in 10ms time base
SysTick->LOAD = SYST_RVR; // set reload register
SysTick->VAL = 0UL; // Load the SysTick Counter Value
SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk |
SysTick_CTRL_ENABLE_Msk |
SysTick_CTRL_TICKINT_Msk; // Enable the Systick
}

void SysTick_Handler(void){
delayCnt++;
if (delayCnt>100){
delayCnt=0;

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 84 / 205
Programming Microcontrollers in C 9.5 Interrupt programming in C

GPIO programming

GPIOA->ODR ˆ= (1<<5);
}
}

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 85 / 205
Programming Microcontrollers in C 9.6 Timing generation and measurement programming in C

Timer programming

Code 9.7
#include "main.h"

#define TIM3_PSC_VALUE 17000-1


#define TIM3_PERIOD_VALUE 10000-1

void GPIO_Init(void);
void Timer3_Init(void);
void TIM3_IRQHandler(void);
void delay(volatile int i);

int main(void){

GPIO_Init();
Timer3_Init();

for (;;) {

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 89 / 205
Programming Microcontrollers in C 9.6 Timing generation and measurement programming in C

Timer programming

}
}

void delay(volatile int i){


while(i--) continue;
}

void GPIO_Init(void){
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOAEN;
delay(5);
GPIOA->MODER &= ˜GPIO_MODER_MODE5;
delay(5);
GPIOA->MODER |= GPIO_MODER_MODE5_0;
delay(5);
}

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 90 / 205
Programming Microcontrollers in C 9.6 Timing generation and measurement programming in C

Timer programming

void Timer3_Init(void){
RCC->APB1ENR1 |= RCC_APB1ENR1_TIM3EN;
TIM3->PSC = TIM3_PSC_VALUE;
TIM3->ARR = TIM3_PERIOD_VALUE;
TIM3->CNT = 0;
TIM3->DIER |= TIM_DIER_UIE;
TIM3->CR1 |= TIM_CR1_CEN;

// Timer 3 at NVIC (Interrupt Set-enable Register)


NVIC->ISER[0] = (1<<TIM3_IRQn);
// Timer 3 Interrupt Set-pending Registers
NVIC->ISPR[0] = (1<<TIM3_IRQn);
}

void TIM3_IRQHandler(void){
TIM3->SR &= ˜TIM_SR_UIF;

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 91 / 205
Programming Microcontrollers in C 9.6 Timing generation and measurement programming in C

Timer programming

GPIOA->ODR ˆ= (1<<5);
}

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 92 / 205
Programming Microcontrollers in C 9.6 Timing generation and measurement programming in C

Timer programming

Code 9.8
#include "main.h"

#define TIM1_PSC_VALUE 1700-1


#define TIM1_PERIOD_VALUE 999-1
#define PWM_DUTY_VALUE 500

void GPIO_Init(void);
void Timer1_Init(void);
void delay(volatile int i);

int main(void){

GPIO_Init();
Timer1_Init();

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 95 / 205
Programming Microcontrollers in C 9.6 Timing generation and measurement programming in C

Timer programming

for (;;) {
}
}

void delay(volatile int i){


while(i--) continue;
}

void GPIO_Init(void){
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOAEN;
delay(5);
GPIOA->MODER &= ˜(GPIO_MODER_MODE8+GPIO_MODER_MODE9);
delay(5);
// PA8, PA9 in alternate function mode
GPIOA->MODER |= (GPIO_MODER_MODE8_1+GPIO_MODER_MODE9_1);

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 96 / 205
Programming Microcontrollers in C 9.6 Timing generation and measurement programming in C

Timer programming

delay(5);

// PA8 for TIM1 CH1, PA8 for TIM1 CH2 (AF6)


GPIOA->AFR[1] &= ˜(GPIO_AFRH_AFSEL8+GPIO_AFRH_AFSEL9);
GPIOA->AFR[1] |= (GPIO_AFRH_AFSEL8_1+GPIO_AFRH_AFSEL8_2
+GPIO_AFRH_AFSEL9_1+GPIO_AFRH_AFSEL9_2);
}

void Timer1_Init(void){
RCC->APB2ENR |= RCC_APB2ENR_TIM1EN;
TIM1->PSC = TIM1_PSC_VALUE;
TIM1->ARR = TIM1_PERIOD_VALUE;
TIM1->CNT = 0;

// Select PWM Mode 1 output on channel 1


TIM1->CCMR1 &= ˜(TIM_CCMR1_OC1M+TIM_CCMR1_OC2M);

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 97 / 205
Programming Microcontrollers in C 9.6 Timing generation and measurement programming in C

Timer programming

TIM1->CCMR1 |= (TIM_CCMR1_OC1M_1+TIM_CCMR1_OC2M_1
+TIM_CCMR1_OC1M_2+TIM_CCMR1_OC2M_2);

// Main output enable (MOE)


TIM1->BDTR |= TIM_BDTR_MOE;

// Enable PWM CH1 and CH2


TIM1->CCER |= (TIM_CCER_CC1E+TIM_CCER_CC2E);

// Initial duty cycle PWM_DUTY_VALUE


TIM1->CCR1 = PWM_DUTY_VALUE;
TIM1->CCR2 = PWM_DUTY_VALUE;

// Enable TIM1
TIM1->CR1 |= TIM_CR1_CEN;
}

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 98 / 205
Programming Microcontrollers in C 9.6 Timing generation and measurement programming in C

Timer programming

 Example 9.14
Write a C program for Timer 1 to generate a PWM signal on pins PA8 and PA9 to
control the speed of two motors with IC DRV8833. The circuit includes 4 buttons.
Button RUN, DEC, INC, and STOP are to make the motor run, decrease, increase
the motor speed, and stop the motor, respectively.

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 99 / 205
Programming Microcontrollers in C 9.6 Timing generation and measurement programming in C

Timer programming
STM32G431RBT6
12 8
PA0 PC0
13 9
PA1 PC1 B1
14 10 RUN
PA2 PC2
17 11
PA3 PC3 B2
18 22 DEC
PA4 PC4
19 23
PA5 PC5 B3
20 38 INC
PA6 PC6 VCC
IN1 21 39 C2
PA7 PC7 B4
IN2 42 40 STOP
PA8 PC8
IN3 43 41
PA9 PC9
IN4 44 52

14
12
PA10 PC10 DRV8833P
53
PC11
24 54 1
PC12

VINT
VM
PB0 2 nSLEEP
25 PC13 11
PB1 VCP
RUN 26 C1 3
PB2 AISEN
56 6 8
PB3 BISEN nFAULT M1
DEC 57
PB4
INC 58
PB5 55 IN1 16 2 M1P

GND(PPAD)
59 PD2 AIN1 AOUT1
PB6 IN2 15 4 M1N
STOP 60 AIN2 AOUT2
PB7 IN3 9 7 M2P
62 BIN1 BOUT1
PB9

GND
62 IN4 10 5 M2N
BIN2 BOUT2 M1
PB9
30
PB10
33
17
13
PB11
34
PB12
35
PB13
36
PB14
37
PB15

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 100 / 205
Programming Microcontrollers in C 9.6 Timing generation and measurement programming in C

Timer programming

Solution:

Code 9.9
#include "main.h"

#define MAX7219_CS 1<<12


#define MAX7219_CLK 1<<13
#define MAX7219_DIN 1<<15

#define TIM1_PSC_VALUE 1700-1


#define TIM1_PERIOD_VALUE 999-1
#define PWM_DUTY_VALUE 500

#define INCREMENT 5
#define DUTYCYCLEMAX 999-INCREMENT

#define BCCOEFF 500

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 101 / 205
Programming Microcontrollers in C 9.6 Timing generation and measurement programming in C

Timer programming

#define BUTTON1 1<<2


#define BUTTON2 1<<4
#define BUTTON3 1<<5
#define BUTTON4 1<<7

uint8_t digit, LED;


uint32_t tempCycle;
uint32_t divisor=10000000;

uint32_t key;
uint16_t BouncingCycles=0;

uint16_t dutyCycle=0;

uint8_t OpMode=0;

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 102 / 205
Programming Microcontrollers in C 9.6 Timing generation and measurement programming in C

Timer programming

void GPIO_Init(void);
void MAX7219_Init(void);
void Timer1_Init(void);
void delay(volatile int i);
void sendWordToMax7219(uint16_t dataSend);
void MAX7219Display(void);

int main(void){

GPIO_Init();
Timer1_Init();
MAX7219_Init();

dutyCycle=150;
MAX7219Display();

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 103 / 205
Programming Microcontrollers in C 9.6 Timing generation and measurement programming in C

Timer programming

for (;;) {
key=GPIOB->IDR;

switch (key&((BUTTON1)+(BUTTON2)+(BUTTON3)+(BUTTON4))){

case 0xB0:
if (OpMode==0){
BouncingCycles++;
if (BouncingCycles>=BCCOEFF){
OpMode=1;
TIM1->CCR1 = dutyCycle;
TIM1->CCR2 = dutyCycle;
MAX7219Display();
BouncingCycles=0;
}
}

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 104 / 205
Programming Microcontrollers in C 9.6 Timing generation and measurement programming in C

Timer programming

break;

case 0xA4:
if (OpMode==1){
BouncingCycles++;
if (BouncingCycles>=BCCOEFF){
if (dutyCycle>=INCREMENT){
dutyCycle -= INCREMENT;
TIM1->CCR1 = dutyCycle;
TIM1->CCR2 = dutyCycle;
MAX7219Display();
BouncingCycles=0;
}
}
}
break;

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 105 / 205
Programming Microcontrollers in C 9.6 Timing generation and measurement programming in C

Timer programming

case 0x94:
if (OpMode==1){
BouncingCycles++;
if (BouncingCycles>=BCCOEFF){
if (dutyCycle<=DUTYCYCLEMAX){
dutyCycle += INCREMENT;
TIM1->CCR1 = dutyCycle;
TIM1->CCR2 = dutyCycle;
MAX7219Display();
BouncingCycles=0;
}
}
}
break;

case 0x34:

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 106 / 205
Programming Microcontrollers in C 9.6 Timing generation and measurement programming in C

Timer programming

if (OpMode==1){
BouncingCycles++;
if (BouncingCycles>=BCCOEFF){
if (dutyCycle>=INCREMENT){
OpMode=0;
TIM1->CCR1 = 0;
TIM1->CCR2 = 0;
BouncingCycles=0;
}
}
}
break;

default:
BouncingCycles=0;
}

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 107 / 205
Programming Microcontrollers in C 9.6 Timing generation and measurement programming in C

Timer programming

delay(5000);
}
}

void delay(volatile int i){


while(i--) continue;
}

void GPIO_Init(void){
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOAEN;
delay(5);
GPIOA->MODER &= ˜(GPIO_MODER_MODE8+GPIO_MODER_MODE9);
delay(5);

// PA7, PA10 as outputs


GPIOA->MODER &= ˜(GPIO_MODER_MODE7+GPIO_MODER_MODE10);

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 108 / 205
Programming Microcontrollers in C 9.6 Timing generation and measurement programming in C

Timer programming

GPIOA->MODER |= (GPIO_MODER_MODE7_0+GPIO_MODER_MODE10_0);

// PA8, PA9 in alternate function mode


GPIOA->MODER |= (GPIO_MODER_MODE8_1+GPIO_MODER_MODE9_1);
delay(5);

// PA8 for TIM1 CH1, PA8 for TIM1 CH2 (AF6)


GPIOA->AFR[1] &= ˜(GPIO_AFRH_AFSEL8+GPIO_AFRH_AFSEL9);
GPIOA->AFR[1] |= (GPIO_AFRH_AFSEL8_1 + GPIO_AFRH_AFSEL8_2
...+GPIO_AFRH_AFSEL9_1+GPIO_AFRH_AFSEL9_2);
// Enable the clock to the GPIOB Port
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOBEN;

// PB12, PB13, and PB15 as outputs


GPIOB->MODER &= ˜(GPIO_MODER_MODE12+GPIO_MODER_MODE13
...+GPIO_MODER_MODE15);

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 109 / 205
Programming Microcontrollers in C 9.6 Timing generation and measurement programming in C

Timer programming

GPIOB->MODER |= (GPIO_MODER_MODE12_0+GPIO_MODER_MODE13_0
...+GPIO_MODER_MODE15_0);

// Enable input mode for PB2, PB4, PB5, PB7


GPIOB->MODER &= ˜(GPIO_MODER_MODE2+GPIO_MODER_MODE4
...+GPIO_MODER_MODE5+GPIO_MODER_MODE7);

// Enable pull-up resistors


GPIOB->PUPDR &= ˜(GPIO_PUPDR_PUPD2+GPIO_PUPDR_PUPD4
... +GPIO_PUPDR_PUPD5+GPIO_PUPDR_PUPD7);
GPIOB->PUPDR |= (GPIO_PUPDR_PUPD2_0+GPIO_PUPDR_PUPD4_0
... +GPIO_PUPDR_PUPD5_0+GPIO_PUPDR_PUPD7_0);
}

void Timer1_Init(void){
RCC->APB2ENR |= RCC_APB2ENR_TIM1EN;

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 110 / 205
Programming Microcontrollers in C 9.6 Timing generation and measurement programming in C

Timer programming

TIM1->PSC = TIM1_PSC_VALUE;
TIM1->ARR = TIM1_PERIOD_VALUE;
TIM1->CNT = 0;

// Select PWM Mode 1 output on channel 1


TIM1->CCMR1 &= ˜(TIM_CCMR1_OC1M+TIM_CCMR1_OC2M);
TIM1->CCMR1 |= (TIM_CCMR1_OC1M_1+TIM_CCMR1_OC2M_1
...+TIM_CCMR1_OC1M_2+TIM_CCMR1_OC2M_2);

// Main output enable (MOE)


TIM1->BDTR |= TIM_BDTR_MOE;

// Enable PWM CH1 and CH2


TIM1->CCER |= (TIM_CCER_CC1E+TIM_CCER_CC2E);

// Initial duty cycle PWM_DUTY_VALUE

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 111 / 205
Programming Microcontrollers in C 9.6 Timing generation and measurement programming in C

Timer programming

TIM1->CCR1 = 0;
TIM1->CCR2 = 0;

// Enable TIM1
TIM1->CR1 |= TIM_CR1_CEN;
}

void MAX7219Display(void){
tempCycle = dutyCycle;
divisor=10000000;
for (LED=0; LED<8; LED++){
digit=tempCycle/divisor;
sendWordToMax7219(((8-LED)<<8)|digit);
tempCycle %= divisor;
divisor /= 10;
}

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 112 / 205
Programming Microcontrollers in C 9.6 Timing generation and measurement programming in C

Timer programming

void sendWordToMax7219(uint16_t dataSend){


...
}

void MAX7219_Init(void){
...
}

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 113 / 205
Programming Microcontrollers in C 9.6 Timing generation and measurement programming in C

Timer programming

 Example 9.15
Consider again the circuit of a line-following robot below. Write a C program for
Timer 1 to control the speed of two motors with IC DRV8833 and the two line
sensors activated at a high level. The robot is allowed to run if the button RUN is
pressed. The robot will stop running when the button STOP is pressed.

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 114 / 205
Programming Microcontrollers in C 9.6 Timing generation and measurement programming in C

Timer programming
STM32G431RBT6
12 8 SEN1
PA0 PC0
13 9
PA1 PC1 B1
14 10 RUN
PA2 PC2
17 11
PA3 PC3 Line sensor 1
18 22 B2
PA4 PC4 STOP VCC
19 23
PA5 PC5
20 38 SEN2
PA6 PC6 VCC
IN1 21 39 C2
PA7 PC7
IN2 42 40
PA8 PC8
IN3 43 41
PA9 PC9
IN4 44 52 Line sensor 2

14
12
PA10 PC10 DRV8833P
53 VCC
PC11
51 54 1
PC12

VINT
VM
PA15 2 nSLEEP
PC13 11
24 VCP
PB0 C1 3
25 AISEN
PB1 6 8
RUN 26 BISEN nFAULT M1
PB2
56
PB3
57 55 IN1 16 2 M1P

GND(PPAD)
PB4 PD2 AIN1 AOUT1
58 IN2 15 4 M1N
PB5 AIN2 AOUT2
59 IN3 9 7 M2P
PB6 BIN1 BOUT1

GND
STOP 60 IN4 10 5 M2N
PB7 BIN2 BOUT2 M1
62
PB9
62
PB9

17
13
SEN1 30
PB10
SEN2 33
PB11
LAT 34
PB12
SCK 35
PB13
36
PB14
DIN 37
PB15

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 115 / 205
Programming Microcontrollers in C 9.6 Timing generation and measurement programming in C

Timer programming

Solution:

Code 9.10
#include "main.h"

#define MAX7219_CS 1<<12


#define MAX7219_CLK 1<<13
#define MAX7219_DIN 1<<15

#define TIM1_PSC_VALUE 1700-1


#define TIM1_PERIOD_VALUE 999-1
#define PWM_DUTY_VALUE 500

#define BUT_RUN 1<<2


#define SENSOR_L 1<<10
#define SENSOR_R 1<<11
#define BUT_STOP 1<<7

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 116 / 205
Programming Microcontrollers in C 9.6 Timing generation and measurement programming in C

Timer programming

#define STRAIGHT 1
#define STOP 3

#define BCCOEFF 500

uint8_t digit, LED;


uint32_t tempCycle;
uint32_t divisor;

uint32_t key;
uint16_t BouncingCycles=0;
uint16_t dutyCycleL=0, dutyCycleR=0;
uint8_t OpMode=0;

void GPIO_Init(void);
void MAX7219_Init(void);

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 117 / 205
Programming Microcontrollers in C 9.6 Timing generation and measurement programming in C

Timer programming

void Timer1_Init(void);
void delay(volatile int i);
void sendWordToMax7219(uint16_t dataSend);
void displayDutyCycle(void);

int main(void){

GPIO_Init();
MAX7219_Init();
Timer1_Init();

dutyCycleL=0;
dutyCycleR=0;
displayDutyCycle();

for (;;) {

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 118 / 205
Programming Microcontrollers in C 9.6 Timing generation and measurement programming in C

Timer programming

key=GPIOB->IDR;

switch (key&((BUT_RUN)+(SENSOR_L)+(SENSOR_R)+(BUT_STOP))){

case 0xC80:
if (OpMode!=STRAIGHT){
BouncingCycles++;
if (BouncingCycles>=BCCOEFF){
OpMode=STRAIGHT;
dutyCycleL=500;
dutyCycleR=500;
TIM1->CCR1 = dutyCycleL;
TIM1->CCR2 = dutyCycleR;
BouncingCycles=0;
}
}

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 119 / 205
Programming Microcontrollers in C 9.6 Timing generation and measurement programming in C

Timer programming

break;

case 0x484:
if (OpMode!=STOP){
BouncingCycles++;
if (BouncingCycles>=BCCOEFF){
dutyCycleL=100;
TIM1->CCR1 = dutyCycleL;
BouncingCycles=0;
}
}
break;

case 0x884:
if (OpMode!=STOP){
BouncingCycles++;

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 120 / 205
Programming Microcontrollers in C 9.6 Timing generation and measurement programming in C

Timer programming

if (BouncingCycles>=BCCOEFF){
dutyCycleR=100;
TIM1->CCR2 = dutyCycleR;
BouncingCycles=0;
}
}
break;

case 0xC04:
if (OpMode!=STOP){
BouncingCycles++;
if (BouncingCycles>=BCCOEFF){
OpMode=STOP;
dutyCycleL=0;
dutyCycleR=0;
TIM1->CCR1 = dutyCycleL;

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 121 / 205
Programming Microcontrollers in C 9.6 Timing generation and measurement programming in C

Timer programming

TIM1->CCR2 = dutyCycleR;
BouncingCycles=0;
}
}
break;

default:
BouncingCycles=0;
if (OpMode==STRAIGHT){
dutyCycleL=500;
dutyCycleR=500;
TIM1->CCR1 = dutyCycleL;
TIM1->CCR2 = dutyCycleR;
}
}
displayDutyCycle();

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 122 / 205
Programming Microcontrollers in C 9.6 Timing generation and measurement programming in C

Timer programming

delay(5000);
}
}

void delay(volatile int i){


while(i--) continue;
}

void GPIO_Init(void){
...
}

void Timer1_Init(void){
...
}

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 123 / 205
Programming Microcontrollers in C 9.6 Timing generation and measurement programming in C

Timer programming

void displayDutyCycle(void){
tempCycle = dutyCycleL;
divisor=100;
for (LED=0; LED<3; LED++){
digit=tempCycle/divisor;
sendWordToMax7219(((8-LED)<<8)|digit);
tempCycle %= divisor;
divisor /= 10;
}
tempCycle = dutyCycleR;
divisor=100;
for (LED=5; LED<8; LED++){
digit=tempCycle/divisor;
sendWordToMax7219(((8-LED)<<8)|digit);
tempCycle %= divisor;
divisor /= 10;

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 124 / 205
Programming Microcontrollers in C 9.6 Timing generation and measurement programming in C

Timer programming

}
}

void sendWordToMax7219(uint16_t dataSend){


...
}

void MAX7219_Init(void){
...
}

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 125 / 205
Programming Microcontrollers in C 9.6 Timing generation and measurement programming in C

Timer programming

 Example 9.16
Consider again the circuit of a three-phase AC to DC converter using thyristors for
the velocity regulation of a DC motor as shown in the figure. Assume that the
supplied clock for Timer 1 is 170 MHz. Write a program to control firing angles of
the thyristors.

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 126 / 205
Programming Microcontrollers in C 9.6 Timing generation and measurement programming in C

Timer programming

Solution:

Code 9.11
#include "main.h"

#define TIM1_PSC_VALUE 169


#define TIM1_PERIOD_VALUE 20000-1

#define TIM2_PSC_VALUE 169


#define TIM2_PERIOD_VALUE 750-1

#define PWM_CH1 1<<8


#define PWM_CH2 1<<7
#define PWM_CH3 1<<9
#define PWM_CH4 1<<0
#define PWM_CH5 1<<10
#define PWM_CH6 1<<1

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 131 / 205
Programming Microcontrollers in C 9.6 Timing generation and measurement programming in C

Timer programming

uint16_t halfCycleCH1=0,halfCycleCH2=0,halfCycleCH3=0;
uint8_t secondHalfCH1=0, secondHalfCH2=0, secondHalfCH3=0;
uint8_t intCHx=0;

void GPIO_Init(void);
void Timer1_Init(void);
void Timer2_Init(void);
void TIM1_CC_IRQHandler(void);
void TIM2_IRQHandler(void);
void delay(volatile int i);

int main(void){
GPIO_Init();
Timer1_Init();
Timer2_Init();
}

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 132 / 205
Programming Microcontrollers in C 9.6 Timing generation and measurement programming in C

Timer programming

void delay(volatile int i){


while(i--) continue;
}

void GPIO_Init(void){
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOAEN;
delay(5);

// PA7, PA8, PA9, and PA10 as outputs


GPIOA->MODER &= ˜(GPIO_MODER_MODE7+GPIO_MODER_MODE8
+GPIO_MODER_MODE9+GPIO_MODER_MODE10);
GPIOA->MODER |= (GPIO_MODER_MODE7_0+GPIO_MODER_MODE8_0
+GPIO_MODER_MODE9_0+GPIO_MODER_MODE10_0);

RCC->AHB2ENR |= RCC_AHB2ENR_GPIOBEN;
delay(5);

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 133 / 205
Programming Microcontrollers in C 9.6 Timing generation and measurement programming in C

Timer programming

// PB0, PB1 as outputs


GPIOB->MODER &= ˜(GPIO_MODER_MODE0+GPIO_MODER_MODE1);
GPIOB->MODER |= (GPIO_MODER_MODE0_0+GPIO_MODER_MODE1_0);
}

void Timer1_Init(void){
RCC->APB2ENR |= RCC_APB2ENR_TIM1EN;
TIM1->PSC = TIM1_PSC_VALUE;
TIM1->ARR = TIM1_PERIOD_VALUE;
TIM1->CNT = 0;

// Select PWM Mode 2 output on channels 1 and 2


TIM1->CCMR1 &= ˜(TIM_CCMR1_OC1M+TIM_CCMR1_OC2M);
TIM1->CCMR1 |= (TIM_CCMR1_OC1M_0+TIM_CCMR1_OC1M_1+TIM_CCMR1_OC1M_2
+TIM_CCMR1_OC2M_0+TIM_CCMR1_OC2M_1+TIM_CCMR1_OC2M_2);

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 134 / 205
Programming Microcontrollers in C 9.6 Timing generation and measurement programming in C

Timer programming

// Select PWM Mode 2 output on channel 3


TIM1->CCMR2 &= ˜TIM_CCMR2_OC3M;
TIM1->CCMR2 |= (TIM_CCMR2_OC3M_0+TIM_CCMR2_OC3M_1+TIM_CCMR2_OC3M_2);

// Enable PWM CH1, CH2, and CH3


TIM1->CCER |= (TIM_CCER_CC1E+TIM_CCER_CC2E+TIM_CCER_CC3E
+TIM_CCER_CC1NE+TIM_CCER_CC2NE+TIM_CCER_CC3NE);

// Main output enable (MOE)


TIM1->BDTR |= TIM_BDTR_MOE;

// Initial duty cycle PWM_DUTY_VALUE


TIM1->CCR1 = 1667;
TIM1->CCR2 = 5000; // 1667+3333
TIM1->CCR3 = 8333; // 5000+3333

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 135 / 205
Programming Microcontrollers in C 9.6 Timing generation and measurement programming in C

Timer programming

// Enable TIM1 interrupt


TIM1->DIER |= (TIM_DIER_UIE+TIM_DIER_CC1IE+TIM_DIER_CC2IE
+TIM_DIER_CC3IE);

// Timer 1 at NVIC (Interrupt Set-enable Register)


NVIC->ISER[0] = (1<<TIM1_CC_IRQn);
// Timer 1 Interrupt Set-pending Registers
NVIC->ISPR[0] = (1<<TIM1_CC_IRQn);

// Enable TIM1
TIM1->CR1 |= TIM_CR1_CEN;
}

void Timer2_Init(void){
RCC->APB1ENR1 |= RCC_APB1ENR1_TIM2EN;

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 136 / 205
Programming Microcontrollers in C 9.6 Timing generation and measurement programming in C

Timer programming

TIM2->PSC = TIM2_PSC_VALUE;
TIM2->ARR = TIM2_PERIOD_VALUE;
TIM2->CNT = 0;

// Enable TIM2 interrupt


TIM2->DIER |= TIM_DIER_UIE;
// Enable TIM2
TIM2->CR1 |= TIM_CR1_CEN;

// Timer 2 at NVIC (Interrupt Set-enable Register)


NVIC->ISER[0] = (1<<TIM2_IRQn);
// Timer 2 Interrupt Set-pending Registers
NVIC->ISPR[0] = (1<<TIM2_IRQn);
}
void TIM1_CC_IRQHandler(void){
if (((TIM1->SR)&(TIM_SR_CC1IF))!=0){
TIM1->SR &=˜TIM_SR_CC1IF;

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 137 / 205
Programming Microcontrollers in C 9.6 Timing generation and measurement programming in C

Timer programming

intCHx=1;
}

if (((TIM1->SR)&(TIM_SR_CC2IF))!=0){
TIM1->SR &=˜TIM_SR_CC2IF;
intCHx=2;
}

if (((TIM1->SR)&(TIM_SR_CC3IF))!=0){
TIM1->SR &=˜TIM_SR_CC3IF;
intCHx=3;
}
TIM2->CNT = 0;

switch (intCHx){

case 1:

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 138 / 205
Programming Microcontrollers in C 9.6 Timing generation and measurement programming in C

Timer programming

if (secondHalfCH1==0){
GPIOA->ODR ˆ= (PWM_CH1);
GPIOB->ODR ˆ= (PWM_CH6);
TIM1->CCR1 = 11666; // Change the duty cycle for CH1
secondHalfCH1=1;
}else{
GPIOA->ODR ˆ= (PWM_CH3);
GPIOB->ODR ˆ= (PWM_CH4);
TIM1->CCR1 = 1667; // Change the duty cycle for CH1
secondHalfCH1=0;
}
break;

case 2:
if (secondHalfCH2==0){
GPIOA->ODR ˆ= ((PWM_CH2)+(PWM_CH1));

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 139 / 205
Programming Microcontrollers in C 9.6 Timing generation and measurement programming in C

Timer programming

TIM1->CCR2 = 14999; // Change the duty cycle for CH2


secondHalfCH2=1;
}else{
GPIOA->ODR ˆ= (PWM_CH5);
GPIOB->ODR ˆ= (PWM_CH4);
TIM1->CCR2 = 5000; // Change the duty cycle for CH2
secondHalfCH2=0;
}
break;

case 3:
if (secondHalfCH3==0){
GPIOA->ODR ˆ= ((PWM_CH3)+(PWM_CH2));
TIM1->CCR3 = 18332; // Change the duty cycle for CH3
secondHalfCH3=1;
}else{

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 140 / 205
Programming Microcontrollers in C 9.6 Timing generation and measurement programming in C

Timer programming

GPIOA->ODR ˆ= (PWM_CH5);
GPIOB->ODR ˆ= (PWM_CH6);
TIM1->CCR3 = 8333; // Change the duty cycle for CH3
secondHalfCH3=0;
}
break;
}
}

void TIM2_IRQHandler(void){
TIM2->SR &= ˜TIM_SR_UIF;
if (intCHx!=0){
GPIOA->ODR &= ˜((PWM_CH1)+(PWM_CH2)+(PWM_CH3)+(PWM_CH5));
GPIOB->ODR &= ˜((PWM_CH4)+(PWM_CH6));
}
}

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 141 / 205
Programming Microcontrollers in C 9.6 Timing generation and measurement programming in C

Timer programming

 Example 9.17
Write a C program to generate center-aligned variable frequency PWM signal 4KHz
output on multiple complementary channels (CH1, CH1N, CH2N, CH2N, CH3, and
CH3N) as shown in the figure.

CH1

CH1N

CH2

CH2N

CH3

CH3N

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 142 / 205
Programming Microcontrollers in C 9.6 Timing generation and measurement programming in C

Timer programming

Solution:

Code 9.12
#include "main.h"

#define TIM1_PSC_VALUE 16
#define TIM1_PERIOD_VALUE 2125

void GPIO_Init(void);
void Timer1_Init(void);
void delay(volatile int i);

int main(void){
GPIO_Init();
Timer1_Init();
}

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 144 / 205
Programming Microcontrollers in C 9.6 Timing generation and measurement programming in C

Timer programming

void delay(volatile int i){


while(i--) continue;
}

void GPIO_Init(void){
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOAEN;
delay(5);

// PA7, PA8, PA9, PA10 in alternate function mode


GPIOA->MODER &= ˜(GPIO_MODER_MODE7+GPIO_MODER_MODE8
+GPIO_MODER_MODE9+GPIO_MODER_MODE10);
GPIOA->MODER |= (GPIO_MODER_MODE7_1+GPIO_MODER_MODE8_1
+GPIO_MODER_MODE9_1+GPIO_MODER_MODE10_1);

// PA7 for TIM1 (AF6)


GPIOA->AFR[0] &= ˜GPIO_AFRL_AFSEL7;

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 145 / 205
Programming Microcontrollers in C 9.6 Timing generation and measurement programming in C

Timer programming

GPIOA->AFR[0] |= (GPIO_AFRL_AFSEL7_1+GPIO_AFRL_AFSEL7_2);

// PA8, PA9, and PA10 for TIM1 (AF6)


GPIOA->AFR[1] &= ˜(GPIO_AFRH_AFSEL8+GPIO_AFRH_AFSEL9+GPIO_AFRH_AFSEL10
GPIOA->AFR[1] |= (GPIO_AFRH_AFSEL8_1+GPIO_AFRH_AFSEL9_1+GPIO_AFRH_AFSE
+GPIO_AFRH_AFSEL8_2+GPIO_AFRH_AFSEL9_2+GPIO_AFRH_AFSE

RCC->AHB2ENR |= RCC_AHB2ENR_GPIOBEN;
delay(5);

GPIOB->MODER &= ˜(GPIO_MODER_MODE0+GPIO_MODER_MODE1);


GPIOB->MODER |= (GPIO_MODER_MODE0_1+GPIO_MODER_MODE1_1);

// PB0, PB1 for TIM1 (AF6)


GPIOB->AFR[0] &= ˜(GPIO_AFRL_AFSEL0+GPIO_AFRL_AFSEL1);
GPIOB->AFR[0] |= (GPIO_AFRL_AFSEL0_1+GPIO_AFRL_AFSEL1_1

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 146 / 205
Programming Microcontrollers in C 9.6 Timing generation and measurement programming in C

Timer programming

+GPIO_AFRL_AFSEL0_2+GPIO_AFRL_AFSEL1_2);
}

void Timer1_Init(void){
RCC->APB2ENR |= RCC_APB2ENR_TIM1EN;

//; Choose Center-aligned mode 1 for TIM1


TIM1->CR1 &= ˜TIM_CR1_CMS;
TIM1->CR1 |= TIM_CR1_CMS_0;

TIM1->PSC = TIM1_PSC_VALUE;
TIM1->ARR = TIM1_PERIOD_VALUE;
TIM1->CNT = 0;

// Select PWM Mode 1 output on channels 1 and 2


TIM1->CCMR1 &= ˜(TIM_CCMR1_OC1M+TIM_CCMR1_OC2M);

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 147 / 205
Programming Microcontrollers in C 9.6 Timing generation and measurement programming in C

Timer programming

TIM1->CCMR1 |= (TIM_CCMR1_OC1M_1+TIM_CCMR1_OC1M_2
+TIM_CCMR1_OC2M_1+TIM_CCMR1_OC2M_2);

// Select PWM Mode 1 output on channel 3


TIM1->CCMR2 &= ˜TIM_CCMR2_OC3M;
TIM1->CCMR2 |= (TIM_CCMR2_OC3M_1+TIM_CCMR2_OC3M_2);

// Enable PWM CH1, CH2, and CH3


TIM1->CCER |= (TIM_CCER_CC1E+TIM_CCER_CC2E+TIM_CCER_CC3E
+TIM_CCER_CC1NE+TIM_CCER_CC2NE+TIM_CCER_CC3NE);

// Main output enable (MOE)


TIM1->BDTR |= TIM_BDTR_MOE;

// Initial duty cycle PWM_DUTY_VALUE


TIM1->CCR1 = 500;

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 148 / 205
Programming Microcontrollers in C 9.6 Timing generation and measurement programming in C

Timer programming

TIM1->CCR2 = 1000;
TIM1->CCR3 = 1500;

// Enable TIM1
TIM1->CR1 |= TIM_CR1_CEN;
}

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 149 / 205
Programming Microcontrollers in C 9.6 Timing generation and measurement programming in C

Timer programming

 Example 9.18
Write a C program to interface with an encoder using Timer 2 and display counter
value on LEDs driven by MAX7219 IC.

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 150 / 205
Programming Microcontrollers in C 9.6 Timing generation and measurement programming in C

Timer programming

Solution:

Code 9.13
#include "main.h"

#define MAX7219_CS 1<<12


#define MAX7219_CLK 1<<13
#define MAX7219_DIN 1<<15

uint8_t digit, LED;


uint32_t Cnt=0, tempCnt=0, divisor;

void GPIO_Init(void);
void MAX7219_Init(void);
void Timer4_Init(void);
void delay(volatile int i);
void sendWordToMax7219(uint16_t dataSend);

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 152 / 205
Programming Microcontrollers in C 9.6 Timing generation and measurement programming in C

Timer programming

void MAX7219Display(void);

int main(void){
GPIO_Init();
Timer2_Init();
MAX7219_Init();

for (;;) {
Cnt = TIM2->CNT;
MAX7219Display();
delay(5000);
}
}
void delay(volatile int i){
while(i--) continue;
}

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 153 / 205
Programming Microcontrollers in C 9.6 Timing generation and measurement programming in C

Timer programming

void GPIO_Init(void){
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOBEN;
delay(5);
// PB6, PB7 in alternate function mode
GPIOB->MODER &= ˜(GPIO_MODER_MODE6+GPIO_MODER_MODE7);
GPIOB->MODER |= (GPIO_MODER_MODE6_1+GPIO_MODER_MODE7_1);
// PB6, PB7 for TIM4 as CH1 and CH2
GPIOB->AFR[0] &= ˜(GPIO_AFRL_AFSEL6+GPIO_AFRL_AFSEL7);
GPIOB->AFR[0] |= (GPIO_AFRL_AFSEL6_1+GPIO_AFRL_AFSEL7_1);
// Enable output mode for PB12, PB13, PB15
GPIOB->MODER &= ˜(GPIO_MODER_MODE12+GPIO_MODER_MODE13
+GPIO_MODER_MODE15);
GPIOB->MODER |= (GPIO_MODER_MODE12_0+GPIO_MODER_MODE13_0
+GPIO_MODER_MODE15_0);

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 154 / 205
Programming Microcontrollers in C 9.6 Timing generation and measurement programming in C

Timer programming

}
void Timer4_Init(void){
// Timer 4 RCC enable
RCC->APB1ENR1 |= RCC_APB1ENR1_TIM4EN;

// Settings ARR value


TIM4->ARR = 0xFFFF;

// tim_ti1fp1 mapped on tim_ti1, tim_ti1fp2 mapped on tim_ti2


TIM4->CCMR1 &= ˜(TIM_CCMR1_CC1S+TIM_CCMR1_CC2S);
TIM4->CCMR1 |= (TIM_CCMR1_CC1S_0+TIM_CCMR1_CC2S_0);

// CH1 and CH2 filters


TIM4->CCMR1 &= ˜(TIM_CCMR1_IC1F+TIM_CCMR1_IC2F);
TIM4->CCMR1 |= (TIM_CCMR1_IC1F_1+TIM_CCMR1_IC1F_2
+TIM_CCMR1_IC2F_1+TIM_CCMR1_IC2F_2);

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 155 / 205
Programming Microcontrollers in C 9.6 Timing generation and measurement programming in C

Timer programming

// tim_ti1fp1 non-inverted, tim_ti1fp1=tim_ti1


TIM4->CCER &= ˜(TIM_CCER_CC1P+TIM_CCER_CC1NP);

// tim_ti1fp2 non-inverted, tim_ti1fp2=tim_ti2


TIM4->CCER &= ˜(TIM_CCER_CC2P+TIM_CCER_CC2NP);

// Both inputs are active on both rising and falling edges


TIM4->SMCR &= ˜(TIM_SMCR_SMS);
TIM4->SMCR |= (TIM_SMCR_SMS_0+TIM_SMCR_SMS_1);

// Enable TIM4
TIM4->CR1 |= TIM_CR1_CEN;
}
void MAX7219Display(void){
tempCnt = Cnt;
divisor=10000000;

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 156 / 205
Programming Microcontrollers in C 9.6 Timing generation and measurement programming in C

Timer programming

for (LED=0; LED<8; LED++){


digit=tempCnt/divisor;
sendWordToMax7219(((8-LED)<<8)|digit);
tempCnt %= divisor;
divisor /= 10;
}
}

void sendWordToMax7219(uint16_t dataSend){


...
}

void MAX7219_Init(void){
...
}

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 157 / 205
Programming Microcontrollers in C 9.7 Analog interface programming in C

Analog-to-Digital Converter (ADC) programming

 Example 9.19
Consider again the circuit of the STM32G431RBT6 with IC MAX7219, an
eight-digit seven-segment LEDs, and two potentiometers connected to pins PC0
(ADC IN6) and PC1 (ADC IN7) as shown in the figure. Write C codes to show the
ADC values of each channels on the the group of 4 LEDs. The ADC conversion is
in the single mode.

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 160 / 205
Programming Microcontrollers in C 9.7 Analog interface programming in C

Analog-to-Digital Converter (ADC) programming

STM32G431RBT6
12 8 IN1
PA0 PC0
13 9 IN2
PA1 PC1
14 10
PA2 PC2
17 11
PA3 PC3
18 22
PA4 PC4
19 23
PA5 PC5
20 38
PA6 PC6
21 39
PA7 PC7
42 40
PA8 PC8 VCC
43 41
PA9 PC9
44 52 VCC
PA10 PC10
53
PC11
24 54
PB0 PC12
25 2 IN1 R1
PB1 PC13 R2
26 10K
PB2 18 DIGITS
56 ISET DIG0 - DIG7
PB3
57 U2
PB4
58 DIN 1
PB5 55 DIN
59 PD2
PB6 LAT 12
60 LOAD (CS)
PB7
62 VCC SCK 13
PB9 CLK SEGMENTS
62 SEG A-G
PB9
30 SEG DP
PB10 IN2
33 R3 9
PB11 GND
LAT 34
PB12 GND
SCK 35
PB13
36 MAX7219
PB14
DIN 37
PB15

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 161 / 205
Programming Microcontrollers in C 9.7 Analog interface programming in C

Analog-to-Digital Converter (ADC) programming

Solution:

Code 9.14
#include "main.h"

#define MAX7219_CS 1<<12


#define MAX7219_CLK 1<<13
#define MAX7219_DIN 1<<15

uint8_t digit, LED;


uint32_t divisor;

uint16_t value_ADC1=0, value_ADC2=0;


uint16_t prev_ADC1=0, prev_ADC2=0;
uint16_t temp_ADC=0;

void GPIO_Init(void);

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 162 / 205
Programming Microcontrollers in C 9.7 Analog interface programming in C

Analog-to-Digital Converter (ADC) programming

void ADC1_Init(void);
void MAX7219_Init(void);
void delay(volatile int i);
void sendWordToMax7219(uint16_t dataSend);
void displayADCValues(void);

int main(void){

GPIO_Init();
ADC1_Init();
MAX7219_Init();

for (;;) {

// ADC1 start conversion


ADC1->CR |= ADC_CR_ADSTART;

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 163 / 205
Programming Microcontrollers in C 9.7 Analog interface programming in C

Analog-to-Digital Converter (ADC) programming

// Check_conversion_CH1
while (((ADC1->ISR)&(ADC_ISR_EOC))==0){}
value_ADC1 = ADC1->DR;

// Check_conversion_CH2
while (((ADC1->ISR)&(ADC_ISR_EOC))==0){}
value_ADC2 = ADC1->DR;

value_ADC1 = (49*prev_ADC1+value_ADC1)/50;
value_ADC2 = (49*prev_ADC2+value_ADC2)/50;

prev_ADC1=value_ADC1;
prev_ADC2=value_ADC2;

displayADCValues();
delay(50000);

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 164 / 205
Programming Microcontrollers in C 9.7 Analog interface programming in C

Analog-to-Digital Converter (ADC) programming

}
}

void delay(volatile int i){


while(i--) continue;
}

void GPIO_Init(void){
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOCEN;
delay(5);

// Enable analog mode for PC0 and PC1


GPIOC->MODER |= (GPIO_MODER_MODE0+GPIO_MODER_MODE1);

// No pull-down resistor
GPIOC->PUPDR &= ˜(GPIO_PUPDR_PUPD0+GPIO_PUPDR_PUPD1);

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 165 / 205
Programming Microcontrollers in C 9.7 Analog interface programming in C

Analog-to-Digital Converter (ADC) programming

RCC->AHB2ENR |= RCC_AHB2ENR_GPIOBEN;
delay(5);

// Enable output mode for PB12, PB13, PB15


GPIOB->MODER &= ˜(GPIO_MODER_MODE12+GPIO_MODER_MODE13
+GPIO_MODER_MODE15);
GPIOB->MODER |= (GPIO_MODER_MODE12_0+GPIO_MODER_MODE13_0
+GPIO_MODER_MODE15_0);
}

void ADC1_Init(void){

// Enable clock for ADC1, ADC2


RCC->AHB2ENR |= RCC_AHB2ENR_ADC12EN;

// Enable ADC1/2 clock source selection

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 166 / 205
Programming Microcontrollers in C 9.7 Analog interface programming in C

Analog-to-Digital Converter (ADC) programming

RCC->CCIPR &= ˜(RCC_CCIPR_ADC12SEL);


RCC->CCIPR |= (RCC_CCIPR_ADC12SEL_1);

// Disable continous mode


ADC1->CFGR &= ˜(ADC_CFGR_CONT);

// Enable Auto-delayed conversion mode


ADC1->CFGR |= ADC_CFGR_AUTDLY;

// Select prescale for ADC1


ADC12_COMMON->CCR &= ˜(ADC_CCR_PRESC);
ADC12_COMMON->CCR |= (ADC_CCR_PRESC_0+ADC_CCR_PRESC_1
+ADC_CCR_PRESC_3);

// Select sampling time for channel 1 (111 = 640.5 ADC clock cycles)
ADC1->SMPR1 |= ADC_SMPR1_SMP1;

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 167 / 205
Programming Microcontrollers in C 9.7 Analog interface programming in C

Analog-to-Digital Converter (ADC) programming

// ADC group regular sequencer scan length (0001=2 conversion)


ADC1->SQR1 &= ˜(ADC_SQR1_L);
ADC1->SQR1 |= (ADC_SQR1_L_0);

// Enable PC0 as ADC IN6


ADC1->SQR1 &= ˜(ADC_SQR1_SQ1);
ADC1->SQR1 |= (ADC_SQR1_SQ1_1+ADC_SQR1_SQ1_2);

// Enable PC1 as ADC IN7


ADC1->SQR1 &= ˜(ADC_SQR1_SQ2);
ADC1->SQR1 |= (ADC_SQR1_SQ2_0+ADC_SQR1_SQ2_1+ADC_SQR1_SQ2_2);

// Disable the Deep-power-down mode


ADC1->CR &= ˜(ADC_CR_DEEPPWD);

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 168 / 205
Programming Microcontrollers in C 9.7 Analog interface programming in C

Analog-to-Digital Converter (ADC) programming

// Enable the ADC voltage regulator


ADC1->CR |= ADC_CR_ADVREGEN;

// Clear ADC ready bit by writing 1 to it


ADC1->ISR |= ADC_ISR_ADRDY;

// Enable ADC1
ADC1->CR |= ADC_CR_ADEN;

// Wait until ADRDY = 1


while (((ADC1->ISR)&(ADC_ISR_ADRDY))==0){};
}

void displayADCValues(void){
temp_ADC = value_ADC1;
divisor=1000;

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 169 / 205
Programming Microcontrollers in C 9.7 Analog interface programming in C

Analog-to-Digital Converter (ADC) programming

for (LED=0; LED<4; LED++){


digit=temp_ADC/divisor;
sendWordToMax7219(((8-LED)<<8)|digit);
temp_ADC %= divisor;
divisor /= 10;
}
temp_ADC = value_ADC2;
divisor=1000;
for (LED=4; LED<8; LED++){
digit=temp_ADC/divisor;
sendWordToMax7219(((8-LED)<<8)|digit);
temp_ADC %= divisor;
divisor /= 10;
}
}

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 170 / 205
Programming Microcontrollers in C 9.7 Analog interface programming in C

Analog-to-Digital Converter (ADC) programming

void sendWordToMax7219(uint16_t dataSend){


...
}

void MAX7219_Init(void){
...
}

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 171 / 205
Programming Microcontrollers in C 9.7 Analog interface programming in C

Digital-to-Analog Converter (DAC) programming

 Example 9.20
Consider again the speed-adjustable belt conveyor scale system shown in the figure.
It consists of a weight measurement sensor (loadcell), a speed control unit with an
electric motor and drive, and a belt speed measurement structure. The continuous
conveyor belt scales (or continuous weighing devices) keep the material flux at a
constant feedrate in kilograms per second (or Ton per hour). Write an Assembly
program to control the speed of the belt such that the feedrate is kept at the desired
value Qref regardless the changes in the weight of the feeding material.
Hopper Induction
motor Head pulley

Tail pulley Impact Loadcell Speed Vertical


idlers sensor gravity

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 173 / 205
Programming Microcontrollers in C 9.7 Analog interface programming in C

Digital-to-Analog Converter (DAC) programming

Solution:

Code 9.15
#include "main.h"

#define MAX7219_CS 1<<12


#define MAX7219_CLK 1<<13
#define MAX7219_DIN 1<<15

uint8_t digit, LED;


uint32_t divisor;

uint16_t value_ADC1=0, value_ADC2=0;


uint16_t prev_ADC1=0, prev_ADC2=0;
uint16_t tempValue=0;

float Qref, weight, velocity, fluxErr, prevErr;

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 174 / 205
Programming Microcontrollers in C 9.7 Analog interface programming in C

Digital-to-Analog Converter (DAC) programming

uint16_t dispWeight, displayVelocity;


uint16_t controlValue;

float kw, kv, kp, ki, kd;


float partP, partI, partD;
float prevI, prevD;

void GPIO_Init(void);
void ADC1_Init(void);
void DAC1_Init(void);
void MAX7219_Init(void);
void delay(volatile int i);
void sendWordToMax7219(uint16_t dataSend);
void displaySABSValues(void);

int main(void){

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 175 / 205
Programming Microcontrollers in C 9.7 Analog interface programming in C

Digital-to-Analog Converter (DAC) programming

GPIO_Init();
ADC1_Init();
DAC1_Init();
MAX7219_Init();

Qref=333.0;
kw = 0.07326; kv = 0.001221;
kp = 2.75; ki = 0.25; kd = 0.025;

for (;;) {

// ADC1 start conversion


ADC1->CR |= ADC_CR_ADSTART;

// Check_conversion_CH1
while (((ADC1->ISR)&(ADC_ISR_EOC))==0){}

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 176 / 205
Programming Microcontrollers in C 9.7 Analog interface programming in C

Digital-to-Analog Converter (DAC) programming

value_ADC1 = ADC1->DR;

// Check_conversion_CH2
while (((ADC1->ISR)&(ADC_ISR_EOC))==0){}
value_ADC2 = ADC1->DR;

value_ADC1 = (49*prev_ADC1+value_ADC1)/50;
value_ADC2 = (49*prev_ADC2+value_ADC2)/50;

prev_ADC1=value_ADC1;
prev_ADC2=value_ADC2;

weight = kw* value_ADC1;


velocity = kv* value_ADC2;

dispWeight = weight*10;

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 177 / 205
Programming Microcontrollers in C 9.7 Analog interface programming in C

Digital-to-Analog Converter (DAC) programming

displayVelocity = velocity*100;

fluxErr = Qref - (weight*velocity);


partP = kp*fluxErr;
partI = ki*fluxErr + prevI;
partD = kd*(fluxErr - prevErr);

prevI = partI;
prevErr = fluxErr;

controlValue = (uint16_t)(partP+partI+partD);

if (controlValue>4095){
controlValue=4095;
}else{
if (controlValue<0){
controlValue=0;

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 178 / 205
Programming Microcontrollers in C 9.7 Analog interface programming in C

Digital-to-Analog Converter (DAC) programming

}
}
DAC1->DHR12R1 = controlValue;

displaySABSValues();
delay(50000);
}
}

void delay(volatile int i){


while(i--) continue;
}

void GPIO_Init(void){
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOCEN;
delay(5);

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 179 / 205
Programming Microcontrollers in C 9.7 Analog interface programming in C

Digital-to-Analog Converter (DAC) programming

// Enable analog mode for PC0 and PC1


GPIOC->MODER |= (GPIO_MODER_MODE0+GPIO_MODER_MODE1);

// No pull-down resistor
GPIOC->PUPDR &= ˜(GPIO_PUPDR_PUPD0+GPIO_PUPDR_PUPD1);

RCC->AHB2ENR |= RCC_AHB2ENR_GPIOBEN;
delay(5);

// Enable output mode for PB12, PB13, PB15


GPIOB->MODER &= ˜(GPIO_MODER_MODE12+GPIO_MODER_MODE13
+GPIO_MODER_MODE15);
GPIOB->MODER |= (GPIO_MODER_MODE12_0+GPIO_MODER_MODE13_0
+GPIO_MODER_MODE15_0);
}

void ADC1_Init(void){

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 180 / 205
Programming Microcontrollers in C 9.7 Analog interface programming in C

Digital-to-Analog Converter (DAC) programming

// Enable clock for ADC1, ADC2


RCC->AHB2ENR |= RCC_AHB2ENR_ADC12EN;

// Enable ADC1/2 clock source selection


RCC->CCIPR &= ˜(RCC_CCIPR_ADC12SEL);
RCC->CCIPR |= (RCC_CCIPR_ADC12SEL_1);

// Disable continous mode


ADC1->CFGR &= ˜(ADC_CFGR_CONT);

// Enable Auto-delayed conversion mode


ADC1->CFGR |= ADC_CFGR_AUTDLY;

// Select prescale for ADC1


ADC12_COMMON->CCR &= ˜(ADC_CCR_PRESC);
ADC12_COMMON->CCR |= (ADC_CCR_PRESC_0+ADC_CCR_PRESC_1
+ADC_CCR_PRESC_3);

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 181 / 205
Programming Microcontrollers in C 9.7 Analog interface programming in C

Digital-to-Analog Converter (DAC) programming

// Sampling time for channel 1 (111 = 640.5 ADC clock cycles)


ADC1->SMPR1 |= ADC_SMPR1_SMP1;

// ADC group regular sequencer scan length (0001=2 conversion)


ADC1->SQR1 &= ˜(ADC_SQR1_L);
ADC1->SQR1 |= (ADC_SQR1_L_0);

// Enable PC0 as ADC IN6


ADC1->SQR1 &= ˜(ADC_SQR1_SQ1);
ADC1->SQR1 |= (ADC_SQR1_SQ1_1+ADC_SQR1_SQ1_2);

// Enable PC1 as ADC IN7


ADC1->SQR1 &= ˜(ADC_SQR1_SQ2);
ADC1->SQR1 |= (ADC_SQR1_SQ2_0+ADC_SQR1_SQ2_1+ADC_SQR1_SQ2_2);

// Disable the Deep-power-down mode


ADC1->CR &= ˜(ADC_CR_DEEPPWD);

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 182 / 205
Programming Microcontrollers in C 9.7 Analog interface programming in C

Digital-to-Analog Converter (DAC) programming

// Enable the ADC voltage regulator


ADC1->CR |= ADC_CR_ADVREGEN;

// Clear ADC ready bit by writing 1 to it


ADC1->ISR |= ADC_ISR_ADRDY;

// Enable ADC1
ADC1->CR |= ADC_CR_ADEN;

// Wait until ADRDY = 1


while (((ADC1->ISR)&(ADC_ISR_ADRDY))==0){};
}

void DAC1_Init(void){

//Enable clock for DAC1

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 183 / 205
Programming Microcontrollers in C 9.7 Analog interface programming in C

Digital-to-Analog Converter (DAC) programming

RCC->AHB2ENR |= RCC_AHB2ENR_DAC1EN;

// Trigger disable
DAC1->CR &= ˜DAC_CR_TEN1;

// Triangle wave disable


DAC1->CR &= ˜DAC_CR_WAVE1;

// Use the Buffer


DAC1->MCR &= ˜DAC_MCR_MODE1;

// Triangle wave disable


DAC1->CR |= DAC_CR_EN1;

DAC1->DHR12R1 = 0;
}

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 184 / 205
Programming Microcontrollers in C 9.7 Analog interface programming in C

Digital-to-Analog Converter (DAC) programming

void displaySABSValues(void){
tempValue = (uint16_t)(dispWeight);
divisor=1000;
for (LED=0; LED<4; LED++){
digit=tempValue/divisor;
if (LED==2){
sendWordToMax7219(((8-LED)<<8)|digit|0x80);
}else{
sendWordToMax7219(((8-LED)<<8)|digit);
}
tempValue %= divisor;
divisor /= 10;
}
tempValue = (uint16_t)(displayVelocity);
divisor=100;
for (LED=5; LED<8; LED++){

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 185 / 205
Programming Microcontrollers in C 9.7 Analog interface programming in C

Digital-to-Analog Converter (DAC) programming

digit=tempValue/divisor;
if (LED==5){
sendWordToMax7219(((8-LED)<<8)|digit|0x80);
}else{
sendWordToMax7219(((8-LED)<<8)|digit);
}
tempValue %= divisor;
divisor /= 10;
}
}
void sendWordToMax7219(uint16_t dataSend){
...
}
void MAX7219_Init(void){
...
}

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 186 / 205
Programming Microcontrollers in C 9.8 Serial communication programming

USART programming

 Example 9.21
Suppose we have a computer interfacing with STM32G431RBT6 through an ST-
Link circuit and USART2 as shown in the figure. Write a program to receive a
character and transmit it back to the receiver (echo).
U1
12 24
PA0 PB0
13 25
PA1 PB1
TX 14 26
PA2 PB2
ST-Link RX 17 56
PA3 PB3
18 57
PC PA4 PB4
19 58
PA5 PB5
20 59
PA6 PB6
21 60
PA7 PB7
42
PA8
43 62
PA9 PB9
44 30
PA10 PB10
45 33
PA11 PB11
46 34
PA12 PB12
49 35
PA13 PB13
50 36
PA14 PB14
51 37
PA15 PB15

STM32G431RBT6

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 189 / 205
Programming Microcontrollers in C 9.8 Serial communication programming

USART programming

Solution:

Code 9.16
#include "main.h"

uint8_t data=0;

void GPIO_Init(void);
void USART2_Init(void);
void delay(volatile int i);

int main(void){
GPIO_Init();
USART2_Init();

for (;;) {

if (((USART2->ISR)&(USART_ISR_RXNE))!=0){
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 190 / 205
Programming Microcontrollers in C 9.8 Serial communication programming

USART programming

data = USART2->RDR;
USART2->TDR = data;
while(((USART2->ISR)&(USART_ISR_TC))==0){}
}
delay(50000);
}
}

void delay(volatile int i){


while(i--) continue;
}

void GPIO_Init(void){
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOAEN;
delay(5);

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 191 / 205
Programming Microcontrollers in C 9.8 Serial communication programming

USART programming

// Enable alternative function for PA2 and PA3


GPIOA->MODER &= ˜(GPIO_MODER_MODE2+GPIO_MODER_MODE3);
GPIOA->MODER |= (GPIO_MODER_MODE2_1+GPIO_MODER_MODE3_1);

// Select UART mode (AF7) for PA2, PA3


GPIOA->AFR[0] &= ˜(GPIO_AFRL_AFSEL2+GPIO_AFRL_AFSEL3);
GPIOA->AFR[0] |= (GPIO_AFRL_AFSEL2_0+GPIO_AFRL_AFSEL2_1
+GPIO_AFRL_AFSEL2_2
+GPIO_AFRL_AFSEL3_0+GPIO_AFRL_AFSEL3_1
+GPIO_AFRL_AFSEL3_2);
}

void USART2_Init(void){

//Enabled the clock to the USART


RCC->APB1ENR1 |= RCC_APB1ENR1_USART2EN;

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 192 / 205
Programming Microcontrollers in C 9.8 Serial communication programming

USART programming

// Specify Oversampling mode (bit OVER8)


USART2->CR1 &= ˜(USART_CR1_UE+USART_CR1_M0+USART_CR1_M1
+USART_CR1_OVER8);
USART2->CR1 |= (USART_CR1_RE+USART_CR1_TE);

// Set UART Baud rate = 170.000.000/115.200 = 1476


USART2->BRR = 1476;

// Enable the USART


USART2->CR1 |= USART_CR1_UE;
}

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 193 / 205
Programming Microcontrollers in C 9.8 Serial communication programming

SPI programming

 Example 9.22
Consider again the circuit of the STM32G431RBT6 with IC MAX7219 and eight
seven-segment LEDs as shown in the figure. Write a C program to display numbers
12345678 on the LEDs using SPI.
STM32G431RBT6
12 24
PA0 PB0
13 25
PA1 PB1
14 26
PA2 PB2
17 56
PA3 PB3
18 57
PA4 PB4
19 58
PA5 PB5
59
PB6
60
PB7
8 62 VCC
PC0 PB9
9 30
PC1 PB10
10 33
PC2 PB11
11 34 LAT
PC3 PB12
22 35 SCK 10K
PC4 PB13
23 36
PC5 PB14
38 37 DIN 18 DIGITS
PC6 PB15 ISET DIG0 - DIG7
39
PC7 U2
40
PC8 DIN 1
41 55 DIN
PC9 PD2
52 LAT 12
PC10 LOAD (CS)
53
PC11 SCK 13
54 CLK SEGMENTS
PC12 SEG A-G
2
PC13 SEG DP
9
GND
GND
MAX7219

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 195 / 205
Programming Microcontrollers in C 9.8 Serial communication programming

SPI programming

Solution:

Code 9.17
#include "main.h"

#define MAX7219_CS 1<<12

void GPIO_Init(void);
void SPI2_Init(void);
void MAX7219_Init(void);
void SPI2_Transmit(uint16_t dataTX);
void delay(volatile int i);

int main(void){

GPIO_Init();
SPI2_Init();

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 196 / 205
Programming Microcontrollers in C 9.8 Serial communication programming

SPI programming

MAX7219_Init();

SPI2_Transmit(0x0801);
SPI2_Transmit(0x0702);
SPI2_Transmit(0x0603);
SPI2_Transmit(0x0504);
SPI2_Transmit(0x0405);
SPI2_Transmit(0x0306);
SPI2_Transmit(0x0207);
SPI2_Transmit(0x0108);

for (;;) {
delay(50000);
}
}

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 197 / 205
Programming Microcontrollers in C 9.8 Serial communication programming

SPI programming

void delay(volatile int i){


while(i--) continue;
}

void GPIO_Init(void){
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOBEN;
delay(5);

// Enable output mode for PB12


GPIOB->MODER &= ˜GPIO_MODER_MODE12;
GPIOB->MODER |= GPIO_MODER_MODE12_0;

// PB13, PB14, PB15 in the alternate function mode


GPIOB->MODER &= ˜(GPIO_MODER_MODE13+GPIO_MODER_MODE14
+GPIO_MODER_MODE15);
GPIOB->MODER |= (GPIO_MODER_MODE13_1+GPIO_MODER_MODE14_1

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 198 / 205
Programming Microcontrollers in C 9.8 Serial communication programming

SPI programming

+GPIO_MODER_MODE15_1);

GPIOB->AFR[1] &= ˜(GPIO_AFRH_AFSEL13+GPIO_AFRH_AFSEL14


+GPIO_AFRH_AFSEL15);
GPIOB->AFR[1] |= (GPIO_AFRH_AFSEL13_0+GPIO_AFRH_AFSEL13_2
+GPIO_AFRH_AFSEL14_0+GPIO_AFRH_AFSEL14_2
+GPIO_AFRH_AFSEL15_0+GPIO_AFRH_AFSEL15_2);
}

void SPI2_Init(void){

// Enabled the clock to the SPI2


RCC->APB1ENR1 |= RCC_APB1ENR1_SPI2EN;
delay(5);

// Configure the serial clock baud rate


SPI2->CR1 |= (SPI_CR1_BR_0+SPI_CR1_BR_1+SPI_CR1_BR_2);

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 199 / 205
Programming Microcontrollers in C 9.8 Serial communication programming

SPI programming

// Configure the CPOL and CPHA bits


SPI2->CR1 |= (SPI_CR1_CPOL+SPI_CR1_CPHA);

// Select simplex or half-duplex mode, RXONLY = 0


SPI2->CR1 &= ˜(SPI_CR1_RXONLY);

// Configure the LSBFIRST bit, MSB first


SPI2->CR1 &= ˜(SPI_CR1_LSBFIRST);

// Configure the MSTR bit, Master Mode


SPI2->CR1 |= (SPI_CR1_MSTR);

// Software Slave Management


SPI2->CR1 |= (SPI_CR1_SSI+SPI_CR1_SSM);

// Configure the DS[3:0] bits to select 16 bit for the transfer.

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 200 / 205
Programming Microcontrollers in C 9.8 Serial communication programming

SPI programming

SPI2->CR2 |= SPI_CR2_DS;

// Enable SPI
SPI2->CR1 |= (SPI_CR1_SPE);
}

void SPI2_Transmit(uint16_t dataTX){

GPIOB->ODR &= ˜(MAX7219_CS);


delay(5);

while(((SPI2->SR)&(SPI_SR_TXE))==0){} // Wait for TXE bit to set


SPI2->DR = dataTX;
while(((SPI2->SR)&(SPI_SR_TXE))==0){} // Wait for buffer empty
while(((SPI2->SR)&(SPI_SR_BSY))!=0){} // Wait for Busy flag

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 201 / 205
Programming Microcontrollers in C 9.8 Serial communication programming

SPI programming

GPIOB->ODR |= (MAX7219_CS);
delay(5);
}

void MAX7219_Init(void){
SPI2_Transmit(0x0F00);
SPI2_Transmit(0x0C00);
SPI2_Transmit(0x0A03);
SPI2_Transmit(0x09FF);
SPI2_Transmit(0x010F);
SPI2_Transmit(0x020F);
SPI2_Transmit(0x030F);
SPI2_Transmit(0x040F);
SPI2_Transmit(0x050F);
SPI2_Transmit(0x060F);
SPI2_Transmit(0x070F);

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 202 / 205
Programming Microcontrollers in C 9.8 Serial communication programming

SPI programming

SPI2_Transmit(0x080F);
SPI2_Transmit(0x0B07);
SPI2_Transmit(0x0C01);
}

Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 203 / 205

You might also like