找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3232|回复: 0
收起左侧

GPIO控制led程序

[复制链接]
ID:80436 发表于 2015-5-21 22:40 | 显示全部楼层 |阅读模式

const GPIO_PIN_ID Pin_LED[] = {
  { GPIOD, 12 },
  { GPIOD, 13 },
  { GPIOD, 14 },
  { GPIOD, 15 }
};

#define NUM_LEDS (sizeof(Pin_LED)/sizeof(GPIO_PIN_ID))


/*-----------------------------------------------------------------------------
*      LED_Initialize:  Initialize LEDs
*
* Parameters: (none)
* Return:     (none)
*----------------------------------------------------------------------------*/
void LED_Initialize (void) {
  uint32_t n;

  /* Configure pins: Push-pull Output Mode (50 MHz) with Pull-down resistors */
  for (n = 0; n < NUM_LEDS; n++) {
    GPIO_PortClock   (Pin_LED[n].port, true);
    GPIO_PinWrite    (Pin_LED[n].port, Pin_LED[n].num, 0);
    GPIO_PinConfigure(Pin_LED[n].port, Pin_LED[n].num,
                      GPIO_MODE_OUTPUT,
                      GPIO_OUTPUT_PUSH_PULL,
                      GPIO_OUTPUT_SPEED_50MHz,
                      GPIO_PULL_DOWN);
  }
}


/*-----------------------------------------------------------------------------
*      LED_Uninitialize:  Uninitialize LEDs
*
* Parameters: (none)
* Return:     (none)
*----------------------------------------------------------------------------*/
void LED_Uninitialize (void) {
  uint32_t n;

  /* Configure pins: Input mode, without Pull-up/down resistors */
  for (n = 0; n < NUM_LEDS; n++) {
    GPIO_PinConfigure(Pin_LED[n].port, Pin_LED[n].num,
                      GPIO_MODE_INPUT,
                      GPIO_OUTPUT_PUSH_PULL,
                      GPIO_OUTPUT_SPEED_2MHz,
                      GPIO_NO_PULL_UP_DOWN);
  }
}

/*-----------------------------------------------------------------------------
*      LED_On: Turns on requested LED
*
* Parameters:  num - LED number
* Return:     (none)
*----------------------------------------------------------------------------*/
void LED_On (uint32_t num) {
  GPIO_PinWrite(Pin_LED[num].port, Pin_LED[num].num, 1);
}

/*-----------------------------------------------------------------------------
*      LED_Off: Turns off requested LED
*
* Parameters:  num - LED number
* Return:     (none)
*----------------------------------------------------------------------------*/
void LED_Off (uint32_t num) {
  GPIO_PinWrite(Pin_LED[num].port, Pin_LED[num].num, 0);
}

/*-----------------------------------------------------------------------------
*      LED_Out: Write value to LEDs
*
* Parameters:  val - value to be displayed on LEDs
* Return:     (none)
*----------------------------------------------------------------------------*/
void LED_Out (uint32_t val) {
  uint32_t n;

  for (n = 0; n < NUM_LEDS; n++) {
    if (val & (1<<n)) {
      LED_On (n);
    } else {
      LED_Off(n);
    }
  }
}

/*-----------------------------------------------------------------------------
*      LED_Num: Get number of available LEDs
*
* Parameters: (none)
* Return:      number of available LEDs
*----------------------------------------------------------------------------*/
uint32_t LED_Num (void) {
  return (NUM_LEDS);
}




回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表