Label Addressing in HCS12

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Label Addressing in HCS12

1,478 Views
WEbots
Contributor I
Hello,
I am working on an autonomous robot for a university robotics club.
I am trying to program a system that periodically interrupts and performs a different action from a sequential list of instructions. I've done this before using pure assembly with the P&E WinIDE on the same chip, so it shouldn't be too difficult in Codewarrior, but it has been. I can't seem to find a way to load a label into a variable.
I've tried using something like this:
void *address = &&Label_1

but CodeWarrior does not seem to recognize &&.
I tried using inline assembly to mark off each label using something like this:
__asm Label_1: ; // define a label
__asm LDD #Label_1; //load the address of the label into accumulator D

but Codewarrior gives me an error saying that Label Addressing mode is not supported!

So if someone knows how to make one of these methods work, that would be great. Or, if you know of a fast alternative (does not use many cpu cycles since this is a fairly short periodic timer interrupt), please let me know.

Here is an idea of the actions I want to take place:
[In init, define a label variable with the address of init]
[Interrupt occurs]
[goto/BRA to the label]

Init: // perform any initialization that only occurs on first interrupt
.
.
.
[Load Step1 address into variable] - This is where the problem occurs
[Return from Interrupt]

Step1:
.
.
.
[Load Step1 into variable] - problem again
[Return from Interrupt]

...
Step2: // Assume this is the last step
.
.
.
[Load Step1 into variable to start sequence all over] - problem again
[Return from Interrupt]

Notice that this method does not need to perform ANY checks after the interrupt,
it simply performs a goto/BRA, and starts into that stage of execution.
Thanks for any help you can provide with this!
Labels (1)
0 Kudos
2 Replies

362 Views
bigmac
Specialist III
Hello,
 
I am not quite sure what you are trying to achieve, but I wonder if an array of function pointers might work.  In the following example, three functions are defined, that each require an int argument.
 
/* Define 3-element array of function pointers */
void (*pfunc[3])(int) = {funcA, funcB, funcC};
 
void funcA (int)
{
 
}
 
void funcB (int)
{
 
}
 
void funcC (int)
{
 
}
 
(pfunc[0])(3);  // Call funcA(3);
(pfunc[2])(0);  // Call funcC(0);
 
Regards,
Mac
 
 
0 Kudos

362 Views
Steve
NXP Employee
NXP Employee
This is really a C programming question rather than anything to do with the S12, so you may have more luck in the CodeWarrior forum.
Goto is regarded as "infinitely abusable" and "never necessary" by the creators of C so you should avoid using this approach if possible.
If you have a finite set of labels you can simply store the function number in a variable and call the relevant function using a switch statement. Alternatively, you can pass a pointer to the function you want but that is adding a lot of complexity that probably isn't necessary here.
If the initialisation is known at compile time you can just use macros to select the appropriate function.
0 Kudos