Jump to content

Recommended Posts

/**********************************************************************
 GPIO Mode Register - GPIOx->MODER

 00 = INPUT
 01 = OUTPUT
 10 = ALTERNATE FUNCTION
 11 = ANALOG

 Example: Make LED pin output
**********************************************************************/
#define LED 3
GPIOx->MODER |= (1 << (LED * 2)); //Set to Output


/**********************************************************************
 GPIO Type Register - GPIOx->OTYPER

 0 = PUSH-PULL (default)
 1 = Open Drain

 Example: Make LED pin push-pull type
**********************************************************************/

#define LED 3
GPIOx->OTYPER &= !(1 << LED); //Set to Push-Pull


/**********************************************************************
 GPIO Output Speed Register - GPIOx->OSPEEDR

 00 = 2MHz (Low Speed)
 01 = 25MHz (Med. Speed)
 10 = 50MHz (Fast Speed)
 11 = 100MHz/80MHz (High Speed)(100MHz(30pf) - 80MHz(15pf))

 Example: Make LED pin 100MHz
**********************************************************************/
#define LED 3
GPIOx->OSPEEDR |= (0x02 << (LED * 2)); //Set Max of 50MHz Speed


/**********************************************************************
 GPIO Pull-Up / Pull-Down Register - GPIOx->PUPDR

 00 = No Pull-Up / No Pull-Down
 01 = Pull-Up Enabled
 10 = Pull-Down Enabled
 11 = Reserved

 Example: LED pin with NO PU/PD
**********************************************************************/
#define LED 3
GPIOx->PUPDR |= (0x03 << (LED * 2)); //Set it for NO PUPD


/**********************************************************************
 GPIO Input Data Register - GPIOx->IDR
 Read Pin State

 Example: Get input from button
**********************************************************************/
#define BTN 2
variable = ((GPIOx->IDR >> BTN) & 1); //Get Button State


/**********************************************************************
 GPIO Output Data Register - GPIOx->ODR
 Write Pin State

 Example: Turn on LED
**********************************************************************/
#define LED 3
GPIOx->ODR |= (1<<LED); //Turn on LED


/**********************************************************************
 GPIO Bit Set/Reset Register - GPIOx->BSRRn
 n = H (RESET)
 n = L (SET)

 Set or Reset Pins

 Example: Turn LED On then Off
**********************************************************************/
#define LED 3
GPIOx->BSRRL |= (1<<LED); //Turn On LED
GPIOx->BSRRH |= (1<<LED); //Turn Off LED

/**********************************************************************
 RCC AHB1 peripheral clock register (RCC_AHB1ENR)(5.3.10 in REF MAN)
 BIT H = ON
 BIT L = OFF

 Example: Enable all GPIO Clocks (A:I)
**********************************************************************/
#define ALL_GPIO 0x1FF
RCC->AHB1ENR = ALL_GPIO;

 

GPIO Funtion:

void ConfigPin(GPIO_TypeDef *myGPIO, uint32_t PIN, uint32_t MODE, uint32_t SPEED, uint32_t PUPD)
{
/*
 myGPIO: The GPIOx port for the selected pin
	 MODE: 0 = INPUT .... 1 = OUTPUT .... 2 = ALTERNATE FUNCTION .... 3 = ANALOG
	 SPEED: 0 = 2MHz (Low Speed) .... 1 = 25MHz (Med. Speed) .... 2 = 50MHz (Fast Speed) .... 3 = 100MHz/80MHz (High Speed)(100MHz(30pf) - 80MHz(15pf))
	 PUPD: 0 = No Pull-Up / No Pull-Down .... 1 = Pull-Up Enabled .... 2 = Pull-Down Enabled .... 3 = Reserved
*/
myGPIO->MODER |= (MODE << (PIN * 2));//OUTPUT
myGPIO->OSPEEDR |= (SPEED << (PIN * 2));//50MHz
myGPIO->PUPDR |= (PUPD << (PIN * 2)); //Set it for NO PUPD
}

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...