不久后就要参加电子设计竞赛了。Keil的界面实在不好看,自动补全等功能也不太方便,于是决定想办法将VSCode配置为Keil的编辑器,并用Makefile自动化编译和下载。

无标题.png


步骤如下:
  1、在Keil项目文件夹下新建一个文件夹.vscode,并在其中新建文件c_cpp_properties.json,其内容为:

{
	"configurations": [
		{
			"name": "STM32",
			"includePath": [
				"C://keil_v5/ARM/ARMCC/**",		 // keil_v5安装目录
				"${workspaceFolder}/**"
			],
			"browse": {
				"limitSymbolsToIncludedHeaders": true,
				"databaseFilename": "${workspaceRoot}/.vscode/.browse.c_cpp.db",
				"path": [
					"C://keil_v5/ARM/ARMCC/**",	 // keil_v5安装目录
					"${workspaceFolder}/**"
				]
			},

			// 宏定义
			"defines": [
				"_DEBUG",
				"UNICODE",
				"_UNICODE",
				"__CC_ARM",
				"USE_STDPERIPH_DRIVER",
				"STM32F10X_HD"					  // 此项根据实际情况更改
			],
			"intelliSenseMode": "gcc-x64"
		}
	],
	"version": 4
}

  2、在刚刚新建的.vscode文件夹中,继续新建文件tasks.json,其内容为:

{
	"version": "2.0.0",
	"tasks": [
		{
			"label": "make",
			"command": "make.exe",      // make.exe路径
			"group": {
				"kind": "build",
				"isDefault": true
			},
			"args": [
				"all"
			],
		}
	]
}

  3、在Keil项目文件夹下新建文件Makefile,其内容为:

UV = C:/Keil_v5/UV4/UV4.exe							# UV4.exe路径
UV_PRJ_PATH = $(wildcard $(shell cd)/**/*.uvprojx)	# 自动查找uvprojx文件
TEMP_LOG_PATH = $(shell cd)\temp_log.txt

.PHONY: all build download

all: build download

build:
	- @$(UV) -j0 -b $(UV_PRJ_PATH) -o $(TEMP_LOG_PATH)
	@type $(TEMP_LOG_PATH)
	@del $(TEMP_LOG_PATH)

download:
	- @$(UV) -j0 -f $(UV_PRJ_PATH) -o $(TEMP_LOG_PATH)
	@type $(TEMP_LOG_PATH)
	@del $(TEMP_LOG_PATH)

  至此,在VSCode中,运行生成任务...或按快捷键Ctrl+Shift+B即可将项目编译并下载到单片机了。


参考:

c_cpp_properties.json reference
µVision User’s Guide

Q.E.D.