第8-9周作业--STM32嵌入式应用系统设计
vscode代码调试
- 在虚拟机上安装vscode
- 安装完成后将c/c++的插件安装完成
- 新建一个文件rever.c
- 写入以下代码
#include<stdio.h>
void ShowRevertNum(int iNum)
{
while(iNum>10)
{
printf("%d",iNum%10);
iNum=iNum/10;
}
}int main(void)
{
int iNum;
printf("please input a number: ");
scanf("%d",&iNum);
printf("After revert:");
ShowRevertNum(iNum);
}
- 编译
- 运行后在将launch.json和tasks.json修改为以下代码
tasks.json
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "build",
"type": "shell",
"command": "g++",
"args": [
"-g",
"${file}",
"-std=c++11",
"-o",
"${fileBasenameNoExtension}.out"
],
"problemMatcher": [
"$gcc"
]
}
]
}
launch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/${fileBasenameNoExtension}.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"preLaunchTask": "build",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
- 运行
- 结果
Proteus仿真运行stm32程序
- 创建一个新的工程文件
- 电路仿真图
- 打开keil新建一个工程
写入以下代码
#include "stm32f10x.h"
GPIO_InitTypeDef GPIO_InitStructure;
void delay_ms(uint32_t ms)
{
uint32_t i_cnt,j_cnt;
for(i_cnt=0;i_cnt<3000;i_cnt++);
for(j_cnt=0;j_cnt<ms;j_cnt++);
}
uint32_t i;
int main(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIOC->BSRR=0xff;
while (1)
{
for(i=0;i<8;i++)
{
delay_ms(99000);
GPIOC->BRR=(1<<i);
delay_ms(99000);
GPIOC->BSRR=(1<<i);
}
for(i=0;i<8;i++)
{
delay_ms(99000);
GPIOC->BRR=0x000000ff;
delay_ms(99000);
GPIOC->BSRR=0x000000ff;
}
}
}
- 选择生成hex文件
- 双击芯片,修改其晶振为8M,导入 .hex文件
- 结果