十速单片机学习归纳总结(三):代码编写的前期准备

注:该文章中所讲述内容均是在VSCode编辑器中操作!

  1. 在正式编写业务代码之前,需要先搭建好整体框架,包括忽略文件、代码片段自动生成文件、安装各种插件、防止VSCode编译器报错的文件、各种今后可能会用到的.c和.h文件等等。
  2. 忽略文件为“.gitignore”文件和“settings.json”文件
  3. 防止VSCode编译器报错的文件为: “c_cpp_properties.json”文件
  4. 代码片段自动生成文件为:点击菜单栏的“文件”—>“首选项”—>“用户代码片段”,在弹出的搜索框中选择“C”并回车确定即可看到生成一个“c.json”文件。
  5. 点击快捷键:“Ctrl+Shift+P”或点击菜单栏“编辑”—>“Emmet…”打开搜索框,输入“>hide Gitignored…”回车确定就会生成一个“settings.json”文件。
  6. 在.gitignore文件中,添加“Listings/”和“Objects/”,保存后在“settings.json”文件中查看是否已经添加。
  7. 所需安装的插件:
    (1)C/C++
    (2)C++ Intellisense
    (3)Clang-Format
    (4)hide-gitignored
    (5)Markdown All in One
    (6)vscode-icons
    (7)One Dark Pro
  8. “.vscode”里面的{}c_cpp_properties.json是代码自动生成的。通过在includeAll.h里面设置一些不存在的头文件,当其出现一个黄色的小灯泡时,点击它,然后点击“Edit “includePath” setting”即可出现c_cpp_properties.json文件。

1、由于本次实验是利用VSCode编辑器去做嵌入式开发,而VSCode编辑器本身并不支持嵌入式C,仅支持标准C,因此嵌入式C中的很多关键字在VSCode编辑器中会出现报错信息,我们需要通过编辑“c_cpp_properties.json”文件来防止VSCode编辑器出现报错信息:在c_cpp_properties.json文件中的“defines”选项,添加"sbit=char",“sfr=char”,“bit=char”,“interrupt 2 =”。保存后会发现“REGtenxTM52F5278B.h”文件里面的错误已经消失了。

2、在代码自动补全文件:“c.json”中配置:

{
	// Place your snippets for c here. Each snippet is defined under a snippet name and has a prefix, body and 
	// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
	// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the 
	// same ids are connected.
	// Example:
	// "Print to console": {
	// 	"prefix": "log",
	// 	"body": [
	// 		"console.log('$1');",
	// 		"$2"
	// 	],
	// 	"description": "Log output to console"
	// }
	"H file frame": {
		"prefix": "hframe",
		"body": [
			"#ifndef __$1_h",
			"#define __$1_h ",
			"//Hal: exp: #define P_led P10 ----------------\n",
			"//Const: exp: #define D_data 1 ---------------\n",
			"//Globle var ---------------------------------\n",
			"#ifdef __$1_c\n",
			"#else\n",
			"#endif\n",
			"//Action Macro: exp: #define F_getData() -----\n",
			"//Function -----------------------------------\n",	
			"#endif"
		]
	},
	"C file frame":{
		"prefix": "cframe",
		"body": [
			"#define __$1_c ",
			"#include \"includeAll.h\"\n"
		]
	},
	"C function div":{
		"prefix": "fdiv",
		"body": [
			"//============================================\n"
		]
	},
	"main":{
		"prefix": "mainframe",
		"body":[
			"//============================================",
			"void main(){",
			"InitSys();\n",
			"while(1){",
			"}",
			"}",
			"//============================================",
			"void DisplayProcess(){\n",
			"}",
			"//============================================",
			"void UserSettingProcess(){\n",
			"}",
			"//============================================",
			"void TaskProcess(){\n",
			"}",
			"//============================================",
			"void TimeProcess(){\n",
			"}"
		]
	}
}

3、格式化代码(需要用到插件:Clang-Format):点击菜单栏中的“文件”—>“首选项”—>“设置”打开“Settings”文件。在扩展—>“C/C++”—>“C_Cpp:Clang_format_fallback Style”选项填写“LLVM”;在扩展—>“Clang-Format configuration”—>Fallback Style选项中填写“LLVM”。
最后,在“Settings”文件中,搜索:“Code Action On Save”,点击“Edit in settings.json”查看一下是否配置成功。也可通过搜索:“C_Cpp:Clang_format_path”,点击“Edit in settings.json”查看一下是否配置成功。

4、局部变量:可能发生动态内存溢出;
全局变量:在编译的时候就已经分配了内存。
使用全局变量会增加文件之间的耦合度。
extern uint8_t ledFlashCounter”表示其为外部变量,这里只是引用进来进行使用而已。

5、在C语言中,函数一定是全局的,没有局部函数之说,所以函数在引用声明时不需要在前面加上“extern”

6、变量的作用域:
·静态全局变量的作用域,可见域就是所定义的本文件;别的文件用“extern”是引用不了的,生命周期为整个程序;
·全局变量,用“extern”将其可见域拓展到其他文件。
静态局部变量:
·局部变量:作用域、可见域、生命周期均为所属函数。

7、“uint8_t ledFlashCounter”:定义并分配内存空间给ledFlashCounter

8、取名字标准

  1. 函数名:首字母大写,采用驼峰形式;
  2. 变量名:首字母小写,采用驼峰形式;
  3. 类型名:基础数据类型全部采用转义的方式,要把其“是否有符号”、“类型形式”、“二进制长度”都表示出来,同时在末尾加上“_t”
    如:
typedef unsigned char uint8_t;
typedef unsigned int uint16_t;
typedef unsigned long uint32_t;
typedef char int8_t;
typedef short int16_t;
  1. 宏定义:第一个字母大写,其次加上一个下划线“_”,如:P_led、D_led、F_ledOn()
    以双划线“__”开头的宏定义
    以“P_”开头的宏定义
    以“D_”开头的宏定义
    以“F_”开头的宏定义

9、代码的总体目录结构
代码总体的目录结构
其中
· “c_cpp_properties.json”、“launch.json”、“settings.json”为系统自动生成的文件;
· “.gitignore”为忽略文件
· “demo.uvproj”为Keil C51的工程文件
· “INTRINS.H”文件可要可不要,本实验不需要用到
· “REGtenxTM52F5278B.h”文件为十速C5178B的头文件
· “STARTUP_TENX52.A51”文件为十速公司提供的文件
· 其余的如“demo.c”、“demo.h”、“includeAll.h”、“init.c”、“init.h”、“main.c”、“main.h”、“README.txt”、“typeAlias.h”均为需要自己编写的源程序。

10、“demo.c”、“demo.h”、“init.c”、“init.h”文件在本次实验中不需要用到,建立它们的目的是为了今后程序的可扩展性着想,其里面代码如下:

  1. “demo.c”文件
#define __demo_c 
#include "includeAll.h"
  1. “demo.h”文件
#ifndef __demo_h
#define __demo_h 
//Hal: exp: #define P_led P10 ----------------
#define P_led2 P10
//Const: exp: #define D_data 1 ---------------
#define D_led2Flash 2
//Globle var ---------------------------------

#ifdef __demo_c

#else
 
#endif

//Action Macro: exp: #define F_getData() -----
#define F_led2()
//Function -----------------------------------

#endif
  1. “init.c”文件
#define __init_c

#include "includeAll.h"

void InitSys() {}
  1. “init.h”文件
#ifndef __init_h
#define __init_h 
//Hal: exp: #define P_led P10 ----------------
#define P_led P1_0
//Const: exp: #define D_data 1 ---------------
#define D_led 1
//Globle var ---------------------------------
#ifdef __init_c
int ledFlashCounter;
#else
extern int ledFlashCounter;
#endif

// macro ===================================
#define F_ledOn() P_led = 1
//Action Macro: exp: #define F_getData() -----

//Function -----------------------------------

#endif

11、“typeAlias.h”文件用于“为复杂的声明定义简单的别名

#ifndef __typeAlias_h
#define __typeAlias_h

typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned long uint32_t;
typedef char int8_t;
typedef short int16_t;
typedef long int32_t;

#define F_turnOnWDT() CFGWL = (CFGWL & 0x3f) | 0xC0

#endif

12、“includeAll.h”文件用于将所有的“.h”头文件包括进来:

#include "REGtenxTM52F5278B.h"
#include "typeAlias.h"
#include "INTRINS.H"

#include "main.h"
#include "init.h"

总结

综上所述为做任何一个必须要搭的通用框架,一开始会感觉很繁琐,但只要做好一次,今后便需要使用就可以了!

猜你喜欢

转载自blog.csdn.net/chenlintao_csdn/article/details/86505778