avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Sunday May 19, 2024, 3:08 pm
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: Software timers and interrupts on a microcontroller  (Read 2382 times)


  • Participant
  • Joined in 2017
  • *
  • default avatar
  • Posts: 1
    • View Profile
    • Donate to Member
Software timers and interrupts on a microcontroller
« on: December 05, 2017, 01:09 AM »
I have few questions on software timers and interrupts on a microcontroller. Just for information, I use a PIC32 microcontroller(datasheet:

The final goal is to implement a serial communication protocol: RS485 with Modbus. I managed to transmit and receive a message, and now I have to do a message processing part.

Since I need a bunch of timers for all sorts of different tasks (e.g., 3.5 character delay needed for serial communication, some delay needed for buttons debouncing etc.), I plan to implement timers in a software using a single hardware timer. For example, hardware timer period is set to 100 us, and an interrupt is generated on every "overflow", i.e., every 100 us. In the ISR I just update global counters, which are basically software timers with a resolution of 100 us, whereas the hardware timer ISR has the highest priority. Is this a good way to do this, or is there some better way?

Each counter (i.e., a software timer) has defined its own period, and once the counter reaches its "period value", I want to call some function. Now it is not a very good idea to call this function from inside the hardware timer ISR, because that function will be processed with the highest priority since the calling routine is the hardware timer ISR. What I want is to define some function, let's say:

void Modbus_Protocol(void);
and to be able to define it as an interrupt, which will not be of the highest priority. Only the main hardware timer has the highest priority in this concept. In that way, once the counter in the hardware timer ISR reaches its period value, it wouldn't call its function, but it would rather just set a flag to trigger an interrupt (e.g., void Modbus_Protocol(void)), which will be triggered after the main ISR returns, depending on its priority. Can something like this be done, i.e., can I define software interrupts?

I know there is a possibility to use hardware interrupts which are not used, and to just set an interrupt flag from within the software. But I don't think this is an elegant way to implement interrupts if user defined software interrupts are possible.


  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 3,613
    • View Profile
    • Donate to Member
Re: Software timers and interrupts on a microcontroller
« Reply #1 on: December 05, 2017, 01:29 AM »
Any particular reason you choose such low-level hardware? A little more sazzy and easier programmable, IMHO, would be an ESP01 or ESP12 (ESP 8266 Wikiw) or an Arduinow or an even more high-level Raspberry Piw.