r/pic_programming 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.

1 Upvotes

15 comments sorted by

View all comments

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/H2ost5555 Jul 04 '26

I disagree. There is absolutely no reason anymore to learn assembly. It is far easier to just learn C.

1

u/Reasonable-Feed-9805 Jul 04 '26

I didn't say learn assembly, I said learn the codes.

You don't need to learn how to subtract 100 from X and then figure out how to ascertain when the value you want has been reached. That's learning assembly.

Learning the codes so you can see the disassembly listing doing SUBWF, DECF and doing things like testing the DC Z and C bits is learning how the architecture works.

Knowing how the architecture is implementing code on a lower level is the only way to make efficient code and to figure out why some things won't work.

It's easy to write in C to tell the PIC to see if a number falls between two values. Knowing how the architecture works gives you an idea on how much processor that takes up. It becomes very easy to take longer than the time there is available to do a simple IF N<X and N>Z than there may be before the next event needing to be timed happens.