<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>ad hocumentation • n. fast, instinctive documentation of ideas and solutions.</title>
	<atom:link href="http://batchloaf.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://batchloaf.wordpress.com</link>
	<description></description>
	<lastBuildDate>Sun, 19 May 2013 14:36:57 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='batchloaf.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>ad hocumentation • n. fast, instinctive documentation of ideas and solutions.</title>
		<link>http://batchloaf.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://batchloaf.wordpress.com/osd.xml" title="ad hocumentation • n. fast, instinctive documentation of ideas and solutions." />
	<atom:link rel='hub' href='http://batchloaf.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Starting with Circuitry</title>
		<link>http://batchloaf.wordpress.com/2013/05/19/starting-with-circuitry/</link>
		<comments>http://batchloaf.wordpress.com/2013/05/19/starting-with-circuitry/#comments</comments>
		<pubDate>Sun, 19 May 2013 14:36:57 +0000</pubDate>
		<dc:creator>batchloaf</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://batchloaf.wordpress.com/2013/05/19/starting-with-circuitry/</guid>
		<description><![CDATA[Reblogged from RoboSlam: RoboSlam starts with an introduction to the overall robot-building process and then delves into assembling the electronic components (i.e., building the circuitry).  The photos below track the progress of several groups of participants in the "Engineering Your &#8230; <a href="http://batchloaf.wordpress.com/2013/05/19/starting-with-circuitry/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=batchloaf.wordpress.com&#038;blog=18043761&#038;post=1626&#038;subd=batchloaf&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<div class="reblog-post"><p class="reblog-from"><img alt='' src='http://0.gravatar.com/avatar/91c4af0fd909fcb2cfb16ca9fe957159?s=25&amp;d=identicon&amp;r=G' class='avatar avatar-25' height='25' width='25' /> <a href="http://roboslam.wordpress.com/2013/05/18/starting-with-circuitry/">Reblogged from RoboSlam:</a></p><div class="wpcom-enhanced-excerpt"><div class="wpcom-enhanced-excerpt-content"><a href="http://roboslam.wordpress.com/2013/05/18/starting-with-circuitry/" target="_self"><img src="http://roboslam.files.wordpress.com/2013/05/may-2013-roboslam-15.jpg?w=640&h=138" alt="Click to visit the original post" class="size-full" /></a><ul class="thumb-list"><li><a href="http://roboslam.wordpress.com/2013/05/18/starting-with-circuitry/" target="_self"><img src="http://batchloaf.files.wordpress.com/2011/10/example.png?w=72&h=72&crop=1" alt="Click to visit the original post" class="size-thumb" width="72" height="72" /></a></li><li><a href="http://roboslam.wordpress.com/2013/05/18/starting-with-circuitry/" target="_self"><img src="http://batchloaf.files.wordpress.com/2011/10/netcat_browser_screenshot.png?w=72&h=72&crop=1" alt="Click to visit the original post" class="size-thumb" width="72" height="72" /></a></li></ul>

<p>RoboSlam starts with an introduction to the overall robot-building process and then delves into assembling the electronic components (i.e., building the circuitry).  The photos below track the progress of several groups of participants in the "Engineering Your Future" event held the week of May 13-17 at DIT.</p>
<p>These photos were all taken by DIT's current Fulbright Scholar in Engineering Education, Dr.</p>
</div> <p class="read-more"><a href="http://roboslam.wordpress.com/2013/05/18/starting-with-circuitry/" target="_self"><span>Read more&hellip;</span> 31 more words</a></p></div></div><div class="reblogger-note"><div class='reblogger-note-content'>
We ran a great RoboSlam robot-building workshop in DIT Kevin St last Tuesday (followed by some related events in DIT Bolton St later in the week). Shannon Chance, who is the research PI on the RoboSlam project, got loads of great photos and has started posting about it on <a href="http://roboslam.wordpress.com">the RoboSlam blog</a> and <a href="http://shannonchance.net/">her own blog</a>. 
</div></div>]]></content:encoded>
			<wfw:commentRss>http://batchloaf.wordpress.com/2013/05/19/starting-with-circuitry/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/b1997adbf34b6a941b0b9e88e2c59912?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">batchloaf</media:title>
		</media:content>
	</item>
		<item>
		<title>PWM servo control example for the PIC18F14K50</title>
		<link>http://batchloaf.wordpress.com/2013/04/22/pwm-servo-control-example-for-the-pic18f14k50/</link>
		<comments>http://batchloaf.wordpress.com/2013/04/22/pwm-servo-control-example-for-the-pic18f14k50/#comments</comments>
		<pubDate>Mon, 22 Apr 2013 20:26:14 +0000</pubDate>
		<dc:creator>batchloaf</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://batchloaf.wordpress.com/?p=1604</guid>
		<description><![CDATA[This is my first program for the PIC18F14K50 20-pin microcontroller. I haven&#8217;t used this chip before, but it seems to have some useful features including USB connectivity, so I intend to investigate it further. This program is a simple PWM &#8230; <a href="http://batchloaf.wordpress.com/2013/04/22/pwm-servo-control-example-for-the-pic18f14k50/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=batchloaf.wordpress.com&#038;blog=18043761&#038;post=1604&#038;subd=batchloaf&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>This is my first program for the PIC18F14K50 20-pin microcontroller. I haven&#8217;t used this chip before, but it seems to have some useful features including USB connectivity, so I intend to investigate it further.</p>
<p>This program is a simple PWM example which moves a servo motor to one of two possible angles (0 degrees or 90 degrees) depending on the state of a digital input pin (RC4, pin 6). The PWM output signal is produced on pin 5 (CCP1).</p>
<p>I&#8217;m using the internal RC oscillator. This has a default Fosc of 1MHz, but in order to achieve a PWM period of 20ms (for the servo), I had to slow the clock down to 500kHz, which gives an instruction cycle of 8us.</p>
<pre class="brush: cpp; title: ; notranslate">
//
// PIC18F14K50 servo PWM example program
// Written by Ted Burke (http://batchloaf.com)
// Last updated 22-4-2013
//
// To compile with XC8:
//     xc8 --chip=18F14K50 main.c
//
 
#include &lt;xc.h&gt;
 
#pragma config FOSC=IRC,MCLRE=OFF,WDTEN=0,LVP=OFF,BOREN=OFF
 
int main(void)
{
	// Set clock frequency to 500kHz, therefore Tcy = 8us
	OSCCONbits.IRCF = 0b010;
	
	// Set up PWM
	CCP1CON = 0b00001100;   // Enable PWM on CCP1
	TRISC = 0b11011111;		// Make CCP1 an output
	T2CON = 0b00000110;     // Enable TMR2 with prescaler = 16
	PR2 = 155;	// PWM period = (PR2+1) * prescaler * Tcy = 19.968ms
	CCPR1L = 8;	// pulse width = CCPR1L * prescaler * Tcy = 1.024ms
	
	while(1)
	{
		if (PORTCbits.RC4) CCPR1L = 12;	// 1.536ms pulses, i.e. 90 degrees
		else CCPR1L = 8;				// 1.024ms pulses, i.e. 0 degrees
	}
	
	return 0;
}
</pre>
<p>I don&#8217;t have an actual PIC18F14K50 to try my program on, so instead I ran it on the simulator in MPLAB v8.50. I captured the simulated PWM output using MPLAB&#8217;s logic analyzer tool, as shown below.</p>
<p><a href="http://batchloaf.files.wordpress.com/2013/04/simulator_pwm_logic_analyzer.png"><img src="http://batchloaf.files.wordpress.com/2013/04/simulator_pwm_logic_analyzer.png?w=640" alt="simulator_PWM_logic_analyzer" class="aligncenter size-large wp-image-1605" /></a></p>
<p>The blue waveform is the PWM output on CCP1 (pin 5). The red vertical lines are &#8220;cursors&#8221; provided in the logic analyzer for measuring the time difference between two points on the waveform. I positioned them to measure the period of the PWM waveform, which is 2511 instruction cycles. Since the clock oscillator frequency F<sub>osc</sub> is set to 500kHz, the clock cycle T<sub>osc</sub> is 2us. The PIC18F14K50 performs one machine instruction every 4 clock cycles, so the instruction cycle T<sub>cy</sub> is 8us. So the PWM period is:</p>
<div style="text-align:center;margin:1em;">
<img src='http://s0.wp.com/latex.php?latex=T_%7Bpwm%7D+%3D+2511+%5Ctimes+T_%7Bcy%7D+%3D+20.088ms&amp;bg=ffffff&amp;fg=333333&amp;s=3' alt='T_{pwm} = 2511 &#92;times T_{cy} = 20.088ms' title='T_{pwm} = 2511 &#92;times T_{cy} = 20.088ms' class='latex' />
</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/batchloaf.wordpress.com/1604/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/batchloaf.wordpress.com/1604/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=batchloaf.wordpress.com&#038;blog=18043761&#038;post=1604&#038;subd=batchloaf&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://batchloaf.wordpress.com/2013/04/22/pwm-servo-control-example-for-the-pic18f14k50/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/b1997adbf34b6a941b0b9e88e2c59912?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">batchloaf</media:title>
		</media:content>

		<media:content url="http://batchloaf.files.wordpress.com/2013/04/simulator_pwm_logic_analyzer.png" medium="image">
			<media:title type="html">simulator_PWM_logic_analyzer</media:title>
		</media:content>
	</item>
		<item>
		<title>First attempt at printing to an LCD display from a dsPIC30F4011</title>
		<link>http://batchloaf.wordpress.com/2013/04/16/first-attempt-at-printing-to-an-lcd-display-from-a-dspic30f4011/</link>
		<comments>http://batchloaf.wordpress.com/2013/04/16/first-attempt-at-printing-to-an-lcd-display-from-a-dspic30f4011/#comments</comments>
		<pubDate>Tue, 16 Apr 2013 20:16:03 +0000</pubDate>
		<dc:creator>batchloaf</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[dsPIC]]></category>
		<category><![CDATA[dsPIC30F4011]]></category>
		<category><![CDATA[Hitachi]]></category>
		<category><![CDATA[LCD]]></category>
		<category><![CDATA[XC16]]></category>

		<guid isPermaLink="false">http://batchloaf.wordpress.com/?p=1564</guid>
		<description><![CDATA[The following example is my first attempt at printing to an LCD display using the dsPIC30F4011 microcontroller. The display I&#8217;m using is a 16&#215;1 character screen with what I think is a standard Hitachi interface. It&#8217;s my first time using &#8230; <a href="http://batchloaf.wordpress.com/2013/04/16/first-attempt-at-printing-to-an-lcd-display-from-a-dspic30f4011/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=batchloaf.wordpress.com&#038;blog=18043761&#038;post=1564&#038;subd=batchloaf&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>The following example is my first attempt at printing to an LCD display using the dsPIC30F4011 microcontroller. The display I&#8217;m using is a 16&#215;1 character screen with what I <em>think</em> is a standard Hitachi interface. It&#8217;s my first time using one of these, so the code is a bit long-winded at the moment, but the good news is that it&#8217;s working!</p>
<p><a href="http://batchloaf.files.wordpress.com/2013/04/lcd_working.jpg"><img src="http://batchloaf.files.wordpress.com/2013/04/lcd_working.jpg?w=640&#038;h=640" alt="Photo of dsPIC driving LCD display" width="640" height="640" class="aligncenter size-large wp-image-1567" /></a></p>
<p>This LCD module uses the Hitachi interface, which has 4-bit and 8-bit operating modes. Here, the 4-bit mode is used, which means that each byte transmitted from the dsPIC to the LCD module is split into two 4-bit <em>nibbles</em> which are transmitted one after the other. The advantage of 4-bit mode is that less microcontroller pins are required &#8211; 7 digital outputs in total: 3 control lines and 4 data lines. (Actually, strictly speaking, you could get away with only 6 lines.)</p>
<p>The circuit diagram is shown below. Note that the 5 connections to the PICkit 2 USB programmer are not shown. (<a href="https://docs.google.com/file/d/0B3NaVR72FYQcQjkzYnNCQ3RTaUU/edit?usp=sharing">Link to editable SVG version of this image</a>, created using Inkscape.)</p>
<p><a href="http://batchloaf.files.wordpress.com/2013/04/lcd_circuit_diagram_ted_burke1.png"><img src="http://batchloaf.files.wordpress.com/2013/04/lcd_circuit_diagram_ted_burke2.png?w=480" alt="Circuit diagram of dsPIC30F4011 connected to Hitachi LCD displlay module" width="480" class="aligncenter wp-image-1573" /></a></p>
<p>The 7 connections between the dsPIC&#8217;s digital outputs and the LCD module are:</p>
<table style="width:90%;margin-left:auto;margin-right:auto;">
<tr>
<th>LCD pin number</th>
<th>LCD pin name</th>
<th width="45%">LCD pin description</th>
<th>dsPIC pin number</th>
<th>dsPIC pin name</th>
</tr>
<tr>
<td>4</td>
<td>RS</td>
<td>0: command transfer<br />1: data transfer</td>
<td>16</td>
<td>RC14</td>
</tr>
<tr>
<td>5</td>
<td>R/W</td>
<td>0: write data<br />1: read data<br />(always 0 here)</td>
<td>15</td>
<td>RC13</td>
</tr>
<tr>
<td>6</td>
<td>E</td>
<td>D4-D7 latched on<br />falling edge of E</td>
<td>14</td>
<td>RC15</td>
</tr>
<tr>
<td>11</td>
<td>D4</td>
<td>Bit 0 (lsb)</td>
<td>2</td>
<td>RB0</td>
</tr>
<tr>
<td>12</td>
<td>D5</td>
<td>Bit 1</td>
<td>3</td>
<td>RB1</td>
</tr>
<tr>
<td>13</td>
<td>D6</td>
<td>Bit 2</td>
<td>4</td>
<td>RB2</td>
</tr>
<tr>
<td>14</td>
<td>D7</td>
<td>Bit 3 (msb)</td>
<td>5</td>
<td>RB3</td>
</tr>
</table>
<p>The LCD module I&#8217;m using is shown below (front and back views of the same device). The 6-pin header at one end of the breadboard is the connector for the PICkit 2 USB programmer.</p>
<p><a href="http://batchloaf.files.wordpress.com/2013/04/lcd_front_and_back.jpg"><img src="http://batchloaf.files.wordpress.com/2013/04/lcd_front_and_back.jpg?w=640&#038;h=175" alt="Front and back views of the LCD display module" width="640" height="175" class="aligncenter size-large wp-image-1568" /></a></p>
<p>This is the complete breadboard circuit including the dsPIC and LCD module (two views of the same circuit):</p>
<p><a href="http://batchloaf.files.wordpress.com/2013/04/lcd_forward_and_reverse.jpg"><img src="http://batchloaf.files.wordpress.com/2013/04/lcd_forward_and_reverse.jpg?w=640&#038;h=320" alt="Two views of the complete dsPIC / LCD display system" width="640" height="320" class="aligncenter size-large wp-image-1569" /></a></p>
<p>I was surprised to discover that 16&#215;1 LCD modules of this type (1 line with 16 characters of text) are typically structured <em>as if</em> they had 2 lines of 8 characters. The first 8 characters are &#8220;line 1&#8243; and the second 8 characters are &#8220;line 2&#8243;. As a result, in the program below, half of the single line of text to be displayed is written to &#8220;line 1&#8243; and half is written to &#8220;line 2&#8243;.</p>
<p>This is the C code for the dsPIC program:</p>
<pre class="brush: cpp; title: ; notranslate">
//
// LCD display program for dsPIC30F4011
// Written by Ted Burke - Last updated 16-4-2013
//

#include &lt;xc.h&gt;
#include &lt;libpic30.h&gt;

// Configuration settings
_FOSC(CSW_FSCM_OFF &amp; FRC_PLL16); // Fosc=16x7.5MHz, Fcy=30MHz
_FWDT(WDT_OFF);                  // Watchdog timer off
_FBORPOR(MCLR_DIS);              // Disable reset pin

#define RS_PIN _LATC14
#define RW_PIN _LATC13
#define E_PIN _LATC15

void delay_ms(unsigned int n);
void send_nibble(unsigned char nibble);
void send_command_byte(unsigned char byte);
void send_data_byte(unsigned char byte);

int main()
{
	_TRISD0 = 0; // Make RD0 an output
	
	TRISC = 0; // RC13-15 as digital outputs
	TRISB = 0xFFF0; // RB0-3 as digital outputs
	_PCFG0 = 1; // AN0 is digital
	_PCFG1 = 1; // AN1 is digital
	_PCFG2 = 1; // AN2 is digital
	_PCFG3 = 1; // AN3 is digital

	// Let's just write to the LCD and never read!
	// We'll wait 2ms after every command since we can't
	// check the busy flag.
	RW_PIN = 0;
	RS_PIN = 0;
	E_PIN = 1;
	
	// Initialisation
	delay_ms(16); // must be more than 15ms
	send_nibble(0b0011);
	delay_ms(5); // must be more than 4.1ms
	send_nibble(0b0011);
	delay_ms(1); // must be more than 100us
	send_nibble(0b0011);
	delay_ms(5); // must be more than 4.1ms
	send_nibble(0b0010); // select 4-bit mode
	
	// Display settings
	send_command_byte(0b00101000); // N=0 : 2 lines (half lines!), F=0 : 5x7 font
	send_command_byte(0b00001000); // Display: display off, cursor off, blink off
	send_command_byte(0b00000001); // Clear display
	send_command_byte(0b00000110); // Set entry mode: ID=1, S=0
	send_command_byte(0b00001111); // Display: display on, cursor on, blink on
	
	// Define two 8 character strings
	const char line1[] = &quot; Ted's d&quot;;
	const char line2[] = &quot;sPIC30F &quot;;
	
	// Write the two strings to lines 1 and 2
	int n;
	send_command_byte(0x02); // Go to start of line 1
	for (n=0 ; n&lt;8 ; ++n) send_data_byte(line1[n]);
	send_command_byte(0xC0); // Go to start of line 2
	for (n=0 ; n&lt;8 ; ++n) send_data_byte(line2[n]);
	
	// Now just blink LED indefinitely
	while(1)
	{
		_LATD0 = 1 - _LATD0;
		delay_ms(500);
	}
}

// Delay by specified number of milliseconds
void delay_ms(unsigned int n)
{
	while(n--) __delay32(30000);
}

void send_nibble(unsigned char nibble)
{
	// Note: data is latched on falling edge of pin E
	LATB = nibble;
	delay_ms(1);
	E_PIN = 0;
	delay_ms(1);
	E_PIN = 1;
	delay_ms(2); // Enough time even for slowest command
}

// Send a command byte (i.e. with pin RS low)
void send_command_byte(unsigned char byte)
{
	RS_PIN = 0;
	send_nibble(byte &gt;&gt; 4);
	send_nibble(byte &amp; 0xF);
}

// Send a data byte (i.e. with pin RS high)
void send_data_byte(unsigned char byte)
{
	RS_PIN = 1;
	send_nibble(byte &gt;&gt; 4);
	send_nibble(byte &amp; 0xF);
}
</pre>
<p>I compiled this with Microchip&#8217;s XC16 compiler, using the following simple build script.</p>
<pre class="brush: plain; title: ; notranslate">
xc16-gcc main.c -mcpu=30F4011 -Wl,--script=p30F4011.gld
if errorlevel 0 xc16-bin2hex a.out
</pre>
<p>To use the build script:</p>
<ol>
<li>Create a new folder.</li>
<li>Save the C program in that folder as &#8220;main.c&#8221;.</li>
<li>Save the build script in the same folder as &#8220;build.bat&#8221;.</li>
<li>Open a console window and navigate to that folder.</li>
<li>Type &#8220;build.bat&#8221; to compile the program.</li>
<li>Use the PICkit 2 application (or another program) to download the compiled program (file &#8220;a.hex&#8221;) onto the dsPIC.</li>
</ol>
<h3>References</h3>
<ol>
<li><a href="http://en.wikipedia.org/wiki/Hitachi_HD44780_LCD_controller">Wikipedia article on Hitachi HD44780 LCD controller</a> (contains useful summary of connections and commands)</li>
<li><a href="http://www.mikroe.com/chapters/view/12/appendix-b-examples/">Book: PIC Microcontrollers, Appendix B: Examples</a> (scroll down to the example titled &#8220;LCD DISPLAY&#8221;)</li>
<li><a href="https://www.sparkfun.com/datasheets/LCD/HD44780.pdf">HD44780 LCD controller datasheet</a>, containing all the gory details (courtesy of <a href="https://www.sparkfun.com/categories/76">Sparkfun who sell lots of LCD modules</a>)</li>
</ol>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/batchloaf.wordpress.com/1564/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/batchloaf.wordpress.com/1564/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=batchloaf.wordpress.com&#038;blog=18043761&#038;post=1564&#038;subd=batchloaf&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://batchloaf.wordpress.com/2013/04/16/first-attempt-at-printing-to-an-lcd-display-from-a-dspic30f4011/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/b1997adbf34b6a941b0b9e88e2c59912?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">batchloaf</media:title>
		</media:content>

		<media:content url="http://batchloaf.files.wordpress.com/2013/04/lcd_working.jpg?w=640" medium="image">
			<media:title type="html">Photo of dsPIC driving LCD display</media:title>
		</media:content>

		<media:content url="http://batchloaf.files.wordpress.com/2013/04/lcd_circuit_diagram_ted_burke2.png" medium="image">
			<media:title type="html">Circuit diagram of dsPIC30F4011 connected to Hitachi LCD displlay module</media:title>
		</media:content>

		<media:content url="http://batchloaf.files.wordpress.com/2013/04/lcd_front_and_back.jpg?w=480" medium="image">
			<media:title type="html">Front and back views of the LCD display module</media:title>
		</media:content>

		<media:content url="http://batchloaf.files.wordpress.com/2013/04/lcd_forward_and_reverse.jpg?w=640" medium="image">
			<media:title type="html">Two views of the complete dsPIC / LCD display system</media:title>
		</media:content>
	</item>
		<item>
		<title>A dsPIC program to display bytes received via serial link</title>
		<link>http://batchloaf.wordpress.com/2013/04/16/a-dspic-program-to-display-bytes-received-via-serial-link/</link>
		<comments>http://batchloaf.wordpress.com/2013/04/16/a-dspic-program-to-display-bytes-received-via-serial-link/#comments</comments>
		<pubDate>Tue, 16 Apr 2013 16:48:15 +0000</pubDate>
		<dc:creator>batchloaf</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://batchloaf.wordpress.com/?p=1557</guid>
		<description><![CDATA[I wrote the following short program for the dsPIC30F4011 microcontroller to record a sequence of bytes received via serial link (UART 2) and then print the byte values and the corresponding text string via UART 1. I was using this &#8230; <a href="http://batchloaf.wordpress.com/2013/04/16/a-dspic-program-to-display-bytes-received-via-serial-link/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=batchloaf.wordpress.com&#038;blog=18043761&#038;post=1557&#038;subd=batchloaf&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>I wrote the following short program for the dsPIC30F4011 microcontroller to record a sequence of bytes received via serial link (UART 2) and then print the byte values and the corresponding text string via UART 1. I was using this to verify that another program I had written (SerialSend) was working correctly. SerialSend is a command line application for Windows which is used to transmit arbitrary sequences of bytes via a serial port.</p>
<pre class="brush: cpp; title: ; notranslate">
//
// dsPIC30F4011 program to record bytes received via UART 2
// and then print the byte values and text string via UART 1
//
// Written by Ted Burke - last updated 16-4-2013
//

#include &lt;xc.h&gt;
#include &lt;stdio.h&gt;
 
// Configuration settings
_FOSC(CSW_FSCM_OFF &amp; FRC_PLL16); // Fosc=16x7.5MHz, i.e. 30 MIPS
_FWDT(WDT_OFF);                  // Watchdog timer off
_FBORPOR(MCLR_DIS);              // Disable reset pin

// Function prototype for Timer 1 interrupt service routine
void __attribute__((__interrupt__, __auto_psv__)) _T1Interrupt(void);

int n = 0;
unsigned char buf[100];

int main(void)
{
	// Setup UART 1
	U1BRG = 48;            // 38400 baud @ 30 MIPS
	U1MODEbits.UARTEN = 1; // Enable UART
	
	// Setup UART 2
	U2BRG = 48;            // 38400 baud @ 30 MIPS
	U2MODEbits.UARTEN = 1; // Enable UART

	// Configure Timer 1
	// TMR1 is set to zero whenever a byte is received
	// or whenever the buffer is empty. Whenever TMR1
	// reaches PR1, the Timer 1 ISR prints whatever
	// bytes are in the buffer. This creates a timeout,
	// such that when bytes are received, they are stored
	// in the buffer, but when no bytes are received for
	// 100ms, the buffer data is printed.
	PR1 = 46875;          // Set the Timer 1 period to 100ms
	TMR1 = 0;             // Reset Timer 1 counter
	IEC0bits.T1IE = 1;    // Enable Timer 1 interrupt
	T1CONbits.TCKPS = 2;  // Prescaler (0=1:1, 1=1:8, 2=1:64, 3=1:256)
	T1CONbits.TON = 1;    // Turn on Timer 1

	_TRISD0 = 0; // Make RD0 a digital output

	while(1)
	{
		// Don't let TMR1 increase unless bytes are in buffer
		if (n == 0) TMR1 = 0;

		// Check if any characters were received via UART
		if (U2STAbits.URXDA == 1)
		{
			// Store received byte and increment byte counter
			buf[n++] = U2RXREG;
			TMR1 = 0;
		}
	}

	return 0;
}

// Timer 1 interrupt service routine prints the buffer contents.
// This runs when there are bytes stored in the buffer, but none
// have been received for 100ms.
void __attribute__((__interrupt__, __auto_psv__)) _T1Interrupt(void)
{
	int m;

	// Clear Timer 1 interrupt flag
	IFS0bits.T1IF = 0;

	// Print bytes as numbers in hex format
	for (m=0 ; m&lt;n ; ++m) printf(&quot;%02x &quot;, buf[m]);

	// Print the same bytes again as a text string
	buf[n] = '&#092;&#048;';
	printf(&quot;\n%s\n&quot;, buf);

	// Reset the buffer byte count
	n = 0;
}
</pre>
<p>I compiled the program above with Microchip&#8217;s XC16 C compiler using the following simple build script.</p>
<pre class="brush: plain; title: ; notranslate">
xc16-gcc main.c -mcpu=30F4011 -Wl,--script=p30F4011.gld
if errorlevel 0 xc16-bin2hex a.out
</pre>
<p>To use the build script, just create a new folder, save the C program above in the folder as &#8220;main.c&#8221;, save the build script above as &#8220;build.bat&#8221; in the same folder, and then open a command window and type &#8220;build.bat&#8221;.</p>
<h2>Example</h2>
<p>The following screenshot shows SerialSend running twice in a command window. The serial device used (COM22) is a USB-to-serial converter which is connected to the Tx and Rx pins of UART 2 on the dsPIC.</p>
<p><a href="http://batchloaf.files.wordpress.com/2013/04/serialsend_console.png"><img src="http://batchloaf.files.wordpress.com/2013/04/serialsend_console.png?w=640" alt="SerialSend running twice in a console window" width="640" class="aligncenter size-full wp-image-1558" /></a></p>
<p>The following screenshot shows the UART tool window in the PICkit 2 software application. The PICkit 2 hardware is connected to the Tx and Rx pins of UART 1. The byte sequences sent in the previous screenshot have been received by the dsPIC on UART 2 and, after a 100ms timeout, printed out via UART 1 and displayed in the UART tool.</p>
<p><a href="http://batchloaf.files.wordpress.com/2013/04/serialsend_uart_tool.png"><img src="http://batchloaf.files.wordpress.com/2013/04/serialsend_uart_tool.png?w=640" alt="Screenshot of the PICkit 2 UART tool showing byte values printed by the dsPIC via UART 1" width="640" class="aligncenter size-full wp-image-1559" /></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/batchloaf.wordpress.com/1557/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/batchloaf.wordpress.com/1557/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=batchloaf.wordpress.com&#038;blog=18043761&#038;post=1557&#038;subd=batchloaf&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://batchloaf.wordpress.com/2013/04/16/a-dspic-program-to-display-bytes-received-via-serial-link/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/b1997adbf34b6a941b0b9e88e2c59912?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">batchloaf</media:title>
		</media:content>

		<media:content url="http://batchloaf.files.wordpress.com/2013/04/serialsend_console.png" medium="image">
			<media:title type="html">SerialSend running twice in a console window</media:title>
		</media:content>

		<media:content url="http://batchloaf.files.wordpress.com/2013/04/serialsend_uart_tool.png" medium="image">
			<media:title type="html">Screenshot of the PICkit 2 UART tool showing byte values printed by the dsPIC via UART 1</media:title>
		</media:content>
	</item>
		<item>
		<title>Simple PWM example for the PIC18F4620</title>
		<link>http://batchloaf.wordpress.com/2013/04/04/simple-pwm-example-for-the-pic18f4620/</link>
		<comments>http://batchloaf.wordpress.com/2013/04/04/simple-pwm-example-for-the-pic18f4620/#comments</comments>
		<pubDate>Thu, 04 Apr 2013 18:51:01 +0000</pubDate>
		<dc:creator>batchloaf</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://batchloaf.wordpress.com/?p=1537</guid>
		<description><![CDATA[The PIC18F4620 includes a hardware pulse width modulation feature which is really useful for generating periodic pulse sequences with different frequencies and duty cycles. Applications include motor speed control, servo control, and varying the brightness of an LED. This simple &#8230; <a href="http://batchloaf.wordpress.com/2013/04/04/simple-pwm-example-for-the-pic18f4620/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=batchloaf.wordpress.com&#038;blog=18043761&#038;post=1537&#038;subd=batchloaf&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>The PIC18F4620 includes a hardware pulse width modulation feature which is really useful for generating periodic pulse sequences with different frequencies and duty cycles. Applications include motor speed control, servo control, and varying the brightness of an LED. This simple example program generates a 1kHz PWM (pulse width modulation) signal on pin 17 (RC2 / CCP1). The program is written in C for Microchip&#8217;s XC8 C compiler.</p>
<p>NB The PIC18F4620 provides 10-bit resolution in setting the PWM pulse width. However, to keep things simple, I&#8217;m disregarding the 2 least significant bits, which reside in a different register to the 8 most significant bits. Unless you really need the extra resolution, I recommend ignoring the 2 least significant bits and treating both the PWM period and pulse width as 8-bit values. Because I&#8217;m making this simplification, the calculations I present below are a bit simpler than those described in the PIC18F4620 datasheet.</p>
<h3>Setting the PWM period and duty cycle</h3>
<p>In this example, the clock oscillator frequency of the 18F4620 is left at the default value of Fosc = 1MHz. The oscillator period is therefore Tosc = 1us.</p>
<p>The PIC18F4620 performs one machine code instruction every four clock cycles, so the instruction cycle in this example is Tcy = 4us. This is an important figure because Tcy is really the fundamental unit of time when measuring time in PIC programs.</p>
<p>The period of the PWM waveform is determined by the value written to a special function register called PR2 (short for Period Register for Timer 2). The formula for calculating PWM period is:</p>
<p>PWM period = (PR2+1) * Timer 2 prescaler value * Tcy</p>
<p>In this example, PR2 = 249, the Timer 2 prescaler = 1 and Tcy = 4us. Therefore,</p>
<p>PWM period = 250 * 1 * 4us = 1ms</p>
<p>The pulse width of the PWM waveform is determined by the value written to a special function register called CCPR1L (short for Capture/Compare/PWM channel 1 Register, low byte). The formula for calculating PWM pulse width is:</p>
<p>PWM pulse width = CCPR1L * Timer 2 presacler value * Tcy</p>
<p>Initially, CCPR1L = 125, the Timer 2 prescaler = 1 and Tcy = 4us. Initially therefore, </p>
<p>PWM period = 125 * 1 * 4us = 0.5ms</p>
<p>The values of PR2 and CCPR1L can be changed at any time, which will change the period or pulse width of the waveform. In this example, the period is constant at 1ms, giving a frequency of 1kHz. However, the value of CCPR1L changes every half a seconds, causing the duty cycle of the waveform to cycle through the values 50%, 10%, 0% repeatedly.</p>
<h3>C code and build script</h3>
<p>This is the full C code for the XC8 compiler.</p>
<pre class="brush: cpp; title: ; notranslate">
//
// PIC18F4620 1kHz PWM example program
// Written by Ted Burke (http://batchloaf.com)
// Last updated 4-4-2013
//
// To compile with XC8:
//     xc8 --chip=18F4620 main.c
//

#include &lt;xc.h&gt;

#pragma config OSC=INTIO67,MCLRE=OFF,WDT=OFF,LVP=OFF,BOREN=OFF

int main(void)
{
	// Set up PWM (see section 15.4 of the PIC18F4620 datasheet)
	CCP1CON = 0b00001100;   // Enable PWM on CCP1
	TRISC = 0b11111001;     // Make pin 17 (RC1/CCP2) an output
	T2CON = 0b00000100;     // Enable TMR2 with prescaler = 1
	PR2 = 249;   // PWM period = (PR2+1) * prescaler * Tcy = 1ms
	CCPR1L = 25; // pulse width = CCPR1L * prescaler * Tcy = 100us
	
	while(1)
	{
		// 50% duty cycle for 500ms
		CCPR1L = 125;
		_delay(125000);
		
		// 10% duty cycle for 500ms
		CCPR1L = 25;
		_delay(125000);
		
		// 0% duty cycle for 500ms
		CCPR1L = 0;
		_delay(125000);
	}
}
</pre>
<p>I don&#8217;t use MPLAB; I just write my C code in a text editor, compile it with XC8 in a command window, then use the PICkit 2 application to transfer the hex file to the PIC. The build script I use (which contains just one command) is shown below. I save the C code as &#8220;main.c&#8221; and the build script in the same folder as &#8220;build.bat&#8221;.</p>
<pre class="brush: plain; title: ; notranslate">
xc8 --chip=18F4620 main.c
</pre>
<p>Here&#8217;s how it looked when I compiled the program on my laptop:</p>
<p><a href="http://batchloaf.files.wordpress.com/2013/04/pwm_build_console.png"><img src="http://batchloaf.files.wordpress.com/2013/04/pwm_build_console.png?w=640" alt="Screenshot of PWM example being built in console" class="aligncenter size-full wp-image-1538" /></a></p>
<h3>Measuring the waveform period and pulse width</h3>
<p>I used the PICkit2 Logic Analyzer to take the following snapshot of the waveform when the duty cycle was 50% (when CCPR1L = 125). The signal was connected to channel 3 of the logic analyzer (pin 6 of the PICkit 2):</p>
<p><a href="http://batchloaf.files.wordpress.com/2013/04/pk2_pwm_1khz.png"><img src="http://batchloaf.files.wordpress.com/2013/04/pk2_pwm_1khz.png?w=640" alt="Screenshot of 1kHz PWM waveform viewed in PICkit 2 logic analyzer" width="640" class="aligncenter size-full wp-image-1539" /></a></p>
<p>Here, the logic analyzer cursors are measuring the period of the waveform:</p>
<p><a href="http://batchloaf.files.wordpress.com/2013/04/pk2_pwm_1khz_cursors_period.png"><img src="http://batchloaf.files.wordpress.com/2013/04/pk2_pwm_1khz_cursors_period.png?w=640" alt="pk2_pwm_1kHz_cursors_period" width="640" class="aligncenter size-full wp-image-1540" /></a></p>
<p>Here, the logic analyzer cursors are measuring the pulse width of the waveform:</p>
<p><a href="http://batchloaf.files.wordpress.com/2013/04/pk2_pwm_1khz_cursors_pw.png"><img src="http://batchloaf.files.wordpress.com/2013/04/pk2_pwm_1khz_cursors_pw.png?w=640" alt="pk2_pwm_1kHz_cursors_pw" width="640" class="aligncenter size-full wp-image-1541" /></a></p>
<p>Finally, here&#8217;s the waveform when the duty cycle is set to 10% (i.e. when CCPR1L = 25):</p>
<p><a href="http://batchloaf.files.wordpress.com/2013/04/pk2_pwm_1khz_10percent_dc.png"><img src="http://batchloaf.files.wordpress.com/2013/04/pk2_pwm_1khz_10percent_dc.png?w=640" alt="pk2_pwm_1kHz_10percent_dc" width="640" class="aligncenter size-full wp-image-1542" /></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/batchloaf.wordpress.com/1537/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/batchloaf.wordpress.com/1537/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=batchloaf.wordpress.com&#038;blog=18043761&#038;post=1537&#038;subd=batchloaf&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://batchloaf.wordpress.com/2013/04/04/simple-pwm-example-for-the-pic18f4620/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/b1997adbf34b6a941b0b9e88e2c59912?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">batchloaf</media:title>
		</media:content>

		<media:content url="http://batchloaf.files.wordpress.com/2013/04/pwm_build_console.png" medium="image">
			<media:title type="html">Screenshot of PWM example being built in console</media:title>
		</media:content>

		<media:content url="http://batchloaf.files.wordpress.com/2013/04/pk2_pwm_1khz.png" medium="image">
			<media:title type="html">Screenshot of 1kHz PWM waveform viewed in PICkit 2 logic analyzer</media:title>
		</media:content>

		<media:content url="http://batchloaf.files.wordpress.com/2013/04/pk2_pwm_1khz_cursors_period.png" medium="image">
			<media:title type="html">pk2_pwm_1kHz_cursors_period</media:title>
		</media:content>

		<media:content url="http://batchloaf.files.wordpress.com/2013/04/pk2_pwm_1khz_cursors_pw.png" medium="image">
			<media:title type="html">pk2_pwm_1kHz_cursors_pw</media:title>
		</media:content>

		<media:content url="http://batchloaf.files.wordpress.com/2013/04/pk2_pwm_1khz_10percent_dc.png" medium="image">
			<media:title type="html">pk2_pwm_1kHz_10percent_dc</media:title>
		</media:content>
	</item>
		<item>
		<title>2-D Linear Interpolation in C</title>
		<link>http://batchloaf.wordpress.com/2013/03/12/2-d-linear-interpolation-in-c/</link>
		<comments>http://batchloaf.wordpress.com/2013/03/12/2-d-linear-interpolation-in-c/#comments</comments>
		<pubDate>Tue, 12 Mar 2013 00:22:27 +0000</pubDate>
		<dc:creator>batchloaf</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://batchloaf.wordpress.com/?p=1531</guid>
		<description><![CDATA[Reader Mote wrote in with an interesting dsPIC programming query about estimating the value of a function of two variables using 2-D linear interpolation in a look-up table. We&#8217;re investigating how long it takes to compute a single interpolated value. &#8230; <a href="http://batchloaf.wordpress.com/2013/03/12/2-d-linear-interpolation-in-c/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=batchloaf.wordpress.com&#038;blog=18043761&#038;post=1531&#038;subd=batchloaf&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Reader Mote wrote in with an interesting dsPIC programming query about estimating the value of a function of two variables using 2-D linear interpolation in a look-up table. We&#8217;re investigating how long it takes to compute a single interpolated value.</p>
<p>As a starting point, I&#8217;ve written the following C program to sketch out the basic calculation in a Windows console app before I even think about doing it on the dsPIC. This seems to be working, so I guess the next step is to stick it onto the dsPIC and see how it runs. However, that requires some careful thought on account of the size of the look-up table and the limited RAM on the dsPIC30F4011, which is the microcontroller I usually use.</p>
<p>I&#8217;ll post again once I&#8217;ve made a bit of progress on the dsPIC implementation. </p>
<pre class="brush: cpp; title: ; notranslate">
// 
// 2-D linear interpolation experiment
// Written by Ted Burke
// last updated 11-3-2013
//
// To compile: gcc interpolation.c -o interpolation.exe
// To run: interpolation.exe
//

#include &lt;stdio.h&gt;

// This is just a dummy function for testing
double f(double x, double y)
{
	return (0.1*x*x + 0.2*y*y);
}

// M is number of rows, N is number of columns
#define M 61
#define N 20

int main()
{
	// m and n are row and column indices
	int m, n;
	double mf, nf; // fractional parts

	// x, y are the input parameters for the interpolation
	double x, y, x_min=2.0, x_max=18.0, y_min=5.0, y_max=15.0;
	
	// f is the table of function values - i.e. f(x,y)
	// Fill this with calculated values for testing
	double f_vals[M][N];
	for (m=0 ; m&lt;M ; ++m)
	{
		for (n=0 ; n&lt;N ; ++n)
		{
			x = x_min + (x_max - x_min) * (n / (double)(N-1));
			y = y_min + (y_max - y_min) * (m / (double)(M-1));
			f_vals[m][n] = f(x,y);
		}
	}
	
	// fi is interpolated estimate of f(x,y)
	double fi;

	// Ask for an (x,y) point to calculate f at
	printf(&quot;Please enter x and y values: &quot;);
	scanf(&quot;%lf %lf&quot;, &amp;x, &amp;y);
	
	// Find integer and fractional part of column index
	nf = (N-1) * (x - x_min) / (x_max - x_min);
	n = (int)nf;
	nf = nf - n;
	
	// Find integer and fractional part of row index
	mf = (M-1) * (y - y_min) / (y_max - y_min);
	m = (int)mf;
	mf = mf - m;
	
	// Calculate interpolated estimated
	fi = (1-nf)*(1-mf)*f_vals[m][n] + nf*(1-mf)*f_vals[m][n+1]
			+ (1-nf)*mf*f_vals[m+1][n] + nf*mf*f_vals[m+1][n+1];
	
	// Print results
	printf(&quot;Actual f(x,y) = %lf\n&quot;, f(x,y));
	printf(&quot;Estimated f(x,y) = %lf\n&quot;, fi);
	
	return 0;
}
</pre>
<p>Here&#8217;s what it looked like when I compiled and ran the above code in a console window:</p>
<p><a href="http://batchloaf.files.wordpress.com/2013/03/interpolation_console_screenshot.png"><img src="http://batchloaf.files.wordpress.com/2013/03/interpolation_console_screenshot.png?w=640" alt="Screenshot of experimental 2D linear interpolation program compiling and running in console" class="aligncenter size-full wp-image-1532" /></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/batchloaf.wordpress.com/1531/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/batchloaf.wordpress.com/1531/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=batchloaf.wordpress.com&#038;blog=18043761&#038;post=1531&#038;subd=batchloaf&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://batchloaf.wordpress.com/2013/03/12/2-d-linear-interpolation-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/b1997adbf34b6a941b0b9e88e2c59912?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">batchloaf</media:title>
		</media:content>

		<media:content url="http://batchloaf.files.wordpress.com/2013/03/interpolation_console_screenshot.png" medium="image">
			<media:title type="html">Screenshot of experimental 2D linear interpolation program compiling and running in console</media:title>
		</media:content>
	</item>
		<item>
		<title>PIC18F development in Linux using XC8 and pk2cmd</title>
		<link>http://batchloaf.wordpress.com/2013/03/06/pic18f-development-in-linux-using-xc8-and-pk2cmd/</link>
		<comments>http://batchloaf.wordpress.com/2013/03/06/pic18f-development-in-linux-using-xc8-and-pk2cmd/#comments</comments>
		<pubDate>Wed, 06 Mar 2013 01:59:13 +0000</pubDate>
		<dc:creator>batchloaf</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://batchloaf.wordpress.com/?p=1501</guid>
		<description><![CDATA[This post summarises the process of developing a new program for a PIC18F under Linux and downloading it onto the microcontroller. Firstly, download the Linux version of XC8 from microchip.com and install it. Accept all default options. When it asks &#8230; <a href="http://batchloaf.wordpress.com/2013/03/06/pic18f-development-in-linux-using-xc8-and-pk2cmd/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=batchloaf.wordpress.com&#038;blog=18043761&#038;post=1501&#038;subd=batchloaf&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>This post summarises the process of developing a new program for a PIC18F under Linux and downloading it onto the microcontroller.</p>
<ul>
<li>Firstly, download the <a href="http://www.microchip.com/mplabxc8linux">Linux version of XC8</a> from microchip.com and install it. Accept all default options. When it asks for a registration key, just leave it blank to install the free version.</li>
<li>Secondly, download the Linux version of the <a href="http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&amp;nodeId=1406&amp;dDocName=en023805">pk2cmd utility</a> that matches your Linux kernel version. You&#8217;ll need to scroll down to the &#8220;Downloads&#8221; section of that page and look for the &#8220;Linux &amp; Mac OS X Software&#8221; sub-section. The one I downloaded was titled &#8220;PK2CMD V1.20 Linux Kernel 2.6 Executable Binary&#8221;.</li>
</ul>
<p>This is a simple C example program to try out the XC8 compiler.</p>
<pre class="brush: cpp; title: ; notranslate">
//
// LED blink example for PIC18F4520
// Written by Ted Burke
// Last updated 5-3-2013
//

#include &lt;xc.h&gt;

#pragma config OSC=INTIO67,MCLRE=OFF,WDT=OFF,LVP=OFF,BOREN=OFF

int main(void)
{
	// Make RD0 a digital output
	TRISD = 0b11111110;
	
	// Blink LED on RD0 (pin 19)
	while(1)
	{
		LATDbits.LATD0 = 1 - LATDbits.LATD0;
		_delay(100000);
	}
}
</pre>
<p>Create a directory for the program and save the above code in it as a file called &#8220;main.c&#8221;.</p>
<p>I use the simple build script below to compile the program and (assuming it compiles without errors) download the updated hex file onto the PIC. Save the following into the same directory as a file called &#8220;build_and_program&#8221;.</p>
<pre class="brush: plain; title: ; notranslate">
#!/bin/bash

/mnt/sdb2/microchip/xc8/v1.12/bin/xc8 --chip=18F4520 main.c &amp;&amp;
sudo ./pk2cmd -PPIC18F4520 -F./main.hex -M -T
</pre>
<p>I installed XC8 in the directory &#8220;/mnt/sdb2/microchip/xc8&#8243;, so that&#8217;s the location specified above. The build script needs to modified according to the actual installation directory on a particular computer. If the installation directory was added to the path, the installation directory may not need to be specified at all.</p>
<p>To make the build script executable, use the following command:</p>
<pre>
chmod 755 build_and_program
</pre>
<p>To keep things simple, I create a separate directory for each PIC program and store all of the following files in it:</p>
<ul>
<li><strong>main.c</strong> &#8211; The actual C program (see above).</li>
<li><strong>build_and_program</strong> &#8211; The build script (see above).</li>
<li><strong>pk2cmd</strong> &#8211; This the command line utility for programming PIC microntrollers using the PICkit 2. It&#8217;s a very compact program, so I just place a copy of it in the folder along with each of my programs.</li>
<li><strong>PK2DeviceFile.dat</strong> &#8211; This file is required by pk2cmd, so I place a copy of it into the folder with each of my programs.</li>
</ul>
<p>To run the build script and download the hex file to the PIC, just type the following command:</p>
<pre>
./build_and_program
</pre>
<p>Here&#8217;s how it looks in my terminal window:</p>
<p><a href="http://batchloaf.files.wordpress.com/2013/03/pk2cmd_console_screenshot.png"><img src="http://batchloaf.files.wordpress.com/2013/03/pk2cmd_console_screenshot.png?w=640" alt="Screenshot of XC8 and pk2cmd running in a Linux terminal window"   class="aligncenter size-full wp-image-1502" /></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/batchloaf.wordpress.com/1501/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/batchloaf.wordpress.com/1501/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=batchloaf.wordpress.com&#038;blog=18043761&#038;post=1501&#038;subd=batchloaf&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://batchloaf.wordpress.com/2013/03/06/pic18f-development-in-linux-using-xc8-and-pk2cmd/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/b1997adbf34b6a941b0b9e88e2c59912?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">batchloaf</media:title>
		</media:content>

		<media:content url="http://batchloaf.files.wordpress.com/2013/03/pk2cmd_console_screenshot.png" medium="image">
			<media:title type="html">Screenshot of XC8 and pk2cmd running in a Linux terminal window</media:title>
		</media:content>
	</item>
		<item>
		<title>Gitshots für Windows verwendet CommandCam</title>
		<link>http://batchloaf.wordpress.com/2013/03/05/gitshots-fur-windows-verwendet-commandcam/</link>
		<comments>http://batchloaf.wordpress.com/2013/03/05/gitshots-fur-windows-verwendet-commandcam/#comments</comments>
		<pubDate>Tue, 05 Mar 2013 14:28:34 +0000</pubDate>
		<dc:creator>batchloaf</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://batchloaf.wordpress.com/?p=1495</guid>
		<description><![CDATA[I originally wrote CommandCam (my command line webcam image grabber) for one of my engineering students who needed to add some simple machine vision functionality to his final-year project. To my surprise, we couldn&#8217;t find an existing free command line &#8230; <a href="http://batchloaf.wordpress.com/2013/03/05/gitshots-fur-windows-verwendet-commandcam/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=batchloaf.wordpress.com&#038;blog=18043761&#038;post=1495&#038;subd=batchloaf&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>I originally wrote <a href="http://batchloaf.wordpress.com/commandcam/">CommandCam</a> (my command line webcam image grabber) for one of my engineering students who needed to add some simple machine vision functionality to his final-year project. To my surprise, we couldn&#8217;t find an existing free command line image grabber for Windows, so I wrote one. Once I published it online, I discovered that quite a few people were downloading it and some of them were using it for fascinating projects &#8211; photographing laptop thieves, time-lapse photography, etc.</p>
<p>Anyway, I came across another interesting use of it today. <a href="https://coderwall.com/p/xlatfq">Gitshots</a> is something clever that Víctor Martínez created for Mac OS to automatically take a webcam snapshot each time a programmer commits code to a git repository (that&#8217;s basically a source code management system). German software engineer Benedikt Eger wanted to do the same thing in Windows, so he came up with <a href="http://www.zustandsforschung.de/index.php/gitshots-fuer-windows/">an elegant three-line hack</a> (<a href="http://translate.google.com/translate?sl=auto&amp;tl=en&amp;js=n&amp;prev=_t&amp;hl=en&amp;ie=UTF-8&amp;eotf=1&amp;u=http%3A%2F%2Fwww.zustandsforschung.de%2Findex.php%2Fgitshots-fuer-windows%2F">English translation</a>) using CommandCam.</p>
<p>When you write a simple program like CommandCam and send it out into the world, it&#8217;s really interesting to hear about the places it ends up!</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/batchloaf.wordpress.com/1495/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/batchloaf.wordpress.com/1495/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=batchloaf.wordpress.com&#038;blog=18043761&#038;post=1495&#038;subd=batchloaf&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://batchloaf.wordpress.com/2013/03/05/gitshots-fur-windows-verwendet-commandcam/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/b1997adbf34b6a941b0b9e88e2c59912?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">batchloaf</media:title>
		</media:content>
	</item>
		<item>
		<title>Measuring phase difference between two 50Hz sine waves with the dsPIC30F4011</title>
		<link>http://batchloaf.wordpress.com/2013/03/04/measuring-phase-difference-between-two-50hz-sine-waves-with-dspic30f4011/</link>
		<comments>http://batchloaf.wordpress.com/2013/03/04/measuring-phase-difference-between-two-50hz-sine-waves-with-dspic30f4011/#comments</comments>
		<pubDate>Mon, 04 Mar 2013 21:27:46 +0000</pubDate>
		<dc:creator>batchloaf</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://batchloaf.wordpress.com/?p=1485</guid>
		<description><![CDATA[Reader Nand wrote in with a query about using the dsPIC30F4011 to measure the phase difference between two 50Hz sine waves. This could, for example, be used to measure the phase difference between voltage and current waveforms in a reactive &#8230; <a href="http://batchloaf.wordpress.com/2013/03/04/measuring-phase-difference-between-two-50hz-sine-waves-with-dspic30f4011/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=batchloaf.wordpress.com&#038;blog=18043761&#038;post=1485&#038;subd=batchloaf&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>Reader Nand wrote in with a query about using the dsPIC30F4011 to measure the phase difference between two 50Hz sine waves. This could, for example, be used to measure the phase difference between voltage and current waveforms in a reactive electrical load. The example program below shows how this can be done using one of the dsPIC&#8217;s built-in timers. A brief explanation of the program&#8217;s operation follows after the code.</p>
<pre class="brush: cpp; title: ; notranslate">
//
// dsPIC30F4011 phase difference measurement example
//
// Written by Ted Burke
// Last updated 4-3-2013
//
// Measure the phase difference between two 50Hz sine waves.
// The leading sine wave is assumed to be connected to AN0.
// The lagging sine wave is assumed to be connected to AN1.
// Both sine waves are assumed to vary between 0V and 5V.
// The phase difference is measured as the time delay
// between the time each signal crosses a threshold (2.5V).
//

#include &lt;xc.h&gt;
#include &lt;stdio.h&gt;
#include &lt;libpic30.h&gt;

// Configuration settings
_FOSC(CSW_FSCM_OFF &amp; FRC_PLL16); // Fosc=16x7.5MHz, Fcy=30MHz
_FWDT(WDT_OFF);                  // Watchdog timer off
_FBORPOR(MCLR_DIS);              // Disable reset pin

// Function prototype for analog input function
unsigned int read_analog_channel(int channel);

int main()
{
	// Configure AN0-AN8 as analog inputs
	ADCON3bits.ADCS = 15;  // Tad = 266ns, conversion time is 12*Tad
	ADCON1bits.ADON = 1;   // Turn ADC ON
	
	// Setup UART
	U1BRG = 48;            // 38400 baud @ 30 MIPS
	U1MODEbits.UARTEN = 1; // Enable UART

	// Configure Timer 1
	PR1 = 65535;          // Set the Timer 1 period (max 65535)
	TMR1 = 0;             // Reset Timer 1 counter
	T1CONbits.TCKPS = 2;  // Prescaler (0=1:1, 1=1:8, 2=1:64, 3=1:256)
	T1CONbits.TON = 0;    // Disable Timer 1
			
	while(1)
	{
		// Reset timer to zero
		TMR1 = 0;
		
		// Wait until AN0 is below voltage threshold
		while (read_analog_channel(0) &gt;= 512);
		
		// Wait for AN0 to rise above voltage threshold
		while (read_analog_channel(0) &lt; 512);
		
		// Start timer to measure phase difference
		T1CONbits.TON = 1;
		
		// Wait for AN1 to rise above voltage threshold
		while (read_analog_channel(1) &lt; 512);
		
		// Stop timer
		T1CONbits.TON = 0;
		
		// Print time delay. Note 64:1 timer prescaling.
		// Also note that 30000 clock cycles is 1ms at 30 MIPS.
		printf(&quot;Phase diff = %4.2f ms\n&quot;, 64.0 * TMR1 / 30000.0);
	}
	
	return 0;
}

// This function reads a single sample from the specified
// analog input. It should take less than 5us when the
// microcontroller is running at 30 MIPS.
// The dsPIC30F4011 has a 10-bit ADC, so the value
// returned is between 0 and 1023 inclusive.
unsigned int read_analog_channel(int channel)
{
	ADCHS = channel;          // Select the requested channel
	ADCON1bits.SAMP = 1;      // Start sampling
	__delay32(30);            // 1us delay @ 30 MIPS
	ADCON1bits.SAMP = 0;      // Start Converting
	while (!ADCON1bits.DONE); // Should take 12 * Tad = 3.2us
	return ADCBUF0;
}
</pre>
<p>I built this program using Microchip&#8217;s XC16 C compiler. I don&#8217;t use MPLAB, so I used the following build script to invoke the compiler. To use it, save the C code as &#8220;main.c&#8221; and the build script as &#8220;build.bat&#8221; in the same folder. Then just open a command window, navigate to that folder and type &#8220;build.bat&#8221; to compile the program.</p>
<pre class="brush: plain; title: ; notranslate">
xc16-gcc main.c -mcpu=30F4011 -Wl,--script=p30F4011.gld
if errorlevel 0 xc16-bin2hex a.out
</pre>
<p>The output file is &#8220;a.hex&#8221;. I use the PICkit 2 software to download the compiled program onto the dsPIC.</p>
<h3>Code explanation</h3>
<p>There&#8217;s not much to it really:</p>
<ul>
<li><strong>Lines 29-31</strong> enable 8 analog inputs (although only AN0 and AN1 are actually used).</li>
<li><strong>Lines 33-35</strong> enable the UART, which is used to transmit the measured phase difference as plain text to be displayed on the PC (as a time in ms).</li>
<li><strong>Lines 37-41</strong> configure Timer 1 to run with a prescaler value of 64:1. This means that the timer counter TMR1 increments once every 64 instruction cycles. With the dsPIC running at 30 MIPS, that means TMR1 increments once every 2.1333us. This is what determines the time resolution of the phase measurement. TMR1 is a 16-bit register, so its maximum value is 65535, after which is resets to zero. The longest phase difference that can be measured at this prescaler setting is therefore 139.8ms.</li>
<li>The main loop of the program (<strong>lines 43-66</strong>) simply measures the time delay beween the voltage on AN0 crossing a threshold (in the positive-going direction) and the voltage on AN1 crossing the same threshold. I have set the threshold to 2.5V. If each sine waves varies between 0V and 5V, this corresponds to the what I would regard as the zero-crossing point in the waveform.</li>
<li>Each time the phase difference is measured, it is printed to the screen via the UART (<strong>line 65</strong>). The result is converted from timer ticks into milliseconds using the timer prescaler ratio (64:1) and the number of clock cycles per millisecond (30,000).</li>
<li>I&#8217;m using the <a href="http://ww1.microchip.com/downloads/en/DeviceDoc/PICkit%202%20v2.61.00%20Setup%20A.zip">PICkit 2 software&#8217;s</a> UART tool to display the result (see image below).</li>
</ul>
<p>Here&#8217;s a screenshot of a few phase difference measurements displayed in the <a href="http://ww1.microchip.com/downloads/en/DeviceDoc/PICkit%202%20v2.61.00%20Setup%20A.zip">PICkit 2 software&#8217;s</a> UART tool. Since I don&#8217;t have two 50Hz waveforms on hand to test the program, I used an RC circuit and a switch to create a reasonably consistent time difference between two rising voltage signals. Obviously, since 50Hz waveforms have a period of 20ms, actual phase difference measurements will be considerably shorter than these example readings.</p>
<p><a href="http://batchloaf.files.wordpress.com/2013/03/uart_tool_phase_measurement.png"><img src="http://batchloaf.files.wordpress.com/2013/03/uart_tool_phase_measurement.png?w=640" alt="Screenshot of phase difference measurement displayed in the PICkit 2 software&#039;s UART tool" width="640" class="aligncenter size-full wp-image-1486" /></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/batchloaf.wordpress.com/1485/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/batchloaf.wordpress.com/1485/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=batchloaf.wordpress.com&#038;blog=18043761&#038;post=1485&#038;subd=batchloaf&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://batchloaf.wordpress.com/2013/03/04/measuring-phase-difference-between-two-50hz-sine-waves-with-dspic30f4011/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/b1997adbf34b6a941b0b9e88e2c59912?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">batchloaf</media:title>
		</media:content>

		<media:content url="http://batchloaf.files.wordpress.com/2013/03/uart_tool_phase_measurement.png" medium="image">
			<media:title type="html">Screenshot of phase difference measurement displayed in the PICkit 2 software&#039;s UART tool</media:title>
		</media:content>
	</item>
		<item>
		<title>Sending a key to a window that&#8217;s not in focus (C program)</title>
		<link>http://batchloaf.wordpress.com/2013/02/13/sending-a-key-to-a-window-thats-not-in-focus-c-program/</link>
		<comments>http://batchloaf.wordpress.com/2013/02/13/sending-a-key-to-a-window-thats-not-in-focus-c-program/#comments</comments>
		<pubDate>Wed, 13 Feb 2013 18:00:12 +0000</pubDate>
		<dc:creator>batchloaf</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[bot]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[find window title]]></category>
		<category><![CDATA[FindWindowEx]]></category>
		<category><![CDATA[GetConsoleTitle]]></category>
		<category><![CDATA[GetWindowText]]></category>
		<category><![CDATA[INPUT]]></category>
		<category><![CDATA[keyboard event]]></category>
		<category><![CDATA[keypress]]></category>
		<category><![CDATA[keystroke]]></category>
		<category><![CDATA[PostMessage]]></category>
		<category><![CDATA[robot]]></category>
		<category><![CDATA[send key]]></category>
		<category><![CDATA[SendInput]]></category>
		<category><![CDATA[sendkey]]></category>
		<category><![CDATA[sendkey.c]]></category>
		<category><![CDATA[SendMessage]]></category>
		<category><![CDATA[SetForegroundWindow]]></category>
		<category><![CDATA[simulate]]></category>
		<category><![CDATA[toupper]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[Win32]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://batchloaf.wordpress.com/?p=1469</guid>
		<description><![CDATA[In a previous post, I showed how a C program can simulate a keystroke in Windows using SendInput. I also posted a subsequent example showing how to simulate a keypress with the Control key included (e.g. Ctrl+V). In this post &#8230; <a href="http://batchloaf.wordpress.com/2013/02/13/sending-a-key-to-a-window-thats-not-in-focus-c-program/">Continue reading <span class="meta-nav">&#8594;</span></a><img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=batchloaf.wordpress.com&#038;blog=18043761&#038;post=1469&#038;subd=batchloaf&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>In a <a href="http://batchloaf.wordpress.com/2012/04/17/simulating-a-keystroke-in-win32-c-or-c-using-sendinput/">previous post</a>, I showed how a C program can simulate a keystroke in Windows using SendInput. I also posted a subsequent example showing <a href="http://batchloaf.wordpress.com/2012/10/18/simulating-a-ctrl-v-keystroke-in-win32-c-or-c-using-sendinput/">how to simulate a keypress with the Control key included</a> (e.g. Ctrl+V). In this post I&#8217;m revisting SendInput, but this time an out-of-focus window is the target for the simulated keystroke(s).</p>
<p>The following C program sends a keypress to a specified window that does not need to be in focus. The keystroke to send is specified as the first command line argument. The second command line argument is a &#8220;target&#8221; word that appears in the title of the target window. The target word can be anything that appears in the title of the taregt window, but it makes sense to pick something that definitely won&#8217;t appear in the title of any other windows. The search word is case sensitive and the simulated keystroke is sent to the first matching window.</p>
<pre class="brush: cpp; title: ; notranslate">
//
// sendkey.c - Send a key press to a specific window
// Written by Ted Burke
// Last updated 13-2-2013
//
// To compile using gcc:
//
//		gcc -o sendkey.exe sendkey.c
//
// To send the letter 'a' to a WordPad window:
//
//		sendkey.exe a WordPad
//

#define WINVER 0x0500
#include &lt;windows.h&gt;
#include &lt;stdio.h&gt;

int main(int argc, char* argv[])
{
	// Check number of command line arguments
	if (argc &lt; 3)
	{
		fprintf(stderr, &quot;Too few command line arguments\n&quot;);
		fprintf(stderr, &quot;Usage: sendkey.exe KEY_TO_SEND&quot;);
		fprintf(stderr, &quot; WORD_FROM_TARGET_WINDOW_TITLE\n&quot;);
		return 1;
	}
	
	// Get the character to send from the first command
	// line argument
	char char_to_send = toupper(argv[1][0]);
	
	// Get first window on desktop
	HWND firstwindow = FindWindowEx(NULL, NULL, NULL, NULL);
	HWND window = firstwindow;
	TCHAR windowtext[MAX_PATH];
	
	// We need to get the console title in case we
	// accidentally match the search word with it
	// instead of the intended target window.
	TCHAR consoletitle[MAX_PATH];
	GetConsoleTitle(consoletitle, MAX_PATH);
	
	while(1)
	{
		fprintf(stderr, &quot;.&quot;);
		
		// Check window title for a match
		GetWindowText(window, windowtext, MAX_PATH);
		if (strstr(windowtext, argv[2]) != NULL &amp;&amp;
			strcmp(windowtext, consoletitle) != 0) break;
		
		// Get next window
		window = FindWindowEx(NULL, window, NULL, NULL);
		if (window == NULL || window == firstwindow)
		{
			fprintf(stderr, &quot;Window not found\n&quot;);
			return 1;
		}
	}
	fprintf(stderr, &quot;Window found: %s\n&quot;, windowtext);
	
	// Bring specified window into focus
	SetForegroundWindow(window);

	// Create the desired keyboard event
	INPUT ip;
	ip.type = INPUT_KEYBOARD;
	ip.ki.wVk = char_to_send;
	ip.ki.wScan = 0;
	ip.ki.dwFlags = 0;
	ip.ki.time = 0;
	ip.ki.dwExtraInfo = 0;
	
	// Send the keyboard event to the specified window
	SendInput(1, &amp;ip, sizeof(INPUT));
	
	// Exit normally
	return 0;
}
</pre>
<p>This is what it looked like in the console where I compiled and ran sendkey.exe:</p>
<p><a href="http://batchloaf.files.wordpress.com/2013/02/sendkey_console_screenshot.png"><img src="http://batchloaf.files.wordpress.com/2013/02/sendkey_console_screenshot.png?w=640" alt="screenshot of console compiling and running sendkey" class="aligncenter size-full wp-image-1470" /></a></p>
<p>This screenshot shows the WordPad window that received the &#8216;A&#8217; key press:</p>
<p><a href="http://batchloaf.files.wordpress.com/2013/02/sendkey_wordpad_screenshot.png"><img src="http://batchloaf.files.wordpress.com/2013/02/sendkey_wordpad_screenshot.png?w=640" alt="screenshot of WordPad showing character typed remotely using sendkey.exe" class="aligncenter size-full wp-image-1471" /></a></p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/batchloaf.wordpress.com/1469/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/batchloaf.wordpress.com/1469/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=batchloaf.wordpress.com&#038;blog=18043761&#038;post=1469&#038;subd=batchloaf&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://batchloaf.wordpress.com/2013/02/13/sending-a-key-to-a-window-thats-not-in-focus-c-program/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/b1997adbf34b6a941b0b9e88e2c59912?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">batchloaf</media:title>
		</media:content>

		<media:content url="http://batchloaf.files.wordpress.com/2013/02/sendkey_console_screenshot.png" medium="image">
			<media:title type="html">screenshot of console compiling and running sendkey</media:title>
		</media:content>

		<media:content url="http://batchloaf.files.wordpress.com/2013/02/sendkey_wordpad_screenshot.png" medium="image">
			<media:title type="html">screenshot of WordPad showing character typed remotely using sendkey.exe</media:title>
		</media:content>
	</item>
	</channel>
</rss>
