<?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>Starlino Electronics &#187; pic</title>
	<atom:link href="http://www.starlino.com/tag/pic/feed" rel="self" type="application/rss+xml" />
	<link>http://www.starlino.com</link>
	<description>Electronics and Robotics Projects, Tutorials, Reviews, Experiments</description>
	<lastBuildDate>Sat, 28 Jan 2012 01:38:28 +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>Quadcopter Prototype using Acc_Gyro and a PIC</title>
		<link>http://www.starlino.com/quadcopter_acc_gyro.html</link>
		<comments>http://www.starlino.com/quadcopter_acc_gyro.html#comments</comments>
		<pubDate>Wed, 09 Jun 2010 23:27:15 +0000</pubDate>
		<dc:creator>starlino</dc:creator>
				<category><![CDATA[Quadcopter]]></category>
		<category><![CDATA[accelerometer]]></category>
		<category><![CDATA[gyroscope]]></category>
		<category><![CDATA[imu]]></category>
		<category><![CDATA[pic]]></category>
		<category><![CDATA[quadcopter]]></category>

		<guid isPermaLink="false">http://quadcopter_acc_gyro</guid>
		<description><![CDATA[<p>This article introduces a new project I am working on - a&#160; Quadcopter using Acc_Gyro and a PIC. I share some things I learned along the way so far.</p>]]></description>
			<content:encoded><![CDATA[<p>For anyone following this site, here is what I&#39;ve been up to lately &#8211; building a quadcopter based on the <a href="http://gadgetgangster.com/213">Acc_Gyro 5DOF IMU</a> sensor and a 16bit PIC. Although it&#39;s still a work in progress I decided to start putting together an article placeholder and build it up as project evolves. It&#39;s going to be a long one !</p>
<p>The source code will be Open-Source and will be hosted on Google Code, you can get code for quad copter here:</p>
<p><a href="http://code.google.com/p/picquadcontroller/source/browse/#svn/trunk">http://code.google.com/p/picquadcontroller/source/browse/#svn/trunk</a></p>
<p>As usual I like to start with a video demo, it&#39;s basically me controlling the tilt of the quad using a RC controller:</p>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" height="505" width="640"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube.com/v/q_BZfG2xYdI&amp;hl=en_US&amp;fs=1&amp;" /><embed allowfullscreen="true" allowscriptaccess="always" height="505" src="http://www.youtube.com/v/q_BZfG2xYdI&amp;hl=en_US&amp;fs=1&amp;" type="application/x-shockwave-flash" width="640"></embed></object></p>
<p><span id="more-22"></span>This is a PID feedback algorithm and a &quot;Simplified Kalman Filter&quot; as&nbsp; described in my other articles.</p>
<p>First of all there&#39;s a a RCGroups thread where I first introduced the project</p>
<p>http://www.rcgroups.com/forums/showthread.php?t=1235360</p>
<p>You&#39;ll find some specs there and I will put some material here&nbsp; as well.</p>
<p>Here is Revision 0.1 of schematic (what I started with):</p>
<p><a href="data/PicQuadController/PicQuadControllerRev01.pdf">PicQuadControllerRev01.pdf</a></p>
<p>The major parts list is as follows:</p>
<p><strong>MCU:</strong>&nbsp; DSPIC33FJ128MC802&nbsp; (<a href="http://www.microchip.com/wwwproducts/Devices.aspx?dDocName=en532302">http://www.microchip.com/wwwproducts/Devices.aspx?dDocName=en532302</a>)</p>
<p><strong>Sensors</strong>: 5DOF Acc_Gyro ( <a href="http://gadgetgangster.com/213">http://gadgetgangster.com/213</a> )&nbsp; + 1Dof Pololu LIST300AL-BREAKOUT (<a href="http://www.pololu.com/catalog/product/765">http://www.pololu.com/catalog/product/765</a>).</p>
<p><strong>Motor Drivers:</strong>&nbsp; Power N-Channel Mosfets (Many choices -Low Rds(on) and rated for the current , I used&nbsp; IRLR8743 ). Schottky diodes to protect against back-EMF ( I used SS2H10-E3/52T )</p>
<p><strong>Frame , motors propellers</strong>: I picked a ready platform that was on sale <a href="http://www.rctoys.com/rc-toys-and-parts/DF-COMPLETE-AIRFRAME/RC-PARTS-DRAGANFLYER-FRAME.html" target="_blank">Dragandfly IV Frame</a> these are actually brushed motors same ones as used in Esky helicopters.</p>
<p>I did some tests on the lift force potential of this frame+motors+props, and was surprise to find out it performed better than some entry-level brushless motors. The lift force of this platform is up to 2.25 lbs.&nbsp; The results of lift-force experiments are compiled in this spreadheet: <a href="data/PicQuadController/MotorLiftPower.pdf" target="_blank">MotorLiftPower.pdf</a>.</p>
<p>How I measured the lift force ?&nbsp; I didn&#39;t have any fancy equipment so I simply fixed the quad in a vise and weighted&nbsp; it it at different throttle levels &#8211; the difference in weight gave me the lift power.</p>
<p><a href="/wp-content/uploads/data/PicQuadController/IMG_1463.JPG" target="_blank"><img alt="IMG_1463.JPG"  src="/wp-content/uploads/data/PicQuadController/IMG_1463.JPG" width="740" /></p>
</a><p>For testing just one motor, I used a string attached to the weight:</p>
<p><a href="/wp-content/uploads/data/PicQuadController/IMG_1460.JPG" target="_blank"><img alt="IMG_1460.JPG"  src="/wp-content/uploads/data/PicQuadController/IMG_1460.JPG" width="740" /></p>
</a><p>I built a test rig out of wood for testing the tilt balancing algorithm as you see in the video.</p>
<p>Here are some close-ups of the motor drivers. You&#39;ll&nbsp; see the Mosfets and the Schottky diodes, also note the extra solder added to support the high currents that will flow through those traces ( up to 5A per motor !).</p>
<p><a href="/wp-content/uploads/data/PicQuadController/IMG_1465.JPG" target="_blank"><img alt="IMG_1465.JPG"  src="/wp-content/uploads/data/PicQuadController/IMG_1465.JPG" width="740" /></p>
</a><p>Finally here is a view on the top of the board ( notice the PIC , the switching regulator&nbsp; ,&nbsp; Acc_Gyro 5DOF IMU and single axes Gyro breakout , from top-left to bottom right).</p>
<p><a href="/wp-content/uploads/data/PicQuadController/IMG_1466.JPG" target="_blank"><img alt="IMG_1466.JPG"  src="/wp-content/uploads/data/PicQuadController/IMG_1466.JPG" width="740" /></p>
</a><p>This is it for now hope to bring you more interesting stuff on this and other projects if time allows&nbsp; !</p>
<p>//starlino//</p>
]]></content:encoded>
			<wfw:commentRss>http://www.starlino.com/quadcopter_acc_gyro.html/feed</wfw:commentRss>
		<slash:comments>41</slash:comments>
		</item>
		<item>
		<title>Trickster Mouse Emulator – April Fools Day Practical Joke</title>
		<link>http://www.starlino.com/usbthumb_mouse_emulator.html</link>
		<comments>http://www.starlino.com/usbthumb_mouse_emulator.html#comments</comments>
		<pubDate>Wed, 31 Mar 2010 18:57:43 +0000</pubDate>
		<dc:creator>starlino</dc:creator>
				<category><![CDATA[Fun Projects]]></category>
		<category><![CDATA[fun]]></category>
		<category><![CDATA[mouse]]></category>
		<category><![CDATA[pic]]></category>
		<category><![CDATA[usb]]></category>
		<category><![CDATA[UsbThumb]]></category>

		<guid isPermaLink="false">http://usbthumb_mouse_emulator</guid>
		<description><![CDATA[<p>Trickster is a simple application built on UsbThumb platform. It tricks your victim into thinking they have a virus on their computer, by hardware emulating and moving their mouse in circles on intermittent intervals.</p>]]></description>
			<content:encoded><![CDATA[<p>Trickster is a simple application built on UsbThumb platform. It tricks your victim into thinking they have a virus on their computer, by hardware emulating and moving their mouse in circles on intermittent intervals.</p>
<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" height="300" width="400"><param name="allowfullscreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://vimeo.com/moogaloop.swf?clip_id=10510369&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" /><embed allowfullscreen="true" allowscriptaccess="always" height="300" src="http://vimeo.com/moogaloop.swf?clip_id=10510369&amp;server=vimeo.com&amp;show_title=1&amp;show_byline=1&amp;show_portrait=0&amp;color=&amp;fullscreen=1" type="application/x-shockwave-flash" width="400"></embed></object></p>
<p><span id="more-20"></span>It is basically a customization of Mouse emulation demo that Microchip bundles with their USB Framework.</p>
<p>The code responsible for enabling/disabling mouse emulation is triggered by a timer interrupt like so:</p>
<p>
	<span class="edit_monospace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; OpenTimer0( TIMER_INT_ON &amp;<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; T0_16BIT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &amp;<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; T0_SOURCE_INT&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &amp;<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; T0_PS_1_256);</p>
<p>	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8230;&#8230;&#8230;&#8230;.</span></p>
<p><span class="edit_monospace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //These are your actual interrupt handling routines.</span></p>
<p>&nbsp;</p>
<p><span class="edit_monospace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #pragma interrupt YourHighPriorityISRCode<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; void YourHighPriorityISRCode()<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //Check which interrupt flag caused the interrupt.<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //Service the interrupt<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //Clear the interrupt flag<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //Etc.<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #if defined(USB_INTERRUPT)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; USBDeviceTasks();<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; #endif<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(INTCONbits.TMR0IF){<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; counter_s++; <br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //to interrupt every 1s we need to count FOSC / 4/ 256&nbsp; = 46875 ticks (0xB71B)<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //to count these ticks till overflow we need to set TMR0 to&nbsp; 0xFFFF &#8211; 0xB71B = 0x48E4 <br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TMR0H = 0&#215;48; //will write to TMR0H buffer<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; TMR0L = 0xE4; //will write actual TMR0L / TMR0H&nbsp; </p>
<p>	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; INTCONbits.TMR0IF = 0;<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }</p>
<p>
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //This return will be a &quot;retfie fast&quot;, since this is in a #pragma interrupt section</span></p>
<p class="edit_monospace">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8230;&#8230;&#8230;&#8230;&#8230;..</p>
<p>	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; INTCONbits.TMR0IE = 0;&nbsp; //disable Timer0 interrupt<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if(emulate_mode &amp;&amp; counter_s &gt; 3){&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // seconds on<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; counter_s = 0;<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; emulate_mode = 0;<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }else if (!emulate_mode &amp;&amp; counter_s &gt; 60){&nbsp;&nbsp;&nbsp;&nbsp; //seconds off<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; counter_s = 0;<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; emulate_mode = 1;<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br />
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; INTCONbits.TMR0IE = 1;</p>
<p>&nbsp;</p>
<p><span class="edit_monospace"><br />
	</span></p>
<p>For full source code see:</p>
<p><a href="http://code.google.com/p/usbthumb/source/browse/#svn/trunk/UsbThumbMousePrank" target="_blank">http://code.google.com/p/usbthumb/source/browse/#svn/trunk/UsbThumbMousePrank</a></p>
<p><strong>UsbThumb is an open-hardware platform, anyone can build their own:</strong></p>
<p><a href="http://code.google.com/p/usbthumb/source/browse/#svn/trunk/Hardware" target="_blank">http://code.google.com/p/usbthumb/source/browse/#svn/trunk/Hardware</a></p>
<p>or pick up a prebuilt SMT version of UsbThumb here: <a href="http://www.gadgetgangster.com/303">http://www.gadgetgangster.com/303</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.starlino.com/usbthumb_mouse_emulator.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Updating Firmware on USBThumb</title>
		<link>http://www.starlino.com/usbthumb_firmware_upgrade.html</link>
		<comments>http://www.starlino.com/usbthumb_firmware_upgrade.html#comments</comments>
		<pubDate>Thu, 18 Mar 2010 16:57:21 +0000</pubDate>
		<dc:creator>starlino</dc:creator>
				<category><![CDATA[USBThumb]]></category>
		<category><![CDATA[firmware]]></category>
		<category><![CDATA[pic]]></category>
		<category><![CDATA[programmer]]></category>
		<category><![CDATA[propeller]]></category>
		<category><![CDATA[serial]]></category>
		<category><![CDATA[usb]]></category>
		<category><![CDATA[UsbThumb]]></category>

		<guid isPermaLink="false">http://usbthumb_firmware_upgrade</guid>
		<description><![CDATA[<p>This article describes how to update firmware on USBThumb.</p>]]></description>
			<content:encoded><![CDATA[<p><a href="http://gadgetgangster.com/240" target="_blank">USBThumb</a> can be programmed with different firmware, including all Microchip samples from <a href="http://www.google.com/search?q=Microchip+USB+Framework" target="_blank">USB Framwework</a> (use the ones for low pin count USB demo board).</p>
<p>In order to upload a new firmware to USBThumb you need to enter the bootloader mode. This is done by connecting the&nbsp; VPP pin to ground during the time when device is plugged into the USB port.</p>
<p>This can achieved by placing a jumper wire or a 1K resistor (recommended) between GND and VPP pin.</p>
<p><strong>GND pin is number 1, and VPP is number 5. <span style="background-color: rgb(255, 255, 0);">Make sure you do not confuse VPP with the nearby VDD(pin number 4) or you will get a short !!! To be safe use a 1K or 10K resistor instead of the wire.</span> Wire/resistor can be held by the spring action, it only needs to make contact during a split second while the device is plugged. If you plan on making frequent firmware updates , soldering a header is recommended.</strong></p>
<p><img alt="IMG_1408.JPG" src="http://www.starlino.com/wp-content/uploads/data/usbthumb_firmware_upgrade/IMG_1408.JPG" /></p>
<p><span id="more-19"></span>Before plugging the device to USB port,&nbsp; start the &quot;USB HID Bootloader&quot; application (it is installed as part of the Microchip USB Framework, look in C:\Microchip Solutions\USB Device &#8211; Bootloaders\HID &#8211; Bootloader), a copy is placed here for download:</p>
<p><a href="data/usbthumb_firmware_upgrade/HIDBootLoader.zip">HIDBootLoader.zip</a></p>
<p>Next insert the USBThumb into the USB port (with the VPP and GND pins connected) , you should see a &quot;Device attached.&quot; message in the USB HID Bootloader window.</p>
<p>Next steps are simple:</p>
<p>1. Click on &ldquo;Open Hex File&rdquo; button and select the new firmware .HEX file you want to load<br />
	2. Click on &ldquo;Program/Verify&rdquo; and wait for the process to complete<br />
	3. Unplug the device from USB port. Remove the jumber wire/resistor between GND and VPP pin.<br />
	4. Plug the device back and verify the new firmware is working (it depends on firmware and what you expect it to do).</p>
<p>The pictures below illustrate the above steps:</p>
<p><img alt="UsbHidBootloaderSelectFile.png" src="http://www.starlino.com/wp-content/uploads/data/usbthumb_firmware_upgrade/UsbHidBootloaderSelectFile.png" /></p>
<p>&nbsp;</p>
<p><img alt="UsbHidBootloaderBurnFirmware.png" src="http://www.starlino.com/wp-content/uploads/data/usbthumb_firmware_upgrade/UsbHidBootloaderBurnFirmware.png" /></p>
]]></content:encoded>
			<wfw:commentRss>http://www.starlino.com/usbthumb_firmware_upgrade.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Using USBThumb as a Propeller Programmer  and  as a USB to Serial Converter</title>
		<link>http://www.starlino.com/usbthumb_propeller_programmer.html</link>
		<comments>http://www.starlino.com/usbthumb_propeller_programmer.html#comments</comments>
		<pubDate>Mon, 15 Mar 2010 22:44:19 +0000</pubDate>
		<dc:creator>starlino</dc:creator>
				<category><![CDATA[USBThumb]]></category>
		<category><![CDATA[pic]]></category>
		<category><![CDATA[programmer]]></category>
		<category><![CDATA[propeller]]></category>
		<category><![CDATA[serial]]></category>
		<category><![CDATA[usb]]></category>
		<category><![CDATA[UsbThumb]]></category>

		<guid isPermaLink="false">http://usbthumb_propeller_programmer</guid>
		<description><![CDATA[<p>This article describes how you can use USBThumb as a Propeller Programmer or USB to Serial converter.</p>]]></description>
			<content:encoded><![CDATA[<p>The Parallax&#39;s classic propeller programmer PropPlug&nbsp; is simply a USB to Serial converter, the DTR/RTS signals are used to reset the Propeller chip before starting the programming sequence.</p>
<p>A custom PIC firmware&nbsp; comes preloaded on the <br />
	<a href="http://www.gadgetgangster.com/289" target="_blank">&quot;USBThumb Propeller Programmer&quot;</a> available on GadgetGangster.com, it allows you to use USBThumb as a Propeller Programmer but also as USB to Serial converter.</p>
<p>USBThumb (loaded with <a href="http://code.google.com/p/usbthumb/">USBThumbSerial </a>firmware)&nbsp; is detected on your computer as a standard&nbsp; CDC Modem device and works on all modern operating systems (Windows, Mac os , Linux).</p>
<p>On Windows you might be asked for driver path once you attach the device, simply use the driver part of the Microchip&#39;s USB Framework (it can be found in C:\Microchip Solutions\USB Device &#8211; CDC &#8211; Serial Emulator\inf\win2k_winxp ) , and I am including a copy for direct download here:</p>
<p><a href="data/usbthumb_propeller_programmer/microchip_cdc_win2k_winxp.zip">microchip_cdc_win2k_winxp.zip</a></p>
<p>On Windows you can identify USBThumb&#39;s COM port number from Device Manager:</p>
<p><img alt="3-15-2010 10-24-15 PM.png" src="/wp-content/uploads/data/usbthumb_propeller_programmer/3-15-2010 10-24-15 PM.png" /></p>
<p><span id="more-18"></span>On Mac OS, once you attach the device&nbsp; you will get a prompt &quot;A new network interface has been detected&quot;. Look in the /dev folder. You should see it as &quot;/dev/tty.usbmodem411&quot; or similar.</p>
<p>PropellerTool can scan all available ports and find the one that has a propeller chip attached. So the above step might not be necessary, however if you already know the COM port of USBThumb you can specify it explicitly.</p>
<p>In the PropellerTool go to&nbsp; Edit &gt; Preferences&gt;Operation and update the &quot;Serial Port Search&quot; to the port number of USBThumb (COM15 in my example). In the &quot;Propeller Reset Signal&quot; choose DTR.</p>
<p><img alt="3-15-2010 10-37-28 PM.png" src="/wp-content/uploads/data/usbthumb_propeller_programmer/3-15-2010 10-37-28 PM.png" /></p>
<p>&nbsp;</p>
<p>Next attach the USBThumb to your Propeller development board, USBThumb&#39;s serial&nbsp; 4-pin connector is pin compatible with PropPlug. The pins are marked as follows RX,TX,RST,GND.</p>
<p>To test the communication with the propeller board press F7.</p>
<p>If you experience problems make sure the port is not open by any other software (like for example the propeller terminal software)</p>
<p>In some rare cases if you computer power supply is too noisy, to ensure greater stability you can place a 18pF filtering capacitor between pins 24 (RB5)&nbsp; and 25 (RB6). See picture below:</p>
<p><img alt="usbthumb_adding_filter_capacitor.jpg" src="/wp-content/uploads/data/usbthumb_propeller_programmer/usbthumb_adding_filter_capacitor.jpg" /></p>
<p>You can solder the capacitor permanently between these pins, or if you&#39;re planning to use USBThumb for other projects and you have soldered sockets to the board simply insert the capacitor in the socket between pins 24 and 25. You only need to take this step if you have intermittent problems with PropellerTool not detecting the Propeller chip.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.starlino.com/usbthumb_propeller_programmer.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>USB Motion Gamepad Update: wide accelerometer and gyroscope support, configuration utility software</title>
		<link>http://www.starlino.com/usb_gamepad_gyro.html</link>
		<comments>http://www.starlino.com/usb_gamepad_gyro.html#comments</comments>
		<pubDate>Sat, 26 Sep 2009 09:44:54 +0000</pubDate>
		<dc:creator>starlino</dc:creator>
				<category><![CDATA[Motion Sensing USB Devices]]></category>
		<category><![CDATA[accelerometer]]></category>
		<category><![CDATA[gyroscope]]></category>
		<category><![CDATA[imu]]></category>
		<category><![CDATA[Motion Gamepad]]></category>
		<category><![CDATA[pic]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[usb]]></category>

		<guid isPermaLink="false">http://usb_gamepad_gyro</guid>
		<description><![CDATA[<p>I have received some feedback from my readers regarding my first usb gamepad project , so for the past few weeks I was working on a new imrpoved design.&#160;&#160;&#160; There are plenty of new improvements that I hope will address many of your requests. The new device supports a wide range of gyroscopes and accelerometers that can be configured using&#160; Configuration Utility for Windows. Have a look at this article as it features new suggested schematics as well as a full feature log. Firmware and configuration utility are free to download for private use.</p>]]></description>
			<content:encoded><![CDATA[<p>I have received some feedback from my readers regarding my first <a href="usb_gamepad.html">usb gamepad project</a> , so for the past few weeks I was working on a new imrpoved design. There are plenty of new improvements that I hope will address many of your requests.</p>
<p><span id="more-11"></span><strong>Release Notes / Change Log</strong>:</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
	2009-09-25<br />
	Config Utility Version 1.0.0<br />
	Device Firmware Version 1.5<br />
	&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p>- device supports a wide range of accelerometers and gyroscopes<br />
	- implements smoothing filtering algorithm <br />
	- implements algorithm for combining gyroscope and accelerometer data<br />
	- HID USB interface makes the device compatible virtually with all modern operating systems (Mac,Windows,Linux)<br />
	- configuration utility (for Windows only for now)<br />
	- adjustable smoothing and scale factor for outputs<br />
	- configurable sensibility for accelerometer and gyroscope (can be set per axis)<br />
	- configurable midpoint (0g voltage) for accelerometer (can be set per axis)<br />
	- auto-find gyroscope midpoint (0 deg/s voltage)<br />
	- you can use separate accelerometer/gyroscopes for different axis<br />
	- Z axis not implemented in this release<br />
	- configurable analog input ports AN0-AN5 for each axis (please note 18F2550 does not have AN5 input , but 18F4550 does)<br />
	- ability to invert axis<br />
	- capture output and raw adc data into a comma separated values file (.csv)<br />
	- charting feature of all inputs/outputs <br />
	- graphical display of output and buttons<br />
	- supports up to 8 buttons<br />
	- configuration is stored in device EEPROM<br />
	- it is possible to use accelerometer alone on any axis (1, 2 or 3-axis) in this case set Smoothing to 0-10 value<br />
	- it is possible to gyro alone on any axis (1,2 or 3 axis) in this case set Smoothing to 100-255 , please note that the output will snap to middle after a while since gyro only provides rate information<br />
	- it is possible to use to use gyro and accelerometer on any axis (RECOMMENDED) in this case set smoothing to 20-100 value<br />
	- you can use multiple accelerometers or gyroscopes<br />
	- you can orient gyro or accelerometer as you like (but aligned to 90 degrees increments)<br />
	- you can connect your gyro and accelerometer outputs to any of the AN0-AN4 ports (for PIC18F2550).</p>
<p><strong>Suggested Circuit Schematic</strong></p>
<p>Below is a new suggested schematic , using a 2-axis Gyroscope and an a 2-axis Accelerometer. As mentioned above the new firmware and configuration utility is very flexible regarding the way the gyro and accelerometer are connected (this is one of the features requested , to be able to use device with various MEMS sensors).</p>
<p><img alt="Schematic  for USB Gamepad with Accelerometer and Gyroscope" height="748" src="data/usb_gamepad_gyro/usb_gamepad_gyro_schematic.gif" width="698" /></p>
<p><strong>Configuration Utility</strong></p>
<p>Here is a screen shot of the configuration utility, please note that I am using the gyro just for one output axis (X), thus I set smoothing to 50 as recommended in release notes above. The other axis (Y) uses just accelerometer data so Smoothing is set to 5. Device automatically detected the Gyro midpoint to be 1.35V , you must hold device still for at least 1s to let it calibrate when you plug it in. The Gyro axis is also inverted due to the way it is mounted in the case.</p>
<p><a href="data/usb_gamepad_gyro/usb_gamepad_config_utility_1.gif" target="_blank"><img alt="usb motion gamepad configuration utility"  src="data/usb_gamepad_gyro/usb_gamepad_config_utility_1.gif" width="740" /></p>
</a><p><strong>Hardware Setup</strong></p>
<p>My old accelerometer-only device , got modified to make space for a gyro. Here is how it all fit inside the case:</p>
<p><img alt="usb tilt / pan gamepad in Wii steering wheel, uses accelerometer, gyro and pic18f2550" height="480" src="data/usb_gamepad_gyro/IMG_1116.JPG" width="640" /></p>
<p><strong>Demo</strong></p>
<p>Finally here is a new demo with another game I found to work well with this device PURE. In this game you can actually use the FORWARD/BACK tilt (Y axis of the device), besides the usual LEFT/RIGHT (X axis) I demonstrated in the TMNations demos (see my <a href="usb_gamepad.html">first usb gamepad article)</a>.</p>
<p><object height="385" width="480"><param name="movie" value="http://www.youtube.com/v/_FQgmpv_Wc4&amp;hl=en&amp;fs=1&amp;rel=0" /><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><embed allowfullscreen="true" allowscriptaccess="always" height="385" src="http://www.youtube.com/v/_FQgmpv_Wc4&amp;hl=en&amp;fs=1&amp;rel=0" type="application/x-shockwave-flash" width="480"></embed></object></p>
<p><strong>Downloads</strong></p>
<p>Configuration Utility Setup (Windows XP/Vista) <a href="data/usb_gamepad_gyro/StarlinoGamepadConfigSetup_100.exe">StarlinoGamepadConfigSetup_100.exe</a></p>
<p>PIC18F2550 Firmware <a href="data/usb_gamepad_gyro/18Fx550_USB_GAMEPAD_GYRO_15.hex">18Fx550_USB_GAMEPAD_GYRO_15.hex</a> (right click on the link and choose Save Link/Target As if HEX file opens in browser).</p>
<p><em>I will continue to edit this article , please let me know what questions/suggestions you have in the comments area below. I will try to update this page and design based on your feedback.</em></p>
<p>//starlino//</p>
]]></content:encoded>
			<wfw:commentRss>http://www.starlino.com/usb_gamepad_gyro.html/feed</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Use macro definitions to simplify work with I/O ports in C (MICROCHIP PIC/ATMEL AVR)</title>
		<link>http://www.starlino.com/port_macro.html</link>
		<comments>http://www.starlino.com/port_macro.html#comments</comments>
		<pubDate>Mon, 10 Aug 2009 00:00:00 +0000</pubDate>
		<dc:creator>starlino</dc:creator>
				<category><![CDATA[Tricks and Tips]]></category>
		<category><![CDATA[avr]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[pic]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://port_macro</guid>
		<description><![CDATA[<p>This article describes a series of techniques that facilitates defining and programming I/O ports in embeded C with the same ease you do these task in PBASIC/PicBasic. Macro definitions and sample code is given for PIC C18/C30&#160; and&#160; gcc-avr (ATMEL MCUs).</p>]]></description>
			<content:encoded><![CDATA[<p>Wouldn&#39;t it be nice that instead of the following PIC&nbsp; C&nbsp; ( or similar AVR C ) code:</p>
<pre><span style="background-color:#ffff00;">TRISAbits.TRISA2 = 1;  //make switch pin an input
WPUAbits.WPUA2 = 1;    //turn on the pull-up on switch pin
TRISBbits.TRISB5 = 0;  //make led pin an output</span></pre>
<p>&nbsp;</p>
<p>You could just right something like this, that allows you to change later on the PIN assigned for a specific function:</p>
<p>&nbsp;</p>
<pre><span style="background-color:#ffff00;"><span class="edit_monospace">#define pinSwitch(f)       f(A,2)        //Declare Switch pin RA2</span>
<span class="edit_monospace">#define pinLed(f)          f(B,5)        //Declar Led pin RB5
</span><span class="edit_monospace">_TRIS(pinSwitch) = 1; //make pin an input
_WPU(pinSwitch) = 1;  // turn on internal pull-up on switch pin
_TRIS(pinLed) = 0;    // make led pin an output</span></span></pre>
<p>&nbsp;</p>
<p>If you&#39;re interested how you can simplify your PIC/AVR C code then read on &#8230;.</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</p>
<p>I started working with MCUs using Basic Stamps 2. My first programs where written in PBASIC. I enjoyed the simplicity of this language but soon moved on to C. One particular thing that I was missing in the C enviroment was the ability to define a pin at the begining of the program and then perform various operations on it. (Make it an input or output, toggle the pin, read its level, output a high or low signal). PIC Microcontrollers make use of several registers to perform these operations, and if you change your pin you need to update all your lines of code that perform these operation.</p>
<p>To illustrate let&#39;s take a simple example. Let&#39;s say you have a button and a led, you&#39;d like to create a simple application that will turn on the led when a momentary switch is pressed. The hardware setup will be very simple: the led is connected to an output pin of MCU (with a resitor in series to avoid burning the led); the switch is connected to another pin with an optional external pull-up resistor (some PIC and AVR can use an internal pull-up). Please note when the switch is pressed it will output 0 (LOW) and when it is not pressed it will output 1 (HIGH) due to the pull-up resistor.</p>
<p><span id="more-6"></span></p>
<p><span class="edit_monospace">&nbsp;[VDD 5V]</span><span class="edit_monospace">&nbsp;&nbsp; <br />
	&nbsp;&nbsp; |<br />
	&nbsp; [RESISTOR 10K]<br />
	&nbsp;&nbsp; |<br />
	P1 &#8212;-[SWITCH] &#8212; [GND] </span></p>
<p><span class="edit_monospace">P2 &#8212;-[RESISTOR ~ 200Ohm] &#8212;[LED]&#8212;[GND]</span></p>
<p>Now here is the PBASIC code:</p>
<p class="edit_monospace">pinSwitch&nbsp; PIN 1<br />
	pinLed&nbsp;&nbsp;&nbsp;&nbsp; PIN 2</p>
<p>	INPUT&nbsp; pinSwitch<br />
	OUTPUT pinLed</p>
<p class="edit_monospace">main:</p>
<p class="edit_monospace">&nbsp;IF pinSwitch = 0 THEN<br />
	&nbsp; HIGH pinLed <br />
	&nbsp;ELSE<br />
	&nbsp; LOW pinLed<br />
	&nbsp;ENDIF</p>
<p><span class="edit_monospace">GOTO main</span></p>
<p>If you ever need to change the pins, let&#39;s say you want to swap the pins for led and switch all you need to change is the first two lines of code:</p>
<p>	<span class="edit_monospace">pinSwitch&nbsp; PIN <span style="color: rgb(255, 0, 0);">2</span><br />
	pinLed&nbsp;&nbsp;&nbsp;&nbsp; PIN <span style="color: rgb(255, 0, 0);">1</span></span></p>
<p>All the remaining PBASIC code remains the same.</p>
<p>Now let&#39;s look at the C18/C30 code for the PIC <!--[if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:TrackMoves /> <w:TrackFormatting /> <w:PunctuationKerning /> <w:ValidateAgainstSchemas /> <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid> <w:IgnoreMixedContent>false</w:IgnoreMixedContent> <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText> <w:DoNotPromoteQF /> <w:LidThemeOther>EN-US</w:LidThemeOther> <w:LidThemeAsian>X-NONE</w:LidThemeAsian> <w:LidThemeComplexScript>X-NONE</w:LidThemeComplexScript> <w:Compatibility> <w:BreakWrappedTables /> <w:SnapToGridInCell /> <w:WrapTextWithPunct /> <w:UseAsianBreakRules /> <w:DontGrowAutofit /> <w:SplitPgBreakAndParaMark /> <w:DontVertAlignCellWithSp /> <w:DontBreakConstrainedForcedTables /> <w:DontVertAlignInTxbx /> <w:Word11KerningPairs /> <w:CachedColBalance /> </w:Compatibility> <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> <m:mathPr> <m:mathFont m:val="Cambria Math" /> <m:brkBin m:val="before" /> <m:brkBinSub m:val="&#45;-" /> <m:smallFrac m:val="off" /> <m:dispDef /> <m:lMargin m:val="0" /> <m:rMargin m:val="0" /> <m:defJc m:val="centerGroup" /> <m:wrapIndent m:val="1440" /> <m:intLim m:val="subSup" /> <m:naryLim m:val="undOvr" /> </m:mathPr></w:WordDocument> </xml><![endif]--><!--[if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" DefUnhideWhenUsed="true"   DefSemiHidden="true" DefQFormat="false" DefPriority="99"   LatentStyleCount="267"> <w:LsdException Locked="false" Priority="0" SemiHidden="false"    UnhideWhenUsed="false" QFormat="true" Name="Normal" /> <w:LsdException Locked="false" Priority="9" SemiHidden="false"    UnhideWhenUsed="false" QFormat="true" Name="heading 1" /> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 2" /> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 3" /> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 4" /> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 5" /> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 6" /> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 7" /> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 8" /> <w:LsdException Locked="false" Priority="9" QFormat="true" Name="heading 9" /> <w:LsdException Locked="false" Priority="39" Name="toc 1" /> <w:LsdException Locked="false" Priority="39" Name="toc 2" /> <w:LsdException Locked="false" Priority="39" Name="toc 3" /> <w:LsdException Locked="false" Priority="39" Name="toc 4" /> <w:LsdException Locked="false" Priority="39" Name="toc 5" /> <w:LsdException Locked="false" Priority="39" Name="toc 6" /> <w:LsdException Locked="false" Priority="39" Name="toc 7" /> <w:LsdException Locked="false" Priority="39" Name="toc 8" /> <w:LsdException Locked="false" Priority="39" Name="toc 9" /> <w:LsdException Locked="false" Priority="35" QFormat="true" Name="caption" /> <w:LsdException Locked="false" Priority="10" SemiHidden="false"    UnhideWhenUsed="false" QFormat="true" Name="Title" /> <w:LsdException Locked="false" Priority="1" Name="Default Paragraph Font" /> <w:LsdException Locked="false" Priority="11" SemiHidden="false"    UnhideWhenUsed="false" QFormat="true" Name="Subtitle" /> <w:LsdException Locked="false" Priority="22" SemiHidden="false"    UnhideWhenUsed="false" QFormat="true" Name="Strong" /> <w:LsdException Locked="false" Priority="20" SemiHidden="false"    UnhideWhenUsed="false" QFormat="true" Name="Emphasis" /> <w:LsdException Locked="false" Priority="59" SemiHidden="false"    UnhideWhenUsed="false" Name="Table Grid" /> <w:LsdException Locked="false" UnhideWhenUsed="false" Name="Placeholder Text" /> <w:LsdException Locked="false" Priority="1" SemiHidden="false"    UnhideWhenUsed="false" QFormat="true" Name="No Spacing" /> <w:LsdException Locked="false" Priority="60" SemiHidden="false"    UnhideWhenUsed="false" Name="Light Shading" /> <w:LsdException Locked="false" Priority="61" SemiHidden="false"    UnhideWhenUsed="false" Name="Light List" /> <w:LsdException Locked="false" Priority="62" SemiHidden="false"    UnhideWhenUsed="false" Name="Light Grid" /> <w:LsdException Locked="false" Priority="63" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Shading 1" /> <w:LsdException Locked="false" Priority="64" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Shading 2" /> <w:LsdException Locked="false" Priority="65" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium List 1" /> <w:LsdException Locked="false" Priority="66" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium List 2" /> <w:LsdException Locked="false" Priority="67" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 1" /> <w:LsdException Locked="false" Priority="68" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 2" /> <w:LsdException Locked="false" Priority="69" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 3" /> <w:LsdException Locked="false" Priority="70" SemiHidden="false"    UnhideWhenUsed="false" Name="Dark List" /> <w:LsdException Locked="false" Priority="71" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful Shading" /> <w:LsdException Locked="false" Priority="72" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful List" /> <w:LsdException Locked="false" Priority="73" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful Grid" /> <w:LsdException Locked="false" Priority="60" SemiHidden="false"    UnhideWhenUsed="false" Name="Light Shading Accent 1" /> <w:LsdException Locked="false" Priority="61" SemiHidden="false"    UnhideWhenUsed="false" Name="Light List Accent 1" /> <w:LsdException Locked="false" Priority="62" SemiHidden="false"    UnhideWhenUsed="false" Name="Light Grid Accent 1" /> <w:LsdException Locked="false" Priority="63" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Shading 1 Accent 1" /> <w:LsdException Locked="false" Priority="64" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Shading 2 Accent 1" /> <w:LsdException Locked="false" Priority="65" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium List 1 Accent 1" /> <w:LsdException Locked="false" UnhideWhenUsed="false" Name="Revision" /> <w:LsdException Locked="false" Priority="34" SemiHidden="false"    UnhideWhenUsed="false" QFormat="true" Name="List Paragraph" /> <w:LsdException Locked="false" Priority="29" SemiHidden="false"    UnhideWhenUsed="false" QFormat="true" Name="Quote" /> <w:LsdException Locked="false" Priority="30" SemiHidden="false"    UnhideWhenUsed="false" QFormat="true" Name="Intense Quote" /> <w:LsdException Locked="false" Priority="66" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium List 2 Accent 1" /> <w:LsdException Locked="false" Priority="67" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 1 Accent 1" /> <w:LsdException Locked="false" Priority="68" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 2 Accent 1" /> <w:LsdException Locked="false" Priority="69" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 3 Accent 1" /> <w:LsdException Locked="false" Priority="70" SemiHidden="false"    UnhideWhenUsed="false" Name="Dark List Accent 1" /> <w:LsdException Locked="false" Priority="71" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful Shading Accent 1" /> <w:LsdException Locked="false" Priority="72" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful List Accent 1" /> <w:LsdException Locked="false" Priority="73" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful Grid Accent 1" /> <w:LsdException Locked="false" Priority="60" SemiHidden="false"    UnhideWhenUsed="false" Name="Light Shading Accent 2" /> <w:LsdException Locked="false" Priority="61" SemiHidden="false"    UnhideWhenUsed="false" Name="Light List Accent 2" /> <w:LsdException Locked="false" Priority="62" SemiHidden="false"    UnhideWhenUsed="false" Name="Light Grid Accent 2" /> <w:LsdException Locked="false" Priority="63" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Shading 1 Accent 2" /> <w:LsdException Locked="false" Priority="64" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Shading 2 Accent 2" /> <w:LsdException Locked="false" Priority="65" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium List 1 Accent 2" /> <w:LsdException Locked="false" Priority="66" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium List 2 Accent 2" /> <w:LsdException Locked="false" Priority="67" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 1 Accent 2" /> <w:LsdException Locked="false" Priority="68" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 2 Accent 2" /> <w:LsdException Locked="false" Priority="69" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 3 Accent 2" /> <w:LsdException Locked="false" Priority="70" SemiHidden="false"    UnhideWhenUsed="false" Name="Dark List Accent 2" /> <w:LsdException Locked="false" Priority="71" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful Shading Accent 2" /> <w:LsdException Locked="false" Priority="72" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful List Accent 2" /> <w:LsdException Locked="false" Priority="73" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful Grid Accent 2" /> <w:LsdException Locked="false" Priority="60" SemiHidden="false"    UnhideWhenUsed="false" Name="Light Shading Accent 3" /> <w:LsdException Locked="false" Priority="61" SemiHidden="false"    UnhideWhenUsed="false" Name="Light List Accent 3" /> <w:LsdException Locked="false" Priority="62" SemiHidden="false"    UnhideWhenUsed="false" Name="Light Grid Accent 3" /> <w:LsdException Locked="false" Priority="63" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Shading 1 Accent 3" /> <w:LsdException Locked="false" Priority="64" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Shading 2 Accent 3" /> <w:LsdException Locked="false" Priority="65" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium List 1 Accent 3" /> <w:LsdException Locked="false" Priority="66" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium List 2 Accent 3" /> <w:LsdException Locked="false" Priority="67" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 1 Accent 3" /> <w:LsdException Locked="false" Priority="68" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 2 Accent 3" /> <w:LsdException Locked="false" Priority="69" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 3 Accent 3" /> <w:LsdException Locked="false" Priority="70" SemiHidden="false"    UnhideWhenUsed="false" Name="Dark List Accent 3" /> <w:LsdException Locked="false" Priority="71" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful Shading Accent 3" /> <w:LsdException Locked="false" Priority="72" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful List Accent 3" /> <w:LsdException Locked="false" Priority="73" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful Grid Accent 3" /> <w:LsdException Locked="false" Priority="60" SemiHidden="false"    UnhideWhenUsed="false" Name="Light Shading Accent 4" /> <w:LsdException Locked="false" Priority="61" SemiHidden="false"    UnhideWhenUsed="false" Name="Light List Accent 4" /> <w:LsdException Locked="false" Priority="62" SemiHidden="false"    UnhideWhenUsed="false" Name="Light Grid Accent 4" /> <w:LsdException Locked="false" Priority="63" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Shading 1 Accent 4" /> <w:LsdException Locked="false" Priority="64" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Shading 2 Accent 4" /> <w:LsdException Locked="false" Priority="65" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium List 1 Accent 4" /> <w:LsdException Locked="false" Priority="66" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium List 2 Accent 4" /> <w:LsdException Locked="false" Priority="67" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 1 Accent 4" /> <w:LsdException Locked="false" Priority="68" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 2 Accent 4" /> <w:LsdException Locked="false" Priority="69" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 3 Accent 4" /> <w:LsdException Locked="false" Priority="70" SemiHidden="false"    UnhideWhenUsed="false" Name="Dark List Accent 4" /> <w:LsdException Locked="false" Priority="71" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful Shading Accent 4" /> <w:LsdException Locked="false" Priority="72" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful List Accent 4" /> <w:LsdException Locked="false" Priority="73" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful Grid Accent 4" /> <w:LsdException Locked="false" Priority="60" SemiHidden="false"    UnhideWhenUsed="false" Name="Light Shading Accent 5" /> <w:LsdException Locked="false" Priority="61" SemiHidden="false"    UnhideWhenUsed="false" Name="Light List Accent 5" /> <w:LsdException Locked="false" Priority="62" SemiHidden="false"    UnhideWhenUsed="false" Name="Light Grid Accent 5" /> <w:LsdException Locked="false" Priority="63" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Shading 1 Accent 5" /> <w:LsdException Locked="false" Priority="64" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Shading 2 Accent 5" /> <w:LsdException Locked="false" Priority="65" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium List 1 Accent 5" /> <w:LsdException Locked="false" Priority="66" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium List 2 Accent 5" /> <w:LsdException Locked="false" Priority="67" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 1 Accent 5" /> <w:LsdException Locked="false" Priority="68" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 2 Accent 5" /> <w:LsdException Locked="false" Priority="69" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 3 Accent 5" /> <w:LsdException Locked="false" Priority="70" SemiHidden="false"    UnhideWhenUsed="false" Name="Dark List Accent 5" /> <w:LsdException Locked="false" Priority="71" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful Shading Accent 5" /> <w:LsdException Locked="false" Priority="72" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful List Accent 5" /> <w:LsdException Locked="false" Priority="73" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful Grid Accent 5" /> <w:LsdException Locked="false" Priority="60" SemiHidden="false"    UnhideWhenUsed="false" Name="Light Shading Accent 6" /> <w:LsdException Locked="false" Priority="61" SemiHidden="false"    UnhideWhenUsed="false" Name="Light List Accent 6" /> <w:LsdException Locked="false" Priority="62" SemiHidden="false"    UnhideWhenUsed="false" Name="Light Grid Accent 6" /> <w:LsdException Locked="false" Priority="63" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Shading 1 Accent 6" /> <w:LsdException Locked="false" Priority="64" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Shading 2 Accent 6" /> <w:LsdException Locked="false" Priority="65" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium List 1 Accent 6" /> <w:LsdException Locked="false" Priority="66" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium List 2 Accent 6" /> <w:LsdException Locked="false" Priority="67" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 1 Accent 6" /> <w:LsdException Locked="false" Priority="68" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 2 Accent 6" /> <w:LsdException Locked="false" Priority="69" SemiHidden="false"    UnhideWhenUsed="false" Name="Medium Grid 3 Accent 6" /> <w:LsdException Locked="false" Priority="70" SemiHidden="false"    UnhideWhenUsed="false" Name="Dark List Accent 6" /> <w:LsdException Locked="false" Priority="71" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful Shading Accent 6" /> <w:LsdException Locked="false" Priority="72" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful List Accent 6" /> <w:LsdException Locked="false" Priority="73" SemiHidden="false"    UnhideWhenUsed="false" Name="Colorful Grid Accent 6" /> <w:LsdException Locked="false" Priority="19" SemiHidden="false"    UnhideWhenUsed="false" QFormat="true" Name="Subtle Emphasis" /> <w:LsdException Locked="false" Priority="21" SemiHidden="false"    UnhideWhenUsed="false" QFormat="true" Name="Intense Emphasis" /> <w:LsdException Locked="false" Priority="31" SemiHidden="false"    UnhideWhenUsed="false" QFormat="true" Name="Subtle Reference" /> <w:LsdException Locked="false" Priority="32" SemiHidden="false"    UnhideWhenUsed="false" QFormat="true" Name="Intense Reference" /> <w:LsdException Locked="false" Priority="33" SemiHidden="false"    UnhideWhenUsed="false" QFormat="true" Name="Book Title" /> <w:LsdException Locked="false" Priority="37" Name="Bibliography" /> <w:LsdException Locked="false" Priority="39" QFormat="true" Name="TOC Heading" /> </w:LatentStyles> </xml><![endif]--><!--[if gte mso 10]> <mce:style><!   /* Style Definitions */  table.MsoNormalTable 	{mso-style-name:"Table Normal"; 	mso-tstyle-rowband-size:0; 	mso-tstyle-colband-size:0; 	mso-style-noshow:yes; 	mso-style-priority:99; 	mso-style-qformat:yes; 	mso-style-parent:""; 	mso-padding-alt:0in 5.4pt 0in 5.4pt; 	mso-para-margin-top:0in; 	mso-para-margin-right:0in; 	mso-para-margin-bottom:10.0pt; 	mso-para-margin-left:0in; 	line-height:115%; 	mso-pagination:widow-orphan; 	font-size:11.0pt; 	font-family:"Calibri","sans-serif"; 	mso-ascii-font-family:Calibri; 	mso-ascii-theme-font:minor-latin; 	mso-fareast-font-family:"Times New Roman"; 	mso-fareast-theme-font:minor-fareast; 	mso-hansi-font-family:Calibri; 	mso-hansi-theme-font:minor-latin;} --><!--[endif]--><span style="font-size: 12pt; line-height: 115%; font-family: &quot;Times New Roman&quot;,&quot;serif&quot;;">microcontroller</span>. Hardware setup is similar except we&#39;re going to use the internal pull-up.</p>
<p><span class="edit_monospace">RA2&#8212;-[SWITCH] &#8212; [GND] </span></p>
<p><span class="edit_monospace">RB5 &#8212;-[RESISTOR ~ 200Ohm] &#8212;[LED]&#8212;[GND]</span></p>
<p>To accomplish same thing we&#39;d have to write something like this in C:</p>
<p class="edit_monospace">TRISAbits.TRISA2 = 1; //make switch pin an input<br />
	WPUAbits.WPUA2 = 1; //turn on the pull-up<br />
	TRISBbits.TRISB5 = 0; //make led pin an output</p>
<p><span class="edit_monospace">while(1){<br />
	&nbsp; PORTBbits.RB5 =&nbsp; ! PORTAbits.RA2;<br />
	}</span></p>
<p>Now if you would need to swap the pins&nbsp; you&#39;d have to update pretty much every line of&nbsp; C code. This is very inconvenient !!! This is why I came up with following macros for C18/C30 :</p>
<p class="edit_monospace"><span style="background-color:#ffff00;"><span style="color: rgb(0, 0, 255);">//&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
	// MACROS FOR EASY PIN HANDLING IN PIC C18/C30<br />
	//&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
	#define _TRIS(pin)&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; pin(_TRIS_F)<br />
	#define _TRIS_F(alpha,bit)&nbsp;&nbsp;&nbsp; (TRIS ## alpha ## bits.TRIS ## alpha ## bit)<br />
	#define _PORT(pin)&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; pin(_PORT_F)<br />
	#define _PORT_F(alpha,bit)&nbsp;&nbsp;&nbsp; (PORT ## alpha ## bits.R ## alpha ## bit)<br />
	#define _LAT(pin)&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; pin(_LAT_F)<br />
	#define _LAT_F(alpha,bit)&nbsp;&nbsp;&nbsp; (LAT ## alpha ## bits.LAT ## alpha ## bit)<br />
	#define _WPU(pin)&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; pin(_WPU_F)<br />
	#define _WPU_F(alpha,bit)&nbsp;&nbsp;&nbsp; (WPU ## alpha ## bits.WPU ## alpha ## bit)<br />
	</span></span></p>
<p class="edit_monospace">//&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<br />
	// USAGE<br />
	//&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;</p>
<p><span class="edit_monospace">#define pinSwitch(f)&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; f(A,2)&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; //Switch, INPUT , pin RA2</span><br />
	<span class="edit_monospace">#define pinLed(f)&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; f(B,5)&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; //Led, OUTPUT , pin RB5<br />
	</span><span class="edit_monospace"><br />
	_TRIS(pinSwitch) = 1; //make pin an input<br />
	_WPU(pinSwitch) = 1; // turn on internal pull-up<br />
	_TRIS(pinLed) = 0; // make pin an output</span></p>
<p><span class="edit_monospace">while(1){</span><span class="edit_monospace">&nbsp; <br />
	&nbsp;_PORT(pinLed) = ! _PORT(pinSwitch)<br />
	}</span></p>
<p>This code is much better. If you need to swap the pins all you need to do is update two lines of code:</p>
<p><span class="edit_monospace">#define pinSwitch(f)&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; f(<span style="color: rgb(255, 0, 0);">B,5</span>)&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; //Switch, INPUT, pin RB5</span><br />
	<span class="edit_monospace">#define pinLed(f)&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; f(<span style="color: rgb(255, 0, 0);">A,2</span>)&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; //Led, OUTPUT,&nbsp; pin RA2</span></p>
<p>
	The rest of the code remains the same. The only confusing thing in this code might be the macro definitions. I have arrived at them through many trials and errors. Each pin is defined as a&nbsp; macro function with a single parameter <em>f</em>&nbsp;&nbsp; &#8211; which is another macro&nbsp; function which we&#39;d like to apply to this pin. Possible values for<em> f</em> are PORT_F , TRIS_F, LAT_F , WPU_F.</p>
<p>Here is an example how compiler interprets these statements:&nbsp;</p>
<p>_PORT(pinLed)&nbsp; =&gt;&nbsp; &nbsp; pinLed (PORT_F)&nbsp;&nbsp; =&gt;&nbsp; PORT_F( B, 5)&nbsp; =&gt;&nbsp; PORTBbits.RB5</p>
<p>&nbsp;</p>
<p>Now moving on to AVR controllers. I am using gcc-avr compiler (Windows version). I came up with following macros that seem to work in this compiler:</p>
<p><span style="color: rgb(0, 0, 255);"><br />
	</span><span style="background-color: rgb(255, 255, 0);"><span style="color: rgb(0, 0, 255);">// MACROS FOR EASY PIN HANDLING FOR </span></span><span style="background-color:#ffff00;"><span style="color: rgb(0, 0, 255);">ATMEL GCC-AVR</span></span><br />
	<span style="color: rgb(0, 0, 255);"> <span style="background-color:#ffff00;">//these macros are used indirectly by other macros , mainly for string concatination<br />
	#define _SET(type,name,bit)&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; type ## name&nbsp; |= _BV(bit)&nbsp;&nbsp;&nbsp; <br />
	#define _CLEAR(type,name,bit)&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; type ## name&nbsp; &amp;= ~ _BV(bit)&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; <br />
	#define _TOGGLE(type,name,bit)&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; type ## name&nbsp; ^= _BV(bit)&nbsp;&nbsp;&nbsp; <br />
	#define _GET(type,name,bit)&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ((type ## name &gt;&gt; bit) &amp;&nbsp; 1)<br />
	#define _PUT(type,name,bit,value)&nbsp;&nbsp;&nbsp; type ## name = ( type ## name &amp; ( ~ _BV(bit)) ) | ( ( 1 &amp; (unsigned char)value ) &lt;&lt; bit )</p>
<p>	//these macros are used by end user<br />
	#define OUTPUT(pin)&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _SET(DDR,pin)&nbsp;&nbsp;&nbsp; <br />
	#define INPUT(pin)&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _CLEAR(DDR,pin)&nbsp;&nbsp;&nbsp; <br />
	#define HIGH(pin)&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _SET(PORT,pin)<br />
	#define LOW(pin)&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _CLEAR(PORT,pin)&nbsp;&nbsp;&nbsp; <br />
	#define TOGGLE(pin)&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _TOGGLE(PORT,pin)&nbsp;&nbsp;&nbsp; <br />
	#define READ(pin)&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; _GET(PIN,pin)</span></span></p>
<p><span style="color: rgb(0, 0, 255);">/*<br />
	&nbsp;&nbsp;&nbsp; BASIC STAMPS STYLE COMMANDS FOR ATMEL GCC-AVR</p>
<p>	&nbsp;&nbsp;&nbsp; Usage Example:<br />
	&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
	&nbsp;&nbsp;&nbsp; #define pinLed B,5&nbsp; //define pins like this</p>
<p>	&nbsp;&nbsp;&nbsp; OUTPUT(pinLED); &nbsp;&nbsp;&nbsp; //compiles as DDRB |= (1&lt;&lt;5);<br />
	&nbsp;&nbsp;&nbsp; HIGH(pinLed); &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; //compiles as PORTB |= (1&lt;&lt;5);<br />
	&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
	*/</span></p>
<p>&nbsp;</p>
<p>As a bonus, below are some macros for calculating MIN/MAX or translating a value to a different range, something similar to the <em>map()&nbsp; </em>function that is already built-in into Arduino.</p>
<p><span style="color: rgb(0, 0, 255);">#define MIN(A,B)&nbsp; (((A)&lt;(B)) ? (A) : (B) )<br />
	#define MAX(A,B)&nbsp; (((A)&gt;(B)) ? (A) : (B) )<br />
	#define PUT_IN_RANGE(V,VMIN,VMAX) MAX(VMIN,MIN(VMAX,V))<br />
	#define MAP_TO_RANGE(V,VMIN0,VMAX0,VMIN1,VMAX1) ( (VMIN1) +&nbsp; ( (V) &#8211; (VMIN0) ) * ( (VMAX1) &#8211; (VMIN1) ) / ( (VMAX0) &#8211; (VMIN0) ) )</span></p>
<p>Now , what do you do when you need your pin to actually be stored as a variable (it updates at run-time, or you might want to pass it as a parameter to a function). Let&#39;s say you would like to have 4 pins and perform some actions on those pins in bulk as well as define those pins are defined at run time ? The answer is pointers. Here I will illustrate with an example for C30 that you can adapt to your needs:</p>
<p><span class="edit_monospace">volatile unsigned int * tris_ptr[4];<br />
	unsigned char* pin_bit[4];<br />
	</span></p>
<p><span class="edit_monospace">//define our pins as&nbsp; RA2 , RA3 , RB5 , RB7 at run time</p>
<p>	</span><span class="edit_monospace">tris</span><span class="edit_monospace">_ptr[0] = &amp;TRISA;&nbsp; </span><span class="edit_monospace">pin_bit[0] = 2;&nbsp; //RA2<br />
	</span><span class="edit_monospace">tris</span><span class="edit_monospace">_ptr[1] = &amp;TRISA;&nbsp; </span><span class="edit_monospace">pin_bit[1] = 3;&nbsp; //RA3</span></p>
<p><span class="edit_monospace">tris</span><span class="edit_monospace">_ptr[3] = &amp;TRISB;&nbsp; </span><span class="edit_monospace">pin_bit[2] = 5;&nbsp; //RB5<br />
	</span><span class="edit_monospace">tris</span><span class="edit_monospace">_ptr[4] = &amp;TRISB;&nbsp; </span><span class="edit_monospace">pin_bit[3] = 7;&nbsp; //RB7</p>
<p>	int i;<br />
	</span></p>
<p><span class="edit_monospace">//perform bulk operations on pins</span></p>
<p><span class="edit_monospace">for(i=0;i&lt;4;i++)<br />
	&nbsp; if(i % 2){<br />
	&nbsp;&nbsp; (*</span><span class="edit_monospace">tris</span><span class="edit_monospace">_ptr[i]) |= 1&lt;&lt;</span><span class="edit_monospace">tris</span><span class="edit_monospace">_bit[i];&nbsp;&nbsp;&nbsp; </span><span class="edit_monospace">//set bit, make odd pins INPUT</span><span class="edit_monospace">&nbsp; <br />
	&nbsp; else<br />
	&nbsp;&nbsp; (*</span><span class="edit_monospace">tris</span><span class="edit_monospace">_ptr[i]) ^=&nbsp; !(1U&lt;&lt;</span><span class="edit_monospace">tris</span><span class="edit_monospace">_bit[i]); </span><span class="edit_monospace">//clear bit, make even pins OUTPUT</span></p>
<p>In the same way you could define&nbsp; port_ptr[] , lat_ptr[] , wpu_ptr[] arrays and perform operations on those ports. Even more smarter would be to group all these in a structure, and then create a single array of these structures. Using macros you could simplify assignment to these arrays in one simple statement. (I leave this as a homework for you) !</p>
<p>I hope you&#39;ll find this article usefull. If you have any comments or suggestions do not hesitate to leave a comment below !</p>
<p>Happy coding !</p>
<p>//starlino//</p>
]]></content:encoded>
			<wfw:commentRss>http://www.starlino.com/port_macro.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

