Lesson 1: Part 3: Controlling GPIO - On-Board Output - LED Control

In this section we will be using what we learn from Part 1 and write some C code. Recite from Part 1, 3 configurations is require as below:
  1. Enable GPIO section for respective pin
    • SFR: RCC_AHBENR , bit: IOPCEN, value: 1
  2. Pin to be output function
    • SFR: GPIOC_MODER, bit: MODER9, value: 01
  3. Control the pin to be high state or low state
    • High state: SFR: GPIOC_ODR, bit: ODR 9, value: 1
    • Low state:  SFR: GPIOC_ODR, bit: ODR 9, value: 0
In order to access SFR in C language, we need to include a header files :
  •  #include stm32f0xx.h
 Open content of stm32f0xx.h by right click on source code file name, select 'Open document' as shown below:
Open SFR Define File
Inside the SFR files define all the SFR that we use in source code below.

Then the corresponding C code for 3 cases above are:
  1. Enable GPIO section for respective pin
    • RCC->AHBENR |= RCC_AHBENR_GPIOCEN;
  2.  Pin to be output function
    • GPIOC->MODER &= ~GPIO_MODER_MODER9;  //mask the bit to 00
    • GPIOC->MODER |= MODE_OUTPUT;         //set the bit
  3.  Control the pin to be high state or low state
    • High state:
      • GPIOC->ODR |= GPIO_ODR_9;
    • Low state:
      • GPIOC->ODR &= ~GPIO_ODR_9;
Below is the image for whole source code. You can either type out or just use Git link here.

Full Source Code To Control LD3

Real life applications:

  1. With proper hardware circuit, instead of turn on/off LED, we can turn on/off fan, light.
  2. Replacing the LED with IR transmittor, and with correct timing on turning on/off, we can use it as remote control, to control electrical appliances: TV, DVD.

Exercise

  1. In the source code we have turn on LED and turn off LED, why the LED is constantly turn on when program is running?
  2. Repeating Part 1 to 3 process, this time try controlling LD4 LED






2 comments:

  1. Hi, Cheng

    I have built the source code for controlling LD3. Referring to the first question in Exercise, I observed that the board seems to be still running the example software in "Pre-Lesson1: Part 6: Building First Example". Is there something that I should set so that the board runs the new build?

    Also, could you briefly explain how the build gets "downloaded" to the board (if that is the right term to use)? Which part in the settings instructs the IDE to do that? I have in mind still the universal programmer and have not quite understood how this is done with just a USB cable.

    ReplyDelete
    Replies
    1. To program the code, you can use one of the the following method
      1. In Menu bar, select 'Flash' then 'Download'
      2. In Menu bar, select 'Debug' then 'Start/Stop Debug Session'
      Usually after the development we will start debug session to check out code. Thus, I use method 2 all the time.

      Once you execute above step, you code will be program into the flash and subsequent power up, the new code will be running (without debugger)

      You can explore more on programmer section by select in menu bar 'Flash' --> 'Configure Flash Tools' --> Settings. The new windows will shown some flash config. The build binary file is transfer to ST-LINK (the debugger) and then from ST-LINK get program into the flash. Most of the work has been taking care by the ST-LINK, we just need to ensure the correct debugger is being selected.

      Delete