MounRiver Studio 创建 Project

创建 Hardware/LED/led.{ch}

include 路径

led.h

#ifndef __LED_H
#define __LED_H

#include "ch32v10x_conf.h"

void LED_Init(void);  // 初始化

#endif

led.c

#include "led.h"

void LED_Init(void)
{

 GPIO_InitTypeDef  GPIO_InitStructure;                // 定义一个GPIO_InitTypeDef类型的结构体

 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);// 使能与LED相关的GPIO端口时钟

 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1; // 配置GPIO引脚
 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;     // 设置GPIO模式为推挽输出
 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;    // 设置GPIO口输出速度
 GPIO_Init(GPIOA, &GPIO_InitStructure);               // 调用库函数,初始化GPIOA

 GPIO_SetBits(GPIOA,GPIO_Pin_0|GPIO_Pin_1);           // 设置引脚输出高电平

}

main.c

/********************************** (C) COPYRIGHT *******************************
 * File Name          : main.c
 * Author             : WCH
 * Version            : V1.0.0
 * Date               : 2020/09/29
 * Description        : Main program body.
 *******************************************************************************/
#include "debug.h"
#include "led.h"

int main(void) {
    u8 i = 0;
    u8 j = 0;

    Delay_Init();   // 延时函数初始化
    LED_Init();     // LED初始化
    while (1) {
        Delay_Ms(250); // 延时250ms
        GPIO_WriteBit(GPIOA, GPIO_Pin_0, (i == 0) ? (i = Bit_SET) : (i =
                                                            Bit_RESET)); // 设置PA0引脚状态为低电平
        Delay_Ms(250); // 延时250ms
        GPIO_WriteBit(GPIOA, GPIO_Pin_1, (j == 0) ? (j = Bit_SET) : (j =
                                                            Bit_RESET)); // 设置PA1引脚状态为低电平
    }
}

编译

15:07:45 **** Build of configuration obj for project LED_GPIO ****
make -j4 all 
   text	   data	    bss	    dec	    hex	filename
   1168	      8	   2052	   3228	    c9c	LED_GPIO.elf

15:07:48 Build Finished. 0 errors, 0 warnings. (took 3s.566ms)

接线

  1. BOOT0 , GND 短接(12)
  2. LED1, LED2 和 代码里面的 PA0, PA1 连接(12)
  3. WCH-Link 接电脑 (11)