ARM Embedded Tutorial - Simple GPIO on and off output: what changed
Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.
Title: ARM Embedded Tutorial - Simple GPIO on and off output
| Old | New | Line | |
|---|---|---|---|
| 1 | + | # Easy GPIO input and output | |
| 2 | + | ||
| 3 | + | A port consists of **16 pins**, which is why the lower **16 bits** are used in most registers. | |
| 4 | + | ||
| 5 | + | Exception: **CRx** and **BSRR**, with **BSRR** you can use the upper 16 bits to set pins. | |
| 6 | + | ||
| 7 | + | ## Port access registers | |
| 8 | + | ||
| 9 | + | The following registers are available for GPIO access: | |
| 10 | + | ||
| 11 | + | - **CRL** - Configuration Register Low | |
| 12 | + | - **CRH** - Configuration Register High | |
| 13 | + | - **IDR** - Input Data Register | |
| 14 | + | - **ODR** - Output Data Register | |
| 15 | + | - **BSRR** - Bit Set Reset Register | |
| 16 | + | - **BRR** - Bit Reset Register | |
| 17 | + | - **LCKR** - Port Configuration Lock Register | |
| 18 | + | ||
| 19 | + | With all registers except **CRL** and **CRH**, each bit corresponds to a microcontroller pin. | |
| 20 | + | ||
| 21 | + | ## Switch on GPIO | |
| 22 | + | ||
| 23 | + | Before you can access the GPIO, you have to switch it on. Without doing this, no access is possible! This is done as follows for the PortA - PortC. | |
| 24 | + | ||
| 25 | + | ``` pascal | |
| 26 | + | RCC.APB2ENR := RCC.APB2ENR or (% 111 shl 2); | |
| 27 | + | ``` | |
| 28 | + | ||
| 29 | + | ## Direct port manipulation | |
| 30 | + | ||
| 31 | + | Before you can switch a GPIO pin to HIGH, you have to configure it as an output. This is done via the **CRL** and **CRH** registers. | |
| 32 | + | ||
| 33 | + | I usually choose **Pin 13 of Port C** for these mini-examples, because the LED of the STM32F103C is connected to this pin. | |
| 34 | + | ||
| 35 | + | Attention: The LED lights up as the anode is connected to the power supply. | |
| 36 | + | ||
| 37 | + | ### **CRx** - Set pin function | |
| 38 | + | ||
| 39 | + | One nibble (4 bit) is available for the function setting. Pin 13 of Port C on output. Since **13** is higher than **7**, the HIGH register CRH is used for this. **13 - 8 = 5;** It is the fifth nibble in **CHR**. Pin0 - Pin7 would be configured with **CHL**. | |
| 40 | + | ||
| 41 | + | ``` pascal | |
| 42 | + | PortC.CRH := $00300000; | |
| 43 | + | ``` | |
| 44 | + | ||
| 45 | + | ### **IDR** - Read out entire port | |
| 46 | + | ||
| 47 | + | ``` pascal | |
| 48 | + | // Is Pin 13 HIGH? | |
| 49 | + | Result := PortC.ODR and (1 shl 13) > 0; | |
| 50 | + | ``` | |
| 51 | + | ||
| 52 | + | ### **ODR** - Write entire port | |
| 53 | + | ||
| 54 | + | With this function, the whole port is written at once. | |
| 55 | + | ||
| 56 | + | ``` pascal | |
| 57 | + | // Only pin13 is set to HIGH. | |
| 58 | + | PortC.ODR := 1 shl 13; | |
| 59 | + | ||
| 60 | + | // All pin on LOW. | |
| 61 | + | PortC.ODR := 0; | |
| 62 | + | ``` | |
| 63 | + | ||
| 64 | + | ### **BSRR** - Single pin to LOW | |
| 65 | + | ||
| 66 | + | With this function, you can set individual pins to **LOW**. | |
| 67 | + | ||
| 68 | + | ``` pascal | |
| 69 | + | // Only set pin13 to LOW, the others remain unchanged. | |
| 70 | + | PortC.BSRR := 1 shl 13; | |
| 71 | + | ``` | |
| 72 | + | ||
| 73 | + | ### **BSRR** - Change the entire port using the bit mask | |
| 74 | + | ||
| 75 | + | With this function, you can set individual pins to **LOW**. | |
| 76 | + | ||
| 77 | + | ``` pascal | |
| 78 | + | // Only set Pin13 to LOW, PIN12 to HIGH, the others remain unchanged. | |
| 79 | + | PortC.BSRR := (1 shl 13) + ((1 shl 12) shl 16); | |
| 80 | + | ``` | |
| 81 | + | ||
| 82 | + | ### **BRR** - Single pin to HIGH | |
| 83 | + | ||
| 84 | + | With this function, individual pins can be set to **HIGH**. | |
| 85 | + | ||
| 86 | + | ``` pascal | |
| 87 | + | // Only set pin13 to HIGH, the others remain unchanged. | |
| 88 | + | PortC.BSRR := 1 shl 13; | |
| 89 | + | ``` | |
| 90 | + | ||
| 91 | + | The upper 16 bits of BSRR correspond to the 16 bits of **BRR**. | |
| 92 | + | ||
| 93 | + | ## See also | |
| 94 | + | ||
| 95 | + | - [[ARM Embedded Tutorials]] Overview | |
| 96 | + | ||
| 97 | + | --- | |
| 98 | + | *From the Free Pascal wiki, page "ARM Embedded Tutorial - Simple GPIO on and off output", as it stood on 2021-02-01 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* | |
| 99 | + | Category: ARM | |
| 100 | + | Category: Embedded | |
| 101 | + | Category: Tutorials |
