Creating your first LED Flash Program using the PIC16f628, Pickit 2, MPLab and written in Assembly Language.

In this tutorial, you will learn how to flash an LED on the famous PIC16F628, written with assembly language code. And while doing this, the project will still be allowed to be reprogrammed in circuit using the Pickit 2. The connections and code outlined here are the bare minimum needed to start programming and executing code on the PIC16f628 in circuit.

First, you’ll need to buy the Pickit 2 clone, which comes with the breadboard wire connectors already. I got mine for £5 from eBay. You could buy an original ICD3 but it is costly and we can make it work fine with these Chinese imports.

1: Components needed:

Then you’ll need a breadboard, breadboard power supply and jumpleads. Source the cheapest or fastest shipping you can get. Grab some LED’s (light emitting diodes). Obviously, the PIC16F628-04/P is still available and I recommend getting it from AliExpress. Make sure it is the 04/P version, as this is the best internal clock speed (4 MHz) to get. Grab a bunch of resistors. A 0.1uF ceramic capacitor will be needed to smooth out the power rails. Don’t get the electrolytic type. USB to USB cable will be needed to connect up the power supply for the breadboard. That’s it.

2: Schematic

Now, build your circuit on your breadboard by following this schematic. This is the basic circuit needed to upload any HEX program to the microcontroller.

Then just add an LED and and resistor in series from pin 7 of the microchip to ground. Long side of the LED is on the pin, short side tied to ground. It should look something like this following image. Note the device on the right is the breadboard power supply.

3: Download the correct software

Download MpLab v8.92 and Pickit 2 software to upload the program. MpLab doesn’t work with the Pickit 2 on Mac, so you’ll need a windows computer. Pickit 2 also doesn’t work with MPLab, even on Windows, so you’ll need the separate Pickit 2 software to burn the program onto the chip. Also, it’s best to use MpLab v8.92 rather than the new MpLab X IDE as the newer “X IDE” won’t work with the PIC16F628.

4: Make a new project in MpLab

Open up MpLab in windows and click “project”, then “project wizard”. Go through the dialog box and choose the PIC16F628 as the chip. Choose all the default settings. When you get to step 4, you’ll need to add the PIC16F628 linker file as a minimum to build the file properly. To do this, select “program files”, then “microchip”, then “MPASM suits”, “LKR”, “PIC16f628_g.lkr”. Add that and you’re good to go.

Add a new source file to write assembly language code into. You can do this by right clicking the “source files” on the left, then add a new file. Name it whatever you like.

Then, make sure the linker file is inside the “linker script” folder. And that’s it, those are the two files you’ll need to make everything work.

5: Add the code, export and upload to the PIC16f628

Now, copy and paste this code to your new asm file you created in step 4. This code was sourced from the great Nigel Goodwin:

;Tutorial 1.2 - Nigel Goodwin 2002
	LIST	p=16F628		;tell assembler what chip we are using
	include "P16F628.inc"		;include the defaults for the chip
	__config 0x3D18			;sets the configuration settings (oscillator type etc.)

	cblock 	0x20 			;start of general purpose registers
		count1 			;used in delay routine
		counta 			;used in delay routine 
		countb 			;used in delay routine
	endc
	
	org	0x0000			;org sets the origin, 0x0000 for the 16F628,
					;this is where the program starts running	
	movlw	0x07
	movwf	CMCON			;turn comparators off (make it like a 16F84)

   	bsf 	STATUS,		RP0	;select bank 1
   	movlw 	b'00000000'		;set PortB all outputs
   	movwf 	TRISB
	movwf	TRISA			;set PortA all outputs
	bcf	STATUS,		RP0	;select bank 0

Loop	
	movlw	0xff
	movwf	PORTA			;set all bits on
	movwf	PORTB
	nop				;the nop's make up the time taken by the goto
	nop				;giving a square wave output
	call	Delay			;this waits for a while!
	movlw	0x00
	movwf	PORTA
	movwf	PORTB			;set all bits off
	call	Delay
	goto	Loop			;go back and do it again

Delay	movlw	d'250'			;delay 250 ms (4 MHz clock)
	movwf	count1
d1	movlw	0xC7
	movwf	counta
	movlw	0x01
	movwf	countb
Delay_0
	decfsz	counta, f
	goto	$+2
	decfsz	countb, f
	goto	Delay_0

	decfsz	count1	,f
	goto	d1
	retlw	0x00

	end  

Now, click project, build all. Then again, project and make. Then lastly, file export and save as a hex file.

Now open up the Pickit 2 programmer software, plug in the Pickit 2 from the breadboard to the USB on the computer. Import the hex file by going to file then import hex. Then finally, click “write” to burn the program to the chip.

And that’s it! Now plug in the power supply for the breadboard and watch the LED FLASH!


Comments

Leave a Reply

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

Buy me a coffee .button { display: inline-block; text-align: center; vertical-align: middle; padding: 12px 24px; border: 1px solid #4b6037; border-radius: 8px; background: #7a9c5a; background: -webkit-gradient(linear, left top, left bottom, from(#7a9c5a), to(#4b6037)); background: -moz-linear-gradient(top, #7a9c5a, #4b6037); background: linear-gradient(to bottom, #7a9c5a, #4b6037); -webkit-box-shadow: #719053 0px 0px 40px 0px; -moz-box-shadow: #719053 0px 0px 40px 0px; box-shadow: #719053 0px 0px 40px 0px; text-shadow: #2f3c23 1px 1px 1px; font: normal normal bold 20px arial; color: #ffffff; text-decoration: none; } .button:hover, .button:focus { border: 1px solid ##5e7845; background: #92bb6c; background: -webkit-gradient(linear, left top, left bottom, from(#92bb6c), to(#5a7342)); background: -moz-linear-gradient(top, #92bb6c, #5a7342); background: linear-gradient(to bottom, #92bb6c, #5a7342); color: #ffffff; text-decoration: none; } .button:active { background: #4b6037; background: -webkit-gradient(linear, left top, left bottom, from(#4b6037), to(#4b6037)); background: -moz-linear-gradient(top, #4b6037, #4b6037); background: linear-gradient(to bottom, #4b6037, #4b6037); } .button:before{ content: "\0000a0"; display: inline-block; height: 24px; width: 24px; line-height: 24px; margin: 0 4px -6px -4px; position: relative; top: 0px; left: 0px; background: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAACXBIWXMAAA7EAAAOxAGVKw4bAAAEyUlEQVRIia2VWWxUVRjHf3eZdtpO22mBllLoxmJlqYQoYGgCQRQTECsEeYFEARMNlAhqDMENH/TBGE0gMUaECIlRHwgoS9WoUJo2NlBDF1onZeje6Uw7nX3mTu/c48O9YG3ApAlf8uXmnnvO//8t/3M/hWlazQIKquew6tFcZsgpYiMaieli/K9dfb3wlBi+JoT7oghc/VhrO/78tR92pO8usmN/KAS/HJjzvYgOCBHuFyIyIETMI8RQvXCfeKHz8/VUT90vT5tBGJAMgBaExDjEfJDmpHzrR5V73//gtzPbM3dP3q5On0CAFoKUAYi7iwBkLdyQtm2/8WUg/KGnto5LAMp08Xc9JrbPd0aWoI1Cei4YE6AnQI+DHsfmnCfPzY48/WfT3yf7o8SVqpk41hazoMPP2KSQHmhbyie2Z/lal0Q66pHddaTl54M9zyRIxUGPkT27JGt2oDH1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sWFnCE7McFNgU1HCSgMuH63wbf3QN0wiMcvdndj/bUsXqT2v4JjOdigduMovjAHKATKY5EWXMNj5U+wcb0TIr9nRDzwAAAABJRU5ErkJggg==") no-repeat left center transparent; background-size: 100% 100%; }