Passing a variable value to an interrupt function

Status
Not open for further replies.

thannara123

Advanced Member level 5
Joined
Jan 7, 2010
Messages
1,602
Helped
122
Reputation
244
Reaction score
116
Trophy points
1,353
Activity points
10,615
I have a project having more source file (ie .c file and correspondig .h files).
I am using MPLAB xc8 compailer .
I require a function return a variable value in adc.c file these varaiable value need to be use in anouther interrupt.c file .

I have tried , as follows
Declared the varaible as global variable (declared in main.c befor the main function) but not working .

Declared as extern int varaiable in header file
and defined in interrupt.c file but not working

may i get the any clarification
 

not working means i didnt get the variable value from one source file the other .
i frequanly updating an adc value which will give to the interrupt function

here i require the updating adc value from adc.c file pass to the interrupt.c file .

How to achive it .
 

I'm confused as to why you want to pass any variable available to an interrupt function at all. Interrupts in PIC devices are hardware triggered, they may use variables and produce variables but I've never heard of a variable being passed to an ISR.

Brian.
 

To add to betwixts comments, a good discipline is to use pointers to external variables
in an ISR, and minimize f() calls within ISRs whenever possible. Impacts performance,
stack push.....


Regards, Dana.
 

    thannara123

    Points: 2
    Helpful Answer Positive Rating
Calling functions in interrupt is generally possible, but has has prerequisites.
- the function must be either reentrant, exclusively used by interrupt or disable interrupts during execution
- stack and register usage in interrupt must be well considered

You want to avoid functions that involve a processing delay. In case of ADC operation, don't start the ADC and wait for completion inside the interrupt. Instead start ADC in one call and use ADC interrupt to process results.
 

    thannara123

    Points: 2
    Helpful Answer Positive Rating
thanks for the comments . Still am not corrected my problem. but learn the working of volatile and used it .

My requirement
i have main.c ,adc.c and interrupt.c source files with corressponding header file where the declared the necessary variables and functions .

in main.c file call a function named read_adc();
C:
void main()
{
       ADC_FDB=  ADC_read();
       LINE1;      
       sprintf(buffer,"%d",ADC_FDB);
        string(buffer);
       __delay_ms(100);

}

Here the ADC_FDB value updating as per changing the adc value working as expected .
C:
int ADC_read(void)
{
//.... some of code to read adc and some calculaton
;;
..
return adc_value;    //here is the adc_value need to be require in interrupt .c file or all over the project it is a volatie varable

}


here i tried the variable adc_value decalared as extern volatile int adc_value ,as global variable in main.c source file but not updating the value in interrupt.c file

here i tried the variable adc_value decalared as extern volatile int adc_value ,as in header file variable in interrupt.h but not updating the value in interrupt.c file

what wrong i did .

How does the datas in one source file takes to other files
ie the adc_value (adc read by interrupt method) which is given to to the interrupt routine to update the CCPR value .

used variable as global , extern ,tried in decalartion in main.c and other header files nothing benifited .

i am using MPLAB XC 8 compailer
 
Last edited:

Finnaly it worked
the problem i defined the variable as
extern volatile unsigned int adc_value; in herder file
and in source file it defined as unsigned int adc_value:
not included the qualifire volatile
changed to volatile unsigned int adc_value:

thankyou
 

Hi
in main.c file call a function named read_adc();
You mean "ADC_read"?

Now in "adc.c" there is the function "ADC_read" and in this function there is the variable "adc_value".
The value of adc_value becomes returned.
So it´s not good programming style to make adc_value a global variable (from an external module). Let it be "local" in the function.
But in "main()" you have the variable "ADC_FDB" (now containing the same value than adc_value) and it´s way better to declare "ADC_FDB" to be extern volatile.

Usually one wants to avoid to spread "global variables across several modules".
Thus the even more clean solution is that in "interrupt. c" you have a global variable (module_global, not external_ global) that becomes updated via an (external) function.
So no global varaible is handled across several modules.

Klaus
 

    thannara123

    Points: 2
    Helpful Answer Positive Rating

    thannara123

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…