|
|
View previous topic :: View next topic |
Author |
Message |
aaronik19
Joined: 25 Apr 2011 Posts: 297
|
Functions order |
Posted: Thu Oct 24, 2019 5:47 am |
|
|
Dear All,
Hope that you are ok. I have a small program where I have 4 independent functions which turn on/off output bit.
Code: | void function1()
{
output_high(PIN_B1);
}
void function2()
{
output_high(PIN_B2);
}
void function3()
{
output_high(PIN_B3);
}
void function4()
{
output_high(PIN_B4);
} |
Now from my main code, the order can be executed dependent on the situtation. I have e.g function4, function2, function3, function1 and sometimes function3, function1, function4, function2.
Is there an efficient way how I can handle this? I am using the PIC18F25J50 crystal 20MHz. |
|
|
elcrcp
Joined: 11 Mar 2016 Posts: 62 Location: izmir / Turkey
|
|
Posted: Thu Oct 24, 2019 12:02 pm |
|
|
Not sure if this is the thing you are looking for but you can use this to do it on one line.
This won't do if you are using delays between each output bit changes.
Code: | #byte LATB=GETENV(“SFR:LATB”)
.
.
.
void func_name(int8 input_val)
{
int8 mask_byte=LATB;
input_val&=0x0F;
mask_byte&=0xF0;
mask_byte|=input_val;
LATB=mask_byte;
} |
This way you can change last 4 bits of portb without changing ms 4 bits. _________________ There is nothing you can't do if you try |
|
|
allenhuffman
Joined: 17 Jun 2019 Posts: 552 Location: Des Moines, Iowa, USA
|
|
Posted: Fri Oct 25, 2019 8:02 am |
|
|
If you want to get fancy, you could always use some arrays that hold the pins you want set in the proper order, and have one routine that just does what it finds in the array.
Code: |
unsigned int first[4] = { PIN_B1, PIN_B2, PIN_B3, PIN_B4 };
unsigned int second[4] = { PIN_B4, PIN_B3, PIN_B2, PIN_B1 };
// pseudo code - not sure what data type this array should be.
...
void setPins(unsigned int *pinArray, int pinCount)
{
for (int idx=0 ; idx < pinCount ; idx++)
{
output_high(pinArray[idx]);
}
}
...
setPins(first, 4);
setPins(second, 4);
...etc...
|
It could be made a lot cleaner than that, of course. But then you could just sequence the pins any order you wanted.
This is basically a "pattern sequencer" or "piano sequencer/piano roll" if you want to do some reading. I've used stuff like this on Arduino and BASIC Stamp to sequence outputs for haunted house prop controllers, triggering things in a proper order, with the addition of time delays and such between the steps. _________________ Allen C. Huffman, Sub-Etha Software (est. 1990) http://www.subethasoftware.com
Embedded C, Arduino, MSP430, ESP8266/32, BASIC Stamp and PIC24 programmer.
http://www.whywouldyouwanttodothat.com ? |
|
|
allenhuffman
Joined: 17 Jun 2019 Posts: 552 Location: Des Moines, Iowa, USA
|
|
Posted: Fri Oct 25, 2019 8:07 am |
|
|
And, if you really wanted to run functions in the order you want, you can make the table contain function pointers, then in the loop, just call each function by the pointer. The CCS compiler supports function pointers but we've ran into issues with them.
EDIT: See "Pointers and Arrays" topic, recently posted, for an example of this. _________________ Allen C. Huffman, Sub-Etha Software (est. 1990) http://www.subethasoftware.com
Embedded C, Arduino, MSP430, ESP8266/32, BASIC Stamp and PIC24 programmer.
http://www.whywouldyouwanttodothat.com ? |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|