How to pass I2C data from ISR to Main

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

How to pass I2C data from ISR to Main

2,027 Views
Gig
Contributor I

If I write the ISR in the MCUinit.c file where do I define the global variables to pass info to main()?

Currently I have the global variables and ISR prototyped before main() and the ISR following main().

Seems that Device Initialization expects the ISR to be written in MCUinit.c but there is no place to define the globals there.

What am I missing?

Labels (1)
0 Kudos
Reply
3 Replies

580 Views
Alban
Senior Contributor II
Hello,
 
Global variables can be declared in any file.
Nothing can be "passed" to the main() as it is main(void);
ISR can be written anywhere.
 
In you next posts/questions, you need to state what you are using and with what
CodeWarrior version, if Processor Expert is used and more importantly put the MCU name in the subject line to clarify for everyone.
 
You have an example folder within your CodeWarrior installation.
Examples there show how to use interrupts.
 
Regards,
Alban.
0 Kudos
Reply

580 Views
Gig
Contributor I
I'm using CW5.1 and Device Initialization, target is MC9S08QG8.
The issue is I'm using Device Initialization to setup the IIC peripheral and it sets up the vector table, peripheral control register, and enables interupts etc.. In the MCUinit.c file it has the ISR started and says "put your code here", I cant put the global variables in that file because they will be deleted if I "generate code" again.
__interrupt void Viic_isr(void)
{
 /* Write your interrupt code here ... */
}
 
My mistake, i cant "pass" data to main but I do need to have data that the ISR gets from the peripheral made available to main, I assume I need to do this with a global variable.
 
In the example they dont use Device initialization to set up the peripheral, they put the globals and ISR in main.c.
 
So the question is if i put the ISR where Device Initialization asks me to put it where and how do I define/declare the global variables that are used in the ISR.
 
 
0 Kudos
Reply

580 Views
bigmac
Specialist III
Hello,
 
I guess there a number of possible approaches.  However, if you would prefer that the global variables be defined in the "main" file, and you also wish to retain the ISR function in the file MCUinit.c, it should only be necessary to declare the global variables used by the ISR function, prior to the ISR code.
 
extern byte myglobal;
 
Alternatively, you might have the ISR function simply call a single function located in the "main" file, or wherever.  In this case, the function prototype would need to be visible to the MCUinit.c file.
 
void IIC_proc (void);
 
__interrupt void Viic_isr(void)
{
  IIC_proc();
}
 
Regards,
Mac
 
0 Kudos
Reply