I've read lots about functions and passing arguments but I'm completely confused.
I want to declare a function to split a 16-bit address into two 8-bit parts. I know how to do this, having found the solution:
addr_low=curr_addr&0x00FF;
addr_high=(curr_addr>>8)&0x00FF;
So, during my attempts to find out how to do this, I wrote:
addr_low=curr_addr&0x00FF;
addr_high=(curr_addr>>8)&0x00FF;
Serial.print("Address:");
Serial.print(curr_addr, DEC);
Serial.print('\n');
Serial.print("High Byte:");
Serial.print(addr_high, DEC);
Serial.print('\n');
Serial.print("Low Byte:");
Serial.print(addr_low, DEC);
Result in serial monitor:
Address:3000
High Byte:11
Low Byte:184
Which proved to me the method works. I'm defining my variables before void setup() thus:
unsigned int curr_addr=3000; //Decimal address
unsigned int addr_low = 0,addr_high = 0;
What I'd like to do is declare a function like this:
void addr_to_bytes()
{
addr_low=curr_addr&0x00FF;
addr_high=(curr_addr>>8)&0x00FF;
}
and call it in the code like this:
addr_to_bytes();
Serial.print("Address:");
Serial.print(curr_addr, DEC);
Serial.print('\n');
Serial.print("High Byte:");
Serial.print(addr_high, DEC);
Serial.print('\n');
Serial.print("Low Byte:");
Serial.print(addr_low, DEC);
and have the same result. I'm guessing that I'm getting incorrect results because the function is operating on the variables locally, despite them having being defined at the beginning of the program, I thought, as global variables.
Could someone show me what I should be doing or point me to a non-confusing tutorial please?