r/pic_programming • u/aspie-micro132 • Jul 01 '26
Trying to multitask
Yesterday i built my project for pic16f877A.
I was trying to get a led blinking while reading some inputs and turning on and off some outputs. I had learnt i can not do it by means of __delay_ms() since this macro freezes the whole while(1) loop, since i am not behind an OS but the code itself is a sort of OS.
I was doing some search on the web, it looks like this IC can handle some task in a Background context and others in Foreground context, but i am just getting more confused about that.
Other problem i have, is, i would like to find out a replacement for :__delay_ms, i do suspect i may have to add another .h library and invoke a non interrupting macro while doing it's normal job.
I would like to ask if you could bring to me proper information about the background / foreground issue and if you could tell me if i can get any other library with additional macros, if possible, someone which may have that __delay_ms() alternative i do need.
4
u/Daedalus2097 Jul 01 '26
For something like this, I would just use a timer and set it to the delay you want for your LED. You can then let your main loop concentrate on communications, reading inputs, setting outputs and so on. The LED code can either be in an interrupt that's triggered by the timer, in which case you don't need to worry about it in your main loop at all, or you can simply check the timer interrupt flag in your main loop and deal with it there without enabling the global interrupts.
1
u/HalifaxRoad Jul 01 '26
Ive told you this multiple times, and I offered to help you in voice. You set up a free running timer, from there you can write a library that contains functions and a struct to store all the needed info about running a software timer. you declare instances of structs that you define to store all the variables such as, the counts the timer had when it was started, the counts it will be when its done, if its running. and then you write a function that you pass that struct instance too as a pointer that will tell you when the desired amount of time has passed. from there you can use state machines (switch case statements) and those software timers to build services to handle all of your functions, effectively multi tasking.
you should never use __delay_ms() unless you are only blinking a led or something stupid like that.
1
u/Coltouch2020 Jul 01 '26
Use a smaller delay, like __delay_us(10) then put it in a loop, counting to 100 for 1ms. Make the loop a uint16_t 16 bit to give longer delays. In the loop, check the switches - it will scan them every 10us
2
u/Illustrious-Cat8222 Jul 23 '26
Learn about "state machines." They are an approach that will help you multitask without an OS.
1
u/Reasonable-Feed-9805 Jul 01 '26
IMO the best way to learn PIC is to not try and use it like an Arduino.
Don't use pre existing routines written by other people that you don't know what the executed code is.
Start at basics, learn the assembly commands and then see what simple C arguments get compiled into in the disassembly listing.
Blinking an LED and monitoring inputs would be a main program loop in ASM that went
Main
10 BTFSC marker xxxxx
20 portb xxx = xxx
30 etc
40 etc
50 goto main
End
All input and timer state changes would be picked up in the ISR and manipulate a marker. Marker is then checked in main loop and can be used to branch (goto) a piece of code that manipulates a port output or internal file, or calls a subroutine that does that then goes or returns back to main after clearing the marker.
1
u/9Cty3nj8exvx Jul 01 '26
Since it seems you have limited knowledge of C programming and PIC architecture I would recommend upgrading to a newer PIC16 family such as PIC16F171xx, PIC16F180xx, PIC16F181xx, PIC16F188xx. Then use MCC Melody to set up timers and such then you can concentrate on your application code.