sdcc giving syntax error on code that works in keil

926 Views Asked by At

I was trying to understand this code. It was in a tutorial, and I was following along the tutorial, and so I tried this code.

I compiled it using sdcc, but it is giving me a syntax error on column 7.

The tutorial shows that the code works in keil. And also I found out that you have to replace the reg51 header file with 8051 header file. but it is still giving a syntax error on column 7.

I don't know what the problem is. I can't even begin to understand it.

Error,

lcd.c:6: syntax error: token -> 'rs' ; column 7

#include <8051.h>

#define lcd_data P2

sbit rs=P0^0;
sbit rw=P0^1;
sbit en=P0^2;

void lcd_init();
void cmd(unsigned char a);
void dat(unsigned char b);
void show(unsigned char *s);
void lcd_delay();

void lcd_init()
{
    cmd(0x38);
    cmd(0x0e);
    cmd(0x01);
    cmd(0x06);
    cmd(0x0c);
    cmd(0x80);
}

void cmd(unsigned char a)
{
    lcd_data=a;
    rs=0;
    rw=0;
    en=1;
    lcd_delay();
    en=0;
}
1

There are 1 best solutions below

0
Jks Liu On

Compiler of Keil C51 has many extensions which are not standard, for e.g. sbit in your code. In SDCC you can use _Bool from C99 standard, or __bit, or __sbit, these 3 keyword are same in SDCC.

In your case, you must also use extension __at:

__bit __at (0x80) rs; /* I/O port 0, bit 0 */

WARNING: Do not use ~ to toggle bit, use ! in SDCC.

b = ~b; /* equivalent to b=1 instead of toggling b */
b = !b; /* toggles b */

For more information, please read the official document: http://sdcc.sourceforge.net/doc/sdccman.pdf