Communication between functions

Go To Last Post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello, I have one problem in AS7, I have written 6 small functions just simple math inside of them, 
I'll write one ex. 
void stripe()
{

for(a=100;a<120;a++)
OCR0A=a;
b=200;
OCR2A=b

 

}

void sec_stripe()
{
for (b=200; b<225;b++)

{

  a=120;
  OCR0A=a;
  OCR2A=b;

 

}

}

So, as you see I wrote variables by hand in second function, but what I want to do is that after finishing first function, I want second one automatically receive updated variables do it's own increment to specific variable and return result for 3rd one. I think I'll need additional variables for doing that but I have no idea how to do this exactly.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Not really understanding your requirement. But in C the way you pass the value of a variable into a function is something like:

void fn(uint8_t n) {
    PORTB = n;
}

int main(void) {
    uint8_t var = 37;
    fn(var);
}

But that just passes 37 into the function the "var" in main(0 cannot be updated by fn(). If you want to achieve that then rather than "pass by value" you "pass by reference". In this case you don't pass the value in "var" to the function you pass the actual location of the "var" variable in. The receiving code can then both read it AND modify it:

void fn(uint8_t * n) {
    PORTB = *n;
    *n = 46;
}

int main(void) {
    uint8_t var = 37;
    fn(&var);
    PORTC = var;
}

The & operator means "address of" so it's the address of var not the value in it that is passed into the function. The interface to the function is changed a bit. Instead of the simple "uint8_t n" previously it is now "uint8_t * n". This means it is a "pointer". It takes the address of a unit8_t variable, not just the value held in it. Because it is a pointer the contents are then accessed using the * operator:

    PORTB = *n;
    *n = 46;

*n when read means "go and get the value that is in location n". *n when written to means "store this new value into the location". So the place in memory where the original "var" lives now has 46 written into it. So when the control gets back to:

    PORTC = var;

The value now written to PORTC will be 46 - the newly written value.

 

For more info Google "C pass by reference value"

 

EDIT: for example:

 

https://gabrieletolomei.wordpres...

https://www.tutorialspoint.com/c...

https://www.haroldserrano.com/bl...

etc.

 

Your own C manual probably has something to say about "pass by reference" too.

Last Edited: Wed. Jan 16, 2019 - 09:10 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Or even, shudder, global variables.

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I just want control 2 PWM in one function first one must take every meaning from 100 to 120 while second one stays same, in other function second one must take every meaning from 200 to 225 while first one stays 120(number from first function) of course I can do it by hand but my program won't work like that after this step I'll need automatic update because I have almost every order of this 6 function.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I still don't follow what you are asking - perhaps try the question in your native language then get Google to translate ?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I think he either wants to return a value from a function or have variables that are known in both functions

 

if you want variables to be known in multiple functions you just do

 

uint8_t var1;
uint8_t var2;
uint8_t var3;

void func1(void)
{
   var1 = 1;
   var2 = 2;
}

void func2(void)
{
   var3 = var1 + var2;
}

int main(void)
{
   func1();
   func2();
 // at this point var1 = 1 var2 = 2 and var3 = 3
}

 

If you want a function to return a specific value

 

 

uint8_t func1(void)
{
   return( 8 );
}

int main(void)
{
    uint8_t var1;
    var1 = func1();
    
    // at this point var1 will be 8
}
Last Edited: Thu. Jan 17, 2019 - 07:32 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

levani2828 wrote:
I just want control 2 PWM in one function first one must take every meaning from 100 to 120 while second one stays same, in other function second one must take every meaning from 200 to 225 while first one stays 120(number from first function)

Just do not update registers which you would not them to be changed, they already keep their last value.

.

void stripe()
{
    for(a = 100; a < 120; a++)
        OCR0A = a;
}

void sec_stripe()
{
    for (b = 200; b < 225; b++)
        OCR2A = b;
}

 

Majid