Blink LED

作者: T_K_233 | 来源:发表于2020-09-14 14:41 被阅读0次
  1. 在 VSCode 中 PIO 主页选择新建项目


    image.png
  2. 选择如下设置


    image.png
  3. 更改 platformio.ini 设置文件为下面的内容

[env:sipeed-longan-nano]
platform = gd32v                ; Platform, choose gd32v
framework = gd32vf103-sdk       ; Optional gd32vf103-sdk or arduino
board = sipeed-longan-nano      ; Development board
monitor_speed = 115200          ; Serial monitor baudrate
upload_protocol = sipeed-rv-debugger    ; Download tool Default serial port, optional jlink, gd-link, dfu, etc.
debug_tool = sipeed-rv-debugger         ; Debugging tool default jlink, optional sipeed-rv-debugger, etc.
  1. 在 src 文件夹下新建 main.c 文件,以下为程序代码
#include "gd32vf103.h"
#include <stdio.h>

#define LED_R_PIN GPIO_PIN_13
#define LED_R_GPIO_PORT GPIOC
#define LED_R_GPIO_CLK RCU_GPIOC
#define LED_G_PIN GPIO_PIN_1
#define LED_G_GPIO_PORT GPIOA
#define LED_G_GPIO_CLK RCU_GPIOA
#define LED_B_PIN GPIO_PIN_2
#define LED_B_GPIO_PORT GPIOA
#define LED_B_GPIO_CLK RCU_GPIOA

void longan_led_init() {
  /* enable the led clock */
  rcu_periph_clock_enable(LED_R_GPIO_CLK);
  rcu_periph_clock_enable(LED_G_GPIO_CLK);  // green and blue shares same clock
  /* configure led GPIO port */ 
  gpio_init(LED_R_GPIO_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, LED_R_PIN);
  gpio_init(LED_G_GPIO_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, LED_G_PIN);
  gpio_init(LED_B_GPIO_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, LED_B_PIN);

  GPIO_BC(LED_R_GPIO_PORT) = LED_R_PIN;
  GPIO_BC(LED_G_GPIO_PORT) = LED_G_PIN;
  GPIO_BC(LED_B_GPIO_PORT) = LED_B_PIN;
}

void longan_led_on() {
  GPIO_BOP(LED_R_GPIO_PORT) = LED_R_PIN;
  GPIO_BOP(LED_G_GPIO_PORT) = LED_G_PIN;
  GPIO_BOP(LED_B_GPIO_PORT) = LED_B_PIN;
}

void longan_led_off() {
  GPIO_BC(LED_R_GPIO_PORT) = LED_R_PIN;
  GPIO_BC(LED_G_GPIO_PORT) = LED_G_PIN;
  GPIO_BC(LED_B_GPIO_PORT) = LED_B_PIN;
}

/**
 * delay a time in milliseconds.
 * 
 * @param t time to wait, in milliseconds
 */
void delay(uint32_t t) {
  // Don't start measuruing until we see an mtime tick
  uint64_t tmp = get_timer_value();
  uint64_t start_mtime = get_timer_value();
  while (start_mtime == tmp) {
    start_mtime = get_timer_value();
  } 

  uint64_t delta_mtime = get_timer_value() - start_mtime;
  while(delta_mtime < (SystemCoreClock / 4000.0 * t )) {
    delta_mtime = get_timer_value() - start_mtime;
  }
}


/**
 * main function
 */
int main(void) {
  longan_led_init();

  while(1) {
    /* turn on builtin led */
    longan_led_on();
    delay(50);

    /* turn off uiltin led */
    longan_led_off();
    delay(50);
  }
}

相关文章

网友评论

      本文标题:Blink LED

      本文链接:https://www.haomeiwen.com/subject/qurzektx.html