在使用vscode 进行keil 项目开发时,希望能够通过快捷键快速进行编译、下载。可以通过配置 .vscode/tasks.json 完成。然后调用 vscode 的 task 任务即可。在文件内容区域按下快捷键是 ctrl + shift + b 可以快速选取要执行的任务。
背后会通过调用 keil uvision 进行编译,通过调用 stcgal 提交烧录操作,开发者只需要冷启动上电即可快速完成开发、构建、烧录、测试的流程。
使用的时候有 4 个注意事项:
- 请合理配置
PATH, 确保相关命令UV4.exe,stcgal.exe可以被调用到 - 可通过配置项目的
ProjectName.uvproj文件路径信息,替换 powershell 查找文件 - 可通过配置项目的
固件 hex文件路径信息,替换 powershell 查找文件 - download 任务使用的 COM1 和 stc89 芯片,需替换为实际使用的信息
{
"version": "2.0.0",
"tasks": [
{
"label": "keil-assistant: build",
"type": "shell",
"command": "UV4.exe -b (Get-ChildItem -Path '${workspaceFolder}' -Filter *.uvproj -Recurse | Sort-Object LastWriteTime -Descending | Select-Object -First 1).FullName -j4 -t 'Target 1' -o '${workspaceFolder}/build.log'",
"problemMatcher": [],
"group": { "kind": "build" }
},
{
"label": "keil-assistant: download",
"type": "shell",
"command": "powershell",
"args": [
"-command",
"Write-Host \"\"; Get-Content '${workspaceFolder}/build.log'; Write-Host \"\"; Write-Host \"\"; Write-Host \"\"; stcgal.exe -p COM1 -P stc89 (Get-ChildItem -Path Objects -Filter *.hex | Sort-Object LastWriteTime -Descending | Select-Object -First 1).FullName"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [],
"group": {
"kind": "build",
"isDefault": false
}
},
]
}
参考资料:
- https://zhuanlan.zhihu.com/p/569959314 使用VS Code代替keil开发stm32和51单片机的教程
- https://github.com/grigorig/stcgal/ stcgal - STC MCU ISP flash tool
- https://www.bu44er.ink/blog/2025/keil-to-pio 使用 VSCode 做嵌入式开发 on macOS
- ChatGPT 对话问答
https://discuss.plugins-world.cn/post/NYjI3AHe