安装必要插件
在VSCode中安装官方扩展"PlatformIO IDE"或"Arduino"。PlatformIO功能更全面,支持多平台开发;Arduino扩展更轻量,适合简单项目。
配置开发环境
PlatformIO方式 :
安装完成后,左侧工具栏会出现PlatformIO图标。点击"New Project",选择Arduino开发板型号(如Uno、Nano)。项目会自动生成platformio.ini配置文件,包含编译和调试设置。
Arduino扩展方式 :
安装后按F1搜索"Arduino: Board Config",选择开发板型号及端口。通过"Arduino: Initialize"生成arduino.json配置文件,需手动添加调试配置。
硬件连接准备
确保Arduino通过USB连接电脑,并安装对应驱动(如CH340驱动)。在PlatformIO中,端口会自动识别;Arduino扩展需在arduino.json中指定端口:
json
"port": "COM3",
"board": "arduino:avr:uno"
配置调试参数
PlatformIO调试 :
在platformio.ini中添加调试工具配置(如ST-Link或J-Link):
ini
debug_tool = stlink
debug_port = COM3
点击底部调试图标选择"PIO Debug"环境。
Arduino扩展调试 :
需额外安装cortex-debug扩展,在.vscode/launch.json中添加配置:
json
{
"type": "cortex-debug",
"request": "launch",
"serverType": "openocd",
"device": "ATmega328P"
}
添加断点与启动调试
在代码行号左侧点击添加断点,按F5启动调试。PlatformIO会自动编译并上传程序;Arduino扩展需先手动上传(Ctrl+Alt+U),再启动调试。调试控制台可查看变量值、调用栈等信息。
常见问题解决
- 断点不生效 :检查是否启用优化编译,在
platformio.ini中添加build_flags = -O0禁用优化。 - 端口占用:关闭Arduino IDE或其他串口工具。
- 调试器不支持:部分Arduino板需额外硬件调试器(如Atmel-ICE)。