找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 3846|回復: 0
收起左側

GPIO控制led程序

[復制鏈接]
ID:80436 發(fā)表于 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);
}




回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

手機版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復 返回頂部 返回列表