10 Using TMR4 as HLT to Generate an Interrupt

This example will present how to use the TMR4 as a Hardware Limit Timer (HLT) in order to generate an interrupt and stop TMR2 that also stops the ADCC auto-conversion.

This application will blink the LED0 with Timer2 Period (100 ms), if the potentiometer value is below a desired threshold, and will keep the LED in an ON state constantly if the potentiometer value is above that value. If the ADCC read value is above the maximum threshold and the RC7 pin is pulled to GND for more than Timer4 Period (500 ms), TMR4 will stop TMR2 and LED0 will blink with a 500 ms period for as long as RC7 is tied to GND.

One practical use for this code example is in a motor control application where the ADCC reads the shunt current at a fixed frequency. The user needs to compare that value with a maximum current and, if it is above for more than a period, then the user will stop the motor since it is consuming too much power.

This example uses the PIC18F47Q10 Curiosity Nano board with a POT Click, both inserted into a Curiosity Nano adapter.

To achieve the functionality described by this use case, the following actions will have to be performed:
  • System clock initialization
  • Port initialization
  • PPS initialization
  • ADCC initialization
  • Timer2 initialization
  • Timer4 initialization
  • Interrupts handling and initialization