Electronics > Microcontrollers
Handling tasks in parallel with PIC16
newtekuser:
I have a project using a single PIC16 MCU that interacts with external devices (stepper, rtc) as well as driving a 16x2 LCD for the UI. Menu navigation is handled using interrupts and currently, the stepper is firing whenever an alarm is hit - all of this is polled in the main loop by checking the alarm flag in the rtc periodically.
While this works it is not ideal and I’d like to handle the stepper controls and rtc using interrupts or timers but I can’t figure out how while maintaining the ISR small and not crash the device.
Do I need to use a second MCU and “delegate” the stepper/rtc control to it while the main MCU is handling the UI or can this be done with just one PIC?
My issue is that even if I have an interrupt triggered by the rtc in the ISR when the alarm triggers I still need to call the code that moves the stepper. That is too much to have in the ISR and the device hangs/crashes.
forrestc:
I didn't hear anything that should be hard to do.
How are you writing the code for the PIC16? Is it in XC8?
Which PIC16? The suggestions of how to handle this is going to depend on the PIC and whether it's new enough to have the newer core independent peripherals and the like.
How are you moving the steppers? Do you have a driver that you give step and direction signals to or are you actually controlling the coils directly?
What type of motion are you doing with the steppers? Are we talking full-on motion control (CNC, 3d Printer, Robot) or something else?
Andy Chee:
I would adjust your ISR periods so that the stepper gets more ISR time than the LCD, human input. The latter items surely don't need to be checked/updated nearly as frequently as a stepper motor?
The term is task scheduling.
newtekuser:
The device is based of a PIC16F887. Compiler is XC8. The motor driver is a DRV8825 and the code for moving the stepper that get's called within the ISR is pretty simple, just a few lines but it's taking too many cycles as the stepper needs to move thousand of steps.
As far as I know the PIC16F887 does not support interrupt priorities and I can't move to another PIC. That's why I was asking if maybe my only option is to add another PIC, something cheap and tiny like the PIC16F18013T to handle the stepper separately from the rest.
Andy Chee:
--- Quote from: newtekuser on September 17, 2024, 05:19:53 am ---The device is based of a PIC16F887. Compiler is XC8. The motor driver is a DRV8825 and the code for moving the stepper that get's called within the ISR is pretty simple, just a few lines but it's taking too many cycles as the stepper needs to move thousand of steps.
As far as I know the PIC16F887 does not support interrupt priorities and I can't move to another PIC. That's why I was asking if maybe my only option is to add another PIC, something cheap and tiny like the PIC16F18013T to handle the stepper separately from the rest.
--- End quote ---
My first thought is;
is it necessary to wait for the stepper cycle to complete its entire movement, all within the ISR period? It seems to defeat the purpose of using an ISR in the first place!
My first thought is that the ISR can initiate the stepper movement, then resume main body code, then the ISR can periodically check whether the stepper movement has completed, upon which a flag "movement complete" can be set. Then your other routines can check whether this flag is set.
Alternatively, the ISR should only ever execute one stepper "step" at a time, NOT the complete stepper movement. Re-entering the ISR at regular (and frequent!) intervals will increment the stepper position step by step.
Navigation
[0] Message Index
[#] Next page
Go to full version