Write 1 on Pin 16 in ODR Register using 5 ways
Write 1 on Pin 16 in ODR Register using 5 ways
1. Using numeric memory Address directly
#define GPIO_ODR_ADDRESS 0X00010004
Void main()
{
*(volatile uint32_t*) GPIO_ODR_ADDRESS |= 1<16;
}
2. Casting an address to a pointer
#define GPIO_ODR ((volatile uint32_t*)GPIO_ODR_ADDRESS)
Void main()
{
*GPIO_ODR |= 1<16;
}
3. Casting to a pointer and then dereferencing it
#define GPIO_ODR *((volatile uint32_t*)GPIO_ODR_ADDRESS)
Void main()
{
GPIO_ODR |= 1<16;
}
4. Use structure union and pointer for one register
#define GPIO_ODR *((volatile uint32_t*)GPIO_ODR_ADDRESS)
#pragma pack(1)
struct SGPIO_ODR_t
{
uint32_t pin0:1;
uint32_t pin1:1;
uint32_t pin2:1;
uint32_t pin3:1;
uint32_t pin4:1;
...
...
...
}
union U_ODR
{
struct SGPIO_ODR_t S_ODR;
uint32_t ODR;
}
volatile union U_ODR *GPIO_ODR_R = (union volatile *)GPIO_ODR;
void main()
{
GPIO_ODR_R -> S_ODR.pin16 = 1;
}
5. Use structure and Pointer for all registers in a GPIO peripheral
#define GPIO_ADDRESS 0X00010004
#pragma pack(1)
struct SGPIO
{
volatile uint32_t ODR;
volatile uint32_t MODER;
volatile uint32_t ASCR;
volatile uint32_t IDR;
};
#define GPIOB ((struct SGPIO*)GPIO_ADDRESS)
void main()
{
GPIOB -> ODR = 1<< 16;
}
Comments
Post a Comment