您尚未登录。

楼主 #1 2020-02-24 00:17:58

OggyJFX
会员
注册时间: 2019-05-21
已发帖子: 109
积分: 108.5

vscode下编写c51程序

vscode写代码,怎么一个爽字了得。

c51的有些关键字,导致vscode总是报错,不能自动补全。

比如:
void a(void) interrupt 5
{
}

我的解决办法:
通过vscode的forceinclude功能,增加一个头文件,头文件中,定义VSCODE宏
然后在工程的源文件中,加入如下一段:
#ifndef VSCODE
    #define INTERRUPT(NO) interrupt NO
#else
    #define INTERRUPT(NO)
#endif

把中断函数改写成如下格式:
void a(void) INTERRUPT(5)
{
}

离线

楼主 #6 2020-02-25 08:56:28

OggyJFX
会员
注册时间: 2019-05-21
已发帖子: 109
积分: 108.5

Re: vscode下编写c51程序

五十嵐雪華 说:

#ifndef __VS_CODE_H__
#define __VS_CODE_H__
#ifdef VSCODE
    #define _nop_() (void*)0;
    #define interrupt(x)
    #define using(x)
    #include <stdbool.h>
    #include <stdint.h>
    typedef bool bit;
    class sfr{
    public:
        sfr(int){};
        sfr(){};
        ~sfr(){};
        bool operator ^ (uint16_t data);
        bool operator ^ (int data);
        sfr& operator=(const sfr& other);
        sfr& operator=(const int other);
        int operator ^= (int data);
        int operator &= (int data);
        int operator |= (int data);
        int operator | (int data);
        int operator & (int data);
        operator int(){};
    };
    #define sbit bool
#else
    #define interrupt(x) interrupt x
    #define using(x) using x
#endif
#endif

牛逼,大神级人物
我用sbit定义引脚的时候,总提示后面的变量需要是常量值
sbit LepPin = P1^2;
总提示这个P1需要是常量,怎么解?
我目前也是在代码中,用
#ifndef VSCODE
sbit LedPin = P1^2;
#else
sbit LedPin;
#endif
这种方式处理的。

离线

楼主 #9 2020-04-21 13:23:45

OggyJFX
会员
注册时间: 2019-05-21
已发帖子: 109
积分: 108.5

Re: vscode下编写c51程序

五十嵐雪華 说:

我这边源代码后缀是cpp但是不用cpp的特征, 仅仅是为了vscode不会报错, 编译的时候是写脚本把文件后缀从cpp改成c, 最后通过脚本调用Keil提供的C51工具链在命令行进行编译, vscode默认下面有终端所以基本编译都是上箭头+回车就可以解决.
通过脚本调用C51工具链, 顺序是C51编译器, A51汇编器, lx51链接器, OHx51文件转换器, 这四个程序的命令行使用方法可以在http://www.keil.com/c51里面查到.
附件是我自己用的脚本, 程序用python 3.6.3验证过了, 有一个KEILPATH变量需要自行修改, 可能要使用pip install click, 用于解析命令行参数.
附件: buildC51_py.zip

你可以直接通过在settings.json添加以下行:
"files.associations": {
        "main.c": "cpp"
    }
让vscode按cpp来解释你的.c文件就可以了。我现在也是用你的办法,非常好使。我原来的笨办法,还得改系统自带的reg52.h这种头文件,很麻烦。

离线

页脚

工信部备案:粤ICP备20025096号 Powered by FluxBB

感谢为中文互联网持续输出优质内容的各位老铁们。 QQ: 516333132, 微信(wechat): whycan_cn (哇酷网/挖坑网/填坑网) service@whycan.cn