What Is It:
This circuit is a Blinking LED. It is one of the most basic circuits you can have using a Microcontroller.
This circuit is a great starter for someone new to Microcontrollers and coding in Assembly.
Video:
*Note: The video above demonstrates a PIC16F84A running a similar program. The program listed below is for a PIC16F628A with the LED connected to PORTA,3 (pin 2) and uses the Internal Oscillator therefore no crystal is required.
How It Works:
On power up, the software originates (ORG) at address 0. At this address a GOTO instruction is executed and the program jumps to the SETUP routine.
The SETUP routine initialises the chip and assigns the inputs and outputs.
The program then moves on to the FLASHLOOP routine. Here is where the output pin is set and cleared continuously. Setting an output gives a high logic level while clearing an output gives a low logic level. When an LED is connected it will turn on when the output is set, and off when the output is cleared. Providing that the Anode of the LED is connected to the output pin and the Cathode is connected to ground.
The first instruction in this routine is the BSF instruction. It stands for Bit Set in File register and it should be arranged like this.
BSF PORTA, 3
The File register that contains the bit we wish to set is PORTA (or 0x05, see page 16 of the PIC16F628A datasheet for memory organisation), and the bit we wish to set is bit 3, pin 2 of the Microcontroller.
To make the LED flash, we now need to turn it off. To do this the BCF instruction is used. It stands for Bit Clear in File register and it should be arranged like this.
BCF PORTA, 3
In between turning the LED on and off a delay must be called, I will explain the reason in a second. To call a routine elsewhere in the program memory, we use the CALL instruction.
CALL DELAY_250mS
The CALL instruction forces the program to jump to the routine to which it is calling. In this case the routine with the label DELAY_250mS will be executed.
Now, back to why a delay must be used. Majority of instructions takes 4 clock cycles to complete. With the internal oscillator running at 4MHz, 1 instruction takes 1µs (1 micro second). There are 1 million micro seconds per second. So, without a delay between turning the LED on and off, it will flash 1 million times per second. Which is way too fast for the human eye to see. In fact because the LED is on for half the time and off half the time, the LED will just appear dim or 50% bright.
After setting and clearing the output with a delay proceeding each instruction, a GOTO instruction is executed to loop the routine. The full FLASHLOOP routine is shown below.