This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Pointer/Function help

So, I am trying to put together two (rather involved) functions that calculate the DAC. My processing routine for CalcDAC1 and CalcDAC2 are more or less the same, but with different variables. Currently, I'm using global variables that are directly called from the routine. I would like to use the same functionality but send it the variables I want the program to use to process so instead of:

global variables:
unsigned int var1a;
unsigned int var2a;
unsigned int var3a;
unsigned int var1b;
unsigned int var2b;
unsigned int var3b;

void main(void)
{

     while(1)
     {
          CalcDAC1();
          CalcDAC2();
     }
}

void CalcDAC1(void)
{

//Do stuff with var1a
//Do stuff with var2a
//Do stuff with var3a

}

Void CalcDAC2(void)
{

//Do stuff with var1b
//Do stuff with var2b
//Do stuff with var3b

}

I instead would like to do

void main(void)
{

     while(1)
     {
          CalcDAC(var1a, var2a, var3a);
          CalcDAC(var1b, var2b, var3b);
     }
}

void CalcDAC(unsigned char var1, unsigned char var2, unsigned char var3)
{

     //Do stuff with var1
     //Do stuff with var2
     //Do stuff with var3

}

Unfortunately, my particular function has about 7-10 variables (complicated equation with lots of variables) so it requires me to create a LOT of local variables for each iteration, which I don't have the local space for (if that's the right term). Instead, I would like to send it pointers to local variables (if possible).

I ASSUME it would look something like this:

void main(void)
{

     while(1)
     {
          CalcDAC(*var1a, *var2a, *var3a);
          CalcDAC(*var1b, *var2b, *var3b);
     }
}

void CalcDAC(unsigned char *var1, unsigned char *var2, unsigned char *var3)
{

     //Do stuff with var1
     //Do stuff with var2
     //Do stuff with var3

}

Is this correct or is this even possible in C? Is this something you can only do in C++? Any help with implementation you can give me would be greatly appreciated. Thanks!

Parents Reply Children
  • To answer your question, yes, I spend a lot of time writing code and testing it. The whole reason I originally came here is because I am maxed out on global variables in the data register and need to start using the idata register. Unfortunately, I don't know the correct way to setup things like structs in the idata register (which I think I understand a bit now).

    Most of my recent questions have been clarifying what you're suggesting for me. I don't fully understand some of what you're asking so I'm creating example code to demonstrate this (my code looks nothing like the examples I'm giving. My current code is about 6k).

    I have a number of different projects I am working on at the same time. At this point, I'm doing research on the best way to approach this portion of it. While I've been asking questions here, I've been working on other parts of my code. I didn't realize posting in these forums was going to be such a nuisance.

    My experience with forums has typically been as long as it's for learning and you're not expecting other people to write your code for you, people are generally pretty helpful. My questions have fallen into that category for the most part (with the only request for code pertaining to suggestions I didn't fully understand the implementation of).

    Is it a problem to ask a lot of questions in a forum that is, for the most part, pretty dead? Or is there a better place to ask these questions?

  • because I am maxed out on global variables in the data register and need to start using the idata register.
    they are not registers, they are memory areas

    Is it a problem to ask a lot of questions in a forum that is, for the most part, pretty dead? Or is there a better place to ask these questions?
    For a '51 question not related specifically to Keil a better place might be http://www.8052.com/forum/.

    back to your question:
    there are many address spaces in a '51
    CODE
    register banks
    BDATA
    DATA
    IDATA
    SFRs
    XDATA

    you need to fsmiliarize yourself with the processor, an the best place is "the bible" chapters 1, 2 and 3, you can find a link here www.8052.com/.../120112

    Erik

  • Heh, thanks for the reply. I wasn't sure of the word I needed (as stated in previous posts) but I had a feeling "register" was the incorrect word.

    Regarding the chip/code I'm using, just to specify based on what you share, I am using a M8051W chip, which has 256 bytes of on chip RAM so I have:

    8K code space (I am currently utilizing just under 7K of it)
    128 bytes of Data space (which is all used up)
    128 bytes of Idata space (of which I'm using approximately 62 bytes of it)
    No Xdata on my chip
    The ESFR and SFR registers are all assigned to various functions of the chip

    Right now, I seem to be juggling between data/idata space and code space so I'm trying to find the most efficient ways of doing so, hence why I come to this forum. I can mickey mouse my code and MAKE it work but I'd rather learn how to do things correctly from people who have coding experience, hence why I came here.

    Thanks for the links you gave me. I will check them out. Thanks for the help!

  • On a sidenote, is there any reason everything designed by Keil or anything related to the 8051 looks like it was designed in the 90's? Every tool or forum looks extremely dated. Is that because long time programmers don't like changes or are the developers unable/unwilling to update the look/feel of the program? For a $2k-3k piece of software, you'd think they'd want to make it look a bit more up to date.

  • Right now, I seem to be juggling between data/idata space and code space so I'm trying to find the most efficient ways of doing so, hence why I come to this forum. I can mickey mouse my code and MAKE it work but I'd rather learn how to do things correctly from people who have coding experience, hence why I came here.

    basically the right way is to base it on "access frequency" and size.
    the stack (which is indirectly addressed anyhow is automatically placed at the top of (I)DATA which is where it belongs

    frequently accessed "directly addressed" variables should be in DATA
    frequently accessed "indirectly addressed" variables (straucts, arrays) should be in DATA
    large stuff should be in XDATA

    take a peak on the generated assembler for this

    char xdata x;
    char idata i;
    char data  d;
    bit  bdata b;
    
    char justFun j;
    
    j = x;
    j = i;
    j = d;
    j = P0;
    

    Erik

  • Sorry, I completely forgot to get back to this. I got everything working. Thanks a lot for the explanations!

  • i have the same problem exact. what you did do to fix it?
    waiting for honest reply.

  • Exactly what specific problem.

    And exactly what do you find haven't already been answered in this thread?

  • hi. i dont understand the bdata and xdata and idata and code and bit. what is it doing?

  • Then you do not have exactly the same problem and this thread isn't for you.

    Start my reading some documentation about the 8051 architecture, and the different memory address spaces it has, and the corresponding processor instructions for addressing data in the different memory address spaces.

    You may also take a quick look on Wikipedia about Harvard and Von Neumann architectures.

    It's hard to look into the finer points of architecture-specific keywords for the C compiler, if you don't understand the architecture that the compiler have to map the generated code to.

  • i dont understand the bdata and xdata and idata and code and bit. what is it doing

    Start my reading some documentation about the 8051 architecture, and the different memory address spaces it has, and the corresponding processor instructions for addressing data in the different memory address spaces.

    you need to familiarize yourself with the processor, and the best place is "the bible" chapters 1, 2 and 3, you can find a link here www.8052.com/.../120112

    Erik

  • my book doesnt have them in the index. why dont you tell me what they are for?

  • my book doesnt have them in the index.
    it it is not a '51 book it will not. if it is a '51 book, look for "memory areas"

    why dont you tell me what they are for
    sure, but there is not room to write 25000 words in a post. READ "THE BIBLE" (link in above post)

    if you are one of those i eh people that believe "C is C" then drop all intents of working with micros.

    Erik

  • what do you mean. i am using keil c. it understand normal c programs?

  • If the Keil C compiler have extra keywords - added specifically because the 8051 processor is a bit different from normal processors - that relates specifically to the 8051 processor, then you might be able to realize the need to figure out exactly what is different with the 8051.

    A normal processor can't address individual bits. So the C language don't have "bit" as a data type. So what happens if a user wants their C program to take advantage of the specific bit commands in the 8051 processor?

    A "normal" processor don't have multiple memory regions that all can have address 0 (zero) but that represents totally different data. So a "normal" C compiler don't need ways for the programmer to specify which memory region that should be accessed.

    So - have you read up on the 8051 architecture yet? Because you will not be able to understand the extra 8051 keywords before you have understood the specials with the 8051 architecture.

    And by the way - why are you still asking questions in this thread? This thread was about when to use the different regions. Your problem is to even understand that they exist. Why they exist. And why you need to know that they exist. First them, will it be possible to debate the advantages of making use of them in different ways - which this thread was about.