Tutorial: PinName Configuration

Pinmap Configuration

Instead of inserting Port_name and Pin_name separately in the function argument, we can combine them as one argument.

Compare these two similar function argument. Which is easier to use?

GPIO_mode(GPIOA, 5, OUTPUT);

GPIO_mode(PA_5, OUTPUT);

We can combine PORT and PIN as one information by user-defined PinNames header file ecPinNames.h. Here we have defined PA_0 to PC_15.

You can also define additional Port and Pins in the header file.

Examples

Usage example 1

// GPIO Mode          : Input(00), Output(01), AlterFunc(10), Analog(11, reset)
void GPIO_mode(PinNames_t pinName, int mode){
	// Separate port-name and pin-number from pinName
    GPIO_TypeDef *Port;
	unsigned int pin;
	ecPinmap(pinName, &Port, &pin);
    
   	Port->MODER &= ~(3UL<<(2*pin));     
   	Port->MODER |= mode<<(2*pin);    
}


//  In MAIN()
GPIO_mode(PA_5, OUTPUT);
GPIO_mode(PC_15, INPUT);


////////////////////////////////////////////////////////////////////////////////////
//
// You can also use PORT name and PIN name separately for the function argument

/*  
void GPIO_mode(GPIO_TypeDef *Port, int pin, int mode){
   Port->MODER &= ~(3UL<<(2*pin));     
   Port->MODER |= mode<<(2*pin);    
}
*/

/* In MAIN()
GPIO_mode(GPIOA, 5, OUTPUT);
GPIO_mode(GPIOC, 15, INPUT);
*/

Usage example 2

Different syntax for the same result

PinName Header File

ecPinNames.h

ecPinNames.c

Last updated

Was this helpful?