Tutorial: Bitwise Macro

Tutorial: Bitwise Macro

Bitwise Operation Macro

Instead of using bitwise operations in C programming, which can be confusing to some students, we can use simple Macros for bitwise operations.

Example:


// GPIO Output Set
GPIOA->ODR |= (1UL << LED_PIN);	 		
BIT_SET(GPIOA->ODR, LED_PIN);

// GPIO Output Clear
GPIOA->ODR &= ~(1UL << LED_PIN);	 	
BIT_CLEAR(GPIOA->ODR, LED_PIN);


// GPIO Mode Register 
GPIOA->MODER &= ~(3UL<<(2*LED_PIN)); 	// Clear as `b11=0x3` starting from  2*LED_PIN
BITS_CLEAR(GPIOA->MODER, 2 * LED_PIN, 3); // Clear as `b11=0x3` starting from  2*LED_PIN

GPIOA->MODER |=   3UL<<(2*LED_PIN);   // Set '11' for Pin 5
BITS_SET(GPIOA->MODER, 2 * LED_PIN, 3);

Defining Bitwise Macro

Exercise

Modify ecPinNames.h to include the Bitwise Macro.

Last updated

Was this helpful?