Turning on a LED using PIC16F84

Summary of Turning on a LED using PIC16F84


This experiment uses a microcontroller to light an LED on Port B bit0 when a button on Port A bit0 is pressed (positive logic). Main continuously polls the input and runs loops to set RB0 HIGH or LOW. Experiment 1a adds a 0.5s delay so the LED stays on for 0.5s when A is pressed; debounce is not handled here. Bit manipulation is used to control single output lines safely; longer delays are created by calling a shorter Delay subroutine multiple times.

Parts used in the Experiment 1 (Turning on a LED):

  • Microcontroller (PIC16F84)
  • Push-button switch connected to Port A bit0 (RA0)
  • LED connected to Port B bit0 (RB0)
  • Current-limiting resistor for LED
  • Power supply (Vcc and ground rails)
  • Driver transistor (optional, for alternate program)

This experiment turns on a LED when button A is pressed.
The button is connected to the lowest bit of Port A (bit0). When the button is pressed, the line goes HIGH. This is called positive logic.
The LED is connected to the lowest bit of Port B (Bit0). Any bit of any port can be an input or output. It is convenient to make port “A” an input and port “B” an output. Port A has 5 lines and port B has 8 lines.
The circuit below shows the active components for Experiment 1. The switch is connected to RA0 and the LED is connected to RB0.

LED

Any port-line can be changed from an input to an output or vice versa, at any time,  during the running of a program.
If you are going to change the direction of a port-line, you must take into account any devices connected to the line. For instance, if a switch is connected between an output of the chip and rail; if the line is an output and is made LOW, the chip will be damaged when the button is pressed.  

The program consists of two loops in Main. The first instruction in Main looks at the input line and determines if the button has been pressed. This where a decision is made. If is is  not pressed, the micro is taken to the next instruction in the program and a loop is executed that makes an output line LOW to turn off a LED. If it is pressed, the micro is taken to the third instruction in Main to turn ON an output bit to illuminate a LED.
The micro never stops executing instructions. It must be executing one loop or the other.
Main must be a loop (or more than one loop) as this is where the LED is turned ON and OFF and the microcontroller will loop this routine many times per second.
In Main, instructions will take the micro to other routines called sub-routines, carry out operations such as delays, produce beeps, etc and return to Main, as you will see in a moment.
Study the following experiment then click HERE for a test.

EXPERIMENT 1a
Turning on a LED for 0.5sec – without debounce
This experiment turns on a LED for 0.5 sec when button “A” is pressed. It does not matter how long button “A” is pressed. The LED turns on immediately button “A” is pressed and turns off after 0.5 sec.
If the button is kept pressed after the LED turns off, the LED turns on AGAIN as the program detects noise from the switch.
This highlights the need for DEBOUNCE.
Debounce is shown in the next experiment.
There are variations of this: The LED can turn on for 0.5sec AFTER the button has been released (Experiment 1d) or the LED can turn on for 0.5sec MORE than the button-press time (Experiment 1e).
All these programs show how to control the output. You can extend the program to two flashes or a delay between flashes or a flash then turn on a LED or driver transistor to operate a globe or motor, or any other output device.
This program is an extension of Experiment 1, with the introduction of a delay.
The maximum delay for 2 files is 250mS. To achieve 500mS, the Delay sub-routine is called two times. This requires less instructions than creating a 500mS delay, and is simpler to understand.
The program is shown below and an alternate program is shown that controls the driver transistor.
The control of a single line of the output port is called BIT MANIPULATION.
Two or more output lines can be controlled at the same time by loading W with a hex value and moving the value to file 06. If this is done, all the output lines will be affected and if a line is HIGH and the corresponding bit in the instruction is “0” the line will change state. This may not be wanted and that’s why bit manipulation is the safest.

 

For more detail: Turning on a LED using PIC16F84

Quick Solutions to Questions related to Experiment 1 (Turning on a LED):

  • How is the button wired for this experiment?
    The button is connected to the lowest bit of Port A (RA0) and goes HIGH when pressed (positive logic).
  • Which port and bit is the LED connected to?
    The LED is connected to the lowest bit of Port B (RB0).
  • Does the microcontroller stop executing instructions?
    No, the micro never stops; Main is a loop that continuously executes one loop or the other.
  • What happens when button A is pressed in Experiment 1a?
    The LED turns on immediately and stays on for 0.5 seconds regardless of how long the button is held.
  • Is switch debounce handled in Experiment 1a?
    No, Experiment 1a does not include debounce and may detect noise or multiple presses.
  • How is a 0.5 second delay achieved?
    By calling a 250 ms Delay subroutine twice to create a total 500 ms delay.
  • Can port lines be changed between input and output during program execution?
    Yes, any port-line can be changed from input to output or vice versa at any time during program execution.
  • Why is bit manipulation recommended?
    Bit manipulation controls a single output line safely without affecting other output lines, avoiding unintended state changes.

About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter