Professional Documents
Culture Documents
Chapter1-Các Trang Đã Xóa
Chapter1-Các Trang Đã Xóa
Chapter 1
Introduction
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 10 / 74
Introduction 1.3 Binary digital system
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
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:
AREA STACK,NOINIT,READWRITE,ALIGN=3
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 17 / 81
Assembly programming language 3.3 Move instructions
MOV instruction
ALIGN
END
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:
ALIGN
END
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:
ALIGN
END
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:
AREA STACK,NOINIT,READWRITE,ALIGN=3
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 29 / 81
Assembly programming language 3.4 Load/Store Instructions
LDR instruction
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:
AREA STACK,NOINIT,READWRITE,ALIGN=3
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
ALIGN
END
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:
AREA STACK,NOINIT,READWRITE,ALIGN=3
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
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:
AREA STACK,NOINIT,READWRITE,ALIGN=3
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 43 / 81
Assembly programming language 3.4 Load/Store Instructions
STR instruction
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:
Reset_Handler
MOV R1,#2
LDR R2,=StoredData
MOV32 R3,#0xF0C2E7A5
STR R3,[R2,R1]
NOP
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:
Reset_Handler
LDR R2,=StoredData
MOV32 R3,#0xF0C2E7A5
STR R3,[R2,#2]!
NOP
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:
Reset_Handler
LDR R2,=StoredData
MOV32 R3,#0xF0C2E7A5
STR R3,[R2],#2]
NOP
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:
Reset_Handler
MOVT R1,#0xE0F5
MOVT R2,#0x60D3
MOVT R3,#0x79A7
ADD R1,R2
ADD R1,R3
NOP
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 59 / 81
Assembly programming language 3.5 Arithmetic instructions
ADD instruction
Solution:
Reset_Handler
Reset_Handler
MOVT R1,#0xE0F5
MOVT R2,#0x60D3
MOVT R3,#0x79A7
ADDS R1,R2
ADDS R1,R3
NOP
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 61 / 81
Assembly programming language 3.5 Arithmetic instructions
ADC instruction
Solution:
Reset_Handler
MOVT R1,#0xE0F5
MOVT R2,#0x60D3
MOVT R3,#0x79A7
ADCS R1,R2
ADCS R1,R3
NOP
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 64 / 81
Assembly programming language 3.5 Arithmetic instructions
SUB instruction
Solution:
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:
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:
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
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 92 / 186
Assembly programming language 3.6 Logical instructions
AND instruction
Solution:
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
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 95 / 186
Assembly programming language 3.6 Logical instructions
ORR instruction
Solution:
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
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 101 / 186
Assembly programming language 3.6 Logical instructions
ORR instruction
Solution:
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
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 104 / 186
Assembly programming language 3.6 Logical instructions
BIC instruction
Solution:
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
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 110 / 186
Assembly programming language 3.6 Logical instructions
EOR instruction
Solution:
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
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 116 / 186
Assembly programming language 3.6 Logical instructions
EOR instruction
Solution:
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
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:
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:
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:
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:
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
Solution:
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:
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:
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
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
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
Solution:
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
Done
B Done
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
Solution:
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
Done
B Done
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
Solution:
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
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
Solution:
INCLUDE ../inc/stm32G431xx.s
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 75 / 82
Interfacing methods 4.17 GPIO port initialization procedure
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 76 / 82
Interfacing methods 4.17 GPIO port initialization procedure
ORR R1,R2
LSL R2,R2,#1
BIC R1,R2
STR R1,[R0]
ALIGN
END
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 77 / 82
Interfacing methods 4.18 GPIO output ports
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 79 / 82
Interfacing methods 4.18 GPIO output ports
Solution:
INCLUDE ../inc/stm32G431xx.s
LED EQU BIT5
DLVAL EQU 5000000
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 80 / 82
Interfacing methods 4.18 GPIO output ports
Reset_Handler
BL GPIO_Init
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 81 / 82
Interfacing methods 4.18 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
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 82 / 82
Interfacing methods 4.18 GPIO output ports
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
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
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
Solution:
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 81 / 178
Interfacing methods 4.5 General purpose input output ports
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
N3 EQU BIT5
N4 EQU BIT6
N5 EQU BIT7
N6 EQU BIT8
N7 EQU BIT9
AREA STACK,NOINIT,READWRITE,ALIGN=3
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 83 / 178
Interfacing methods 4.5 General purpose input output ports
__Vectors
DCD Stack_Mem+Stack_Size
DCD Reset_Handler
ALIGN
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 84 / 178
Interfacing methods 4.5 General purpose input 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
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
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
ORR R1,#GPIOA_EN
STR R1,[R0]
NOP
;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
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
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
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
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
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
Solution:
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 98 / 178
Interfacing methods 4.5 General purpose input output ports
AREA STACK,NOINIT,READWRITE,ALIGN=3
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 100 / 178
Interfacing methods 4.5 General purpose input 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
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 102 / 178
Interfacing methods 4.5 General purpose input output ports
LDR R1,[R0]
ORR R1,#GPIOB_EN
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
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
Solution:
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 105 / 178
Interfacing methods 4.5 General purpose input output ports
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 106 / 178
Interfacing methods 4.5 General purpose input output ports
N0 EQU BIT0
N1 EQU BIT1
N2 EQU BIT4
N3 EQU BIT5
N4 EQU BIT6
N5 EQU BIT7
N6 EQU BIT8
N7 EQU BIT9
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
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 108 / 178
Interfacing methods 4.5 General purpose input 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
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
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]
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 111 / 178
Interfacing methods 4.5 General purpose input 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]
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 112 / 178
Interfacing methods 4.5 General purpose input 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
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
Solution:
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 120 / 178
Interfacing methods 4.5 General purpose input output ports
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 121 / 178
Interfacing methods 4.5 General purpose input output ports
AREA STACK,NOINIT,READWRITE,ALIGN=3
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 122 / 178
Interfacing methods 4.5 General purpose input 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
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 123 / 178
Interfacing methods 4.5 General purpose input 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
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
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
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
Solution:
INCLUDE ../inc/stm32G431xx.s
AREA STACK,NOINIT,READWRITE,ALIGN=3
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 138 / 178
Interfacing methods 4.6 GPIO Port interfaces
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 139 / 178
Interfacing methods 4.6 GPIO Port interfaces
; 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
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
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
; 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
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
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]
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 145 / 178
Interfacing methods 4.6 GPIO Port interfaces
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
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
Solution:
INCLUDE ../inc/stm32G431xx.s
AREA STACK,NOINIT,READWRITE,ALIGN=3
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 148 / 178
Interfacing methods 4.6 GPIO Port interfaces
EXPORT __Vectors
__Vectors
DCD Stack_Mem+Stack_Size
DCD Reset_Handler
ALIGN
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
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 150 / 178
Interfacing methods 4.6 GPIO Port interfaces
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
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
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
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 153 / 178
Interfacing methods 4.6 GPIO Port interfaces
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
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 162 / 229
Interfacing methods 4.6 GPIO Port interfaces
Solution:
Code 4.8
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 163 / 229
Interfacing methods 4.6 GPIO Port interfaces
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 164 / 229
Interfacing methods 4.6 GPIO Port interfaces
AREA STACK,NOINIT,READWRITE,ALIGN=3
Stack_Mem SPACE Stack_Size
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 165 / 229
Interfacing methods 4.6 GPIO Port interfaces
LDR R0,=RCC_BASE+AHB2ENR_OFFSET
LDR R1,[R0]
ORR R1,#GPIOB_EN
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
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] ;
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 167 / 229
Interfacing methods 4.6 GPIO Port interfaces
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
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
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
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 170 / 229
Interfacing methods 4.6 GPIO Port interfaces
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 171 / 229
Interfacing methods 4.6 GPIO Port interfaces
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 172 / 229
Interfacing methods 4.6 GPIO Port interfaces
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 173 / 229
Interfacing methods 4.6 GPIO Port interfaces
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 174 / 229
Interfacing methods 4.6 GPIO Port interfaces
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
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
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
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 182 / 229
Interfacing methods 4.7 GPIO input port
AREA STACK,NOINIT,READWRITE,ALIGN=3
Stack_Mem SPACE Stack_Size
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 183 / 229
Interfacing methods 4.7 GPIO input port
__Vectors
DCD Stack_Mem+Stack_Size
DCD Reset_Handler
ALIGN
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 184 / 229
Interfacing methods 4.7 GPIO input port
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 185 / 229
Interfacing methods 4.7 GPIO input port
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 186 / 229
Interfacing methods 4.7 GPIO input port
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
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
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
Solution:
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++
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
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 193 / 229
Interfacing methods 4.7 GPIO input port
Anti-bouncing phenomenon
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
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 195 / 229
Interfacing methods 4.7 GPIO input port
Anti-bouncing phenomenon
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 196 / 229
Interfacing methods 4.7 GPIO input port
Anti-bouncing phenomenon
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
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
Solution:
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 202 / 229
Interfacing methods 4.7 GPIO input port
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 203 / 229
Interfacing methods 4.7 GPIO input port
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
AREA STACK,NOINIT,READWRITE,ALIGN=3
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 205 / 229
Interfacing methods 4.7 GPIO input port
DCD Stack_Mem+Stack_Size
DCD Reset_Handler
ALIGN
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 206 / 229
Interfacing methods 4.7 GPIO input port
Loop
CMP R4,R5
BGT Start
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
LDR R0,=GPIOB_BASE+ODR_OFFSET
MVN R3,#0x400 ;
STR R3,[R0] ;
LDR R2,=DLVAL ;
BL Delay
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
STR R2,[R0] ;
LDR R0,=GPIOB_BASE+ODR_OFFSET
MVN R3,#0x20 ;
STR R3,[R0] ;
LDR R2,=DLVAL ;
BL Delay ;
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
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
MVN R3,#0x8 ;
STR R3,[R0] ;
LDR R2,=DLVAL ;
BL Delay ;
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
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
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
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 214 / 229
Interfacing methods 4.7 GPIO input port
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 215 / 229
Interfacing methods 4.7 GPIO input port
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2022-2023 216 / 229
Interfacing methods 4.7 GPIO input port
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 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)
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)
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 43 / 97
Interrupts 5.3 Extended interrupts and events controller (EXTI)
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 44 / 97
Interrupts 5.3 Extended interrupts and events controller (EXTI)
LDR R0,=RCC_APB2ENR
LDR R1,[R0]
ORR R1,#RCC_APB2ENR_SYSCFGEN
STR R1,[R0]
LDR R0,=SYSCFG_EXTICR1
LDR R1,[R0]
BIC R1,#SYSCFG_EXTICR1_EXTI1 ; Line 1
STR R1,[R0]
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)
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)
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)
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)
LDR R0,=NVIC_ISPR0
LDR R1,=EXTI1_IRQ_EN
STR R1,[r0]
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 50 / 97
Interrupts 5.3 Extended interrupts and events controller (EXTI)
Solution:
Code 5.1
INCLUDE ../inc/stm32G431xx.s
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 51 / 97
Interrupts 5.3 Extended interrupts and events controller (EXTI)
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]
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 52 / 97
Interrupts 5.3 Extended interrupts and events controller (EXTI)
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 53 / 97
Interrupts 5.3 Extended interrupts and events controller (EXTI)
LDR R1,[R0]
MOV32 R2,#PUPDR1_0
BIC R1,R2
LSL R2,R2,#1
ORR R1,R2
STR 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)
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 55 / 97
Interrupts 5.3 Extended interrupts and events controller (EXTI)
LDR R1,[R0]
BIC R1,#EXTI_FTSR1_FT1 ; Disable line 1
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)
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)
Example 5.3
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 60 / 97
Interrupts 5.3 Extended interrupts and events controller (EXTI)
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
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 61 / 97
Interrupts 5.3 Extended interrupts and events controller (EXTI)
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)
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]
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 63 / 97
Interrupts 5.3 Extended interrupts and events controller (EXTI)
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 64 / 97
Interrupts 5.3 Extended interrupts and events controller (EXTI)
LDR R1,[R0]
ORR R1,#RCC_APB2ENR_SYSCFGEN
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)
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 66 / 97
Interrupts 5.3 Extended interrupts and events controller (EXTI)
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)
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
INCLUDE ../inc/stm32G431xx.s
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.
INCLUDE ../inc/stm32G431xx.s
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
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
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
Solution:
Code 6.1
INCLUDE ../../../inc/stm32G431xx.s
AREA STACK,NOINIT,READWRITE,ALIGN=3
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
EXPORT __Vectors
__Vectors
DCD Stack_Mem+Stack_Size
DCD Reset_Handler
ALIGN
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
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
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
; 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
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
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
Solution:
Code 6.2
INCLUDE ../../inc/stm32G431xx.s
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
BL Timer3_Init
LDR R5,=LED
Loop B Loop
GPIO_Init
...
BX LR
Timer3_Init
...
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
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]
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
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
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
Solution:
Code 6.3
INCLUDE ../../inc/stm32G431xx.s
Loop B Loop
GPIO_Init
; Enable clock for GPIOA
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 67 / 182
Timing generation and measurements 6.4 Timer in PWM mode
BX LR
Timer2_Init
; Timer 2 RCC enable
LDR R0,=RCC_APB1ENR1
LDR R1,[R0]
ORR R1,#RCC_APB1ENR1_TIM2EN
STR R1,[R0]
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 68 / 182
Timing generation and measurements 6.4 Timer in PWM mode
LDR R0,=TIM2_PSC
MOV R1,#TIM2_PSC_VALUE
STR R1,[R0]
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 69 / 182
Timing generation and measurements 6.4 Timer 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]
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 70 / 182
Timing generation and measurements 6.4 Timer 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
Solution:
Code 6.4
INCLUDE ../inc/stm32G431xx.s
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 84 / 182
Timing generation and measurements 6.6 Advanced-control timer registers
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
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
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 87 / 182
Timing generation and measurements 6.6 Advanced-control timer registers
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]
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 88 / 182
Timing generation and measurements 6.6 Advanced-control timer registers
; 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
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
Solution:
Code 6.5
INCLUDE ../../inc/stm32G431xx.s
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 97 / 182
Timing generation and measurements 6.6 Advanced-control timer registers
Loop B Loop
GPIO_Init
LDR R0,=RCC_AHB2ENR
LDR R1,[R0]
ORR R1,#RCC_GPIOA_EN
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
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
LDR R0,=RCC_APB2ENR
LDR R1,[R0]
ORR R1,#RCC_APB2ENR_TIM1EN
STR R1,[R0]
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 100 / 182
Timing generation and measurements 6.6 Advanced-control timer registers
LDR R0,=TIM1_CNT
MOV R1,#0x00
STR R1,[R0]
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 101 / 182
Timing generation and measurements 6.6 Advanced-control timer registers
LDR R0,=TIM1_CCER
LDR R1,[R0]
MOV32 R2,#TIM_CCER_CC1NE
ORR R1,R2
STR 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
MOV32 R2,#TIM_CCER_CC1E
ORR R1,R2
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
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
Timer1_Init
...
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 105 / 182
Timing generation and measurements 6.6 Advanced-control timer registers
MOV32 R2,#TIM_BDTR_MOE
ORR R1,R2
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
; 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
Example 6.12
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
INCLUDE ../../../inc/stm32G431xx.s
TIM1_PSC_VALUE EQU 0
TIM1_PERIOD_VALUE EQU 2000
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
GPIO_Init
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 143 / 182
Timing generation and measurements 6.6 Advanced-control timer registers
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 144 / 182
Timing generation and measurements 6.6 Advanced-control timer registers
MOV32 R2,#GPIO_AFSEL8_2+GPIO_AFSEL9_2
... +GPIO_AFSEL10_2 ; AF6
ORR R1,R2
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
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
STR R1,[R0]
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 147 / 182
Timing generation and measurements 6.6 Advanced-control timer registers
MOV R1,#TIM1_PERIOD_VALUE
STR R1,[R0]
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 148 / 182
Timing generation and measurements 6.6 Advanced-control timer registers
MOV32 R2,#TIM_CCMR1_OC1M_2+TIM_CCMR1_OC2M_2
ORR R1,R2
STR R1,[R0]
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 149 / 182
Timing generation and measurements 6.6 Advanced-control timer registers
LDR R0,=TIM1_CCER
LDR R1,[R0]
MOV32 R2,#TIM_CCER_CC1NE+TIM_CCER_CC2NE
... +TIM_CCER_CC3NE
ORR R1,R2
STR R1,[R0]
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 150 / 182
Timing generation and measurements 6.6 Advanced-control timer registers
LDR R1,[R0]
MOV32 R2,#TIM_CCER_CC1E+TIM_CCER_CC2E
... +TIM_CCER_CC3E
ORR R1,R2
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
; 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:
INCLUDE ../../../inc/stm32G431xx.s
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
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
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
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]
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]
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]
; 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:
Timer2_Init
; Timer 2 RCC enable
LDR R0,=RCC_APB1ENR1
LDR R1,[R0]
ORR R1,#RCC_APB1ENR1_TIM2EN
STR R1,[R0]
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]
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]
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
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
Solution:
Code 7.1
INCLUDE ../inc/stm32G431xx.s
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
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
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
LDR R1,[R0]
ORR R1,#RCC_GPIOA_EN
STR R1,[R0]
NOP
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 77 / 203
Analog interfacing 7.2 ADC registers
LDR R1,[R0]
ORR R1,#RCC_GPIOB_EN
STR R1,[R0]
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 78 / 203
Analog interfacing 7.2 ADC registers
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
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 79 / 203
Analog interfacing 7.2 ADC registers
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 80 / 203
Analog interfacing 7.2 ADC registers
BIC R1,#ADC_SQR1_L
STR R1,[R0]
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 81 / 203
Analog interfacing 7.2 ADC registers
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 82 / 203
Analog interfacing 7.2 ADC registers
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
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
Solution:
Code 7.2
INCLUDE ../inc/stm32G431xx.s
...
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
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]
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 86 / 203
Analog interfacing 7.2 ADC registers
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
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 87 / 203
Analog interfacing 7.2 ADC registers
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 ;
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 88 / 203
Analog interfacing 7.2 ADC registers
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
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
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
Solution:
Code 7.3
INCLUDE ../inc/stm32G431xx.s
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
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
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
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
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
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
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
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
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
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
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
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
Solution:
Code 7.5
INCLUDE ../../inc/stm32G431xx.s
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
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
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]
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 106 / 203
Analog interfacing 7.2 ADC registers
MOV R1,#50
UDIV R6,R2,R1
MOV R2,R6
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 107 / 203
Analog interfacing 7.2 ADC registers
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
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
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
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
LDR R0,=RCC_AHB2ENR
LDR R1,[R0]
ORR R1,#RCC_GPIOA_EN
STR R1,[R0]
NOP
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 112 / 203
Analog interfacing 7.2 ADC registers
; No pull-down resistor
LDR R0,=GPIOA_PUPDR
LDR R1,[R0]
BIC R1,#GPIO_PUPD0+GPIO_PUPD1
STR R1,[R0]
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 113 / 203
Analog interfacing 7.2 ADC registers
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
ORR R1,#RCC_ADC12_EN
STR R1,[R0]
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 115 / 203
Analog interfacing 7.2 ADC registers
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 116 / 203
Analog interfacing 7.2 ADC registers
ORR R1,#ADC_SMPR1_SMP1
STR R1,[R0]
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 117 / 203
Analog interfacing 7.2 ADC registers
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 118 / 203
Analog interfacing 7.2 ADC registers
ORR R1,#ADC_CR_ADVREGEN
STR R1,[R0]
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 119 / 203
Analog interfacing 7.2 ADC registers
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
ALIGN
END
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 121 / 203
Analog interfacing 7.2 ADC registers
Example 7.6
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 122 / 203
Analog interfacing 7.2 ADC registers
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
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
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
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
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]
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]
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]
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]
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
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 185 / 203
Analog interfacing 7.4 DAC registers
VLDR.F S0,[R0]
VMUL.F S5,S3,S0 ; ki.e
VADD.F S5,S5,S7 ; S5 stores the I part
; 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
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
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
Solution:
Code 8.1
INCLUDE ../inc/stm32G431xx.s
AREA STACK,NOINIT,READWRITE,ALIGN=3
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 37 / 135
Serial communication interface 8.2 USART registers
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 38 / 135
Serial communication interface 8.2 USART registers
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
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 40 / 135
Serial communication interface 8.2 USART registers
BX LR
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 41 / 135
Serial communication interface 8.2 USART registers
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
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 42 / 135
Serial communication interface 8.2 USART registers
BIC R1,#USART_CR1_M0+USART_CR1_M1
BIC R1,#USART_CR1_OVER8 ; Oversampling 16
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
ALIGN
END
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 44 / 135
Serial communication interface 8.2 USART registers
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
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
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
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
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 50 / 135
Serial communication interface 8.2 USART registers
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
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
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]
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]
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]
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
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]
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]
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
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
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
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 45 / 205
Programming Microcontrollers in C Interface programming in C
GPIO programming
#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:
#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);
}
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"
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);
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);
}
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 (;;) {
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 59 / 205
Programming Microcontrollers in C Interface programming in C
GPIO programming
Nguyen Tien Hung (TNUT) Microcomputer principles and applications Academic year 2023-2024 60 / 205
Programming Microcontrollers in C Interface programming in C
GPIO programming
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"
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 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; //
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"
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 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"
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 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;
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"
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 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);
void Timer1_Init(void){
RCC->APB2ENR |= RCC_APB2ENR_TIM1EN;
TIM1->PSC = TIM1_PSC_VALUE;
TIM1->ARR = TIM1_PERIOD_VALUE;
TIM1->CNT = 0;
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);
// 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 INCREMENT 5
#define DUTYCYCLEMAX 999-INCREMENT
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
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 GPIO_Init(void){
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOAEN;
delay(5);
GPIOA->MODER &= ˜(GPIO_MODER_MODE8+GPIO_MODER_MODE9);
delay(5);
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);
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);
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;
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 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"
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
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 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 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"
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 GPIO_Init(void){
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOAEN;
delay(5);
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
void Timer1_Init(void){
RCC->APB2ENR |= RCC_APB2ENR_TIM1EN;
TIM1->PSC = TIM1_PSC_VALUE;
TIM1->ARR = TIM1_PERIOD_VALUE;
TIM1->CNT = 0;
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
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
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;
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
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 GPIO_Init(void){
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOAEN;
delay(5);
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);
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOBEN;
delay(5);
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;
TIM1->PSC = TIM1_PSC_VALUE;
TIM1->ARR = TIM1_PERIOD_VALUE;
TIM1->CNT = 0;
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);
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"
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;
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
// 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
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
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
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
Solution:
Code 9.14
#include "main.h"
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
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 (;;) {
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
// 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
}
}
void GPIO_Init(void){
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOCEN;
delay(5);
// 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
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOBEN;
delay(5);
void ADC1_Init(void){
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
// 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
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
// Enable ADC1
ADC1->CR |= ADC_CR_ADEN;
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
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
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
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
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
Solution:
Code 9.15
#include "main.h"
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
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
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 (;;) {
// 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
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;
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
displayVelocity = velocity*100;
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
}
}
DAC1->DHR12R1 = controlValue;
displaySABSValues();
delay(50000);
}
}
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
// No pull-down resistor
GPIOC->PUPDR &= ˜(GPIO_PUPDR_PUPD0+GPIO_PUPDR_PUPD1);
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOBEN;
delay(5);
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
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
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
// Enable ADC1
ADC1->CR |= ADC_CR_ADEN;
void DAC1_Init(void){
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
RCC->AHB2ENR |= RCC_AHB2ENR_DAC1EN;
// Trigger disable
DAC1->CR &= ˜DAC_CR_TEN1;
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
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
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 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
void USART2_Init(void){
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
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"
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 GPIO_Init(void){
RCC->AHB2ENR |= RCC_AHB2ENR_GPIOBEN;
delay(5);
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);
void SPI2_Init(void){
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
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);
}
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