您尚未登录。

楼主 #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)
{
}

离线

#2 2020-02-24 00:58:05

daydayup
会员
注册时间: 2017-10-09
已发帖子: 343
积分: 343

Re: vscode下编写c51程序

get这种骚操作!

离线

#3 2020-02-24 07:51:28

shaoxi2010
会员
注册时间: 2019-06-13
已发帖子: 364
积分: 313

Re: vscode下编写c51程序

不是提供了个lint.h么,这个没用么?

离线

#4 2020-02-24 08:22:17

jiangming1399
会员
注册时间: 2018-06-14
已发帖子: 113
积分: 113

Re: vscode下编写c51程序

我的方法和楼主的差不多,不过区别就是我用的是compiler.h这个头文件,用于兼容各个编译器不同的写法。把sdcc的compiler.h这个文件复制一份,然后针对不同的编译器的写法写不一样的代码,最后在没有匹配的(即lint)里面禁用。

离线

#5 2020-02-24 23:47:53

五十嵐雪華
会员
注册时间: 2020-02-24
已发帖子: 10
积分: 10

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

离线

楼主 #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
这种方式处理的。

离线

#7 2020-02-25 10:29:16

metro
会员
注册时间: 2019-03-09
已发帖子: 442
积分: 486

Re: vscode下编写c51程序

我也是用类似的方法解决的,不过对于SDCC的__asm ... __endasm;内联汇编代码暂时还没有太好的处理方式(VSCODE支持__asm__ ("...");的形式,但是就格式上来说和__asm ... __endasm;不同,后者类似于支持多行的字符串,也就是Python的三引号)。大家有什么好的办法吗?

离线

#8 2020-02-25 17:11:03

五十嵐雪華
会员
注册时间: 2020-02-24
已发帖子: 10
积分: 10

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

离线

楼主 #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这种头文件,很麻烦。

离线

#10 2020-04-22 20:32:39

liuchangyin
会员
注册时间: 2020-03-17
已发帖子: 204
积分: 199

Re: vscode下编写c51程序

vscode要是能用vax插件那感觉就爽了,可惜不能用,QT creator做IDE也还行

离线

#11 2020-07-02 02:16:17

JARK006
会员
注册时间: 2020-07-02
已发帖子: 1
积分: 1

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

厉害,受教了

离线

页脚

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

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