Professional Documents
Culture Documents
Mct-222 Embedded Systems: Lecture 4: Gpio, Branches and Systick Timer
Mct-222 Embedded Systems: Lecture 4: Gpio, Branches and Systick Timer
Embedded Systems
6-1
LED interfaces
❑ The TM4C1294 can output (or sink? see datasheet) maximum of 12 mA current.
❑ Large number of LEDs or LED needing higher large current (i.e. > 12 mA) may draw
excessive current and damage the microcontroller when directly connected to the PORT
pins
❑ Instead we insert buffers (e.g. ULN2003, 7407) to interface high current LED(s) (or
other peripheral)
6-2
LED interfaces
OR
• GPIO_PORTx_PUN_R
To enable internal pull-up resistance on PORTx
Example
6-6
Code bits: pre-requisite of conditional branches
❑ e.g.
6-7
Code bits: pre-requisite of conditional branches
Suffix Code bit Meaning
EQ Z=1 Equal
NE Z=0 Not equal
CS or HS C=1 Higher or same, unsigned ≥
CC or LO C=0 Lower, unsigned <
MI N=1 Negative
PL N=0 Positive or zero
VS V=1 Overflow
VC V=0 No overflow
HI C = 1 and Z = 0 Higher, unsigned >
LS C = 0 or Z = 1 Lower or same, unsigned ≤
GE N=V Greater than or equal, signed ≥
LT N≠V Less than, signed <
GT Z = 0 and N = V Greater than, signed >
LE Z = 1 and N ≠ V Less than or equal, signed ≤
AL Can have any value Always. This is the default when no suffix is
specified.
BEQ some_instr_label
If Z == 1, go to instruction
whose label is some_instr_label
Suffix: EQ checks if Z = 1
6-9
Example: Conditional Branch Instruction
6-10
Conditional Branch Instructions
❑Unsigned conditional branch
❖follow SUBS CMN or CMP
BEQ target ; Branch if Equal(unsigned/signed)
BNE target ; Branch if not Equal (unsigned/signed)
BLO target ; Branch if unsigned less than
BLS target ; Branch if unsigned less than or equal to
BHS target ; Branch if unsigned greater than or equal to
BHI target ; Branch if unsigned greater than
CMP R0,R1
R0<R1
BLO
R0≥R1 target
Next instruction
6-11
Conditional Branch Instructions
CMP R0,R1
R0<R1
BLT
R0≥R1 target
Next instruction
6-12
Equality Test
6-13
Unsigned Conditional Structures
Program 2.7.6
6-16
While Loops
6-17
For Loops
6-18
For Loops
Count up
Count down
6-19
SysTick Timer
❑Timer/Counter operation
❖24-bit counter decrements at bus clock
frequency
o With 80 MHz bus clock, decrements every 12.5 ns
❖Counting is from n → 0
o Setting n appropriately will make the counter a
modulo n+1 counter. That is:
➢next_value = (current_value-1) mod (n+1)
➢Sequence: n,n-1,n-2,n-3… 2,1,0,n,n-1…
6-20
SysTick Timer
❑ Initialization (4 steps)
❖ Step1: Clear ENABLE to stop counter
❖ Step2: Specify the RELOAD value
❖ Step3: Clear counter by reading NVIC_ST_CURRENT_R
❖ Step4: Set NVIC_ST_CTRL_R
o CLK_SRC = 1 (bus clock)
o INTEN = 0 for no interrupts
o ENABLE = 1 to enable
6-21
SysTick Timer
SysTick_Init
; disable SysTick during setup 24-bit Countdown Timer
LDR R1, =NVIC_ST_CTRL_R
MOV R0, #0 ; Clear Enable
STR R0, [R1]
; set reload to maximum reload value
LDR R1, =NVIC_ST_RELOAD_R
LDR R0, =0x00FFFFFF; ; Specify RELOAD value
STR R0, [R1] ; reload at maximum
; writing any value to CURRENT clears it
LDR R1, =NVIC_ST_CURRENT_R
MOV R0, #0
STR R0, [R1] ; clear counter
; enable SysTick with core clock
LDR R1, =NVIC_ST_CTRL_R
MOV R0, #0x0005 ; Enable but no interrupts (later)
STR R0, [R1] ; ENABLE and CLK_SRC bits set
BX LR
6-22
SysTick Timer
❖ Once Timer has counted all the way from RELOAD value
to 0, it sets COUNT flag and start over to countdown
from RELOAD value. Timer keeps running…
;------------SysTick_Wait------------
; Time delay using busy wait.
; Input: R0 delay parameter in units of the core clock
; 80 MHz(12.5 nsec each tick)
; Output: none
; Modifies: R1
SysTick_Wait
SUB R0, R0, #1 ; delay-1
LDR R1, =NVIC_ST_RELOAD_R
STR R0, [R1] ; time to wait
LDR R1, =NVIC_ST_CURRENT_R
STR R0, [R1] ; any value written to CURRENT clears it
LDR R1, =NVIC_ST_CTRL_R
SysTick_Wait_loop
LDR R0, [R1] ; read status
ANDS R0, R0, #0x00010000 ; bit 16 is COUNT flag
BEQ SysTick_Wait_loop ; repeat until flag set
BX LR
6-24
We have just developed a routine
SysTick_Wait
6-25
SysTick Timer
;------------SysTick_Wait10ms------------
; Call this routine to wait for R0*10 ms
; Time delay using busy wait. This assumes 80 MHz clock
; Input: R0 number of times to wait 10 ms before returning
; Output: none
; Modifies: R0
DELAY10MS EQU 800000 ; clock cycles in 10 ms
SysTick_Wait10ms
PUSH {R4, LR} ; save R4 and LR
MOVS R4, R0 ; R4 = R0 = remainingWaits
BEQ SysTick_Wait10ms_done ; R4 == 0, done
SysTick_Wait10ms_loop
LDR R0, =DELAY10MS ; R0 = DELAY10MS
BL SysTick_Wait ; wait 10 ms
SUBS R4, R4, #1 ; remainingWaits--
BHI SysTick_Wait10ms_loop ; if(R4>0), wait another 10 ms
SysTick_Wait10ms_done
POP {R4, PC}
6-26
Thank You
Q&A
6-27