I have set myself up with on a breadbord with a simple PI16F1825. I am using MPLAB x with XC8 V2.45 i have a led that turn on/off every second using the simple delay function. i would just like to have a variable which increments every time it blinks. To check the increment of the variable I halt the target (I use picKit3) and check that the variable has incremented a sufficient number of times Vs how many times it has blinked. here is the code. easy enough
#include <xc.h>
#define _XTAL_FREQ 500000
#define PUSH RC2
#define LED LATC3
void main(void)
{
volatile int my_var;
my_var=0;
ANSELC=0; //select PORT C as GPIO
TRISC=0b11110111; //RC2 is input (Push) RC3 is output (LED)
OPTION_REG=0b01111111; //allow internal pull ups
while(1)
{
__delay_ms(500);
LED=1;
__delay_ms(500);
LED=0;
my_var=my_var+1;
}
}
the problematic line seems to be:
my_var=0;
this is just supposed to initialize the counter variable.
when the line is absent, and I put a breakpoint on line LED=1; I can see my_var incrementing 1 by one indefinitely. Of course it starts off at any random number as the variable has not been initialized, but clearly it increments with each blink.
when the line my_var=0 is present, on the same breakpoint, my_var will start at '0' and then toggle between 1 and 0. as if for some strange reason it went over the my_var=0 line at one point.
I tried with other values too. if I put my_var=5; the value will toggle between 5 and 6, again as if the code goes over the my_var=5 initialization line at each loop.
in fact if I put a breakpoiny on the my_var=0 line i can see that the codes actualy goes there at every blink! how is that possible? (i tried changing while(1) with for(;;)... same behaviour)
I've been doing a lot of Python lately, so maybe it is a nose in the middle of the face sort of mistake I made because I forgot C. If so I am very sorry, and greatfull at anyone who can point it out. I am a bit blocked in my project if i cannot even increment a counter!
oherwise i really don't see what i can do except check the generated assembly... wherever that is (if it even exists!)
asm("NOP")
and break on those to avoid the exact-line issue. \$\endgroup\$