I am trying to design an ON-OFF Keying decoder for an RFID reader project I've been working on. Below is the code that runs on a PIC16F887 @20MHZ. The compiler is Mikroelektronika mikroC PRO for PIC.
What I was trying to do is to toggle PORTC.1
every 200uS. The code runs as wished, but only for about 5.8 msec, as I measured with the oscilloscope. After that, PORTC.1
do not toggle.
I am guessing that somehow it is related to the overflow of the fiftymicros
and data_time
variables. If we increase fiftymicros
variable every 50 usec - actually this is what progam do, it will overflow after 255*50 microseconds which is equal to 12.75 msec.
Whenever I change the type of the variables fiftymicros
and data_time
to integer, the problem diminishes.
Could you help me understand what is the reason behind this behavior?
volatile unsigned char fiftymicros=0;
volatile unsigned char fiftymicroscounter=0;
volatile unsigned char data_time=0;
volatile unsigned int milis=0;
volatile unsigned int milis_ctr=0;
void calculate_data_time()
{
static volatile unsigned char ctr=0;
static volatile unsigned char ind=0;
if(fiftymicros-data_time>3)
{
data_time=fiftymicros;
PORTC.F1=~PORTC.F1;
}
}
void interrupt()
{
if(INTCON.T0IF)
{
TMR0=198;
INTCON.T0IF=0;
fiftymicros++;
if(++fiftymicroscounter==20) { fiftymicroscounter=0; milis++; }
}
if(INTCON.RBIF)
{
//data_time=fiftymicros;
INTCON.RBIF=0;
}
}
void main()
{
ANSEL =0;
ANSELH=0;
TRISA=0; PORTA=0;
TRISB=0x01; PORTB=0;
TRISC=0; PORTC=0;
TRISD=0; PORTD=0;
TRISE=0; PORTE=0;
OPTION_REG.INTEDG=1;
OPTION_REG.T0CS=0;
OPTION_REG.PSA=0;
OPTION_REG.PS0=1;
OPTION_REG.PS1=0;
OPTION_REG.PS2=0;
INTCON.TMR0IE=1;
INTCON.RBIE=1;
INTCON.GIE=1;
UART1_Init(9600);
Delay_ms(100);
milis_ctr=milis;
while(1)
{
calculate_data_time();
}
}