<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>(Baltimore) Harford Hackerspace &#187; PIC</title>
	<atom:link href="http://baltimorehackerspace.com/tag/pic/feed/" rel="self" type="application/rss+xml" />
	<link>http://baltimorehackerspace.com</link>
	<description>Maryland Electronics, Software, Science and Robotics Club</description>
	<lastBuildDate>Tue, 24 Jan 2012 20:46:41 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>LED Fireflies in a Jar (v1.0)</title>
		<link>http://baltimorehackerspace.com/2010/07/led-fireflies-in-a-jar-v1-0/</link>
		<comments>http://baltimorehackerspace.com/2010/07/led-fireflies-in-a-jar-v1-0/#comments</comments>
		<pubDate>Thu, 08 Jul 2010 15:37:42 +0000</pubDate>
		<dc:creator>superfro</dc:creator>
				<category><![CDATA[How-Tos]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[Soup]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[CCS]]></category>
		<category><![CDATA[Electronics]]></category>
		<category><![CDATA[Microcontroller]]></category>
		<category><![CDATA[PIC]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://harfordhackerspace.org/?p=858</guid>
		<description><![CDATA[Me and my wife recently had our first child.  I&#8217;ve been trying to come up with some projects I can do for the kid.  Well, around this time of the year the fireflies go crazy around my house, so we came up with the idea to do led fireflies in a jar.  It should be [...]]]></description>
			<content:encoded><![CDATA[<p>
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="480" height="385" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube.com/v/TBUS7_ADqwI&amp;hl=en_US&amp;fs=1" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="480" height="385" src="http://www.youtube.com/v/TBUS7_ADqwI&amp;hl=en_US&amp;fs=1" allowscriptaccess="always" allowfullscreen="true"></embed></object>
</p>
<p>Me and my wife recently had our first child.  I&#8217;ve been trying to come up with some projects I can do for the kid.  Well, around this time of the year the fireflies go crazy around my house, so we came up with the idea to do led fireflies in a jar.  It should be something cool for him to stare at and is easy to build.</p>
<p>Schematics, C source code and hex file are available <a href="http://wiki.harfordhackerspace.org/index.php?title=Firefly_jar_v1">here on our wiki</a>.  If you want to build one it should only take about an hour.</p>
<p>I&#8217;m planning on making a version 2 with some changes, stay tuned for that one.</p>

<div class="ngg-galleryoverview" id="ngg-gallery-17-858">

	<!-- Slideshow link -->
	<div class="slideshowlink">
		<a class="slideshowlink" href="http://baltimorehackerspace.com/2010/07/led-fireflies-in-a-jar-v1-0/?show=slide">
			[Show as slideshow]		</a>
	</div>

	
	<!-- Thumbnails -->
		
	<div id="ngg-image-268" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://baltimorehackerspace.com/wp-content/gallery/led-firefly-jar-v1-0/448px-deadbug_soldered_firefly_jar.jpg" title=" " class="shutterset_set_17" >
								<img title="448px-deadbug_soldered_firefly_jar" alt="448px-deadbug_soldered_firefly_jar" src="http://baltimorehackerspace.com/wp-content/gallery/led-firefly-jar-v1-0/thumbs/thumbs_448px-deadbug_soldered_firefly_jar.jpg" width="100" height="75" />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-269" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://baltimorehackerspace.com/wp-content/gallery/led-firefly-jar-v1-0/448px-firefly_lid.jpg" title=" " class="shutterset_set_17" >
								<img title="448px-firefly_lid" alt="448px-firefly_lid" src="http://baltimorehackerspace.com/wp-content/gallery/led-firefly-jar-v1-0/thumbs/thumbs_448px-firefly_lid.jpg" width="100" height="75" />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-270" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://baltimorehackerspace.com/wp-content/gallery/led-firefly-jar-v1-0/448px-fully_assembled_firefly_v1_jar.jpg" title=" " class="shutterset_set_17" >
								<img title="448px-fully_assembled_firefly_v1_jar" alt="448px-fully_assembled_firefly_v1_jar" src="http://baltimorehackerspace.com/wp-content/gallery/led-firefly-jar-v1-0/thumbs/thumbs_448px-fully_assembled_firefly_v1_jar.jpg" width="100" height="75" />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-271" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://baltimorehackerspace.com/wp-content/gallery/led-firefly-jar-v1-0/448px-mounted_inside_components.jpg" title=" " class="shutterset_set_17" >
								<img title="448px-mounted_inside_components" alt="448px-mounted_inside_components" src="http://baltimorehackerspace.com/wp-content/gallery/led-firefly-jar-v1-0/thumbs/thumbs_448px-mounted_inside_components.jpg" width="100" height="75" />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-272" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://baltimorehackerspace.com/wp-content/gallery/led-firefly-jar-v1-0/579px-firefly_jar_schematic.png" title=" " class="shutterset_set_17" >
								<img title="579px-firefly_jar_schematic" alt="579px-firefly_jar_schematic" src="http://baltimorehackerspace.com/wp-content/gallery/led-firefly-jar-v1-0/thumbs/thumbs_579px-firefly_jar_schematic.png" width="88" height="75" />
							</a>
		</div>
	</div>
	
		
 	 	
	<!-- Pagination -->
 	<div class='ngg-clear'></div>
 	
</div>


]]></content:encoded>
			<wfw:commentRss>http://baltimorehackerspace.com/2010/07/led-fireflies-in-a-jar-v1-0/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Using the PICkit 2 : As a Programmer</title>
		<link>http://baltimorehackerspace.com/2010/04/using-the-pickit-2-as-a-programmer/</link>
		<comments>http://baltimorehackerspace.com/2010/04/using-the-pickit-2-as-a-programmer/#comments</comments>
		<pubDate>Sun, 11 Apr 2010 17:11:07 +0000</pubDate>
		<dc:creator>superfro</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Microcontroller]]></category>
		<category><![CDATA[PIC]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://harfordhackerspace.org/?p=695</guid>
		<description><![CDATA[Around the space, we have been using the PICkit 2 Debug Express quite a bit lately.  This has allowed us to get pretty cozy with the functions of this handy little programmer and I thought I would share some of these features with you. I&#8217;m sure you&#8217;ve read the previous tutorial &#8216;Programming PIC Microcontrollers in [...]]]></description>
			<content:encoded><![CDATA[<div id="attachment_535" class="wp-caption alignright" style="width: 155px"><a href="http://harfordhackerspace.org/wp-content/uploads/2010/02/PICKit2.jpg"><img class="size-full wp-image-535 " style="margin: 10px;" title="PICKit2" src="http://harfordhackerspace.org/wp-content/uploads/2010/02/PICKit2.jpg" alt="PicKit2 Debug Express" width="145" height="145" /></a><p class="wp-caption-text">PicKit2 Debug Express</p></div>
<p>Around the space, we have been using the <a href="http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&amp;nodeId=1406&amp;dDocName=en023805">PICkit 2 Debug Express</a> quite a bit lately.  This has allowed us to get pretty cozy with the functions of this handy little programmer and I thought I would share some of these features with you.</p>
<p>I&#8217;m sure you&#8217;ve read the previous tutorial &#8216;<a href="http://harfordhackerspace.org/2010/02/programming-pic-microcontrollers-in-c/">Programming PIC Microcontrollers in C</a>&#8216;, if you haven&#8217;t, you should go familiarize yourself with it now.  Continuing on from there, after we have a successful build of a led blinking we can use the PICkit 2 to program our chip via the ICSP header.</p>
<p><span id="more-695"></span></p>
<p>Our Debug Express Kit&#8217;s came with a Demo Board which has a 44 pin PIC16F887 microchip. The microcontroller is connected to a 6 Pin Male header called the ICSP (In-Circuit Serial Programmer) header.  All PIC microcontrollers with the letter &#8220;F&#8221; in their name can be programmed this way, even if you are using a breadboard and wires to make the connections. The &#8220;F&#8221; stands for Flash.  Maybe in a future article we will show you how to construct the ICSP header from the data sheet, but we will just assume you are using the 44 Pin Demo Board for now.</p>
<p><a href="http://harfordhackerspace.org/wp-content/uploads/2010/04/selectprogrammer.png"><img class="alignleft size-medium  wp-image-696" style="margin: 10px;" title="selectprogrammer" src="http://harfordhackerspace.org/wp-content/uploads/2010/04/selectprogrammer-300x249.png" alt="" width="300" height="249" /></a></p>
<p><br class="spacer_" /></p>
<p>You should already have the PICkit 2 connected to your PC via the USB port, so after a successful build of our program, we can program the chip by selecting the &#8216;Programmer&#8217; menu from within MPLAB, and then selecting &#8216;Select Programmer&#8217;, then &#8216;PICkit 2&#8242;.</p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><a href="http://harfordhackerspace.org/wp-content/uploads/2010/04/connected.png"><img class="size-medium wp-image-698 alignright" style="margin: 10px;" title="connected" src="http://harfordhackerspace.org/wp-content/uploads/2010/04/connected-300x155.png" alt="" width="300" height="155" /></a></p>
<p><br class="spacer_" /></p>
<p>At that point below in the debug window you should see a message such as &#8216;Found PICkit 2 &#8211; Operating System Version 2.32.0 &#8230;&#8217; If you do not, select &#8216;Programmer&#8217; then &#8216;Connect&#8217; from MPLAB.</p>
<p><br class="spacer_" /></p>
<p><a href="http://harfordhackerspace.org/wp-content/uploads/2010/04/program.png"><img class="alignleft size-medium wp-image-725" style="margin: 10px;" title="program" src="http://harfordhackerspace.org/wp-content/uploads/2010/04/program-159x300.png" alt="" width="159" height="300" /></a></p>
<p><br class="spacer_" /></p>
<p>Finally we select &#8216;Programmer&#8217; then &#8216;Program&#8217;, and it should start erasing and programming the chip.</p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><a href="http://harfordhackerspace.org/wp-content/uploads/2010/04/settings1.png"><img class="alignright size-medium wp-image-737" style="margin: 10px;" title="settings" src="http://harfordhackerspace.org/wp-content/uploads/2010/04/settings1-251x300.png" alt="" width="251" height="300" /></a></p>
<p><br class="spacer_" /></p>
<p>Lastly, one tip that speeds up the programming process is to go to the &#8216;Programmer&#8217; menu and then click &#8216;settings&#8217;. Then check the boxes &#8216;Program after a successful build&#8217; and &#8216;Run after a successful program&#8217;.  Once you do that you can just hit F10 to compile and flash the pic over and over as you make small adjustments to your code.</p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p><br class="spacer_" /></p>
<p>In the end, this simple task is actually quite convenient. Being able to program a chip in-circuit, instead of taking the chip out of the socket, putting it in a programmer, programming it, taking it out, putting it back into the other socket, turning on, etc.</p>
<p><br class="spacer_" /></p>
<p>Finally, an often overlooked feature is that the PICkit 2 will power your chip if it doesn&#8217;t detect any voltage, which for me personally is the case before I have a power supply set up.  I wouldn&#8217;t overdue it, you can power small things, led&#8217;s, tiny motors but don&#8217;t try to draw too much current from it.  The maximum power draw is 200-300mA less than the USB port puts out, which in most cases is around 500mA leaving you with 200-300mA.  So don&#8217;t try to overdue it, if you&#8217;ve got a lot of power draw in your circuit, set up your own power source and the PICkit 2 will detect the voltage during program and wont try to power it.</p>
<p><br class="spacer_" /></p>
<p>Happy Hacking!</p>
]]></content:encoded>
			<wfw:commentRss>http://baltimorehackerspace.com/2010/04/using-the-pickit-2-as-a-programmer/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Programming PIC Microcontrollers in C</title>
		<link>http://baltimorehackerspace.com/2010/02/programming-pic-microcontrollers-in-c/</link>
		<comments>http://baltimorehackerspace.com/2010/02/programming-pic-microcontrollers-in-c/#comments</comments>
		<pubDate>Sun, 21 Feb 2010 21:46:18 +0000</pubDate>
		<dc:creator>Squintz</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[CCS]]></category>
		<category><![CDATA[PIC]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://harfordhackerspace.org/?p=526</guid>
		<description><![CDATA[Learning to program microcontrollers seems like an impossible task but with the help and support of a Hackerspace you&#8217;ll find that it&#8217;s actually simple to get started. The members of Harford Hackerspace set out to learn by starting at the basics and gradually taking on more difficult applications. The key to our quick success was group [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://harfordhackerspace.org/wp-content/uploads/2010/02/pic16f877a.png"><img class="alignleft size-full wp-image-527" title="pic16f877a" src="http://harfordhackerspace.org/wp-content/uploads/2010/02/pic16f877a.png" alt="" width="210" height="62" /></a>Learning to program microcontrollers seems like an impossible task but with the help and support of a Hackerspace you&#8217;ll find that it&#8217;s actually simple to get started. The members of Harford Hackerspace set out to learn by starting at the basics and gradually taking on more difficult applications. The key to our quick success was group participation. Most of us had at least attempted PIC programming, but it took a group effort to get the whole picture.</p>
<p>In this tutorial we will teach you what we learned. You can then take this new knowledge to your local hackerspace and put it to good use. Let&#8217;s get started!</p>
<p><span id="more-526"></span></p>
<h1>What will you need?</h1>
<div id="attachment_535" class="wp-caption alignright" style="width: 155px"><a href="http://harfordhackerspace.org/wp-content/uploads/2010/02/PICKit2.jpg"><img class="size-full wp-image-535 " title="PICKit2" src="http://harfordhackerspace.org/wp-content/uploads/2010/02/PICKit2.jpg" alt="PicKit2 Debug Express" width="145" height="145" /></a><p class="wp-caption-text">PicKit2 Debug Express</p></div>
<ul>
<li><a title="PicKits" href="http://www.microchipdirect.com/productsearch.aspx?Keywords=DV164121" target="_blank">PicKit2 or PicKit3 Debug Express</a>- The PicKits come with software, an in-circuit programmer (ICP) and a demo board. On the demo board is a PIC Microcontroller, 8 LEDs, 1 Potentiometer (variable resistor), and 1 momentary push button switch. You&#8217;ll want the Debug Express version.</li>
<li><a title="MPLAB IDE" href="http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&amp;nodeId=1406&amp;dDocName=en019469&amp;part=SW007002" target="_blank">MPLAB IDE</a> &#8211; The PicKits come with MPLAB IDE (Integrated Development Environment) software on a CD. However, you should download and install the latest version, as with any software, to avoid running into bugs. MPLAB is where you will do your C Programming.</li>
<li><a title="CCS Compiler" href="http://www.ccsinfo.com/content.php?page=compilers" target="_blank">CCS Compiler</a>- There is a free demo version of the CCS Compiler. However it&#8217;s limited in features. If you plan to do a lot of programming you may want to fork out the money for a full version. CCS comes with its own IDE but you won&#8217;t be using that.</li>
<li><a title="Debug Express Examples" href="http://ww1.microchip.com/downloads/en/DeviceDoc/PICkit%202-DBE%20887%20Lessons%20(b).zip" target="_blank">Debug Express Examples</a> &#8211; The PicKit 2 comes with a second CD which includes mostly assembly (.asm) examples but does include a couple Hi-Tech C examples. Hi-Tech is an alternative compiler to CCS and chances are these C examples will not work. However, they contain enough information that they are worth looking at.</li>
</ul>
<p style="text-align: center;">
<div id="attachment_538" class="wp-caption aligncenter" style="width: 510px"><a href="http://harfordhackerspace.org/wp-content/uploads/2010/02/PicKit_DemoBoard_edited.jpg"><img class="size-full wp-image-538 " title="PicKit2 44 Pin Demo Board" src="http://harfordhackerspace.org/wp-content/uploads/2010/02/PicKit_DemoBoard_edited.jpg" alt="" width="500" height="285" /></a><p class="wp-caption-text">PicKit2 44Pin Demo Board</p></div>
<h1>Software Installation</h1>
<p>Installing the software is fairly straight forward. Start by installing MPLAB and the Debug Express Examples. Then install the CCS Compiler.  You can post a comment here if you have trouble but I don&#8217;t think you will.</p>
<h1>Start a new Project</h1>
<ol>
<li>Start MPLAB</li>
<li>From the Project Menu select Project Wizard</li>
<li>Click Next &#8211; This is just a splash screen telling you about the wizard</li>
<li>Select your PIC device &#8211; Our PicKit2s came with PIC16F887 chips. Yours may be different so make sure you select the correct chip.<a href="http://harfordhackerspace.org/wp-content/uploads/2010/02/wizard_step_one.jpg"><img class="aligncenter size-full wp-image-553" title="wizard_step_one" src="http://harfordhackerspace.org/wp-content/uploads/2010/02/wizard_step_one.jpg" alt="" width="382" height="317" /></a></li>
<li>Click Next &#8211; This takes you to the second step which is the Compiler Setup.</li>
<li>From the Active Toolsuite drop down box select CCS C Compiler&#8230;<a href="http://harfordhackerspace.org/wp-content/uploads/2010/02/wizard_step_two.jpg"><img class="aligncenter size-full wp-image-555" title="wizard_step_two" src="http://harfordhackerspace.org/wp-content/uploads/2010/02/wizard_step_two.jpg" alt="" width="384" height="296" /></a></li>
<li>Under Toolsuite Contents,  CCS C Compiler (ccsc.exe) should be selected.</li>
<li>Browse to the location of ccsc.exe which by default is C:\Program files\Picc\CCSC.exe</li>
<li>Click Next &#8211; This takes you to the third step which is naming your project.<a href="http://harfordhackerspace.org/wp-content/uploads/2010/02/wizard_step_three.jpg"><img class="aligncenter size-full wp-image-559" title="wizard_step_three" src="http://harfordhackerspace.org/wp-content/uploads/2010/02/wizard_step_three.jpg" alt="" width="387" height="298" /></a></li>
<li>Click browse and create a new project folder and create a new file called Blink. You can use any name but for this tutorial we will be blinking an LED so we call it Blink. It&#8217;s a good idea to put projects in their own folder because a bunch of files will be created and associated with the project.<a href="http://harfordhackerspace.org/wp-content/uploads/2010/02/wizard_step_four1.jpg"><img class="aligncenter size-full wp-image-562" title="wizard_step_four" src="http://harfordhackerspace.org/wp-content/uploads/2010/02/wizard_step_four1.jpg" alt="" width="245" height="176" /></a></li>
<li>Click Next &#8211; This takes you to the fourth step which is adding files to your project. We won&#8217;t be adding any existing files here so you can just Click Next.</li>
<li>Click Finish &#8211; It may look like nothing happened or there are no windows open. In this case select View -&gt; Project and this should show the Project Tree.</li>
<li>Select File -&gt; New</li>
<li>Type the following code:</li>
<pre lang="c">#include &lt;16f887.h&gt;

void main()
{

}
</pre>
<li>Select File -&gt; Save and save the file as main.c in your Blink project folder.</li>
<li>Right Click on the Source Files folder in your Project Tree and select Add Files&#8230; Then select the main.c file you just created.<a href="http://harfordhackerspace.org/wp-content/uploads/2010/02/add_file.jpg"><img class="aligncenter size-full wp-image-547" title="add_file" src="http://harfordhackerspace.org/wp-content/uploads/2010/02/add_file.jpg" alt="Add File" width="312" height="222" /></a></li>
<li>Select Project -&gt; Build All and notice that several new files have been created for you. If you get an error check your code.<a href="http://harfordhackerspace.org/wp-content/uploads/2010/02/build_all.jpg"><img class="aligncenter size-medium wp-image-548" title="build_all" src="http://harfordhackerspace.org/wp-content/uploads/2010/02/build_all-170x300.jpg" alt="" width="170" height="300" /></a></li>
</ol>
<div id="attachment_550" class="wp-caption alignleft" style="width: 212px"><a href="http://harfordhackerspace.org/wp-content/uploads/2010/02/header_in_tree.jpg"><img class="size-full wp-image-550" title="header_in_tree" src="http://harfordhackerspace.org/wp-content/uploads/2010/02/header_in_tree.jpg" alt="" width="202" height="350" /></a><p class="wp-caption-text">Project Tree</p></div>
<p>After you select build all you should notice several new files in your Project Tree. The most important to note is the 16F887.h file under the Header Files folder. Depending on your chip it may be named something different.</p>
<h1>Sample Code</h1>
<p>So, did you pick up on what just happened here?  We created a Source File which we called main.c. It could have been named anything. By using the code #include &lt;16F887.h&gt; and selecting build all the compiler found a compatible header file for our PIC chip and automatically inserted it into our project.</p>
<p>Now you have a complete skeleton project ready for you to fill in the blanks. We&#8217;ll give you your first program and examine it line by line. We will also tell you how we learned which functions would work for the compiler and chip combination we are using.</p>
<p>First, we need to get some reading material or resources. You will need to open the help file for the CCS Compiler. They are nice and give you an executable in Windows Start -&gt; Programs -&gt; Pic-C -&gt;Pic C Help. This was installed with the CCS Compiler. Then, we need to get the Datasheet for our chip.</p>
<p><a title="PIC16F887 Datasheet" href="http://ww1.microchip.com/downloads/en/DeviceDoc/41291F.pdf" target="_blank">PIC16F887 Datasheet</a></p>
<p>Here is our sample Blink code:</p>
<pre lang="c">#include &lt;16f887.h&gt;
#fuses NOMCLR, INTRC_IO, NOBROWNOUT
#use delay(clock=8M)

void main()
{
    while(true)
    {
      output_high(PIN_D1);
      delay_ms(50);
      output_low(PIN_D1);
      delay_ms(50);
    }
}
</pre>
<p><strong>Line 1:</strong><br />
We already know that this file is what tells the compiler to load the header file in our Project Tree. This line also tells the preprocessor to treat the contents of the 16F887.h file as if those contents had appeared in the source program starting from this line. Keywords that start with the character # are called a preprocessor directive. Some examples are #include, #use, #define, and #fuses which are used most often. This code is used to setup the compiler before it starts compiling your code.</p>
<pre lang="c">#fuses NOMCLR, INTRC_IO, NOBROWNOUT
</pre>
<p><strong>Line 2:</strong><br />
The #fuses directive tells the compiler how we want to setup our chip. The available #fuses can be found by looking at the top of the 16f887.h header file. Just double click on it in the Project Tree to open it. At the very top there will be a several-line comment which tells you what fuses can be set. This is one of the murky areas for us still but we can explain what we know so far.</p>
<p>We are setting 3 fuses in our example code. The first NOMCLR stands for No Master Clear. Master Clear is a pin on most chips which can be used to reset the chip. Usually this pin doubles as a Input/Output (I/O) pin. In order to use it as an I/O you must set the fuse NOMCLR to disable it as a Master Clear pin. You&#8217;ll always want to do this if you are not using it as a Master Clear because leaving it float will cause your chip to randomly reboot itself.</p>
<p>The second fuse is INTRC_IO which tells the compiler we want to use the Internal RC (Resistor Capacitor) Oscillator and that we want Pins (9) OSC1/CLKIN and (10) OSC2/CLKOUT set as outputs. In our chips datasheet this is cryptically explained on page 61. You might be tempted to use INTRC instead of INTRC_IO. INTRC outputs a clock on Pin 10 which is 4 times slower than our internal oscillator. This is usually written as Fosc/4. Pin 9 is an I/O when using INTRC.</p>
<p>The third fuse we set in NOBROWNOUT. Brownout is a circuit protection feature. If voltage to our chip drops below a certain threshold the chip will shutdown or reset. On page 216 this is described as Vbor for Voltage Brown Out Reset. In our particular chip the brown out voltage can be set at 4Volts or 2.1volts. In our case we don&#8217;t want the chip to ever reset no matter what the voltage is. So we set NOBROWNOUT.</p>
<pre lang="c">#use delay(clock=8M)
</pre>
<p><strong>Line 3:</strong><br />
The #use directive is followed by another keyword delay. This must be set to the speed at which are oscillator is actually running. Since we are using the INTRC_IO oscillator we know we are using the internal oscillator of this chip. Therefore you need to look in the datasheet of the chip and find out what the normal operating speed of the internal oscillator is. Some chips, including this one, can have their clocks divided to slow them down or they can be clocked faster using an external oscillator such as a crystal. On page 3 of our chip&#8217;s datasheet we can see that our chip actually has two internal oscillators. 8Mhz and 31Khz. We are using the 8Mhz here.</p>
<p>The reason #use delay needs to be defined is actually because we are using the delay_ms() function on line 10. delay_ms() uses the #use delay to know how fast the oscillator is running so that it can correctly delay the execution of our code. If you set #use delay to an incorrect value your delay_ms() will function but it will be at the wrong speed. We&#8217;ll touch on this again in a minute.</p>
<pre lang="c">void main()
{
</pre>
<p><strong>Line 5:</strong><br />
Every program needs a starting point. void main() is the starting point for our program. This is simply how the C language works. Our chip needs to know where to start executing code and it will start in the void main() function. Why void? Void is simply saying that we do not expect our function to return any value. It could be int main but then we would have to return an integer at the end of our program. This is sometimes done to indicate a successful run of the program, but what&#8217;s the point? So we just use void and then we don&#8217;t have to return anything.</p>
<pre lang="c">void main()
{
    while(true)
    {
      output_high(PIN_D1);
      delay_ms(50);
      output_low(PIN_D1);
      delay_ms(50);
    }
}
</pre>
<p><strong>Line 6 and 14, 8 and 13:</strong><br />
Sets of brackets are called blocks. Line 6 starts a block of code and line 14 ends that block of code. Lines 8 and 13 are another pair of brackets and are nested inside of the void main(){} code block. A common syntax error is having too many or two few brackets. For each open bracket you need a matching close bracket.</p>
<pre lang="c">    while(true)
    {
      output_high(PIN_D1);
      delay_ms(50);
      output_low(PIN_D1);
      delay_ms(50);
    }
</pre>
<p><strong>Line 7:</strong><br />
We use a while(true) loop so our code keeps restarting. If we did not use a while(true) loop our LED would turn on and off only one time. We want the LED to keep blinking.</p>
<div id="attachment_598" class="wp-caption aligncenter" style="width: 486px"><a href="http://harfordhackerspace.org/wp-content/uploads/2010/02/help.jpg"><img class="size-full wp-image-598" title="help" src="http://harfordhackerspace.org/wp-content/uploads/2010/02/help.jpg" alt="CCS Help Program" width="476" height="467" /></a><p class="wp-caption-text">CCS Help Program</p></div>
<p><strong>Line 9:</strong><br />
output_high() is a compiler function. This is unique to the CCS Compiler. Remember that Help File I told you to get ready? It&#8217;s located under Windows Start -&gt; Programs -&gt; Pic-C -&gt; Pic C Help. If you select the index tab and type &#8216;output&#8217; you will see a long list of output options. output_A would control an entire bank, or PORT, of outputs. In our case we only want to toggle one output and we want to set it to High (on). The parameter PIN_D1 is defined in the 16f887.h header file.</p>
<p>See&#8230;That header file is coming in handy after all. Instead of you having to look in the datasheet and try to figure out what the hex value of PIN_D1 is, the CCS Compiler has given you a header file which defines a constant name for the value. All you need to know is where to look for the name (in the header file). If you look at the pinout diagram in your datasheet you might notice there is no pin labeled D1. It&#8217;s actually labeled RD1. &#8216;D&#8217; is the Bank and 1 is the Port. Honestly, I don&#8217;t know what the R means but who cares. Let&#8217;s try another one. What do you think output_high(PIN_A0) will do? You&#8217;ve got it. It will turn the pin labeled RA0 in your datasheet to high. See&#8230;It&#8217;s not that hard once you know where to look.</p>
<pre lang="c">delay_ms(50);
</pre>
<p><strong>Line 10 and Line 12:</strong><br />
We already touched on delay_ms(50) when we were talking about the #use directive. Just a refresher here: In order for delay_ms to work you must set #use delay to the proper clock speed. A wrong clock speed will cause delay_ms to not work properly. delay_ms stands for Delay Milliseconds. Where do you think you would look if you wanted to delay micro seconds? You look in the CCS Help program and simply type delay. You would then notice you can delay for Microseconds(_us) or Cycles(_cycles). Clicking on those entries gives you examples of their use.</p>
<p><strong>Line 11:</strong><br />
This one I am going to leave for your homework. If output_high(PIN_D1) turns the LED on then what do you think output_low(PIN_D1) does? Here&#8217;s a tip. Look in the CCS Help file.</p>
<h1>Conclusion</h1>
<p>So, you flashed an LED and now you are bored again. Use the CCS Help file and look up ADC for Analog to Digital Converter. Your Potentiometer is connected to an ADC pin and you should be able to read the help file and learn how to read the position of the potentiometer. You can also search the <a href="http://ww1.microchip.com/downloads/en/DeviceDoc/41296a.pdf">44 Pin Demo Board User guide</a> and learn what port the pushbutton is connected to. Then search the CCS Help for Input_State and learn how to use an input to control the LED. So don&#8217;t stop with this tutorial. There is lots to learn.</p>
<p>This is where I&#8217;m supposed to tell you to come join our hackerspace. But if you don&#8217;t live near Baltimore, check out www.hackerspaces.org and find a hackerspace near you. They are all over the world and are a ton of fun.</p>
<p>We are taking the knowledge we learned about flashing LEDs and applying it to hacking Toy RC Cars to make them autonomous (drive themselves). Check out our <a href="http://wiki.harfordhackerspace.org">wiki.harfordhackerspace.org</a> projects page for details on that project. It&#8217;s actually a very simple yet rewarding project.</p>
]]></content:encoded>
			<wfw:commentRss>http://baltimorehackerspace.com/2010/02/programming-pic-microcontrollers-in-c/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Talking Dirty to MicroControllers!</title>
		<link>http://baltimorehackerspace.com/2010/02/talking-dirty-to-microcontrollers/</link>
		<comments>http://baltimorehackerspace.com/2010/02/talking-dirty-to-microcontrollers/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 03:52:19 +0000</pubDate>
		<dc:creator>solovus</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[Microcontroller]]></category>
		<category><![CDATA[PIC]]></category>

		<guid isPermaLink="false">http://harfordhackerspace.org/?p=504</guid>
		<description><![CDATA[Wednesday was the day we finally got together and started the tedious process of mastering microcontrollers.  What began with just some simple LED illumination evolved and progressed throughout the evening.  In the end we had individually, but collectively, written programs for interpreting analog inputs, displaying binary values, and performing counting and reset operations.  The evening [...]]]></description>
			<content:encoded><![CDATA[<p>Wednesday was the day we finally got together and started the tedious process of mastering microcontrollers.  What began with just some simple LED illumination evolved and progressed throughout the evening.  In the end we had individually, but collectively, written programs for interpreting analog inputs, displaying binary values, and performing counting and reset operations.  The evening was pretty fulfilling for some less experienced members and refreshing for the remainder.  Another microcontroller experiementation session is being planned for the near future.</p>

<div class="ngg-galleryoverview" id="ngg-gallery-14-504">

	<!-- Slideshow link -->
	<div class="slideshowlink">
		<a class="slideshowlink" href="http://baltimorehackerspace.com/2010/02/talking-dirty-to-microcontrollers/?show=slide">
			[Show as slideshow]		</a>
	</div>

	
	<!-- Thumbnails -->
		
	<div id="ngg-image-233" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://baltimorehackerspace.com/wp-content/gallery/3feb2010-microcontroller-session/dsc05421.jpg" title=" " class="shutterset_set_14" >
								<img title="dsc05421" alt="dsc05421" src="http://baltimorehackerspace.com/wp-content/gallery/3feb2010-microcontroller-session/thumbs/thumbs_dsc05421.jpg" width="100" height="75" />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-234" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://baltimorehackerspace.com/wp-content/gallery/3feb2010-microcontroller-session/dsc05426.jpg" title=" " class="shutterset_set_14" >
								<img title="dsc05426" alt="dsc05426" src="http://baltimorehackerspace.com/wp-content/gallery/3feb2010-microcontroller-session/thumbs/thumbs_dsc05426.jpg" width="100" height="75" />
							</a>
		</div>
	</div>
	
		
 		
	<div id="ngg-image-235" class="ngg-gallery-thumbnail-box"  >
		<div class="ngg-gallery-thumbnail" >
			<a href="http://baltimorehackerspace.com/wp-content/gallery/3feb2010-microcontroller-session/8_bit_counter_screenshot.jpg" title=" " class="shutterset_set_14" >
								<img title="8_bit_counter_screenshot" alt="8_bit_counter_screenshot" src="http://baltimorehackerspace.com/wp-content/gallery/3feb2010-microcontroller-session/thumbs/thumbs_8_bit_counter_screenshot.jpg" width="100" height="75" />
							</a>
		</div>
	</div>
	
		
 	 	
	<!-- Pagination -->
 	<div class='ngg-clear'></div>
 	
</div>


]]></content:encoded>
			<wfw:commentRss>http://baltimorehackerspace.com/2010/02/talking-dirty-to-microcontrollers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

