首先需要注意代码的语法
附上本论坛一位网友总结的笔记
其实开源的SDCC+code blocks也不错的。 SDCC语法与keil C有一点点不同,记录如下: * 1 sbit * // keil c : sbit LED1=P1^3 ; // SDCC #define LED1 P1_3 * 2 中断 * keil c 的中断 void SerialComm(void ) interrupt 4 ; { } sdcc 的中断 void SerialComm(void) __interrupt 4; { } * 3 _nop()_ * sdcc 没有 _nop()_ , 可用如下自定义宏代替: #define _nop()_ __asm NOP __endasm头文件包含
lint.h
里面定义了有关硬件相关寄存器的宏,没有包含的话,在直接使用单片机特殊功能寄存器的话都会出现波浪号,是一个很重要的头文件,在单片机的时候,务必首先将此头文件包含进去。
#ifndef _LINT_H #define _LINT_H #if !defined(__SDCC_mcs51) #define __data #define __near #define __idata #define __xdata #define __far #define __pdata #define __code #define __bit bool #define __sfr volatile unsigned char #define __sbit volatile bool #define __critical #define __at(x) #define __using(x) #define __interrupt(x) #define __naked #define data #define near #define idata #define xdata #define far #define pdata #define code #define bit bool #define sfr volatile unsigned char #define sbit volatile bool #define critical #define at(x) #define using(x) #define interrupt(x) #define naked #if defined(S_SPLINT_S) #endif #endif #endif
stdio.h头文件在这里插入代码片
此头文件里面定义了有一个比较常用的printf函数,关于此头文件的内容比较多,就比贴出内容来了。
stdint.h头文件
此头文件里面定义了数据类型的简写。
#ifndef _STDINT_H #define _STDINT_H #include以上头文件的加载的话根据个人使用按需加载,当然还有其他的头文件,可以自行到编译器所在目录下查看翻阅和了解。#define __need_wint_t #define __need_wchar_t #include typedef signed char int8_t; typedef unsigned char uint8_t; typedef short int16_t; typedef unsigned short uint16_t; typedef int int32_t; typedef unsigned uint32_t; __MINGW_EXTENSION typedef long long int64_t; __MINGW_EXTENSION typedef unsigned long long uint64_t; typedef signed char int_least8_t; typedef unsigned char uint_least8_t; typedef short int_least16_t; typedef unsigned short uint_least16_t; typedef int int_least32_t; typedef unsigned uint_least32_t; __MINGW_EXTENSION typedef long long int_least64_t; __MINGW_EXTENSION typedef unsigned long long uint_least64_t; typedef signed char int_fast8_t; typedef unsigned char uint_fast8_t; typedef short int_fast16_t; typedef unsigned short uint_fast16_t; typedef int int_fast32_t; typedef unsigned int uint_fast32_t; __MINGW_EXTENSION typedef long long int_fast64_t; __MINGW_EXTENSION typedef unsigned long long uint_fast64_t; __MINGW_EXTENSION typedef long long intmax_t; __MINGW_EXTENSION typedef unsigned long long uintmax_t; #if !defined(__cplusplus) || defined(__STDC_LIMIT_MACROS) || defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L #define INT8_MIN (-128) #define INT16_MIN (-32768) #define INT32_MIN (-2147483647 - 1) #define INT64_MIN (-9223372036854775807LL - 1) #define INT8_MAX 127 #define INT16_MAX 32767 #define INT32_MAX 2147483647 #define INT64_MAX 9223372036854775807LL #define UINT8_MAX 255 #define UINT16_MAX 65535 #define UINT32_MAX 0xffffffffU #define UINT64_MAX 0xffffffffffffffffULL #define INT_LEAST8_MIN INT8_MIN #define INT_LEAST16_MIN INT16_MIN #define INT_LEAST32_MIN INT32_MIN #define INT_LEAST64_MIN INT64_MIN #define INT_LEAST8_MAX INT8_MAX #define INT_LEAST16_MAX INT16_MAX #define INT_LEAST32_MAX INT32_MAX #define INT_LEAST64_MAX INT64_MAX #define UINT_LEAST8_MAX UINT8_MAX #define UINT_LEAST16_MAX UINT16_MAX #define UINT_LEAST32_MAX UINT32_MAX #define UINT_LEAST64_MAX UINT64_MAX #define INT_FAST8_MIN INT8_MIN #define INT_FAST16_MIN INT16_MIN #define INT_FAST32_MIN INT32_MIN #define INT_FAST64_MIN INT64_MIN #define INT_FAST8_MAX INT8_MAX #define INT_FAST16_MAX INT16_MAX #define INT_FAST32_MAX INT32_MAX #define INT_FAST64_MAX INT64_MAX #define UINT_FAST8_MAX UINT8_MAX #define UINT_FAST16_MAX UINT16_MAX #define UINT_FAST32_MAX UINT32_MAX #define UINT_FAST64_MAX UINT64_MAX #ifdef _WIN64 #define INTPTR_MIN INT64_MIN #define INTPTR_MAX INT64_MAX #define UINTPTR_MAX UINT64_MAX #else #define INTPTR_MIN INT32_MIN #define INTPTR_MAX INT32_MAX #define UINTPTR_MAX UINT32_MAX #endif #define INTMAX_MIN INT64_MIN #define INTMAX_MAX INT64_MAX #define UINTMAX_MAX UINT64_MAX #ifdef _WIN64 #define PTRDIFF_MIN INT64_MIN #define PTRDIFF_MAX INT64_MAX #else #define PTRDIFF_MIN INT32_MIN #define PTRDIFF_MAX INT32_MAX #endif #define SIG_ATOMIC_MIN INT32_MIN #define SIG_ATOMIC_MAX INT32_MAX #ifndef SIZE_MAX #ifdef _WIN64 #define SIZE_MAX UINT64_MAX #else #define SIZE_MAX UINT32_MAX #endif #endif #ifndef WCHAR_MIN #define WCHAR_MIN 0U #define WCHAR_MAX 0xffffU #endif #define WINT_MIN 0U #define WINT_MAX 0xffffU #endif #if !defined(__cplusplus) || defined(__STDC_CONSTANT_MACROS) || defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L #define INT8_C(val) (INT_LEAST8_MAX-INT_LEAST8_MAX+(val)) #define INT16_C(val) (INT_LEAST16_MAX-INT_LEAST16_MAX+(val)) #define INT32_C(val) (INT_LEAST32_MAX-INT_LEAST32_MAX+(val)) #define INT64_C(val) val##LL #define UINT8_C(val) (val) #define UINT16_C(val) (val) #define UINT32_C(val) (val##U) #define UINT64_C(val) val##ULL #define INTMAX_C(val) val##LL #define UINTMAX_C(val) val##ULL #endif #endif
独立安装的SDCC编译我的VSCode PlatformIO SDCC编译器文件位置:C:UsersAdministrator.platformiopackagestoolchain-sdccinclude
SDCC下载的地方:https://sourceforge.net/projects/sdcc/files/独立下载和安装SDCC4.1.0编译器(X64位的):https://jaist.dl.sourceforge.net/project/sdcc/sdcc-win64/4.1.0/sdcc-4.1.0-x64-setup.exe
VSCode PlatformIO调用独立安装的的SDCC编译器进行编译方法例如我安装的独立sdcc编译器路径:D:Program FilesSDCCincludemcs51
在工程项目下的c_cpp_properties.json文件内添加如下语句:
// "compilerPath": "C:/Users/Administrator/.platformio/packages/toolchain-sdcc/bin/sdcc.exe",//这是默认使用平台提供的SDCC编译器位置 "compilerPath":"D:/Program Files/SDCC/bin/sdcc.exe",//添加此独立安装的sdcc编译器位置
VSCode PlatformIO调用独立的SDCC编译工具进行编译
目前使用SDCC来开发STC单片机的人群不多,有许多暗坑,这里不对新手鼓励使用,比起Keil软件,这个工具毕竟是开源免费的编译器,对于个人和企业的话,可以免费使用。 目前发现的暗坑,在使用StC15开发的时候,想通过虚拟串口打印数据,发现编译后上传都没有问题,但是打印的信息是乱码,同样的代码在Keil里面编译上传没有问题,打印OK。
在VSCode PlatformIO平台使用SDCC编译的文件,虚拟串口就是打印的是乱码,刚开始我还以为使用VSCode PlatformIO是stcgal工具进行上传所导致的问题,后面我将编译生成的Hex文件使用STC-ISP工具进行单独烧录,也是不行。目前还没找到解决办法。
虚拟串口打印主要涉及的就是一个位函数
//======================================================================== // 函数: void BitTime(void) // 描述: 位时间函数。 //======================================================================== void BitTime(void) { u16 i; i = (16 * 104) / 13 - 1; //根据主时钟来计算位时间16 while (--i) ; }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)