# my first 8051 project , proplems with the program, hlp

Status
Not open for further replies.

#### evilheart

##### Member level 3
Again it is my first 8051 project , i am using C in programming , kiel as a compiler

i had a problem with one of the programs ,

#include <At89x52.H>
#include <string.h>
#define SIZ 17
#define p P0

typedef struct {

char name[SIZ];
int no;
int rank;
} worker;

void sendstruct(worker w);

main()
{
P3=0; P0=255;
worker w1;
worker w2 ;
worker m1;

this is the first part of the program ,
when i build the project it gives me a lot of of error messages but i think that most of them appear because it doesn't define the structure (worker) , i don't know why , i reviewed some of the C books i had , but i didn't find any syntax problem.

please any help will be valuable.

WB _ MAIN. C( 18 ) : error C141: syntax error near 'worker'

this is the first error , C( 18 ) is the line of "worker w1;"

#### mandar_mahajan

##### Full Member level 2
Try this way!!!!

struct worker
{
unsigned char name[16];
unsigned int no;
unsigned int rank;
};
struct worker my_worker;

use ur str as my_worker.rank // for ex...

#### evilheart

##### Member level 3
thx mandar

it did nearly the same error there is also an error message that i can't explain

WB_MAIN.C( 19 ) : error C141: syntax error near 'worker'

#### arthur0

##### Full Member level 2
Hi,
The syntax looks correct to me in your original code and I see no reason to do the changes suggested above. I believe it could be something compilator-specific, such as unseen/unprintable extra characters in the source, or the macro-definitions (e.g. carriage return not followed by new line; make sure you write the source in a compatible editor).

You could try to move the lines around a bit and see if the error follows that declaration (worker w1 and not a specific line, but I would begin by removing this line:
Code:
#define p P0
because, I think, it might eat up the typedef.

Another suggestion (not a syntax error though) would be to pass a pointer to (instead of a copy of) worker to sendstruct, like this:
Code:
void sendstruct(worker *w);
Arthur

#### evilheart

##### Member level 3
thx Arthur

i tried to move around the lines, and it worked!
it seems that i should put the (worker) variable declaration at the top of main ,
i don't know why but it worked ,
i have another question

the error message

WB_MAIN.C(36): warning C276: constant in condition expression

appear for the first line of

while(P3=0);
if(P3_1) sendstruct(w1);
if(P3_2) sendstruct(w2);
if(P3_3) sendstruct(m1);

ahmed

#### nandhu015

It should be

while(p3==0);

"=" is an assignment symbol & "==" is a comparison

Nandhu

#### evilheart

##### Member level 3
thx Nandhu

the error some how disappear by itself !
but i will keep the info in mind , the problems r not over yet any way!

first error

MAIN.C( 19) : error C141: syntax error near '='

for the code block
Code:
typedef struct{  int hh=0;
int h=0;
int mm=0;
int m=0;
int ss=0;
int s=0;     }time;        //type time

typedef struct{
char name[SIZ];
int no;
int rank;
time enter; }worker;                      //type worker
C( 19 ) is the first line

second error

MAIN.C( 34 ) : error C193: '*': bad operand type

Code:
int ptr=0 , mark=0 , r=0 , key=Q0+((Q1)*2)+((Q2)*4)+((Q3)*8) , k=0;
where

Code:
sbit Q0=P3^0;
sbit Q1=P3^1;
sbit Q2=P3^2;
sbit Q3=P3^3;
third error

MAIN.C( 115 ) : error C161: 'ck': formal parameter ignored

i don't know what that means

Code:
time clk;
worker buffer ; time *ck=&clk;
type time is initialized at the top of the page

Code:
void timer0_ISR() interrupt 1
{void timer0(ck);}

void timer0(time *clk)
{
TFO=0;
k++;

if(k==20)
{ k=0 ; (clk->s)++;} else return ;

if(clk->s==10)
{ clk->s=0; clk->ss++ ;} else return ;

if(clk->ss==60)
{ clk->ss=0; clk->m++;} else return ;

if(clk->m==10)
{ clk->m=0 ; clk->mm++ ;} else return;

if(clk->mm==60)
{ clk->mm=0 ; (clk)->h=1;} else return;

if(clk->h==10)
{clk->h=0 ; clk->hh++ ;} else return;

if(clk->hh==2 && clk->h==4)
{ clk->h=0 ; clk->hh=0 ;};

}          // end of timer subrotine
C( 115 ) is void timer0(ck) >>>>>sec line

Another errors

SEND AND RECV.C( 22 ) : warning C182: pointer to different objects

Code:
Getint(&(*w).no);
Code:
void Getint(int *A)  // grt and save int form port 0 to A
{
x : if(P0==127) goto x ;

EA=0 ; *A=P0 ; EA=1;}
///////////////////////////////////////

FUNCTIONS.C( 3 ) : error C129: missing ';' before 'clk'

it is in another file so it may be need the (type def) of (time) , but i used (extern) keyword , as it points to a declared variable in another file, there is no errors for the other variables by the way

Code:
extern time clk;
extern int ptr=0 ,extern mark=0 ,extern r=0 ,extern key,extern k=0 ;
extern worker list[SZ]; extern worker buffer ;

#### arthur0

##### Full Member level 2
Hi Ahmed,
I'm not trying to be rude here so I hope you're not taking this the wrong way, but I think you'd be better of starting with a book for beginners in C.
Since your problems are mostly programming/syntax errors, I sincerely think you will get more help understanding some basic concepts from a properly written course than you will get on a/this forum.
Now since you've asked, I'll answer, but this series of questioning could get on forever unless you learn the "proper way".

1) MAIN.C( 19) : error C141: syntax error near '='
typedef declares a type, not a variable, so there's nothing in there you could initialize.
A type is like the blueprints of a cabinet: it just shows how to build the thing, but there is no drawer to fill with stuff unless you construct the actual cabinet (equivalent with declaring a variable of that specific type).
In your case, you need to write:
Code:
typedef struct{  int hh;
int h;
int mm;
int m;
int ss;
int s;     }time;        //type time
Later on, when you declare your variable/object, you could write:
Code:
time t={0};	// initialized time var.
Also important to know here is to declare all types in a header file that you include in the modules (source files) referencing them, otherwise the type will be unknown when compiling the module and you get a syntax error (extern doesn't help with types, only with variables).

2) MAIN.C( 34 ) : error C193: '*': bad operand type
This could be many things. If key is a global/static variable (it's not obvious from the single line of code that you show), you can only initialize with an expression that evaluates to a constant, yet Qx are bits in registers/memory that require "runtime action" (a read), which the compiler cannot do.
If key is auto (inside a function, on the stack), then the error must have to do with how keil’s sbit works (I don't actually use keil, but I looked here: .http://www.keil.com/support/man/docs/c51/c51_le_sbit.htm).
It seems like the sbit declarations have different storage endianism than int and my guess is that you need to cast your Qs to int first.
May I also suggest shifting (<<) instead of multiplying with power of 2’s.
Better yet, write:
Code:
int key;
key = (int)(P3 & 0x0f);
3) MAIN.C( 115 ) : error C161: 'ck': formal parameter ignored
Remove the void before the call to timer0. This should take care of the error, but then I have some suggestions:
- why pass an argument to timer0, since that argument is global (clk)? The whole point with functions' arguments is to avoid using global variables, but you already have one, so drop the argument (or maybe you call it with other parameters somewhere else?).
- avoid using formal parameters with the same name as global variables (clk and *clk)! Although the rules are clear for the computer, the program becomes very confusing for the humans, and a program in C should be written for the humans (the computer talks a totally different language anyway).
To summarize, write:
Code:
 void timer0_ISR() interrupt 1
{ timer0();	// declared as void timer0(void); }

void timer0(void)
{
/* ... */
/* here you use the global var. clk as clk.s, clk.m etc. */
/* ... */
}
4) SEND AND RECV.C( 22 ) : warning C182: pointer to different objects
If w is worker *w, then you should write
Code:
Getint(&w->no);
[5][/5] FUNCTIONS.C( 3 ) : error C129: missing ';' before 'clk'
See answer 1). The time type is unknown here. Declare it in a header file and include it where needed.

Arthur

#### evilheart

##### Member level 3
and i appertiate ur efforts , that long reply was very useful

for me , i feel it is normal to ask that much, and also i don't think my questions are that basic(with out the structure question)
i think it is the first time to build a c program that size,first time to progam a microcontroller,first time to use structures and headers that intese, first time to use several files in one program

1)MAIN.C( 19) : error C141: syntax error near '='

iremoved the initiallization (= no.) , and it worked
i used

Code:
time t={0,0,0,0,0,0};   // initialized time var.
i also did ur advice and put my (typedef)s in
a header file, and the fifth error was solved.

2)any way , somehow the two other errors(2&3) disapeared
without fixing their problem , i don't know if they
will appear again by themselves or not

key is global by the way, i want it to represent the
value of a keypad , and i can't give him a whole port,
so i multiplied the pins by 2 powers.if u know other

i didn't know what u mean by shifting ,if it is shifting
bits , i don't know how it will help.

3)about the global variables , at first , i wasn't using arguments but the compiler was telling me that (s) from the
global structure (clk) was undefined in the interrupt , although it
was a member of a global structure, so i considered
that the compiler wasn't considering the members of global structures
(global) somehow (it seems that i was wrong about that also)
any way , that what made me try to use ptrs and arguments,

4)i tried the code

Code:
Getint(&w->no);
put the warning is still there

last thing(that the last error i hope)
i wrote the program in several files lcd.c,functions.c,
send and recv.c,main.c

i should make them (except main) header files , but at that time i didn't feel it was necessary,any way i converted the functions.c into a header file , to solve any var declaration problems (or that what i thought)

after doing that an error appeared

FUNCTIONS.H(11): error C202: 'buffer': undefined identifier

Code:
void menu()                            //menu that appears fo manager
{
if(buffer.rank==0)return;
else
{
lcd_cmd(0x01);lcd_cmd(0x80);
lcd_display("1.Set Time");lcd_display("  ");
lcd_diplay("2.Check");lcd_cmd(0x0c4);lcd_display("3.Exit");
switch(key)
{
case 1:set(clk);
case 2: checkA( );
case 3:exitw(mark);
case 13:return;
default:continue;
};
};
};
buffer is a global variable of defined type (worker), it was declared in main.c , i included functions.h
to main.c , so i think functions.h should read any var in main.c without any problems.

Code:
typedef struct{
char name[SIZ];
int no;
int rank;
time enter; }worker;                      //type worker

time clk = {0,0,0,0,0,0,};
worker buffer ; time *ck=&clk;
int ptr=0 , mark=0 , r=0 , key=Q0+((Q1)*2)+((Q2)*4)+((Q3)*8) , k=0;
thx again for ur help arthur

ahmed

#### arthur0

##### Full Member level 2
I didn't mean to offend you. I agree that there's "a first time" for everyone, and there's certainly no shame in wanting to know and asking. But... never mind, let's get down to business...

2) About your key variable, I guessed its purpose, and what you are trying to do it's not necessarily wrong, it's just unnecessarily complicated!
Code:
key = Q0+((Q1)*2)+((Q2)*4)+((Q3)*8);
makes use of 3 multiplications, which are very expensive on small uC (in terms of code size and clock cycles). I suggested you use the fact that shifting left (<<) one bit is equivalent with a multiplication with 2 (while shifting right is equivalent with dividing by 2), and shifting is more efficient.
Even replacing addition (+) with bitwise-or (|) can be more efficient:
Code:
key = (int)(Q0 | (Q2<<1) | (Q2<<2) | (Q3<<3))
Furthermore, observing that these are bits in the same register and that you don't need to change their order, you could simply read the whole register while masking the bits you're not interested in. So, you can replace your version of the code with mine:
Code:
key = (int)(P3 & 0x0f)
The errors that are "disappearing by themselves", could be caused by the errors you fix, so you should always attack errors in the headers and top of the files first.

4)I don't really know why you get this warning, it might be that you've declared it differently than the implementation. Check the prototype of Getint, it needs to be identical with the implementation and should look like this:
Code:
void Getint(int*);
(Btw, your original version was also syntactically correct, but is harder to read by humans.)

About your last errors: you can't make the source code into header files! (Well, you could, but you shouldn't!)
Header files should only contain macro definitions and declarations (such as types, functions' prototypes and things that do not translate into executable code or things that take space in memory).
Declarations of variables (they take memory space) and any executable code should be in written in .c files.

It's good practice to divide projects into logical and functional blocks that are defined and implemented in pairs of .h and .c files, respectively.
So, leave the .c files as they are, create one empty .h file for every .c file and move all declarations from .c to .h, then include them in the .c files as needed (avoid including .h in .h files).

Arthur

#### evilheart

##### Member level 3
thx arthur, again ur help was very valuable

Code:
key = (int)(P3 & 0x0f)
i divided the program now into main.c , functions.h(contains functions definitions , prototypes and type def and global var declaration) , lcd.h, sendrecv.h.

but finally a new error appeared

MAIN.C( 68 ) : error C249: 'DATA': SEGMENT TOO LARGE

it is because lack of memory , but i don't know is it the code or the variables , or which type of memory is not enough

Code:
#define SIZ 17
#define lcd_data P2
#define p P1
#define SZ 3
//////functions

typedef struct{  int hh;
int h;
int mm;
int m;
int ss;
int s;     }time;        //type time

typedef struct{
char name[SIZ];
int no;
int rank;
time enter; }worker;                      //type worker

void lcd_cmd(unsigned char cmd);
void lcd_display(char *s);
void set(time *L); // set structure type time values
void checkA();
void exitw();
void show_data();
void sendchr(char A);
void show_clk(time t);
void write_no(int no);
void Getstruct(worker *w);
void Getchar(char *A);
void Getint(int *A);
void sendchr(char A);
void lcd_init();
void timer0(time *clk);

time clk = {0,0,0,0,0,0,};
worker buffer ;worker list[SZ]; time *ck=&clk;
int ptr=0 , mark=0 ,
k=0;extern key;
global variables

Code:
int z;int Q ,key = (int)(P3 & 0x0f)
variables in main

any way that is the program ,

#### arthur0

##### Full Member level 2
Your coding style is a bit of mess, so I took the liberty of modifying some parts to exemplify how I would write.
Also, I suggested some changes and made some comments through the code. I have no possibility to compile the project (as I said, I don't use Keil) so there's no guarantee that you won't get more errors.
I didn't either have the time to go through all the code, nor did I understand everything that you're trying to do, but I've rewritten some pieces as an example.
I hope it helps and I didn't waste my time...

Arthur

#### evilheart

##### Member level 3
thx arthur
again i appreciate ur efforts with me,

about my coding style , well, as u r the first one to read most of it , then u see things about it more than me , in fact when i wrote the code i didn't concern myself that much about explaining every thing inside it , specially in main , u r right , all these multi-brackets and similar names can be quit confusing , but i generally like to use short names for variables and defined macros , and when i modify, i don't like to change a lot in the names of variables to decrease any possible syntax errors , any way , the next time i write a C program i may take another look to ur code to modify my style.

in fact i uploaded the program so u can help me to solve the (Large DATA segment) problem , any way the problem was solved , i moved the (worker) type array (list) , i declared it in main , and i used ptr arguments so my functions can access it.

i think i am done now with compiling errors and the errors now are linking errors!
and that it is a step forward (slow one but forward)

*** WARNING L13: RECURSIVE CALL TO SEGMENT
CALLER: ?PR?_CHECKA?MAIN

*** WARNING L16: UNCALLED SEGMENT, IGNORED FOR OVERLAY PROCESS
SEGMENT: ?PR?_SENDSTRUCT?MAIN

*** ERROR L114: SEGMENT DOES NOT FIT
SPACE: DATA
SEGMENT: _DATA_GROUP_
BASE: 0000H
LENGTH: 0083H

*** WARNING L1: UNRESOLVED EXTERNAL SYMBOL
SYMBOL: KEY
MODULE: main.obj (MAIN)

*** WARNING L2: REFERENCE MADE TO UNRESOLVED EXTERNAL
SYMBOL: KEY
MODULE: main.obj (MAIN)

the last error repeated several times
now i have object file for main but no hexa file for the project.

Ahmed

Status
Not open for further replies.