Detecting Missed Events using TMR2 with HLT

 Objective

In this example the MPLAB® Code Configurator (MCC) is used to set up Timer 2 (TMR2) on the PIC16F18855 populating the MPLAB Xpress Board to expect a push button press within 2 seconds of the previous push button press. Otherwise all four LED on the Xpress board will toggle ON/OFF every 100 ms as a sort of alarm.

This will be implemented without writing any code by using the TMR2 to setup the 2 second window, a Configurable Logic Cell (CLC1) to generate a logic HIGH when a pushbutton press hasn’t occurred, and a second Configurable Logic Cell (CLC2) that will serve as a gate and will pass a Pulse-Width Modulated Waveform (PWM6) toggling at a period of 100 ms to the LEDs when the first CLC output goes HIGH.

Press Switch Every 2 Second or Less!

OVERVIEW%201.png

Or Flashing LED Alarm Goes Off!

OVERVIEW%202.png

Background

A common task in embedded applications is detecting a missed event or, in other words, when something that is supposed to happen…doesn’t.

One example could be the detection of a motor stall. In this case, a hall effect sensor could be used to monitor motor rotation at a predetermined RPM. This type of application is often implemented using a Timer to set up the expected window of operation and a general purpose I/O to receive the input pulse from the hall effect sensor.

Core-centric Microcontrollers with very basic timing peripherals rely on the developer to write and then debug code to implement this kind of detection scheme. These software interrupt-based approaches introduce latencies that can dramatically delay responses to critical situations when these missed events occur.

These delays may be unacceptable with slower devices leading to system damage or potential danger to the end user. One solution is to configure the Microcontroller to run at very high clock rate, consuming more power.

However, 8-bit PIC Microcontrollers with the 8-bit Timer 2 with Hardware Limit Timer provide a hardware-based solution which reduces or even eliminates the amount of code that a developer needs to write and then debug while conserving power with slower clock speeds.

Applying Timer 2 capabilities to the previously mentioned motor stall detection example, the Timer can be configured to setup a window of operation where an event is expected to occur such as the periodic pulse from the sensor confirming motor rotation. As long as the pulse is received all is considered well. However, should the motor cease to turn unexpectedly and this expected pulse doesn’t occur within that window then the Timer 2 will generate an output pulse that can be routed using the 8-bit PIC’s signal routing capabilities to generate critical evasive measures such as removing power to the system. Since this is a hardware-based response the situation is dealt with almost instantly.

A flag can then be set to notify the CPU that a missed event has occurred and has been quickly dealt with automatically in hardware. In this way the CPU can make less critical changes to the system when it can.

 Materials

Hardware Tools (Optional)

Tool About Purchase
Xpress-50px.png
MPLAB® Xpress
Development Board

Software Tools

Tool About Installers
Installation
Instructions
Windows Linux Mac OSX
MPLAB® Xpress
Cloud Integrated Development Environment

Exercise Files

File Download
Installation
Instructions
Windows Linux Mac OSX
Project and Source Files
Board Schematic

 Procedure

Follow along with these steps to use the MPLAB Xpress Development Board with MPLAB Xpress (online) and the MPLAB Code Configurator plug-in.

1

Create Project

Create a new project in MPLAB Xpress for a PIC16F18855 using the MPLAB Xpress Development Board called Missing_Event_Alarm_using_the_HLT.

Instructions are below if this is your first project.

2

Open MCC

Open the MPLAB Code Configurator (MCC).

The default System settings can be used.

Instructions are below if this is your first project.

3

Timer 2 Setup

In this step the Timer 2 peripheral will be added and configured. Timer 2 will be used to monitor a pushbutton press within a 2 second window.

a

Adding TMR2

Add the TMR2 peripheral to the Project Resources by double-clicking on the peripheral instance under the Peripheral > Timer category in Device Resources.

deviceResources.JPG

b

TMR2 MCC Setup

Select Project Resources > Peripherals > TMR2 to open the Hardware Settings for TMR2 and configure as follows:

  • Clock Source: FOSC/4
  • Postscaler: 1:16
  • Prescaler: 1:128
  • Timer Period: 2 s (Actual Period: 1.998848 s) is used. This is the time that the TMR2 peripheral will count to before generating an output pulse.
  • Control Mode: Roll over pulse. This mode will generate an output pulse after the timer reaches the defined timer period of 2 s.
  • Ext. Reset Source: T2CKIPPS pin. This will use the T2IN input signal that will be tied to the RA5 pin later in this tutorial.
  • Start/Reset Option: Resets at falling TMR2_ers. The S2 switch on the MPLAB Xpress Board is tied to pin RA5 of the PIC16F18855. The RA5 pin is also pulled HIGH meaning that when the switch is pressed the RA5 input will transition from HIGH-to-LOW and then from LOW-to-HIGH when released. Later the T2IN signal will be tied to this pin which will make the RA5 input signal (the S2 switch) the TMR2_ers signal source. Therefore, using the Start/Reset Option of "Resets at falling TMR2_ers", when the S2 switch is pressed and the RA5 pin voltage transitions from HIGH to LOW (falling edge) the TMR2 clock will reset and restart counting from 0.

The TMR2 Hardware Settings should resemble the image below.

T2_Settings.JPG

4

PWM Setup

In this step the PWM will be setup to flash the LEDs ON/OFF.

a

Adding PWM6

Add the PWM6 peripheral to the Project Resources by double-clicking on the peripheral instance under the Peripheral > PWM category in Device Resources.

selectPWM6.JPG

b

PWM6 Timer

The PWM6 peripheral is a timer-based peripheral meaning that it will need a timer to drive it. PWM peripherals on 8-bit PIC Microcontrollers use the 8-bit even number timers. Therefore, TMR2, TMR4, TMR6 etc…

NOTE: All even numbered timers on recent 8-bit PIC Microcontrollers like the PIC16F18855 populating the MPLAB Xpress Development Board are multiple instances of the same type of timer that have the following features:

  • 8-bit resolution
  • Generate an output pulse when the Timer count matches the user-defined Timer Period value
  • Pre and Post scalers
  • Roll-Over Pulse, Monostable and One-Shot modes
  • Used as time-base for PWM peripherals

Add the TMR4 peripheral to the Project Resources by double-clicking on the peripheral instance under the Peripheral > PWM category in Device Resources.

selectTMR4.JPG

c

TMR4 Setup

Select Project Resources > Peripherals > TMR4 to open the Hardware Settings for TMR4 and configure as follows:

  • Clock Source: FOSC/4
  • Postscaler: 1:1
  • Prescaler: 1:128
  • Timer Period: 100 ms. This will set the associated PWM period when the timer is used as the time-base for a PWM module.
  • **All other settings can remain at their default settings.

The TMR4 Hardware Settings should resemble the image below.

T4_Settings(1).JPG

d

PWM Configuration

Select Project Resources > Peripherals > PWM6 to open the Hardware Settings for PWM6 and configure as follows:

  • Select a Timer: Timer 4 (configured in the previous step)
  • Duty Cycle: Make sure it is set to 50%

The PWM6 Hardware Settings should resemble the image below.

PWM6_Settings.JPG

5

Setting Up CLC1 and CLC2

In this step the logic will be configured and the appropriate signals routed to the flash the LEDs when the switch has not been pressed within the 2 second window developed in the previous steps.

a

CLC1

Add the CLC1 peripheral to the Project Resources by double-clicking on the peripheral instance under the Peripheral > CLC category in Device Resources.

selectCLC1.JPG

b

CLC1 Configuration

Select Project Resources > Peripherals > CLC1 to open the Hardware Settings for CLC1 and configure as follows:

  • Mode: 1-input D flip-flop with S and R
  • OR Gate 1: Select the TMR2=PR2 signal from the drop-down menu of the top input signal and connect to the top input of OR Gate 1 connected to the clock signal of the D Flip-Flop. The TMR2=PR2 is the pulse that will be generated by the TMR2 peripheral when the S2 switch hasn't been pressed within 2 seconds of the previous press.
  • OR Gate 2: Invert the output by clicking inside of the dotted box on the OR Gate 2 output connected to the D input of the D Flip-Flop. The output of the OR gates are default logic LOW. Inverting the outputs will drive the output HIGH.

In this configuration, the CLC1 output will be driven HIGH when the TMR2=PR2 pulse "clocks-in" the logic HIGH present on the input of the D Flip-Flop. The output will remain HIGH until a device RESET is performed. (i.e. either pressing the S1 RESET switch or by unplugging and then reconnecting the USB cable on the MPLAB Xpress Board)

The CLC1 Hardware Settings should resemble the image below.

CLC1_Settings.JPG

c

CLC2

Add the CLC2 peripheral to the Project Resources by double-clicking on the peripheral instance under the Peripheral > CLC category in Device Resources.

selectCLC2.JPG

d

CLC2 Configuration

Select Project Resources > Peripherals > CLC2 to open the Hardware Settings for CLC2 and configure as follows:

  • Mode: AND-OR
  • OR Gate 1: Select the CLC1_OUT signal from the drop-down menu of the top input signal and connect to the top input of OR Gate 1 connected to top input of the first AND Gate.
  • OR Gate 2: Select the PWM6_OUT signal from the drop-down menu of the top input signal and connect to the top input of OR Gate 2 connected to bottom input of the first AND Gate.

In this configuration, the CLC2 output will present the PWM6 output when the output of the D Flip-Flop in CLC1 goes HIGH as a result of the S2 switch not being pressed within the 2 second window defined by TMR2.

The CLC2 Hardware Settings should resemble the image below.

CLC2_Settings.JPG

e

Output Pin Setup

Navigate to the Pin Manager window and connect the CLC2OUT signal to the pins on the PIC16F18855 connected to the four LEDs on the MPLAB Xpress Development Board. Thanks to the Peripheral Pin Select capabilities this is as simple as clicking the pin fields under pins 0,1,2 and 3. In this way, all four of the LEDs will toggle ON/OFF with the PWM6 period when present on the output of CLC2 peripheral.

The **Pin Manager* should resemble the image below.

pinManagerOutput.JPG

6

Generate Code

Generate the code based on the above setting by clicking on the Generate button at the top right of the IDE and then click OK in the Generation Completed dialog

7

Program Xpress Board

Compile the project by clicking on the Make and Program Device button and then drag the .hex file to the Xpress board shown in the file manager.

 Results

Once the PIC16F18855 on the MPLAB Xpress Development is programmed and the S2 switch is pressed/released repeatedly within 2 seconds of power up/RESET and then thereafter within 2 seconds of the previous press the LEDs will remain OFF.

pressImage.JPG

If a press/release does not occur within the 2 second window the TMR2 Hardware Limit Timer will output a pulse into the first CLC which will clock the HIGH value into the D Flip-Flop. This will produce a HIGH on the CLC1 output that will subsequently cause the AND gate within CLC2 to follow the connected PWM6 signal toggling the four LEDs connected to the output of CLC2 HIGH/LOW (ON/OFF).

timeoutImage.JPG
© 2016 Microchip Technology, Inc.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.