{"id":9,"date":"2023-08-03T18:22:51","date_gmt":"2023-08-03T17:22:51","guid":{"rendered":"https:\/\/odonodesign.com\/blog\/?p=9"},"modified":"2023-08-06T17:55:39","modified_gmt":"2023-08-06T16:55:39","slug":"creating-your-first-led-flash-program-using-the-pic16f628-pickit-2-mplab-and-written-in-assembly-language","status":"publish","type":"post","link":"https:\/\/odonodesign.com\/blog\/2023\/08\/03\/creating-your-first-led-flash-program-using-the-pic16f628-pickit-2-mplab-and-written-in-assembly-language\/","title":{"rendered":"Creating your first LED Flash Program using the PIC16f628, Pickit 2, MPLab and written in Assembly Language."},"content":{"rendered":"\n<p>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.<\/p>\n\n\n\n<p>First, you\u2019ll need to buy the Pickit 2 clone, which comes with the breadboard wire connectors already. I got mine for \u00a35 from eBay. You could buy an original ICD3 but it is costly and we can make it work fine with these Chinese imports.<\/p>\n\n\n\n<p>1: Components needed:<\/p>\n\n\n\n<p>Then you\u2019ll need a breadboard, breadboard power supply and jumpleads. Source the cheapest or fastest shipping you can get. Grab some LED\u2019s (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\u2019t get the electrolytic type. USB to USB cable will be needed to connect up the power supply for the breadboard. That\u2019s it.<\/p>\n\n\n\n<p>2: Schematic<\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-2 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"705\" data-id=\"10\" src=\"https:\/\/odonodesign.com\/blog\/wp-content\/uploads\/2023\/08\/pic16f628-schematic-1024x705.jpg\" alt=\"\" class=\"wp-image-10\" srcset=\"https:\/\/odonodesign.com\/blog\/wp-content\/uploads\/2023\/08\/pic16f628-schematic-1024x705.jpg 1024w, https:\/\/odonodesign.com\/blog\/wp-content\/uploads\/2023\/08\/pic16f628-schematic-600x413.jpg 600w, https:\/\/odonodesign.com\/blog\/wp-content\/uploads\/2023\/08\/pic16f628-schematic-300x207.jpg 300w, https:\/\/odonodesign.com\/blog\/wp-content\/uploads\/2023\/08\/pic16f628-schematic-768x529.jpg 768w, https:\/\/odonodesign.com\/blog\/wp-content\/uploads\/2023\/08\/pic16f628-schematic-1536x1058.jpg 1536w, https:\/\/odonodesign.com\/blog\/wp-content\/uploads\/2023\/08\/pic16f628-schematic.jpg 1549w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n<\/figure>\n\n\n\n<p>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.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"576\" src=\"https:\/\/odonodesign.com\/blog\/wp-content\/uploads\/2023\/08\/setup-1024x576.jpg\" alt=\"\" class=\"wp-image-11\" srcset=\"https:\/\/odonodesign.com\/blog\/wp-content\/uploads\/2023\/08\/setup-1024x576.jpg 1024w, https:\/\/odonodesign.com\/blog\/wp-content\/uploads\/2023\/08\/setup-600x338.jpg 600w, https:\/\/odonodesign.com\/blog\/wp-content\/uploads\/2023\/08\/setup-300x169.jpg 300w, https:\/\/odonodesign.com\/blog\/wp-content\/uploads\/2023\/08\/setup-768x432.jpg 768w, https:\/\/odonodesign.com\/blog\/wp-content\/uploads\/2023\/08\/setup-1536x864.jpg 1536w, https:\/\/odonodesign.com\/blog\/wp-content\/uploads\/2023\/08\/setup.jpg 1600w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>3: Download the correct software<\/p>\n\n\n\n<p>Download MpLab v8.92 and Pickit 2 software to upload the program. MpLab doesn\u2019t work with the Pickit 2 on Mac, so you\u2019ll need a windows computer. Pickit 2 also doesn\u2019t work with MPLab, even on Windows, so you\u2019ll need the separate Pickit 2 software to burn the program onto the chip. Also, it\u2019s best to use MpLab v8.92 rather than the new MpLab X IDE as the newer \u201cX IDE\u201d won\u2019t work with the PIC16F628.<\/p>\n\n\n\n<p>4: Make a new project in MpLab<\/p>\n\n\n\n<p>Open up MpLab in windows and click \u201cproject\u201d, then \u201cproject wizard\u201d. Go through the dialog box and choose the PIC16F628 as the chip. Choose all the default settings. When you get to step 4, you\u2019ll need to add the PIC16F628 linker file as a minimum to build the file properly. To do this, select \u201cprogram files\u201d, then \u201cmicrochip\u201d, then \u201cMPASM suits\u201d, \u201cLKR\u201d, \u201cPIC16f628_g.lkr\u201d. Add that and you\u2019re good to go.<\/p>\n\n\n\n<p>Add a new source file to write assembly language code into. You can do this by right clicking the \u201csource files\u201d on the left, then add a new file. Name it whatever you like.<\/p>\n\n\n\n<p>Then, make sure the linker file is inside the \u201clinker script\u201d folder. And that\u2019s it, those are the two files you\u2019ll need to make everything work.<\/p>\n\n\n\n<p>5: Add the code, export and upload to the PIC16f628<\/p>\n\n\n\n<p>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:<\/p>\n\n\n\n<p><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>;Tutorial 1.2 - Nigel Goodwin 2002\n\tLIST\tp=16F628\t\t;tell assembler what chip we are using\n\tinclude \"P16F628.inc\"\t\t;include the defaults for the chip\n\t__config 0x3D18\t\t\t;sets the configuration settings (oscillator type etc.)\n\n\tcblock \t0x20 \t\t\t;start of general purpose registers\n\t\tcount1 \t\t\t;used in delay routine\n\t\tcounta \t\t\t;used in delay routine \n\t\tcountb \t\t\t;used in delay routine\n\tendc\n\t\n\torg\t0x0000\t\t\t;org sets the origin, 0x0000 for the 16F628,\n\t\t\t\t\t;this is where the program starts running\t\n\tmovlw\t0x07\n\tmovwf\tCMCON\t\t\t;turn comparators off (make it like a 16F84)\n\n   \tbsf \tSTATUS,\t\tRP0\t;select bank 1\n   \tmovlw \tb'00000000'\t\t;set PortB all outputs\n   \tmovwf \tTRISB\n\tmovwf\tTRISA\t\t\t;set PortA all outputs\n\tbcf\tSTATUS,\t\tRP0\t;select bank 0\n\nLoop\t\n\tmovlw\t0xff\n\tmovwf\tPORTA\t\t\t;set all bits on\n\tmovwf\tPORTB\n\tnop\t\t\t\t;the nop's make up the time taken by the goto\n\tnop\t\t\t\t;giving a square wave output\n\tcall\tDelay\t\t\t;this waits for a while!\n\tmovlw\t0x00\n\tmovwf\tPORTA\n\tmovwf\tPORTB\t\t\t;set all bits off\n\tcall\tDelay\n\tgoto\tLoop\t\t\t;go back and do it again\n\nDelay\tmovlw\td'250'\t\t\t;delay 250 ms (4 MHz clock)\n\tmovwf\tcount1\nd1\tmovlw\t0xC7\n\tmovwf\tcounta\n\tmovlw\t0x01\n\tmovwf\tcountb\nDelay_0\n\tdecfsz\tcounta, f\n\tgoto\t$+2\n\tdecfsz\tcountb, f\n\tgoto\tDelay_0\n\n\tdecfsz\tcount1\t,f\n\tgoto\td1\n\tretlw\t0x00\n\n\tend  <\/code><\/pre>\n\n\n\n<p>Now, click project, build all. Then again, project and make. Then lastly, file export and save as a hex file.<\/p>\n\n\n\n<p>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 \u201cwrite\u201d to burn the program to the chip.<\/p>\n\n\n\n<p>And that\u2019s it! Now plug in the power supply for the breadboard and watch the LED FLASH!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-9","post","type-post","status-publish","format-standard","hentry","category-tutorials"],"_links":{"self":[{"href":"https:\/\/odonodesign.com\/blog\/wp-json\/wp\/v2\/posts\/9","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/odonodesign.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/odonodesign.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/odonodesign.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/odonodesign.com\/blog\/wp-json\/wp\/v2\/comments?post=9"}],"version-history":[{"count":4,"href":"https:\/\/odonodesign.com\/blog\/wp-json\/wp\/v2\/posts\/9\/revisions"}],"predecessor-version":[{"id":25,"href":"https:\/\/odonodesign.com\/blog\/wp-json\/wp\/v2\/posts\/9\/revisions\/25"}],"wp:attachment":[{"href":"https:\/\/odonodesign.com\/blog\/wp-json\/wp\/v2\/media?parent=9"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/odonodesign.com\/blog\/wp-json\/wp\/v2\/categories?post=9"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/odonodesign.com\/blog\/wp-json\/wp\/v2\/tags?post=9"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}