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.
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.
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