How does one create threads that run in parallel while programming PIC18, since there is no OS?
相关问题
- Multiple sockets for clients to connect to
- What is the best way to do a search in a large fil
- How to let a thread communicate with another activ
- glDrawElements only draws half a quad
- Index of single bit in long integer (in C) [duplic
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".
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
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.
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
andlongjmp
.The CCS compiler includes an RTOS. I haven't used it, but from the compiler manual:
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).
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.