In the previous Blog clearly explained about, “How to configure inbuilt LED procedure and using through registers(special function registers)”. Link add
In this Blog full and fully gonna be configured inbuilt LED through special function registers using code.
#code explanation would be from the scratch
As well as how to create a new project in stm32cube ide also explained in the previous blog Just visit this blog and check(Link add).
Now go with a code
Code Explanation
Target : To blink inbuilt LED with 3 seconds delay
#include <stdint.h>
#include, which is directives to the header file. The header file <stdint.h> standard integer
Which tells the exact width, size and limits of the data types.
Eg. In normal C <stdio.h> has contain functions like printf and scanf
In embedded C <stdint.h> define exact-width integer types and their limits.
uint32_t *pAHB1ENR = (uint32_t*)0x40023830;
uint32_t, This uint32_t, defines the unsigned integer data type width of 32-bit range. It can hold the non-negative values.
32bit=2^32=0 to 4,294,967,295
*pAHB1ENR, pointer variable to store the address of AHB1ENR
(uint32_t*), typecasting from hexadecimal to pointer
0x40023830, hexadecimal value of the AHB1ENR address.
uint32_t *pMODER = (uint32_t*)0x40020000;
As same as AHB1ENR , declare the value of moder register *pMODER address value is 0x40020000.
uint32_t *pODR = (uint32_t*)0x40020014;
uint32_t *pODR = (uint32_t*)0x40020014;
To declare the value of ODR register *pODR address value is 0x40020014.
void delay();
void delay();
Function call, to going to make delay in the LED so we use functions here void delay();
Function call can we use at any of the time in the code, it will go to the function definition and go to execute the function definition
void delay()
void delay(){
}
Function definition, this for how long your LED going to blink and off
Use through for loop
for(uint32_t i=0;i<300000;i++);
{ , open curly tends to be beginning of the function starts
for(uint32_t i=0;i<300000;i++); Using for loop make a 3 second delay uint32_t(datatype), i(variable), 300000 is 3 seconds, i++(increment)
}, close bracket which tends for end of the code
Function definition, this for how long your LED going to blink and off
Use through for loop
int main(void){
int main(void)
Main function starts from here!!
*pAHB1ENR | = (1U<<0);
*pAHB1ENR |= (1U<<0);
Here we going to enable the pins through program go to the reference manual 6.3.9 AHB1 in this you have to enable the GPIO A, Here there is an option you can enable the “BITS” using BITWISE OPERATOR, HEXADECIMAL VALUES and using 1 or 0.
Bitwise operator
- Right shift
- Left shift
We have to enable the bit using right shift (1U<<0) <<(right shift) 1U(set the bit) 0, tends to shift the bit 0 times because GPIOA is in the first place of 0th position.
Here we enable GPIO A , if you want to set the bit use “OR” logical operator |.
*pMODER | = (1U<<10);
*pMODER |= (1U<<10);
(1U<<10); set the 10th bit as 01 so make a right shift upto 10 times because the moder5 has in 10th position.
while(1){
}
while(1), Infinite loop
We make the ODR infinite loop because the function going to be happens again and again LED going to “ON” and “OFF” again and again.
*pODR | = (1U<<5);
*pODR |= (1U<<5);
Set the ODR5, the LED is connected with GPIO A of the 5th pin. So enables the 5th bit in the ODR register. Make a right shift upto 5 times.
delay();
delay();
Function call
*pODR &= ~(1U<<5);
*pODR &= ~(1U<<5);
Reset the bit using ~ not, so make a 5th has become 0.
delay();
}
This is all about our code, I hope you have got a clear idea about this code.
#source code