r/pic_programming • • 4d ago

Timing issues

I got the pic doing what i want, selecting a menu and blinking leds accordingly.

One problem i have is: if in one option i do have certain timings between leds on and off, if i run another one with the same timings, they do all betweeen 10 and 100x faster. Does exist any setting i may be missing that XC8 is resolving itself about how to count miliseconds between tasks? I am not using __delay_ms but this. I did change all my variables to char since using int did exceed the pic16f877a 's capacity.

void Wait(unsigned char waitticks)

{

unsigned char waitcount = 0;

INTCONbits. T0IF = 0; // Clear flag

while(waitcount < braketicks)

{

if(INTCONbits.T0IF)

{

INTCONbits.T0IF = 0; //Reset Overflow Flag

waitcount++;

}

}

}

0 Upvotes

1 comment sorted by

2

u/HalifaxRoad 3d ago

I tire of this shit with you. You do not understand you have been asking the same question for over a year. You are missing fundamental knowledge about the C language, yes you are using a timer instead of calling __delay_ms() . But it sure looks like you are polling the timer until it has expired in a blocking call! Defeating the purpose of even using a HW timer!

As I have stated, and other people have stated several times. You are missing basic building blocks of how you multitask in C languages.

I would be more than happy to give you a lesson on how to do this over a video call, because it will be hard for me to do this over DM's, especially when the time difference appears to be a bit large. I will even wake up in the night to match your schedule if need be.

Assuming you dont want a lesson on this.The building blocks at a minimum it appears to me that you are missing is:

Task service functions. You build functions to service any time dependant tasks that must be done in parallel. 

To accomplish this, you need to know at a minimum, how to build and use finite state machines in C along side software timers that poll a hardware timer, so that each state machine checks if a softwaretimer has reached or exceeded a calculated number of ticks, all while handling the inevitable wrap around that you have with HW timers.

That also means you need knowledge of how integer overflow and underflow work on the cpu, and how you can use that to work around the wrap around that will will inevitably happen as the timer overflows and starts back from 0, and how your time when done may end up being lower than the time when started.

Non of this is particular difficult assuming you understand all the building blocks, and can put all the pieces together.

Again I will gladly help you to understand everything you need. But I cant do it over text, aside from just doing it for you and sending the code. Which you will learn nothing from.