Keeping scom.h as is, your app.c should have #include "scom.h"
Confirm that variable i
of the type byte
is declared as volatile byte i;
and not being updated in any other piece of code after the ISR is executed and before it is assigned to j
in func()
.
In your function
void fun(void)
{
//some code
j=i;
//operations on j;
j = j & 0x0F;
k = j + 0x30;
lcd_puta(k);// displaying k on LCD
}
i
is not declared locally in //some code
, else i
will be taken locally due to block scope. You said that,
after using breakpoint at j=i; value of j itself is random value.
Reading this doesn't make clear if you have a breakpoint after the instruction i.e. at the next instruction or to the line where it says i=j;
If latter the case then, 'j' is yet to be assigned with the value of 'i'. Instead have i
in watch window while debugging.
This doesn't look like happening in your case but, I can see that j
is a global variable, is it extern
ed elsewhere and modified by some other ISR than the one shown in the post?
Being blind to that part of code (if it is there) I can predict that there are chances that one such ISR(if it is there) that change value of j
after is it assigned with i
and before being printed. For example:
ISR(TIMER_0)
{
//Clear and disable interrupt
j += 1; //can be anything that modifies j.
//Enable interrupt
}
void fun(void)
{
//some code
j=i;
//operations on j;
j = j & 0x0F; // Interrupt received here Or
k = j + 0x30; // Interrupt received here
//Here value of `k` is different than what is expected, obviously
lcd_puta(k);// displaying k on LCD
}
I am assuming that k
is of the compatible data type, preferably of the type byte
.
Though its not a really strict rule, should you declare a variable in an ISR? No, you shouldn't. Assuming the subroutines use the same register bank of course, Interrupts may have local variables just like normal functions. But, higher levels of optimization in the compiler will move variables automatically into the registers, thus saving them when an interrupt triggers. Please do not declare byte data;
in void interrupt_Rx(void)
.
but i am not able to operate on i/j.
kindly elaborate