0% found this document useful (0 votes)
10 views7 pages

Laboratory Exercise 5 - Peripherals, GPIO

Uploaded by

JNZSeries
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views7 pages

Laboratory Exercise 5 - Peripherals, GPIO

Uploaded by

JNZSeries
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Karlsruhe University of Applied Sciences Laboratory Microcontroller Systems

Faculty EIT Laboratory Exercise 5

Laboratory Exercise 5: Peripherals, GPIO

1.) Task description

For control tasks, microcontrollers must communicate with their environment.

Peripheral input and output devices, which are set (configured) via Special Function Registers
(SFR), serve for this purpose.

These SFRs differ fundamentally for each type of microcontroller.

Therefore, a large part of the documentation in the data sheets serves to describe these peripherals
and their use.

In this and the following exercises, we will take a closer look at the peripherals units built into our
microcontroller.

We will start with the so-called general purpose registers (GPIO), which can be used to directly
control LEDs or read the states of buttons.

2. Task
Task 1: LED test pattern
Unlike other microcontrollers, ARM controllers use so-called memory mapped peripherals, i.e. the
SFR are addressed like main memory; they are mapped to defined memory addresses and can thus
be read and written to like internal RAM memory.

The SFR are all located in the upper address space (0xE0000000 - 0xFFFFFF).

When using the periphery as GPIO pins, the data direction (input or output) is defined.

Other SFRs are used to configure and control operation.

Additional other SFRs define the operating mode of the input and output pins.

With a limited number of pins, these can take on various tasks, e.g. serial interfaces (COM port,
CAN, SPI, I2C) or inputs of A/D converters.

The SFR PINSEL0 and PINSEL1 and PINSEL2 (Pin function Select) determine the use of ports
0 and 1.

Although 32-bit SFRs are available for each port, port 0 has only 30 pins (P0.0 - P0.25 and P0.27 -
P0.30), port 1 even has only 21 pins (P1.16 - P1.36).

Christian Langen, Laboratory Exercise 5 - Peripherals, GPIO.odt, 13.07.2020, Rev 2.03


Page 1 from 7
Karlsruhe University of Applied Sciences Laboratory Microcontroller Systems
Faculty EIT Laboratory Exercise 5

Since up to four different functions can be assigned to the individual pins, the three 32-bit pin select
registers are used to configure the two 32-bit ports.

Since the ports are "by default" configured as GPIOs, we do not need to configure the PINSEL
registers for this exercise.

Four registers are used to configure the GPIO ports, each separately for port 0 and port 1:

1.) GPIO port Pin value register IOPIN: Contains the value that is present at a port bit.

2.) GPIO port Direction register IODIR: Direction of the port pin, 0: input or 1: output

3.) GPIO port Output Set register IOSET: A '1' sets the port pin to 'HIGH'.

4.) GPIO port Output Clear register IOCLR: A '1' sets the port pin to 'LOW'.

Note: Setting the value zero (0) in one of the bits of the last two registers has no effect, the value
that has been set previously is retained.

This peculiarity is common with ARM processors and initially takes some getting used to.

The buttons on our experimental hardware are connected to port 0 and the LEDs to port 1.

After reset, the pins are set to 'GPIO' and 'input' by default.

With this knowledge, we can now set the registers and their associated addresses.

. equIOPIN0, 0xE0028000 // Port 0 Pin value register address

. equIODIR1, 0xE0028018 // Port 1 Direction register address

. equIOSET1, 0xE0028014 // Port 1 Output set register address

. equIOCLR1, 0xE002801C // Port 1 Output clear register

At the beginning of the program, we set the base address of the stack to 0x40001000 as usual.

We use register r1 as a pointer to the IODIR1 register:

ldr r1, =IODIR1

We use pins P1.16 to P1.23 as outputs to which eight LEDs are connected:

ldr r0, =0x00ff0000

and save this setting via the "pointer" r1:

Christian Langen, Laboratory Exercise 5 - Peripherals, GPIO.odt, 13.07.2020, Rev 2.03


Page 2 from 7
Karlsruhe University of Applied Sciences Laboratory Microcontroller Systems
Faculty EIT Laboratory Exercise 5

str r0, [r1]

Firstly, we set only the least significant "LED bit" (bit 16) to high (= 1):

ldr r0, =0x10000

Via pointer r2, the GPIO register bits are set in a loop through register IOSET1, where we
increment the "value" of the LED bits in a loop.

Since the bits are not cleared within the loop (writing the value 0 to the IOSET1 register has no
effect), the LEDs - once activated - are no longer switched off.

ldrr2, = IOSET1

strr0 , [r2]

Incrementing is done by a mov command in conjunction with a logical left shift by 1:

mov r0, r0, lsl #1

The use of a "delay" allows the program to be executed in real-time (simply run and count up).

2.) Task 1: Implement the LED test pattern as a sub-program.

Firstly test the function without delay in single steps and execute it with delay in real time if
the function is correct.

3.) Task 2: Test pattern for controlling individual I/O (input/output) bits

In the next step, we want to switch the LEDs on and off individually.

For this purpose we have to use the IOSET1 and IOCLR1 registers.

After initializing the stack pointer and the I/O registers as well as the data direction just as described
above, we use the registers r2 and r3 to access the two peripheral registers IOSET1 and IOCLR1.

We store the start value for the LED pattern, which is again 0x10000, in register r0.

Via pointer r2, we switch on again only the leading bit of the counter value in a loop by
immediately switching off the bit just set via pointer r3, which points to IOCLR1, and double the
counter value by one bit by shifting it to the left:

str r0, [r3]


mov r0, r0, lsl #1

Christian Langen, Laboratory Exercise 5 - Peripherals, GPIO.odt, 13.07.2020, Rev 2.03


Page 3 from 7
Karlsruhe University of Applied Sciences Laboratory Microcontroller Systems
Faculty EIT Laboratory Exercise 5

To start over after setting the last LED, we test the value of r4 (the code below looks a little bit
esoteric – wait a minute and think about what is happening here, the value #0x01 is rotated right
by 8 bit):

cmp r0, #0x01, 8 // alternative notation for cmp r0, #0x1000000

This is done before we increase the counter value.

If it has not been reached the eventual value yet, we increase it, otherwise we start the whole
process from the beginning by setting the start value 0x10000 for the LED pattern in register r0.

Task:

Implement the LED test pattern for controlling individual I/O bits. Firstly test the function
without delay in single steps and execute it with delay in real time if the function is correct.

4.) Task 3:
Input and output via buttons and LEDs

The four push buttons are connected to pins P0.10 to P0.13.

After a reset, P0 is automatically configured as an input, so no further direction switching is


necessary.

To simplify matters, we define bit masks for the GPIO bits corresponding to the push buttons:

. equ BUTTON_1, 0x00000400 // Button 1 mask

. equ BUTTON_2, 0x00000800 // Button 2 mask

. equ BUTTON_3, 0x00001000 // Button 3 mask

. equ BUTTON_4, 0x00002000 // Button 4 mask

Now we assign the masks of the buttons 1 to 4 to the registers r5 to r8.

Again we use r2 and r3 as pointers to the IOSET1 and IOCLR1 registers, and we use register r9 to
hold the values to be switched on.

We use register r4 (= IOPIN0) to read in the buttons.

ldr r1, =IODIR1 // point to port 1 direction register

ldr r2, =IOSET1 // pointer to port 1 pin set control register


ldr r3, =IOCLR1 // pointer to port 1 pin clear control register

Christian Langen, Laboratory Exercise 5 - Peripherals, GPIO.odt, 13.07.2020, Rev 2.03


Page 4 from 7
Karlsruhe University of Applied Sciences Laboratory Microcontroller Systems
Faculty EIT Laboratory Exercise 5

ldr r4, =IOPIN0 // pointer to port 0 input register

ldr r5, =BUTTON_1 // load Button 1 mask to register r1

ldr r6, =BUTTON_2 // load Button 2 mask to register r2

ldr r7, =BUTTON_3 // load Button 3 mask to register r3

ldr r8, =BUTTON_4 // load Button 4 mask to register r4

ldr r0, =0x00ff0000 // set P1.16 to P1.23 as ...

str r0, [r1] // ... outputs

Please note that pressing a push button switches the input pin to ground (active low)!

Christian Langen, Laboratory Exercise 5 - Peripherals, GPIO.odt, 13.07.2020, Rev 2.03


Page 5 from 7
Karlsruhe University of Applied Sciences Laboratory Microcontroller Systems
Faculty EIT Laboratory Exercise 5

The following program excerpt switches the first two LEDs by pressing button 1.

Firstly we switch on the first LED again, this time via register r9:

mov r9, #0x10000

ldr r0, [r4] // Load pushbutton values to register r0 (IOPIN0)

ands r0, r5, r0 // check if button 1 is pressed

bne noled1 // branch if not

str r9, [r2] // button pressed, turn selected pin on (IOSET1)

mov r9, r9, lsl #1 // increment output value

str r9, [r3] // turn selected pin off (IOCLR1)

b led1_done

noled1: // button not pressed

str r9, [r3] // turn selected pin off

mov r9, r9, lsl #1 // increment output value

strr9 , [r2] // turn selected pin on

led1_done:

Tasks:

1. Firstly implement the program for switching between LED0 and LED1 using button 1.

2. If this part of the program works, implement the switching of the other LED pairs
through buttons 2 to four.

3. Make sure that you use the correct bit masks for the LEDs and increment the output
value with the appropriate "bit shift".

4. In the last step, you outsource the query of the individual buttons and the control of
the LEDs to individual subroutines, which you call up in the main routine with (bl =
Branch and Link):

loop:
bl read_button_1

Christian Langen, Laboratory Exercise 5 - Peripherals, GPIO.odt, 13.07.2020, Rev 2.03


Page 6 from 7
Karlsruhe University of Applied Sciences Laboratory Microcontroller Systems
Faculty EIT Laboratory Exercise 5

bl read_button_2

bl read_button_3

bl read_button_4

b loop

Note: Do not forget to use the stack within each of the subroutines to store the link register
(lr), the value of which must be copied back into the program counter (pc) after the
subroutine has ended.

Congratulations, you have successfully completed exercise 5. You can now configure our
microcontroller for simple input and output operations.

Christian Langen, Laboratory Exercise 5 - Peripherals, GPIO.odt, 13.07.2020, Rev 2.03


Page 7 from 7

You might also like