C Program to interface 8051 to a seven segment LED

Status
Not open for further replies.
Joined
Jan 10, 2016
Messages
5
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,455
C:
#include<reg51.h>
#include<stdio.h>

void delay_ms(unsigned int count);

void main()
{
    P1=0X00;
    //int i;
    //char seg_code[]={0x3F,0X06,0X5B,0X4F,0X66,0X6D,0X7D,0X07,0X7F,0X6F};
    while(1)
    {
        int i;
        char seg_code[]={0x3F,0X06,0X5B,0X4F,0X66,0X6D,0X7D,0X07,0X7F,0X6F};

        for(i=0;i<=9;i++)
        {
            P1=seg_code[i];
            delay_ms(1000);
        }
    }
}

void delay_ms(unsigned int count)
{
    int j,k;
    for(j=0;j<count;j++)
    {       
        for(k=0;k<300;k++);
    }
}

/********************************************************************************************************************************/


The above code runs perfectly. But if the integer and array initalization part preceeding the while loop which is currently comment, is enabled the compiler gives error. I'm a rookie in C programming any help is appriciated.
 

Hi
the compiler gives error
It gives an error... and what does the error message say?
Error messages are very useful informations to find out why they are and how to rectify the error.

We can't know unless you tell us.

Klaus
 
Put both the commented lines immediately after the '#include' statements so they are outside main().
Then remove the line starting 'char' inside the while() loop.

Your problem is with the scope of the variables, I think in this case you want the seg_code[] array to be global, that means moving it outside of any functions, including main(). When you use it there is no longer any need to use 'char' again as that would try to re-declare it again.

Brian.
 
Hi
It gives an error... and what does the error message say?
Error messages are very useful informations to find out why they are and how to rectify the error.

We can't know unless you tell us.

Klaus


Error Mesage:

 
Last edited by a moderator:



Duly noted. Thank you.
 

You did not declare the size of P1 in the declaration for starters. Unless your compiler defaults
to int for a variable, mine does not.

Then you need to remove the definition of variables seg_code and i inside the while loop,
and uncomment their dupes just prior to the while loop. Reinitializing variables inside a while
loop is a waste of code space if the compiler does not optimize that out, which mine does,
yours may not. Removing them inside while() and instantiating them inside main() makes
them local to main(), if thats your intention. I am not sure how the ANSI standard treats declar-
ations inside while() that is inside of main(), I assume they are treated the same as far as
scope is concerned.

Code:
    int i;
    char seg_code[]={0x3F,0X06,0X5B,0X4F,0X66,0X6D,0X7D,0X07,0X7F,0X6F};
    while(1)
    {
        int i;
        char seg_code[]={0x3F,0X06,0X5B,0X4F,0X66,0X6D,0X7D,0X07,0X7F,0X6F};

I concur with Brian, think seg_code should be outside main(), as I would guess its global
in usage for your application. Unless of course its not just a LUT but is dynamic in usage,
created and changed in other code.



Regards, Dana.
 
Last edited:
It may help to specify the size of array by putting 10 between the [ and ]. Some compilers will work it out for themselves, some have to be told and some will just set up a pointer to the array.

The more specific you are, the more predictable operation will be.

Brian.
 
Some interesting comments on array initialization -



Regards, Dana.
 

Interesting comments - I think it boils down to:
If you tell it the size it will use it.
If you let it size itself (dynamically) it may take into account the null terminator and be bigger than you anticipated.

Brian.
 

Point made let compiler compute size as it will get it right. I found
more than once I declared size omitting string terminator and got
it wrong.

I worked with compilers 40+ years ago that did not compute size,
would be interesting if anyone can find one today that doe not take
care of that.

Regards, Dana.
 

We can check size created using C.

I would posit if you declare and initialize it with a string it adds the null,
otherwise if initialized as an int it does not add the null.


Regards, Dana.
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…