Inexplainable “Error C141: syntax error” In C on Arduino [closed]

久未见 提交于 2021-02-05 11:15:29

问题


I've been getting into programming an Arduino just recently and I'm getting some weird Syntax errors which I dont understand at all in some of my first exercises.

Here is the first one:

#include <REG552.h>

#define LEDS P4;
#define SWITCH P5;

sbit led1 = P4^0;
sbit led2 = P4^1;
sbit led3 = P4^2;
sbit led4 = P4^3;
sbit led5 = P4^4;
sbit led6 = P4^5;
sbit led7 = P4^6;
sbit led8 = P4^7;

void main() {
    while(1){
        if(SWITCH == 0x0F){
            led1 = 0;
            led2 = 0;
            led3 = 0;
            led4 = 0;
            msec(1000);
            led1 = 1;
            led2 = 1;
            led3 = 1;
            led4 = 1;
        }
        if (SWITCH == 0xF0){
            led5 = 0;
            led6 = 0;
            led7 = 0;
            led8 = 0;
            msec(3000);
            led5 = 1;
            led6 = 1;
            led7 = 1;
            led8 = 1;
        }else
            LEDS = SWITCH;
    }
}

void msec(unsigned int tt){
    unsigned char jj;
    while (tt-- > 0){
        for (jj=0;jj<125;jj++){;}
        }
    }
}

Here im getting two syntax errors on line 17, one near '==' and one near ')'. No clue why.

Heres the second one:

#include <REG552.h>

#define LEDS P4;
#define SWITCH P5;

sbit switch1 = P5^0;

void main() {
    while(1){
        if (switch1 = 0){
            LEDS = 0x01;
            LEDS = LEDS >> 1;
            if else (switch1 = 1){
                LEDS = 0x01;
                LEDS = LEDS << 1;
            }
        }
    }
}

Here Im getting two syntax error on line 15, one near '=' and one near '<<'. Again no clue why. Could someone please enlighten me on the matter? I can't try and run any builds on my arduino this way .


回答1:


Don't use semicolons in macros.

#define LEDS P4;
#define SWITCH P5;



回答2:


You have to remember that the preprocessor (which handles macros) is not the compiler and doesn't need semicolons to terminate the "statements".

Macros are replaced quite verbatim, so in the latter example the line

LEDS = 0x01;

will, after the preprocessor replaces the LEDS macro, be

P4; = 0x01;

which is not a valid C statement.



来源:https://stackoverflow.com/questions/21159211/inexplainable-error-c141-syntax-error-in-c-on-arduino

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!