Skip to content

This project uses STM32CubeIDE and it's a program created to practice my C habilities during the course 'Microcontroller Embedded C Programming: Absolute Beginners' from FastBit.

Rafaelatff/target-LedOn

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

target-LedOn

This project uses STM32CubeIDE and it's a program created to practice my C habilities during the course 'Microcontroller Embedded C Programming: Absolute Beginners' from FastBit Embedded Brain Academy.

Section 18: Embedded C coding exercise for LED

Hardware = NUCLEO-F401RE (differs from the used in course)

First, let's find an avaible LED to control:

image

Just checked the board and there is a LD2, and jumpers SB21 and SB42 are connected (by a 0R 0805 resistor). This means, that to control this LED, PA5 must be used. This means, GPIO A (port A) and pin 5.

For the firmware, we have 3 important steps: Configure to clock to control GPIOA, configure Mode for pin 5 to work as output, and finally control the signal of the output (0 or 1).

For the clock, we need to find the clock path for the GPIOA. This information is available in the datasheet of STM32F401x. See:

image

Now, we need to find the register address and the bits that we need to set. For that, I used the RM0368 Reference Manual (STM32F401x). Check that the section that displays the available configurations for the RCC AHB1 register doesn't show the address:

image

To find the register address, we must sum the 'Address offset' of 0x30 to the initial register boundary addresses, given in the Memory map section of the RM0368 Reference Manual (STM32F401x). Check the ones that we will use:

image

So, we are using as register address: 0x4002 3800 + 0x30 = 0x4002 3830. Basically, the lines in code that accomplish this mission are:

image

Now, we can go to the second step of programming the firmware. Setting the pin 5 as output in the mode register.

image

Let's just clear the bits from MODER5 (10 and 11) of the register GPIOA_MODER. To find the register address, I summed the Address offset to the GPIOA initial boundary address. Being 0x4000 0000 + 0x00 (offset) = 0x4000 0000. Then, I set only the bit 10 of the register, leaving the MODER5 as 01: General purpose output mode. The lines in code that accomplishes this function are:

image

And finally the third step, setting the pin 5 of port A. The register address that control the pin's output is found by summing the Address offset of 0x14 (from GPIOx_ODR) to the inital boundary value of the memory map for GPIOA. In that case we have 0x40020000 + 0x14 = 0x40020014.

image

The code lines for this are:

image

As an option, the following code was written but using the bitwise shift operator.

image

Now, let's debug and test this code.

Final code:

image

To visualize the register addresses, we need to go in Window -> Show View -> SFRs.

image

We can reset application to garante that it is in the begin (I), then we can control and follow the instructions by going step by step (II). Just after it passes by the 21 line (and stops at line 26), it is possible to see that the the register RCC - AHB1ENR - GPIOAEN was modified.

image

Two more steps, passing througt lines 26 and 29 (stopped at line 33), we have cleared and set the register MODER - MODER5.

image

And finally, the line that is responsible for lighting the LD2. Line 33:

image

Final result

image

About

This project uses STM32CubeIDE and it's a program created to practice my C habilities during the course 'Microcontroller Embedded C Programming: Absolute Beginners' from FastBit.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published