{"id":148,"date":"2010-10-11T19:40:20","date_gmt":"2010-10-11T19:40:20","guid":{"rendered":"http:\/\/embedded-lab.com\/blog\/?p=148"},"modified":"2012-07-30T09:29:33","modified_gmt":"2012-07-30T13:29:33","slug":"lab-1-flashing-led","status":"publish","type":"post","link":"https:\/\/embedded-lab.com\/blog\/lab-1-flashing-led\/","title":{"rendered":"Lab 1: Flashing an LED"},"content":{"rendered":"<p><strong>Description<\/strong><\/p>\n<p><strong> <\/strong>Today is our first session in PIC microcontroller lab, and we will begin with an experiment that flashes an LED on and off. While this looks very simple it is the best project to start because this makes sure that we successfully wrote the program, compiled it, loaded inside the PIC, and the circuit is correctly built on the breadboard.<\/p>\n<p>In this lab session we will connect an\u00a0LED to one of the port pin of PIC16F688 and flash it continuously with 1 sec duration.<\/p>\n<p><strong>Required Theory<\/strong><\/p>\n<p>You must be \u00a0familiarized with,<\/p>\n<ul>\n<li>digital I\/O ports (PORTA and PORTC) of PIC16F688<\/li>\n<li>direction control registers, TRISA and TRISC<\/li>\n<li>special function registers CMCON0 and ANSEL<\/li>\n<\/ul>\n<p>If you are not then please read this first: <em><strong><a href=\"http:\/\/embedded-lab.com\/blog\/?p=238\" target=\"_blank\">Digital I\/O Ports in PIC16F688<\/a><\/strong><\/em>.<\/p>\n<p style=\"text-align: left;\"><strong>Circuit Diagram<\/strong><\/p>\n<p>To our basic setup on the breadboard (read <strong><em><a href=\"http:\/\/embedded-lab.com\/blog\/?p=190\" target=\"_blank\">Getting Ready for the First Lab<\/a><\/em><\/strong>), we will add a light-emitting-diode (LED) to port pin RC0 (10) with a current limiting resistor (470 Ohm) in series. The complete circuit diagram is shown below.<\/p>\n<p style=\"text-align: center;\"><a rel=\"attachment wp-att-647\" href=\"http:\/\/embedded-lab.com\/blog\/?attachment_id=647\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-medium wp-image-647\" title=\"Circuit_FlashingLEd_PIC16F688\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/Circuit_FlashingLEd_PIC16F688-550x430.jpg\" alt=\"\" width=\"550\" height=\"430\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/Circuit_FlashingLEd_PIC16F688-550x430.jpg 550w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/Circuit_FlashingLEd_PIC16F688.jpg 635w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><\/a><\/p>\n<p><!--more--><\/p>\n<p style=\"text-align: center;\">Flashing LED circuit<\/p>\n<p style=\"text-align: center;\"><a rel=\"attachment wp-att-701\" href=\"http:\/\/embedded-lab.com\/blog\/?attachment_id=701\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-701 aligncenter\" title=\"Image_FlashLED_PIC16F688\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/Image_FlashLED_PIC16F688-550x381.jpg\" alt=\"\" width=\"550\" height=\"381\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/Image_FlashLED_PIC16F688-550x381.jpg 550w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/Image_FlashLED_PIC16F688.jpg 580w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><\/a><\/p>\n<p style=\"text-align: center;\">Prototyped circuit on the breadboard<\/p>\n<p style=\"text-align: left;\">\n<p><strong>Software<\/strong><\/p>\n<p>Open a new project window in mikroC and select Device Name as PIC16F688. Next assign\u00a04.0 MHz to\u00a0Device Clock. Go to next and provide the project name and the path of the folder. It is always a good practice to have a\u00a0separate\u00a0folder for each project. Create a folder named Lab1 and save the project inside it with a name (say, FlashLED). The mikroC project file has .mccpi extension. The next window is for &#8220;Add File to Project &#8220;. Leave it blank (there are no files to add to this project) and click next. The next step is to include libraries, select Include All option. Next, click Finish button. You will see a program window with a\u00a0void main() function already included. Now, go to Project -&gt; Edit Project. You will see the following window.<\/p>\n<p><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/EditProject1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-249\" title=\"EditProject1\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/EditProject1.jpg\" alt=\"\" width=\"590\" height=\"286\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/EditProject1.jpg 590w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/EditProject1-500x242.jpg 500w\" sizes=\"auto, (max-width: 590px) 100vw, 590px\" \/><\/a><\/p>\n<p style=\"text-align: left;\">This window allows you to program the configuration bits for the 14-bit CONFIG register inside the PIC16F688 microcontroller. The device configuration bits allow each user to customize certain aspects of the device (like reset and oscillator configurations) to the needs of the application. When the device powers up, the state of these bits determines the modes that the device uses. Therefore, we also need to program the configuration bits as per our experimental setup. Select,<\/p>\n<p style=\"text-align: left;\"><em><strong> Oscillator -&gt; Internal RC No Clock<br \/>\nWatchdog Timer -&gt; Off<br \/>\nPower Up Timer -&gt; On<br \/>\nMaster Clear Enable -&gt; Enabled<br \/>\nCode Protect -&gt; Off<br \/>\nData EE Read Protect -&gt; Off<br \/>\nBrown Out Detect -&gt; BOD Enabled, SBOREN Disabled<br \/>\nInternal External Switch Over Mode -&gt; Enabled<br \/>\nMonitor Clock Fail-Safe -&gt; Enabled<\/strong><\/em><\/p>\n<p>Note that we have turned ON the Power-Up Timer. It provides an additional delay of 72 ms to the start of the program execution so that the external power supply will get enough time to be stable. It avoids the need to reset the MCU manually at start up.<\/p>\n<p>Read PIC16F688 datasheet for details on Configuration Word.<\/p>\n<p><a href=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/EditProject2.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-250\" title=\"EditProject2\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/EditProject2.jpg\" alt=\"\" width=\"590\" height=\"286\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/EditProject2.jpg 590w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/EditProject2-500x242.jpg 500w\" sizes=\"auto, (max-width: 590px) 100vw, 590px\" \/><\/a><\/p>\n<p style=\"text-align: center;\">Appropriate configuration bit selection<\/p>\n<p style=\"text-align: left;\">Here&#8217;s the complete program that must be compiled and loaded into PIC16F688 for flashing the LED. Copy and paste this whole program in your main program window. You have to delete the void main() function first that was already included. To compile, hit the build button, and if there were no errors, the output HEX file will be generated in the same folder where the project file is. Then load the HEX file into the PIC16F688 microcontroller using your programmer.<\/p>\n<div id=\"_mcePaste\">\n<div id=\"_mcePaste\">\n<div id=\"_mcePaste\"><span style=\"color: #003366;\">\/*<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"color: #003366;\">Lab 1: Flashing LED with PIC16F688<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"color: #003366;\">Internal Oscillator @ 4MHz, MCLR Enabled, PWRT Enabled, WDT OFF<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"color: #003366;\">Copyright @ Rajendra Bhatt<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"color: #003366;\">Oct 7, 2010<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"color: #003366;\">*\/<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"color: #003366;\"> \/\/ Define LED @ RC0<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"color: #003366;\"> sbit LED at RC0_bit;<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"color: #003366;\"> void main() {<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"color: #003366;\"> ANSEL = 0b00000000; \/\/All I\/O pins are configured as digital<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"color: #003366;\"> CMCON0 = 0x07 ; \/\/ Disbale comparators<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"color: #003366;\"> TRISC = 0b00000000; \/\/ PORTC All Outputs<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"color: #003366;\"> TRISA = 0b00001000; \/\/ PORTA All Outputs, Except RA3<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"color: #003366;\"><br \/>\n<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"color: #003366;\"> do {<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"color: #003366;\"> LED = 1;<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"color: #003366;\"> Delay_ms(1000);<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"color: #003366;\"> LED = 0;<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"color: #003366;\"> Delay_ms(1000);<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"color: #003366;\"> } while(1); \u00a0\/\/ Infinite Loop<\/span><\/div>\n<div id=\"_mcePaste\"><span style=\"color: #003366;\">}<\/span><\/div>\n<div style=\"text-align: right;\"><span style=\"color: #003366;\"><a href=\"http:\/\/embedded-lab.com\/uploads\/HexFiles\/PIC16F688\/FlashLED.zip\" target=\"_blank\">Download the HEX file<\/a><\/span><\/div>\n<div><span style=\"color: #003366;\"><br \/>\n<\/span><\/div>\n<div><span> <\/span><\/div>\n<div><span>We used the in-built library function Delay_ms() to create the 1 sec delay for flashing On and Off. Delay_ms() returns a time delay in milliseconds.<br \/>\n<\/span><\/div>\n<\/div>\n<\/div>\n<p style=\"text-align: left;\"><strong>Output<\/strong><\/p>\n<p style=\"text-align: left;\">You will see the LED flashing On and Off with 1 sec duration.<\/p>\n<p style=\"text-align: center;\"><a rel=\"attachment wp-att-702\" href=\"http:\/\/embedded-lab.com\/blog\/?attachment_id=702\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-702 aligncenter\" title=\"Image_FlashLED_Output_PIC16F688\" src=\"http:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/Image_FlashLED_Output_PIC16F688-550x407.jpg\" alt=\"\" width=\"550\" height=\"407\" srcset=\"https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/Image_FlashLED_Output_PIC16F688-550x407.jpg 550w, https:\/\/embedded-lab.com\/blog\/wp-content\/uploads\/2010\/10\/Image_FlashLED_Output_PIC16F688.jpg 580w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><\/a><\/p>\n<p style=\"text-align: center;\">\n","protected":false},"excerpt":{"rendered":"<p>Description Today is our first session in PIC microcontroller lab, and we will begin with an experiment that flashes an LED on and off. While this looks very simple it is the best project to start because this makes sure that we successfully wrote the program, compiled it, loaded inside the PIC, and the circuit is correctly built on the breadboard. In this lab session we will connect an\u00a0LED to one of the port pin of PIC16F688 and flash it continuously with 1 sec duration. Required Theory You must be \u00a0familiarized with, digital I\/O ports (PORTA and PORTC) of PIC16F688<\/p>\n","protected":false},"author":2,"featured_media":249,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[32,29,38],"tags":[39,18,9,20],"class_list":["post-148","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-embedded-labs","category-embedded-lessons","category-pic-tutorials","tag-breadboard","tag-led","tag-mikroc","tag-pic16f688"],"_links":{"self":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/148","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/comments?post=148"}],"version-history":[{"count":44,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/148\/revisions"}],"predecessor-version":[{"id":5853,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/posts\/148\/revisions\/5853"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/media\/249"}],"wp:attachment":[{"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/media?parent=148"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/categories?post=148"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/embedded-lab.com\/blog\/wp-json\/wp\/v2\/tags?post=148"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}