Your basket is currently empty!
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!
Leave a Reply