Multithreading using C on PIC18

2019-02-05 04:40发布

How does one create threads that run in parallel while programming PIC18, since there is no OS?

13条回答
爷的心禁止访问
2楼-- · 2019-02-05 05:01

I agree with ndim -- you can think of each interrupt handler as something like a "thread". Sometimes all the tasks you need to do can be handled by interrupt handlers triggered by external and internal events -- the "main loop" is an idle loop that does nothing.

I don't know where some commenters get the idea that there is "no OS" for the PIC18. There are quite a few PIC-specific multithreading libraries and "multitasking operating system kernels" for the PIC18, many of them free and open source. See PICList: "PIC Microcontroller specific Multitasking Methods".

查看更多
We Are One
3楼-- · 2019-02-05 05:03

Protothreads library is simple way for multi-tasking, cross-platform: http://dunkels.com/adam/pt/

Example of minimal scheduler for PT with task synchronization,timers,and user data: https://github.com/edartuz/c-ptx

查看更多
小情绪 Triste *
4楼-- · 2019-02-05 05:04

You could try Cooperative multitasking.

For types of problems that PICs solve, you'd probably be better of if you try a different design that uses interrupts or polling instead of multiple threads.

查看更多
手持菜刀,她持情操
5楼-- · 2019-02-05 05:04

You can put an RTOS on there (there's an unofficial ucOS port, or you could check out FreeRTOS's PIC18 port).

Otherwise, you could try implementing coroutines in C by using setjmp and longjmp.

查看更多
姐就是有狂的资本
6楼-- · 2019-02-05 05:09

The CCS compiler includes an RTOS. I haven't used it, but from the compiler manual:

The CCS Real Time Operating System (RTOS) allows a PIC micro controller to run regularly scheduled tasks without the need for interrupts. This is accomplished by a function (RTOS_RUN()) that acts as a dispatcher. When a task is scheduled to run, the dispatch function gives control of the processor to that task. When the task is done executing or does not need the processor anymore, control of the processor is returned to the dispatch function which then will give control of the processor to the next task that is scheduled to execute at the appropriate time. This process is called cooperative multi-tasking.

Just a word of warning - check their forums for info about the specific features you're looking for. Apparently CCS has a habit of releasing new features before they're fully tested. That's one reason I'm still using the older version (v3.249).

查看更多
兄弟一词,经得起流年.
7楼-- · 2019-02-05 05:11

Be aware that on microcontrollers, some "threads" can also be handled by just some specific interrupt handler, and thus run in "parallel" to your main event loop anyway.

E.g. if you have an external event trigger an ADC conversion, your ADC-conversion-done handler can take that value, do a few calculations and then set some output bits to adapt the control output according to the ADC value. All that can happen in the interrupt handler, and thus parallel to everything else.

Depending on the things you need to do in parallel, you can choose a combination of multiple techniques to make stuff work in parallel as intended.

查看更多
登录 后发表回答