Saturday, September 28, 2013

Programming Example : GPIO - Output Mode

#include "stm32f2xx.h"

void GPIO_Config_OutputMode(void);


int main(void)

{
  GPIO_Config_OutputMode(); // to drive a led
}

void GPIO_Config_OutputMode(void)

{
  GPIO_InitTypeDef GPIO_InitStructure;

  /* Enable GPIOG's AHB interface clock */

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG, ENABLE);

  /* Configure PG6 in output pushpull mode */

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_Init(GPIOG, &GPIO_InitStructure);

  /* Set PG6 to high level */

  GPIO_SetBits(GPIOG, GPIO_Pin_6);
}
/*
<Output mode>

1. Enable GPIOG's AHB interface clock

@reg   RCC->AHB1ENR
@fn    void RCC_AHB1PeriphClockCmd(uint32_t RCC_AHB1Periph, FunctionalState NewState)
@param RCC_AHB1Periph_GPIOG, ENABLE
@ref   stm32f2xx_rcc.h, stm32f2xx.h

2. Configure PG6 in output pushpull mode

@reg   GPIOx->MODER, GPIOx->OSPEEDR, GPIOx->OTYPER, GPIOx->PUPDR
@fn    void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)
@param GPIOG, GPIO_Pin_6, GPIO_Mode_OUT, GPIO_Speed_100MHz, GPIO_OType_PP, GPIO_PuPd_UP
@ref   stm32f2xx.h, stm32f2xx_gpio.h

3. Set PG6 to high level

@reg   GPIOx->BSRRL
@fn    void GPIO_SetBits(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)
@param RCC_AHB1Periph_GPIOG, ENABLE
@ref   stm32f2xx.h, stm32f2xx_gpio.h
*/

No comments:

Post a Comment