Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

How Interrupt can return a value in MikroC

Status
Not open for further replies.

sanjoy

Member
In an interrupt in C, the ISR (interrupt service routine) does not receive any argument and dont return any value. But it may be great help to use ISR with return a value facility. Is there any option that the ISR can be used to return value, when an interrupt is on? thanks for help.
 
As you don't make a call to an ISR, how could you possibly return a value?.

As already suggested, set a value in a global variable in the ISR, and read that in the main program - it's probably a good idea to set a flag as well, so the main program can check if it's changed.

For example, a clock timer - the ISR increments a series of rolling counters, say every 10mS, so every 100 interrupts set a seconds flag, and every 6000 interrupts sets a minute flag, and so on. The main program detects the flag has changed, reads the value, displays it, and resets the flag.

Exactly the same in assembler as well.
 
As you don't make a call to an ISR, how could you possibly return a value?.

As already suggested, set a value in a global variable in the ISR, and read that in the main program - it's probably a good idea to set a flag as well, so the main program can check if it's changed.

For example, a clock timer - the ISR increments a series of rolling counters, say every 10mS, so every 100 interrupts set a seconds flag, and every 6000 interrupts sets a minute flag, and so on. The main program detects the flag has changed, reads the value, displays it, and resets the flag.

Exactly the same in assembler as well.
hmm, so it may look like this:

unsigned short min;
void main(){
TRISB = 0x01; ///INT at RB0
TRISA=0x00;
PORTB=PORTA=0x00;

INTCON = 0b10010000; // Set GIE and INTE bits for interupt
OPTION_REG.INTEDG = 0;
if (min<=9) PORTA=1;
}
//ISR
void interrupt(void){
unsigned short min;
if(INTCON.INTF == 1){
min ++;
INTCON.INTF = 0; INTCON.GIE=1;
}
}
The value of min is transferred from ISR. the concept is like that?
 
Remove "unsigned short min;" from the ISR declaration. This way, both main and ISR will access the same thing.
 
so the "unsigned short min;" before main remain unchanged.

Yes, it needs to be a global variable.

Here's a sample from a current project of mine:

Code:
    if(TMR2IF)                        // only do if timeout occured (50ms)
        {
            TMR2IF = 0;                // clear interrupt
            clkcount++;
            if (clkcount==10)        //action once per half second
            {
                clkcount = 0;        // reset 50mS counter
                if (!RB3)
                    RB3 = 1;        // set pulse output high
                else
                    RB3 = 0;        // set pulse low
                halfseconds++;        // increment halfseconds counter
                if (halfseconds >= 3600)    // auto-off after 30 minutes
                {
                    poweroff = 1;
                }

            }
        }

This part of the ISR toggles an I/O pin (RB3) with a 1Hz signal, and maintains a running count of halfseconds in a global variable, also it sets a 'poweroff' flag after 30 minutes running time (assuming that the global variable 'halfseconds' hasn't been reset in the main program). The variable 'clkcount' is also global, but isn't used elsewhere.
 
Not only global... Declare it as volatile or you may experience issues...

If you are using a free compiler then disregard this ( having said that some freebees do optimize a little.. )
 
thanks to all. pls let me ask another problem im currently experiencing. in the following program, what wd be the value of a, as it is assign as 8 bit data, however b is a 16 bit data. is it be option 1) 03 or 2)AF.
thanks
---------------------------
void main(){
unsigned short a;
unsigned int b ;
b=0x03AF;
a=b;
}
---------------------------
 
As an absolute pure guess I'd say 'AF' - but why not just run it and see what the result is?.

Presumably those more knowledgeable about C could tell you though.
 
Status
Not open for further replies.

Latest threads

Back
Top