PIC Microcontroller 16f887 assembly code example for LED blinking is very simple and easy to implement. This is why it is always recommended to blink an LED for any embedded system development beginner. The microcontroller PIC16F887 is used for demo purposes but this code is equally applicable to other variants of PIC16 micro-controllers, like PIC16F877A or PIC16F84A etc.

Components required for test bench

We are using Proteus simulator for simulating our code but if you are testing the code in real world test bench all you need is,

  • PIC16F887 or PIC16F877A
  • LED
  • 330R Resistor
  • 4Mhz Crystal

After wiring LED and crystal to the microcontroller and giving the proper supply connections, you are good to start.

Assembly Code for LED blinking

Here we are breaking down the code into three main parts. The first one is the setup and after that a infinite loop which will toggle the LED continuously and in-between the toggling, we required to call some routine which will waste some of the CPU cycles of the microcontroller. This routine is delay routine. So, once we are done writing the delay routine we can use that in our main loop and call that routine which will delay the process of blinking so that human eye could witness the blinking. The amount of the delay is actually inversely propostional to the speed of the blinking.

Here is the complete code for the LED blinking in PIC16F887 microcontroller using assembly language.



LIST p=16F887			 ;tell assembler what chip we are using
#include 
<P16F887.INC>
 
 __CONFIG  _CONFIG1, _INTOSCIO & _WDT_OFF & _PWRTE_ON & _MCLRE_ON & _CP_OFF & _CPD_OFF & _BOR_OFF & _IESO_OFF & _FCMEN_OFF & _LVP_OFF & _DEBUG_ON


CBLOCK h'20'
	COUNT1
	COUNT2
ENDC
	ORG		0x00

MAIN:
	BANKSEL		TRISB
    BCF 	    TRISB,0     
	BANKSEL		PORTB
MainLoop:
    BCF		    PORTB,0
	CALL		DELAY3
    BSF			PORTB,0 
	CALL		DELAY3
    GOTO	    MainLoop            ; Do it again...
     

DELAY3:
	DECFSZ		COUNT1,1
	GOTO		DELAY3
	DECFSZ		COUNT2,1
	GOTO		DELAY3
	RETURN

END
Code language: PHP (php)

Here is the final output simulated in the Labcenter Proteous ISIS

PIC16F887 LED blinking

Refferences:

By Abdul Rehman

My name is Abdul Rehman and I love to do Reasearch in Embedded Systems, Artificial Intelligence, Computer Vision and Engineering related fields. With 10+ years of experience in Research and Development field in Embedded systems I touched lot of technologies including Web development, and Mobile Application development. Now with the help of Social Presence, I like to share my knowledge and to document everything I learned and still learning.

Leave a Reply

Your email address will not be published. Required fields are marked *

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