I'm having problems reading a char array inside of an ISR. I'm using PIC18F4680 and C18 compiler version 3.41.
The array has several elements which are populated from data read from an RTC and they should be later on used to generate data in another matrix which will be later on used to drive a 5x7 display.
My problem is that when I read the elements of the array, I get one set of values in the ISR and another outside of the ISR.
Here's the output of what I'm getting:
1 1
0 0
0 0
1 1
0 0
0 0
1 1
0 0
0 0
*******************
1 7
0 0
0 0
*******************
1 1
0 0
0 0
1 1
0 0
0 0
1 1
0 0
0 0
The top line is seconds, second line is minutes and third line is hours since the system was started. The three lines with stars above and below them were send from the ISR. As we can see, the value of seconds was 11 before ISR, 17 in ISR and 11 after the ISR. I've also noticed that this never happens during first 10 seconds after start of a minute or during last 10 seconds of a minute. At other times the changes seem to happen at random.
Here's the relevant code:
k=0;//this is a sort of critical section
data[0]=from_seconds (data[0]);
data[1]=from_minutes (data[1]);
data[2]=from_hours (data[2]).time;
k=1;//if it's zero, ISR won't read the data
for (i=0;i<3;i++)
//I made sure here that the counter variable isn't used anywhere else
{
WriteUSART ( (data[i]/10)+48 );//Converting decimal into ASCII
putrsUSART (" ");
WriteUSART ( (data[i]%10)+48 );
putrsUSART ("\r\n");
}
putrsUSART ("\r\n");
putrsUSART ("\r\n");
putrsUSART ("\r\n");
putrsUSART ("\r\n");
Here's the ISR:
if (INTCON3bits.INT1IF)
{
if(k)//I know that I'll miss the interrupt if the k is zero, but I'll fix that later
{
fill_matrix (data[2],data[1], data[0]);
putrsUSART ("\r\n");
putrsUSART ("\r\n");
putrsUSART ("*******************");
putrsUSART ("\r\n");
for (j=0;j<3;j++)
{
WriteUSART ( (data[j]/10)+48 );
putrsUSART (" ");
WriteUSART ( (data[j]%10)+48 );
putrsUSART ("\r\n");
}
putrsUSART ("\r\n");
putrsUSART ("\r\n");
putrsUSART ("\r\n");
putrsUSART ("*******************");
putrsUSART ("\r\n");
}
INTCON3bits.INT1IF=0;
}
Any ideas what could be happening here?
UPDATE:
About tcrosley's comment: The part separated by exclamation marks is read by the reading ISR and the part separated by stars is made by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
„ 0 С 0 ћ
0 „ ћ 0
!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
P ћ ћ P
„ 1 ± 0 ћ
… 0 ± P ћ
„ 0 P ћ
„ 0 С 0 ћ
„ 0 С 0 ћ
0 „ ћ 0
!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
P ћ ћ P
„ 1 ± 0 ћ
… 0 ± P ћ
„ 0 P ћ
„ 0 С 0 ћ
„ 0 С 0 ћ
0 „ ћ 0
!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
P ћ ћ P
„ 1 ± 0 ћ
… 0 ± P ћ
„ 0 P ћ
„ 0 С 0 ћ
„ 0 С 0 ћ
0 „ ћ 0
!!!!!!!!!!!!!!!!!!!!!!!!
As far as I see, the reading ISR always gets the correct data from the matrix, even after several tens of seconds.
kvolatile? \$\endgroup\$