08. Arduino for Production!! How to Output to a Pin to Blink an LED on an ARM Microcontroller Part 3
Share
Before we get to turning the LED on and off, I want to go back and mention something about the mode register. This comes from a comment on YouTube and it notes that the register needs to have a 0 and a 1 in it, but we’re not really ensuring there is a 0 in the first bit place, and we want to make sure we do that because somewhere else (maybe in a program) we may have set the register to an analog or alternate function mode where it has a 1 in the first position. To ensure that we have a 0 in the first place, we need to do an “and” and a “not” on the first bit. Let’s take a look at the defined values they give us. By going to the declaration we see there are 3. We have one without a 0 or a 1, one with the 0, and one with the 1. Let’s go ahead and note this in the program for reference. Now we know which modes to use with 11, 01, and 10. If we do an “and not” with the 10, it will turn the first position to a 0, ensuring the first position is a 0. We change the MODER to &= ~ on the mode 1 to do this.
GPIOC ->MODER |= GPIO_MODER_MODER6_0;
GPIOC &= ~(GPIO_MODER_MODE6_1);
We confirm the Otyper is only a single bit, so we do not need to worry about it. The speed is going to be two bits per pin. In this case it is at the 11 so we don’t need to worry about this. The pull up pull down is also 11 so it can be left as is.
Now let’s talk about turning the LED on and off. Let’s turn on the LED using the BSRR register. We’re going to use the same GPIOC structure. There is a BRR and a BSRR, and we’re going to use the BSRR first. Using the “or” bitwise operation |=, we will see where port C is located. The set is at 6 and the reset is at 22.
There are a few ways of doing this. We can actually just type in GPIO like we did before, and because we’re using BSRR, generally you would use the member from the GPIOC as the next statement after the underscore. We’ll try the BSRR here and go to the number 6. You can also do things like doing a shift, 6 positions to the left, but we don’t really need to do that since it is already stated for you (hex number 40).
GPIOC ->BSRR |= GPIO_BSRR_BS_6;
Now we will turn off the LED. We will use the BRR, the bit reset register. We’re going to use the “or” bitwise operation and the bit reset. The BRR should only have the bit reset which is number 6.
GPIOC ->BRR |= GPIO_BRR_BR_6;
Now we need to create the delays. We will make the first function by entering “void” and then “WaitForAMoment” we will then use an integer here. We need a for loop for a time delay. We will initiate the variables beforehand by using i = 0. We will increase the j. Next we will make sure it is a volatile integer. As long as i is less than the moment, the variable that we pass in, it will keep doing it. If it is not doing it, it will escape the for loop and allow the program to go to the next instruction.
void WaitForAMoment(int Moment)
{
for (i=0; i < Moment; i++) {
We’ll include “WaitForAMoment” with a number of 200,000. The while loop ensures it will be a never-ending loop.
WaitForAMoment(200000);
Here is the code up to this point:
// Create function to wait for a specified amount of time
void WaitForAMoment(int Moment)
{
for (i=0; i < Moment; i++) {
int main(void)
{
RCC ->AHBENR |= RCC_AHBENR_GPIOCEN;
// Set any Control Registers for PortC Pin 6
// Moder
GPIOC ->MODER |= GPIO_MODER_MODER6_0;
// OTyper
GPIOC ->OTYPER &= ~(GPIO_OTYPER_OT_6);
//OSpeedr
GPIO ->OSPEEDR |= GPIO_OSPEEDR_OSPEEDR6;
// PUPDr
GPIOC ->PUPDR &= ~(GPIO_PUPDR_PUPDR6);
while(1)
{
GPIOC ->BSRR |= GPIO_BSRR_BS_6;
// Wait
WaitForAMoment(200000);
// Turn off the LED (BRR)
GPIOC ->BRR |= GPIO_BRR_BR_6;
// Wait
WaitForAMoment(200000);
}
Now we are ready to compile (build) our program. We’re noticing a few errors so let’s first correct those. We go under the boot folder and locate the file and move it to the proper folder. The other error does not concern us at this time so we’re ready to flash the microcontroller. We plug in the ST link and we see that the LED is blinking.
This is when it is on and off, that’s the 200,000 iterations for the for loop. Let’s go ahead and change that and make one of them a lesser value. We want to try and make it on longer than it it is off. To do this we go back into the program and change the value from 200,000 to 100,000. Do the change and build the program. Now we can see the changes represented by the duration of the blinking on the LED.
There are a couple of things to point out before we end this video. If you have issues flashing the microcontroller, this will help you figure out what may be going wrong. The first issue is where you have the resistors from the SWCLK and the SWDIO and remove those and where they connect to the plus and minus rail. I had to do this when I had Windows 7 but when I upgraded to Windows 10 this became a problem although it could have been a number of other issues. I understand that these are actually pulled up internally so you really don’t need those resistors there. The other thing is you can put a 510 ohm resistor going to ground on the boot 0 pin and the boot 0 pin is pin number 60. Another thing I did was take the ground and positive from the ST Link interface as close to the PCB as possible because when you’re working with breadboards, they can cause interference. These are some fixes you can look into if you’re having any trouble.
Now we’ll talk about what to do if you’ve accidentally bricked the microcontroller. Maybe you’ve had an error in the program where you’ve caused the program to doing something unintended to the microcontroller. You can also pull the boot 0 (which is pin 60) high, using a 10k resistor going to the positive rail. While the pin is pulled high you do a full memory erase. You use a program called the ST-link utility. Search for it and download and install it. It is very straight-forward. You can also use this program to program the microcontroller by loading the file and selecting it from the debug and the binary file. Double click that and you will have the program. It will also show you the device memory when you have it connected so you can click on connect to target and it will show you what is on the microcontroller. The settings can be accessed using the icon shown on the video. The mode should be in normal and SWD on connection settings. To do a full chip erase use the icon shown in the video. Thank you for watching.
[[TUTORIALMENU(tutorial-arm" frameborder="0" allowfullscreen>