8051 Programming syntax error problem

Status
Not open for further replies.

Embedded_Geek

Full Member level 6
Joined
Jul 5, 2010
Messages
342
Helped
58
Reputation
116
Reaction score
56
Trophy points
1,318
Location
Germany
Activity points
2,974
When i specified the macro "#define" the program is not getting compiled. It is showing syntax error in the following statement
RS=0;
RW=0;
EN=1;
The full code is
Code:
#include<reg51.h>
#define RS P2^0
#define RW P2^1
#define EN P2^2
sfr ldata=0x90;
void lcdcmd(unsigned char value);
void lcddata(unsigned char value);
void MSDelay(unsigned int itime);


void main()
{ 
  lcdcmd(0x38);
  MSDelay(250);
  lcdcmd(0x0E);
  MSDelay(250);
  lcdcmd(0x01);
  MSDelay(250);
  lcdcmd(0x06);
  MSDelay(250);
  lcdcmd(0x86);
  MSDelay(250);
  lcddata('M');
  MSDelay(25);
  lcddata('D');
  MSDelay(25);
  lcddata('E');
  while(1);
  }

void lcdcmd(unsigned char value)
{
	ldata=value;
	RS=0;
	RW=0;
	EN=1;
	MSDelay(1);
	EN=0;
	
}

void lcddata(unsigned char value)
{
	ldata=value;
	RS=1;
	RW=0;
	EN=1;
	MSDelay(1);
	EN=0;
	
}

void MSDelay(unsigned int itime)
{
	unsigned int i,j;
	for(i=0;i<itime;i++)
	for(j=0;j<1275;j++);
}
 

I don't use the 8051 but #defines are fixed - you cannot then change them in your program as you are trying to do.

Keith
 

ajishgopalr said:
as u can see the value is not changed in my program.

But it is. At the top you make RW equal P2^1 and later you try to say RW=0.

Once you #define something you can only use it, not change it.

Keith

Added after 6 minutes:

It might work if you put quotes round it - #define RW "P2^1" but I am not a C expert.

Keith
 

for example in my program i have used rs in my program. during compiling "rs" will simply be replaced by P2^0. When i use P2_0 instead of P2^0 then it works fine. I cannot understand what the problem is.
 

Sorry, it is out of my area of expertise. Maybe it is specific to your compiler or 8051.

Keith
 

Hi, there is a way for your application.

if you are coding in Keil, then using below code to instead of your,

#include<reg51.h>
sbit RS = P2^0;
sbit RW = P2^1;
sbit EN = P2^2;
//..........
 

if you are coding in Keil, then using below code to instead of your,

#include<reg51.h>
sbit RS = P2^0;
sbit RW = P2^1;
sbit EN = P2^2;
//..........

yeah that works fine. But i want to know why #define is causing problem.
 

hey just have look at ur reg51.h file
How zeroth or any corresponding bit of any port is associated and written in that header file..
this will solve ur problem n ll avoid ur confusion....
 
Ajishgopair has the solution, below is example driver.

#include <reg51.h>
#include "delay.h"

sbit LCD_RS =P1^0; // Register select
sbit LCD_E =P1^1; // Enable

#define LCD_STROBE ((LCD_E = 1),(LCD_E=0))


// Time in Milliseconds
void DelayMs(unsigned char cnt)
{ cnt=2*cnt;
do
{DelayUs(500);}
while(--cnt != 0);
}

// Send to Display
void lcd_write(unsigned char c)
{
unsigned char Movit;
Movit = (c >> 2);
P1 = (P1 & 0xC3) | (Movit & 0x3C);
LCD_STROBE;
Movit = (c << 2);
P1 = (P1 & 0xC3) | (Movit & 0x3C);
LCD_STROBE;
DelayUs(60);
}

// Clear and home the LCD
void lcd_clear(void)
{
LCD_RS = 0;
lcd_write(0x80);
DelayMs(2);
}

// Position the Cursor
void lcd_goto(unsigned char pos)
{
LCD_RS = 0;
lcd_write (0x80+pos);
}

// Send Character String
void lcd_puts(const char * s)
{
unsigned char cursor = 0x00;

while(*s)
{
LCD_RS = 1;
lcd_write(*s);
s++;
cursor++;
if (cursor==0x08)lcd_goto(0x40); // Change to 0x80 for simulation
if (cursor==0x10){cursor=0;lcd_goto(0x00);}
}
}


// Initialise the LCD - mode 4 bits
void lcd_init(void)
{
LCD_RS = 0;
DelayMs(20); // power on delay
lcd_write(0x28); // 4 bit mode, 1/16 duty, 5x8 font
LCD_STROBE;
DelayMs(3);
LCD_STROBE;
DelayMs(3);
LCD_STROBE;
lcd_write(0x28);
lcd_write(0x08); // display off
lcd_write(0x0C); // display on, cursor off
lcd_write(0x06); // entry mode
}
 
Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…