MCLR (Master Clear) on PIC microcontrollers

MCLR (Master Clear) on PIC microcontrollers

The Master Clear or MCLR is a feature present in many microcontrollers, used for resetting the entire device. This means that when the MCLR is triggered, the device will start over without unplugging or disconnecting the power source.

During this tutorial, I will use the PIC16F628A as example. You can download the datasheet by following this link.

Introduction

When the device in turned on, the firmware starts at the RESET vector, at address 0x0000. Each time the MCLR is actioned, the microcontroller will stop wherever it was and it will start over again at the mentioned vector.

The MCLR is an active low input. This means that the pin will be activated when the voltage at that pin will be a zero or low in positive logic; in English this means when the pin is between 0V and 0.8V. You can identify active low input by the bar above the MCLR. It's located at pin 4.

For most cases, a simple resistor and a push button in series will suffice (I really mean it). The resistor value shouldn't be higher that 40KΩ, but any value between 1KΩ and 10KΩ will be fine.

Add a capacitor in parallel to the pushbutton to reduce bumping. A 0,1µF will do the job. If your microcontroller controls sensible circuitry, then the capacitor is a must. A diode in antiparallel to the resistor will help to quickly discharge the capacitor when the power source is turned off.

Another recommendation, but far less used, is a resistor connected to the MCLR pin. This is used for current limitation. Don't place a resistor higher than 100Ω.

Configuration

In order to enable the MCLR pin, write down the following line of code under your configuration function.

#pragma config MCLRE = ON

Notice how there isn't any semicolon ‘;' at the end of the sentence.

If you don't need this function, you can still use the pin as a normal I/O. However, you should read the block diagram of the MCLR pin, which is RA5/MCLR/Vpp (page 33). Make sure that you know

Other microcontrollers, such as the PIC16F877A, has a dedicated MCLR pin on it. You can't disable it.

If you are using a ICSP, such as a PICkit 3 or PICkit 4, you will need this pin to program the microcontroller. This pin can only be used as input.

Example

A push button is connected to the MCLR; when is not pressed, MCLR is not activated. Before starting, the LED is off. When it starts, the LED will turn on and it will remain this way. Let's find out what happens when its pressed.

Diagram

MCLR
MCLR Schematic

Testing

The code for this test you will find it in this post for free.

As the pushbutton is pressed (and remains pressed) the LED turns off. It will remain off until the pushbutton is released, which then turns on the LED again.

What happened? The MCLR stops the entire firmware and also turns off the outputs. The function running at main() won't respond anymore. When the pushbutton is released, the firmware restarts.

Applications for MCLR

Resetting the microcontroller will have the same benefits as starting over your computer or router: it gives a fresh start for the device. You may need to do this for debugging purposes, give an external control to the microcontroller or if the code freezes because of a bug (or error). Consider this a useful feature for most cases.

Other sources for resetting the microcontroller includes: brown-out reset (BOR), sleep interrupt, watchdog timer (WDT), etc.

This function is TREMENDOUSLY useful when using a device that freezes because a bug in your code or if you use an always-on battery connected application or a device that has gone to ‘sleep' mode; even a device that has to restart their duty. Actioning the MCLR will be certainly more practical than unplugging the power cord.

You have reached this far!

Thank you for reading the blog post. Your comments and suggestions are welcomed. At the bottom of this page, leave a message or just say hi! The whole team of techZorro will appreciate it. Don't forget to share it on social media as well.

techZorro’s Index of Content

Click on the following link to browse likewise content in the blog in techZorro. This index will help you see what you are looking for in a bird’s eye view.

techZorro's Newsletter!

If you enjoyed this blog post, please subscribe to techZorro’s newsletter so you don’t miss any future blog posts!

One Comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.