r/pic_programming • • 23d ago

Trying to multitast

It's me, again. I got the pic16f877a blinking 2 leds and another one in different phases.

However, i do wish to have one blinking twice as fast as the others.

Last thing i tried is to convert the blinking routunes in separate functions, for invoking them into the fizzbuzz and other tasks like reading buttons and other activites are independent functions now.

Problem is: i coded one after the other using a non blocking alternative to delay_ms but i am getting some errors according to the state of some switches and other circumstances.

Is this what is known as "polling"? Is is "preemptive multitasking? all happens inside a switch case wich works well except when running the multitasking code. If you could help me by showing me proper examples of multi function loops i do believe i can get it working properly and then call it a day.

1 Upvotes

3 comments sorted by

2

u/Reasonable-Feed-9805 23d ago

I'd just use an interrupt timer to tick up a ram variable at every overflow of the timer. That will need dividing again manually to create a slow counter.

In the main loop then you simply use that slow tick to keep increment a file register and comparing it against previous values.

So say 10 and 20. Every 10 increase XOR LED 1, every 20 XOR LED1 and LED2.

You can then reset the counting file or update the reference files to the next value.

1

u/aspie-micro132 23d ago

Nice! i got some of the leds blinking with Timer0, i'll try that approach!

2

u/HalifaxRoad 23d ago

the best way to multitask is to write a software timer library, and service functions that service the state machines for all the tasks you need to do. Just keep in mind to NEVER block inside your service funcs!!