Lesson 1: Part 1: Controlling GPIO - On-board Output - SFR Study

Introduction

As shown in the last video in 'Building First Example', there is 2 on-board LED, namely LD3 and LD4. In this lesson, we will be learning on controlling these LED.

LED typcial serve as system indicator to the user. It can be use to represent the current status of the process, current state of the system. For example, red LED in TV means the TV is in system standby. Or a light up LED in washing machine means that washing machine is power up.

Working Principles

Typical MCU will generally have pin that can be configure to input or output base on the software configuration. This is generally known as GPIO - General Purpose Input Output. When a pin is configure as output pin, software can control this pin to act as high state(making the pin to be 3.3V) or low state(pin to be 0V Ground). By changing the pin voltage between 3.3V, we are essentially turning on the LED and by 0V, turning off LED. There is more options we can control on GPIO, which is ignored to ease the explanation, you and refer to reference manual for detail description at GPIO sections.

In addition, MCU GPIO section can be enable/disable by software. This feature enable Engineer to disable unused section in order to reduce power consumption.

As summary, in order to control LED we need to configure:
  1. Enable GPIO section for respective pin
  2. Pin to be Output function
  3. Control the pin to be high state or low state
Lets take LD3 as an example.

Step 1: Finding Which MCU Pin Is Actually Controlling LD3

  1. To do this, we need the discovery board schematic, download from here, look for 'schematic pack'.
  2. From schematic PDF file, we can know LD3 is control by PC9, which is connected to MCU PC9 pin(refer to image below). PC9 is the keyword we need to use when programming this pin. 
  3. PC9 let us know it is at Port C, pin 9. These information is critical in deciding which SFR to use, which will demonstrate below.
LD3 -> PC9 -> Port-C, Pin9

Step 2: Enable PC9 GPIO Function

  1.  We need another document that would tell us how to enable GPIO function. This document is like that handbook of MCU which explains:
    • The system design of the MCU
    • The configurations and features for MCU peripheral
    • Some documents may provides example
  2. This handbook is the main reference while programming MCU. For this example, download it from here, refer to 'Reference Manual' section.
  3.  From the reference manual, refer to register name RCC_AHBENR.
    • SFR name: RCC_AHBENR, this will be the name we will be using in programming later on
    • Bit 19:  IOPCEN (We need to enable this bit to enable GPIO Port C9 functionality. In fact, once enable this block, all pin in PC0-PC15 will be enable)

Enable Bit IOPCEN In Register RCC_AHBENR

Step 3: Configure Pin To Be Output Pin

  1. Using the reference manual as in Step 2 above, this control is under SFR name is GPIOx_MODER
    • As shown in description, we need a value of '01' to configure pin as general purpose output mode.
    • Since out pin is PC9, so we should refer to MODER9, which is at bit 18 and 19. Thus we should be setting bit 18 to value 1, bit 19 to value 0.
Set MODER9 To '01' In Register GPIOC_MODER

 Step 4: Controlling Output Pin High(On LED) and Low (Off LED)


Setting bit ODR 9 '1' Or '0' In Register GPIOC_ODR

With these information in place, we can proceed creating empty project and start writing code, which will be explain in next section.

3 comments:

  1. Hi, Cheng

    This may be a trivial question but I just need some verification here: In Step 3, bit 18 is set to 1 and bit 19 to value 0. This means that, in the reference manual, the value '01' is read as 0 (MSB) and 1 (LSB)?

    ReplyDelete
  2. If you refer to SFR table, the description is MODER9[1:0], which correspond to [19:18]. So you are correct 0(MSB) and 1(LSB).

    ReplyDelete
  3. Thanks for the verification. Always wondered what [1:0] means.

    ReplyDelete