<?xml version="1.0" encoding="windows-1252"?>
<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>The Blogging Protagonist &#187; C/C++</title>
	<atom:link href="http://www.SWHarden.com/blog/category/cc/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.SWHarden.com/blog</link>
	<description>A collection of thoughts in technological degradation</description>
	<lastBuildDate>Wed, 12 Oct 2011 02:20:21 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>I before E except after Hellschreiber</title>
		<link>http://www.SWHarden.com/blog/2011-08-05-i-before-e-except-after-hellschreiber/</link>
		<comments>http://www.SWHarden.com/blog/2011-08-05-i-before-e-except-after-hellschreiber/#comments</comments>
		<pubDate>Fri, 05 Aug 2011 23:52:40 +0000</pubDate>
		<dc:creator>Scott</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Circuitry]]></category>
		<category><![CDATA[Microcontrollers]]></category>
		<category><![CDATA[Radio]]></category>
		<category><![CDATA[high altitude balloon]]></category>

		<guid isPermaLink="false">http://www.SWHarden.com/blog/?p=3055</guid>
		<description><![CDATA[This post describes a project I designed which transmits strings of data from a microcontroller to a PC&#8217;s screen using audio beeping in a special mode called Hellschreiber. Although these days it&#8217;s almost exclusively used by amateur radio operators, I thought it would make a cool microcontroller project! The result can be accomplished with a [...]]]></description>
			<content:encoded><![CDATA[<p><table><tr><td style="text-indent: 25px; background-color: #E5E5E5; padding: 10px; border-top-width: 1px; border-bottom-width: 1px; border-left-width: 7px;border-top-style: solid; border-right-style: solid;border-bottom-style: solid;border-left-style: solid;border-top-color: #B5B5B5; border-right-color: #B5B5B5;border-bottom-color: #B5B5B5; border-left-color: #B5B5B5;border-right-width: 1px;background-image: url(http://www.swharden.com/graphics/layout_2006_08_12/quotes.jpg); background-position: left top; background-repeat: no-repeat;">This post describes a project I designed which transmits strings of data from a microcontroller to a PC&#8217;s screen using audio beeping in a special mode called <a href="http://en.wikipedia.org/wiki/Hellschreiber" onclick="javascript:urchinTracker ('/outbound/article/en.wikipedia.org');">Hellschreiber</a>. Although these days it&#8217;s almost exclusively used by amateur radio operators, I thought it would make a cool microcontroller project! The result can be accomplished with a microcontroller and a speaker as a transmitter and a PC with a microphone as a receiver and decoder, or with actual radio equipment (even toy walkie talkies) by transmitting the tones over modulated radio frequencies for long distance communication! <a href="http://www.amazon.com/Midland-GXT760VP4-36-Mile-42-Channel-Two-Way/dp/B0039YON6Q/ref=sr_1_1?ie=UTF8&#038;qid=1312588647&#038;sr=8-1" onclick="javascript:urchinTracker ('/outbound/article/www.amazon.com');">Ideas anyone?</a></p>
<p><i><b>SPECIAL THANKS:</b> I&#8217;d like to think <a href="http://www.mikeseese.com" onclick="javascript:urchinTracker ('/outbound/article/www.mikeseese.com');">Mike Seese</a> for his brainstorming help in making this project a reality.  <a href="http://www.mikeseese.com" onclick="javascript:urchinTracker ('/outbound/article/www.mikeseese.com');">Mike</a> and I are working on a high altitude balloon project together, and a creative inexpensive radio link is one of our goals. Thanks <a href="http://www.mikeseese.com" onclick="javascript:urchinTracker ('/outbound/article/www.mikeseese.com');">Mike</a>!</i><br />
</td></tr></table></p>
<p><b>As a professional dental student by day and amateur electrical/RF engineer by night, I&#8217;m having a very strange summer.</b> I&#8217;m developing rapidly in my experience and skills in both arenas. I finally feel like I have a working knowledge of most fundamental electrical and radio frequency concepts, and I&#8217;m starting to see patients and do procedures on humans (no more mannequins) in the student dental clinic. For legal and ethical reasons I do not write specifics about what I do with my patients, but I certainly make up for it by documenting the electronic projects I work on! My goals of doing this are to (a) inspire potential electronics tinkerers to come up with new ideas and attack new projects, and (b) receive feedback and insight from those more experienced than me to help me grow in my knowledge. My eye caught <a href="http://www.swharden.com/blog/2011-07-24-frequency-counter-gen2/comment-page-1/#comment-16485" >a comment</a> a few posts ago that made me smile: <i>You have been blessed with talent and the drive to attempt things not been tried before, keep it up, great job. &#8211;David S</i>  While I can&#8217;t claim that everything I do is truly novel or never tried before, I appreciate the encouraging words. Thank you David S!</p>
<p><b>Today&#8217;s project is a fun one involving vintage wartime radio equipment, amateur radio computer software, and a healthy dose of microcontrollers!</b> My goal is to design a single chip Hellschreiber (technically Feldhellschreiber) transmitter. &#8220;Hellschreiber&#8221; translates into English as &#8220;Light Writer&#8221; and is a pun on the name of its inventor, Rudolf Hell, who built the first device in 1920. It was intended to allow messages to be transferred over poor radio links too noisy for intelligible voice or <a href="http://en.wikipedia.org/wiki/Radioteletype" onclick="javascript:urchinTracker ('/outbound/article/en.wikipedia.org');">radioteletype (RTTY)</a> communication. Its cool factor is upped by the fact that it was sometimes used by the German military in conjunction with the <a href="http://en.wikipedia.org/wiki/Enigma_machine" onclick="javascript:urchinTracker ('/outbound/article/en.wikipedia.org');">Enigma encryption system</a> during World War 2! [As an aside, RTTY is still pretty sweet and dates back to the mid 1800s! Check out hardware receivers in <a href="http://www.youtube.com/watch?v=mN8pkJoDDfI" onclick="javascript:urchinTracker ('/outbound/article/www.youtube.com');">video 1</a> and <a href="http://www.youtube.com/watch?v=Ml00ngVwrcU" onclick="javascript:urchinTracker ('/outbound/article/www.youtube.com');">video 2</a>]<br />
<iframe width="525" height="393" src="http://www.youtube.com/embed/nXLPUbGYDp4" frameborder="0" allowfullscreen></iframe></p>
<p><b>Seeing a battlefield-ready Hellschreiber receiver gives you a good idea of how it works.</b> (The video isn&#8217;t mine, I found it on youtube.) The concept is relatively simple (shown above), and the receiver has only 2 moving parts. A spinning corkscrew presses a ticker tape into ink when it receives a radio signal. As the radio signal beeps on and off, the corkscrew contacts at different positions at different times, and letters are written on the ticker tape! <a href="http://www.SWHarden.com/blog/images/anaglyph-hell-GL-11.jpg" onclick="javascript:urchinTracker ('/outbound/article/www.SWHarden.com');"><img src="http://www.SWHarden.com/blog/images/anaglyph-hell-GL-11.jpg" alt="anaglyph-hell-GL-11" title="anaglyph-hell-GL-11" width="300" height="250" class="alignright size-thumbnail wp-image-3074" /></a>The <a href="http://www.nonstopsystems.com/radio/hellschreiber-fonts.htm" onclick="javascript:urchinTracker ('/outbound/article/www.nonstopsystems.com');">designers of these things were extraordinarily creative</a>! The picture on the right shows a Hellschreiber transmitter &#8211; basically a typewriter with mechanical wizardry that turns key presses into a series of radio tones corresponding to the pixelated shape of a character.</p>
<p><b>Almost a century later, people are still sending messages around the world using Hellschreiber!</b> With an <a href="http://en.wikipedia.org/wiki/Amateur_radio" onclick="javascript:urchinTracker ('/outbound/article/en.wikipedia.org');">amateur radio license</a> and an amateur radio transceiver you can tune around special <a href="http://www.nonstopsystems.com/radio/frank-radio-dig-mode-freqs.htm" onclick="javascript:urchinTracker ('/outbound/article/www.nonstopsystems.com');">Hellschreiber calling frequencies</a> and engage in conversations with other people who enjoy using this unique mode. Computers have modernized the process, allowing you to send Hellschreiber text by typing on your keyboard and receive it by just looking at your screen. My favorite program (free) to do this is Digital Master 780, part of <a href="http://www.ham-radio-deluxe.com/Downloads.aspx" onclick="javascript:urchinTracker ('/outbound/article/www.ham-radio-deluxe.com');">Ham Radio Deluxe</a>.</p>
<p><iframe width="525" height="393" src="http://www.youtube.com/embed/_MJYwXvwTvY" frameborder="0" allowfullscreen></iframe></p>
<p><b>This is the project I just completed.</b> It takes strings of text stored (or dynamically generated) in an array on a microcontroller (I&#8217;m using an ATMega48, but the code is almost identical for any ATMEL AVR microcontroller, and easy adapted for other architectures) and turns it into an audio tone using PWM. This audio tone could be fed into a speaker and a microphone across the room could receive it and use the software to show the received data, or the audio could be fed into a radio transmitter and a PC hooked to the receiver could decode the audio.  Either way, the text in the microcontroller is converted to Hellschreiber audio tones ready to be used however you see fit!  Although I designed it as a resilient way to transmit GPS/altitude data from a high altitude balloon using a small, cheap, low-power radio transmitter, this project is just the foundation of a plethora of potential projects!<br />
<a href="http://www.SWHarden.com/blog/images/DSCN1663.JPG" onclick="javascript:urchinTracker ('/outbound/article/www.SWHarden.com');"><img src="http://www.SWHarden.com/blog/images/DSCN1663-525x393.jpg" alt="DSCN1663" title="DSCN1663" width="525" height="393" class="alignnone size-medium wp-image-3080" /></a></p>
<p><b>Here&#8217;s the circuit I&#8217;m using.</b> It&#8217;s actually less complicated than shown &#8211; all those yellow wires are going to my AVR programmer! The chip just receives +5V and GND, and the audio is generated automatically and output on the OC0A pin, which happens to be pin 12 on my ATMega48. The output (audio level square waves) is fed to a crystal oscillator <a href="http://www.taydaelectronics.com/servlet/the-709/OSC-dsh-40M-dsh-MEC-dsh-LF-CRYSTAL-OSCILLATOR-40.00/Detail" onclick="javascript:urchinTracker ('/outbound/article/www.taydaelectronics.com');">like this one</a>, which generates square waves with an amplitude equal that to the input. Thus, by audio-frequency AC from the microchip, decoupled through a series capacitor, added to the power supply of the oscillator (provided by the 5V rail through a 1.8k resistor), we effectively produce an amplitude modulated (AM) radio signal!<br />
<a href="http://www.SWHarden.com/blog/images/DSCN1667.JPG" onclick="javascript:urchinTracker ('/outbound/article/www.SWHarden.com');"><img src="http://www.SWHarden.com/blog/images/DSCN1667-525x393.jpg" alt="DSCN1667" title="DSCN1667" width="525" height="393" class="alignnone size-medium wp-image-3081" /></a></p>
<p><b>This is the receiver I&#8217;m using.</b> I&#8217;m lucky enough to have an all-mode, general-coverage, 100W amateur radio transceiver! It&#8217;s a <a href="http://www.eham.net/reviews/detail/3046" onclick="javascript:urchinTracker ('/outbound/article/www.eham.net');">Yaesu 857-D</a> and I&#8217;m completely in love with it. It&#8217;s quite pricey though! You can find wide coverage receive-only radios called <a href="http://en.wikipedia.org/wiki/Scanner_(radio)" onclick="javascript:urchinTracker ('/outbound/article/en.wikipedia.org');">radio scanners</a> (or police scanners), often for $20 or so on eBay which would do just as good a job of receiving all sorts of radio signals! Whatever you use, after tuning into the audio with the ham radio delux software, you&#8217;ll be able to decode Hellschreiber like this:<br />
<a href="http://www.SWHarden.com/blog/images/hell.PNG" onclick="javascript:urchinTracker ('/outbound/article/www.SWHarden.com');"><img src="http://www.SWHarden.com/blog/images/hell-525x380.PNG" alt="hell" title="hell" width="525" height="380" class="alignnone size-medium wp-image-3062" /></a></p>
<p><b>A few notes about the code:</b> Each letter is sent twice vertically and I don&#8217;t think I should have done that. It&#8217;s easy enough to correct by eliminating the second FOR loop in the sendChar() function, and doubling the height of the pixels transmitted by changing on(1) and off(1) to on(2) and off(2). Then again, I could be mistaken &#8211; I don&#8217;t use this mode much.  Also, horizontal width of characters (increase this and horizontally compress the received image to reduce the effects of noise) is controlled by a single variable, dynamically adjustable in software. Characters are created from a 3&#215;5 grid (15 bits) and stored as an integer (16 bits, 2 bytes in AVR-GCC). Custom characters are certainly possible! This program takes 16.1% of program space (658 bytes) and 25.4% of data space (130 bytes) and certainly leaves room for optimization.</p>
<pre class="prettyprint c">
// designed for and tested with ATMega48
#include &lt;avr/io.h&gt;
#define F_CPU 8000000UL
#include &lt;avr/delay.h&gt;
#include &lt;avr/interrupt.h&gt;

/*
character format (3x5):
	KFA
	LGB
	MHC
	NID
	OJE

variable format:
	2-byte, 16-bit int 0b0ABCDEFGHIJKLMNO
	(note that the most significant bit is not used)
*/
#define A 	0b0111111010011111
#define B 	0b0010101010111111
#define C	0b0100011000101110
#define D	0b0011101000111111
#define E	0b0100011010111111
#define F	0b0100001010011111
#define G 	0b0100111000101110
#define H	0b0111110010011111
#define I	0b0100011111110001
#define J	0b0111110000100011
#define K	0b0110110010011111
#define L	0b0000010000111111
#define M	0b0111110110011111
#define N	0b0011111000001111
#define O	0b0011101000101110
#define P	0b0010001010011111
#define Q	0b0111011001011110
#define R	0b0010111010011111
#define S	0b0100101010101001
#define T	0b0100001111110000
#define U	0b0111110000111111
#define V	0b0111100000111110
#define W	0b0111110001111111
#define X	0b0110110010011011
#define Y	0b0110000011111000
#define Z	0b0110011010110011
#define n0	0b0111111000111111
#define n1	0b0000011111101001
#define n2	0b0111011010110111
#define n3	0b0111111010110001
#define n4	0b0111110010011100
#define n5	0b0101111010111101
#define n6	0b0101111010111111
#define n7	0b0110001011110000
#define n8	0b0111111010111111
#define n9	0b0111111010111101
#define SP	0b0000000000000000
#define BK	0b0111111111111111
#define SQ	0b0001000111000100
#define PR	0b0000110001100011
#define AR	0b0001000111011111

volatile char width=1; // width of characters, widen to slow speed

#define spd 8300 // synchronization, incr to make it slant upward

void rest(char times){while (times){times--;_delay_us(spd);}}

void on(char restfor){OCR0A=110;rest(restfor);}
void off(char restfor){OCR0A=0;rest(restfor);}

void sendChar(int tosend){
	char w;
	char bit;
	for(w=0;w&lt;width*2;w++){ // left column
		off(1);
		for (bit=0;bit&lt;5;bit++){
				if ((tosend&gt;&gt;bit)&#038;1) {on(1);}
				else {off(1);}
			}
		off(1);
		}
	for(w=0;w&lt;width*2;w++){ // middle column
		off(1);
		for (bit=5;bit&lt;10;bit++){
				if ((tosend&gt;&gt;bit)&#038;1) {on(1);}
				else {off(1);}
			}
		off(1);
		}
	for(w=0;w&lt;width*2;w++){ // right column
		off(1);
		for (bit=10;bit&lt;15;bit++){
				if ((tosend&gt;&gt;bit)&#038;1) {on(1);}
				else {off(1);}
			}
		off(1);
		}
	off(14); // letter space (1 column)
}

// CUSTOMIZE THE MESSAGE, OR GENERATE IT DYNAMICALLY!
int message[]={AR,AR,AR,S,W,H,A,R,D,E,N,PR,C,O,M,SP,R,O,C,K,S,
	SP,AR,AR,AR,SP,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,
	V,W,X,Y,Z,n0,n1,n2,n3,n4,n5,n6,n7,n8,n9,BK,SP};

void sendMessage(){
	char i;
	for(i=0;i&lt;sizeof(message)/2;i++){
		sendChar(message[i]);
	}
}

int main(){ // ### PROGRAM STARTS HERE ###

	// this sets up CPWM in CTC mode,
	// it may be slightly different for other chips
	DDRD|=255; // OC0A is now an output
	TCCR0A=0b01000010; // toggle on match, CTC mode
	TCCR0B=0B00000011; // set prescalar

	for(;;){
		width=1; // fast mode
		sendMessage();
		width=3; // slow mode
		sendMessage();
	}

	return 0;
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.SWHarden.com/blog/2011-08-05-i-before-e-except-after-hellschreiber/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>PC/microcontroller &#8220;wireless&#8221; data transfer (part 1)</title>
		<link>http://www.SWHarden.com/blog/2011-07-26-pcmicrocontroller-wireless-data-transfer/</link>
		<comments>http://www.SWHarden.com/blog/2011-07-26-pcmicrocontroller-wireless-data-transfer/#comments</comments>
		<pubDate>Wed, 27 Jul 2011 03:35:16 +0000</pubDate>
		<dc:creator>Scott</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Circuitry]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Microcontrollers]]></category>

		<guid isPermaLink="false">http://www.SWHarden.com/blog/?p=2992</guid>
		<description><![CDATA[UPDATE! >>> [FINISHED PROJECT HERE]
Several days ago I had a crazy idea. I was driving to Orlando to pick my wife up from the airport and it was dark and stormy on the highway and I was thinking about the backlash I got from my Sound Card Microcontroller/PC Communication project, where I used an embarrassingly [...]]]></description>
			<content:encoded><![CDATA[<p><code>UPDATE!</code> >>> <b><a href="http://www.swharden.com/blog/2011-07-31-pcmicrocontroller-wireless-data-transfer-part-2/" >[FINISHED PROJECT HERE]</a></b></p>
<p><b>Several days ago I had a crazy idea.</b> I was driving to Orlando to pick my wife up from the airport and it was dark and stormy on the highway and I was thinking about the backlash I got from my <a href="http://www.swharden.com/blog/2011-07-09-sound-card-microcontrollerpc-communication/" >Sound Card Microcontroller/PC Communication</a> project, where I used an embarrassingly simple hardware to accomplish the simple task of exchanging a few bytes of data between a PC and microcontroller (in the face of many people who adamantly prefer more complicated &#8220;traditional standard&#8221; methods). The car in front of me drove with his emergency flashers on, and at times all I could see were his lights. At that moment the crazy idea popped in my head &#8211; I wonder if I could use a PC monitor and phototransistors to send data to a microchip? I can&#8217;t think of any immediate uses for this capability, but perhaps if I make a working prototype I&#8217;ll stumble upon some. Either way, it sounds like a fun project!<br />
<a href="http://www.SWHarden.com/blog/images/DSCN1652.JPG" onclick="javascript:urchinTracker ('/outbound/article/www.SWHarden.com');"><img src="http://www.SWHarden.com/blog/images/DSCN1652-525x393.jpg" alt="DSCN1652" title="DSCN1652" width="525" height="393" class="size-medium wp-image-3021" /></a></p>
<p><b>The circuit is as simple as it gets.</b> <a href="http://www.SWHarden.com/blog/images/PHOTOTRANSISTOR-MICROCONTROLLER-CIRCUIT.png" onclick="javascript:urchinTracker ('/outbound/article/www.SWHarden.com');"><img src="http://www.SWHarden.com/blog/images/PHOTOTRANSISTOR-MICROCONTROLLER-CIRCUIT.png" alt="PHOTOTRANSISTOR MICROCONTROLLER CIRCUIT" title="PHOTOTRANSISTOR MICROCONTROLLER CIRCUIT" width="141" height="233" class="alignright size-full wp-image-2993" /></a>A phototransistor is exactly what it says, a photo (light-triggered) transistor (uses small current to trigger a large current). It&#8217;s a <a href="http://en.wikipedia.org/wiki/Photodiode" onclick="javascript:urchinTracker ('/outbound/article/en.wikipedia.org');">photodiode</a> with a small transistor circuit built in. Make sure you give it right polarity when you plug it in! For some reason (likely known to electrical engineers, not dental students) the larger metal piece in the plastic part, which I normally associate as negative for LEDs, should be plugged in the +5V for my photodiode. Again, make sure you hook yours up right. I purchased mine from eBay quite cheaply, but I&#8217;ll bet you can find some in RadioShack.  Note that the value of the 22k resistor is important, and that your needed value may differ from mine. The resistor relates to sensitivity, the larger the value the more sensitive the device is to light. If it&#8217;s too sensitive, it will sense light even when aimed at a black portion of the screen.<br />
<a href="http://www.SWHarden.com/blog/images/hardcode.jpg" onclick="javascript:urchinTracker ('/outbound/article/www.SWHarden.com');"><img src="http://www.SWHarden.com/blog/images/hardcode-525x104.jpg" alt="hardcode" title="hardcode" width="525" height="104" class="size-medium wp-image-3020" /></a></p>
<p><b>Initial tests were done</b> using the pins as digital inputs. This was difficult to achieve because, even as transistorized photo-diodes, it took a large difference in light to go from 5V to 0V (even past the 2.5V threshold). After a few minutes of frustration, I decided to use ADC to measure the light intensity. I use only the most significant 8 bits (ADCH). I found that in ambient light the readings are 255, and that white monitor light is around 200. Therefore my threshold is 250 (4.88V?) and I use this for logic decisions. Here&#8217;s my setup showing the ADC value of each phototransistor translated into a 1 and 0 for clock (C) and data (D). Both are aimed toward the lamp, so both show a logical 1:<br />
<a href="http://www.SWHarden.com/blog/images/DSCN1651.JPG" onclick="javascript:urchinTracker ('/outbound/article/www.SWHarden.com');"><img src="http://www.SWHarden.com/blog/images/DSCN1651-525x393.jpg" alt="DSCN1651" title="DSCN1651" width="525" height="393" class="size-medium wp-image-3022" /></a></p>
<p><b>My first test</b> involved reading the data from the image above. The clock is on the bottom line, data is on the top. Every time the clock transitions from black to white, the value of the data at that point is read (white=1, black=0) and the number is placed on a screen.  Here&#8217;s what it looks like in action:<br />
<iframe width="480" height="390" src="http://www.youtube.com/embed/lvVjsMMCx0U" frameborder="0" allowfullscreen></iframe></p>
<p><b>Hopefully soon we can get a JavaScript interface going!</b> Rather than swiping I&#8217;d like to just point this at the screen and let JS flash some squares for my device to read. This will allow virtually unlimited amounts of data to be transferred, albeit slowly, to the micro-controller. Here&#8217;s a preliminary sketch of how to send strings.<br />
<a href="http://www.SWHarden.com/blog/images/string.png" onclick="javascript:urchinTracker ('/outbound/article/www.SWHarden.com');"><img src="http://www.SWHarden.com/blog/images/string-525x228.png" alt="string" title="string" width="525" height="228" class="size-medium wp-image-3024" /></a></p>
<p><b>Remember now we&#8217;re using a time domain, not a 2d barcode.</b> I really stink at writing JavaScript, I&#8217;m going to have to pull in some help on this one!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.SWHarden.com/blog/2011-07-26-pcmicrocontroller-wireless-data-transfer/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Frequency Counter Gen2</title>
		<link>http://www.SWHarden.com/blog/2011-07-24-frequency-counter-gen2/</link>
		<comments>http://www.SWHarden.com/blog/2011-07-24-frequency-counter-gen2/#comments</comments>
		<pubDate>Sun, 24 Jul 2011 22:07:24 +0000</pubDate>
		<dc:creator>Scott</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Circuitry]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Microcontrollers]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Radio]]></category>

		<guid isPermaLink="false">http://www.SWHarden.com/blog/?p=3001</guid>
		<description><![CDATA[I&#8217;m working to further simplify my frequency counter design. This one is simpler than my previous design both in hardware and software! Here&#8217;s a video to demonstrate the device in its current state:

I utilize the ATMega48&#8217;s hardware counter which is synchronous with the system clock, so it can only measure frequency less than half of [...]]]></description>
			<content:encoded><![CDATA[<p><b>I&#8217;m working to further simplify my frequency counter design.</b> This one is simpler than <a href="http://www.swharden.com/blog/2011-03-14-frequency-counter-finished/" >my previous design</a> both in hardware and software! Here&#8217;s a video to demonstrate the device in its current state:<br />
<iframe width="480" height="390" src="http://www.youtube.com/embed/FUdxwntNh1c" frameborder="0" allowfullscreen></iframe></p>
<p>I utilize the ATMega48&#8217;s hardware counter which is synchronous with the system clock, so it can only measure frequency less than half of its clock speed. I solve this issue by dividing the input frequency by 8 and clocking the chip at 12mhz. This allows me to measure frequencies up to about 48MHz, but can be easily adapted to measure over 700MHz (really?) by dividing the input by 128. Division occurs by a 74HC590 8-bit counter (not a 74HC595 as I accidentally said in the video, which is actually a common shift register), allowing easy selection of input divided by 1, 2, 4, 8, 16, 32, 64, or 128. The following image shows the o-scope showing the original signal (bottom) and the divided-by-8 result (top)<br />
<a href="http://www.SWHarden.com/blog/images/DSCN1630.JPG" onclick="javascript:urchinTracker ('/outbound/article/www.SWHarden.com');"><img src="http://www.SWHarden.com/blog/images/DSCN1630-525x393.jpg" alt="DSCN1630" title="DSCN1630" width="525" height="393" class="alignright size-medium wp-image-3002" /></a></p>
<p><b>The device outputs</b> graphically to a LCD simply enough. That LCD is from eBay and is only $3.88 shipped! I&#8217;m considering buying a big box of them and implementing them in many more of my projects. They&#8217;re convenient and sure do look nice!<br />
<a href="http://www.SWHarden.com/blog/images/DSCN1634.JPG" onclick="javascript:urchinTracker ('/outbound/article/www.SWHarden.com');"><img src="http://www.SWHarden.com/blog/images/DSCN1634-525x393.jpg" alt="DSCN1634" title="DSCN1634" width="525" height="393" class="alignright size-medium wp-image-3003" /></a></p>
<p><b>The signal I test with</b> comes from an oscillator I built several months ago.  It&#8217;s actually a SA612 style receiver whose oscillator is tapped, amplified, and output through a wire. It&#8217;s tunable over almost all of 40m with a varactor diode configuration. It was the start of a transceiver, but I got so much good use out of it as a function generator that I decided to leave it like it is!<br />
<a href="http://www.SWHarden.com/blog/images/DSCN1637.JPG" onclick="javascript:urchinTracker ('/outbound/article/www.SWHarden.com');"><img src="http://www.SWHarden.com/blog/images/DSCN1637-525x393.jpg" alt="DSCN1637" title="DSCN1637" width="525" height="393" class="alignright size-medium wp-image-3004" /></a></p>
<p><b>THIS IS HOW THE PROGRAM WORKS:</b> I don&#8217;t supply a schematic because it&#8217;s simple as could be. Divide the input frequency to something relatively slow, &lt;1MHz at least.  Configure the 16-bit counter to accept an external pin as the counter source (not a prescaled clock, as I often use in other applications). Then set the timer value to 0, _delay_ms() a certainly amount of time (1/10th second), and read the counter value. Multiply it by 10 to account for the 1/10th second, then multiply it by 8 to account for the divider, and it&#8217;s done! It will update 10 times a second, with a resolution down to 10*8 = 80 Hz. It&#8217;s well within the range of amateur radio uses! If you&#8217;re considering replicating this, read up on how to use hardware counters with ATMEL AVR microcontrollers. That should be enough to get you started! Here&#8217;s the code I used&#8230;</p>
<p>For the LCD, this code requires <a href="http://homepage.hispeed.ch/peterfleury/avr-lcd44780.html" onclick="javascript:urchinTracker ('/outbound/article/homepage.hispeed.ch');">LCD library</a>. </p>
<pre class="prettyprint python">
#include &lt;stdlib.h&gt;
#include &lt;avr/io.h&gt;
#include &lt;avr/pgmspace.h&gt;
#include &lt;util/delay.h&gt;
#include "lcd.h"
#include "lcd.c"

int main(void)
{
	TCCR1B=0b00000111; // rising edge trigger
    char buffer[8];
	long toshow=0;
	char mhz=0;
	int khz=0;
	int hz=0;
    lcd_init(LCD_DISP_ON);
	for(;;){
    	lcd_clrscr();

    	lcd_gotoxy(0,0);
		itoa(mhz , buffer, 10);
		lcd_puts(buffer);
		lcd_puts(".");

		if (khz&lt;100){lcd_puts("0");}
		itoa(khz , buffer, 10);
		lcd_puts(buffer);

		itoa(hz/100 , buffer, 10);
		lcd_puts(buffer);

		lcd_puts(" MHz");

		TCNT1=0;
		_delay_ms(99);
		_delay_us(312);
		toshow=TCNT1;
		toshow=(long)toshow*16*10; // tenth second period
		mhz=toshow/1000000;
		toshow=toshow-mhz*1000000;
		khz=toshow/1000;
		toshow=toshow-khz*1000;
		hz=toshow;
	}
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.SWHarden.com/blog/2011-07-24-frequency-counter-gen2/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Sound Card Microcontroller/PC Communication</title>
		<link>http://www.SWHarden.com/blog/2011-07-09-sound-card-microcontrollerpc-communication/</link>
		<comments>http://www.SWHarden.com/blog/2011-07-09-sound-card-microcontrollerpc-communication/#comments</comments>
		<pubDate>Sun, 10 Jul 2011 04:30:44 +0000</pubDate>
		<dc:creator>Scott</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Circuitry]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Microcontrollers]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Radio]]></category>

		<guid isPermaLink="false">http://www.SWHarden.com/blog/?p=2912</guid>
		<description><![CDATA[ This page describes a method of sending data from a microchip to a PC using pulses of data. It&#8217;s an alternative to more traditional serial or USB methods of connectivity.  It&#8217;s not intended as a solution for consumer products, but rather an easy hack for hobbyists to employ if they don&#8217;t have the [...]]]></description>
			<content:encoded><![CDATA[<p><table><tr><td style="text-indent: 25px; background-color: #E5E5E5; padding: 10px; border-top-width: 1px; border-bottom-width: 1px; border-left-width: 7px;border-top-style: solid; border-right-style: solid;border-bottom-style: solid;border-left-style: solid;border-top-color: #B5B5B5; border-right-color: #B5B5B5;border-bottom-color: #B5B5B5; border-left-color: #B5B5B5;border-right-width: 1px;background-image: url(http://www.swharden.com/graphics/layout_2006_08_12/quotes.jpg); background-position: left top; background-repeat: no-repeat;"> <i>This page describes a method of sending data from a microchip to a PC using pulses of data. It&#8217;s an alternative to more traditional serial or USB methods of connectivity.  It&#8217;s not intended as a solution for consumer products, but rather an easy hack for hobbyists to employ if they don&#8217;t have the equipment for other methods.  This method doesn&#8217;t require *any* circuitry, just a sound card. The one built in your computer is fine, but I&#8217;m using a $1.30 USB sound card for simplicity. It boils down to just a single microcontroller pin connected to a PC sound card microphone jack! </i></td></tr></table></p>
<p><b>UPDATE: This story was featured on <a href="http://hackaday.com/2011/07/10/sound-card-microcontrollerpc-communication/" onclick="javascript:urchinTracker ('/outbound/article/hackaday.com');">this post of HackADay.com</a>!</b></p>
<p>This is the finished product ready to send data to a PC:<a href="http://www.SWHarden.com/blog/images/DSCN1532.JPG" onclick="javascript:urchinTracker ('/outbound/article/www.SWHarden.com');"><img src="http://www.SWHarden.com/blog/images/DSCN1532-525x393.jpg" alt="DSCN1532" title="DSCN1532" width="525" height="393" class="alignnone size-medium wp-image-2926" /></a></p>
<p><b>MY PROBLEM:</b> I want to send data from a simple microcontroller to a PC.  While USART and a serial port is the common solution <a href="http://www.swharden.com/blog/2009-05-14-simple-case-avrpc-serial-communication-via-max232/" >like I&#8217;ve done before</a>, it&#8217;s not convenient because it requires a level converter (like a MAX232, about $4), crystal (specific values based on bit and error rate, if you&#8217;re lucky you might have a right value in your junk box), and an archaic PC which actually has a serial port. A usb serial port adapter sounds clever, but many aren&#8217;t supported on Linux, Windows Vista, or Windows 7.  Also, many small chips (most of the ATTiny series) don&#8217;t have built in serial capabilities, so it has to be bit-banged in software! Yuk! The second choice would be USB. This requires a crystal too, zener diodes, and bit-banging the USB protocol with something like <a href="http://www.obdev.at/products/vusb/index.html" onclick="javascript:urchinTracker ('/outbound/article/www.obdev.at');">V-USB</a> since most of the AVR series don&#8217;t have built in USB (do they even make breadbordable DIP chips with USB?). Even so, it requires drivers, custom software, cross-platform frustrations, etc. I know PIC has some 18f series chips with USB, but I don&#8217;t feel like switching architectures just to send a few bytes of data to a PC. FDTI has a <a href="http://www.ftdichip.com/Products/ICs/FT232R.htm" onclick="javascript:urchinTracker ('/outbound/article/www.ftdichip.com');">FT232R</a> chip which is a USB serial port adapter, but it&#8217;s expensive (about $5) and doesn&#8217;t come in dip, so no breadboarding! Sure there are adapter boards, but that just adds the cost. I&#8217;m not excited about a $5 solution for a $1 microcontroller. I even did <a href="http://www.avrfreaks.net/index.php?name=PNphpBB2&#038;file=viewtopic&#038;t=109298" onclick="javascript:urchinTracker ('/outbound/article/www.avrfreaks.net');">a bit of trolling on AVR Freaks</a> to see if anyone could help me out &#8211; just more of the same!</p>
<p><iframe width="480" height="390" src="http://www.youtube.com/embed/I0UEooQH2bw" frameborder="0" allowfullscreen></iframe></p>
<p><b>MY SOLUTION:</b> Send data through the sound card! USB sound cards are $1.30 (shipped) on eBay!  It couldn&#8217;t be simpler.  Send pulses, measure distance between pulses. Short pulses are a zero, longer ones are a 1, and very long pulses are number separators. <strong>A Python solution with PyAudio allows 1 script which will work on Mac, Linux, Windows, etc, and because it calibrates itself, this will work on any chip at any clock rate.</strong>  Data is initiated with calibration pulses so timing is not critical &#8211; the PC figures out how fast the data is coming in. Check it out! (scroll way down for a bidirectional communication solution)</p>
<p><iframe width="480" height="390" src="http://www.youtube.com/embed/WKp0P43uhzY" frameborder="0" allowfullscreen></iframe></p>
<p>Here is a sound card I used for bidirectional communication:<br />
<a href="http://www.SWHarden.com/blog/images/DSCN1466.JPG" onclick="javascript:urchinTracker ('/outbound/article/www.SWHarden.com');"><img src="http://www.SWHarden.com/blog/images/DSCN1466-150x112.jpg" alt="DSCN1466" title="DSCN1466" width="150" height="112" class="alignnone size-thumbnail wp-image-2917" /></a> <a href="http://www.SWHarden.com/blog/images/DSCN1470.JPG" onclick="javascript:urchinTracker ('/outbound/article/www.SWHarden.com');"><img src="http://www.SWHarden.com/blog/images/DSCN1470-150x112.jpg" alt="DSCN1470" title="DSCN1470" width="150" height="112" class="alignnone size-thumbnail wp-image-2918" /></a></p>
<p>Output graph (python and excel) of temperature when I put a soldering iron near the sensor: <a href="http://www.SWHarden.com/blog/images/python1.png" onclick="javascript:urchinTracker ('/outbound/article/www.SWHarden.com');"><img src="http://www.SWHarden.com/blog/images/python1-525x396.png" alt="python" title="python" width="525" height="396" class="alignnone size-medium wp-image-2919" /></a><a href="http://www.SWHarden.com/blog/images/excel.jpg" onclick="javascript:urchinTracker ('/outbound/article/www.SWHarden.com');"><img src="http://www.SWHarden.com/blog/images/excel-525x394.jpg" alt="excel" title="excel" width="525" height="394" class="alignnone size-medium wp-image-2920" /></a></p>
<h2>~ UNIDIRECTIONAL SOLUTION ~</h2>
<p><b>The following code is designed to have a chip send data to your PC automatically.</b> This can be run on any micro-controller (PIC or AVR I guess, the concept is the same) at any clock rate.  Just make sure the sound card is recording fast enough to differentiate pulses. (keep scrolling down for a bidirectional solution)</p>
<p><b>A NOTE ABOUT MY CODE:</b> This is just the code I used for my demonstration. It might be more logical for you to write your own since the concept is so simple. I&#8217;m a dental student, not a programmer, so I&#8217;m sure it&#8217;s not coded very elegantly. I didn&#8217;t work hard to make this code easy to read or easy to share. With that being said, help yourself!</p>
<pre class="prettyprint c">
/*The following code is written in AVR-GCC for an ATTiny44a.
It reads ADC values on 3 pins and reports it each second along
 with a number which increments each time data is sent.
It's designed as a starting point, allowing anyone to
customize it from here!*/

#include &lt;avr/io.h&gt;
#include &lt;avr/delay.h&gt;
#include &lt;avr/interrupt.h&gt;

// bytes we want to send to the PC
volatile int data1=0;
volatile int data2=0;
volatile int data3=0;
volatile int data4=0;

void solid(){  // dont touch
	_delay_ms(1);
	pulse(1);pulse(1);pulse(1);pulse(3);pulse(3);
	pulse(3);pulse(5);pulse(5);// CALIBRATION PULSES
}
void pulse(char size){ // dont touch
	PORTA|=_BV(PA3);
	_delay_us(100);
	PORTA&#038;=~_BV(PA3);
	while (size){size--;_delay_us(100);}
}
void sendVal(unsigned long tosend){ // dont touch
	pulse(5); // send a space
	while (tosend){
		if (tosend&#038;1){pulse(3);} // send ONE
		else {pulse(1);} // send ZERO
		tosend=tosend&gt;&gt;1;
	}
}

int readADC(char adcNum){
	_delay_ms(1);
	ADMUX=adcNum; // select which ADC to read, VCC as ref.
	ADCSRA=0b11000111; // enable, start, 128 prescale
    while (ADCSRA&#038;( 1&lt;&lt;ADSC)) {}; // wait for measurement
	return ADC;
}

void takeReadings(){
        data1=readADC(0); // ADC0
        data2=readADC(1); // ADC1
        data3=readADC(2); // ADC2
		data4++; // incriment just because we want to
}

void sendStuff(){ // EDIT to send what you want
	solid(); //required
	sendVal(12345); //required
	sendVal(12345); //required
	sendVal(54321); //required

	sendVal(data1);
	sendVal(data2);
	sendVal(data3);
	sendVal(data4);

	pulse(1); //required
}

int main(){
	DDRA|=_BV(PA2)|_BV(PA3);
	for (;;){
		_delay_ms(1000);
		takeReadings();
		sendStuff();
	}
	return 0;
}
</pre>
<pre class="prettyprint python">
"""
file name: listenOnly.py

This is the PC code to listen to the microphone and display
and log the data. It probably does NOT need adjustment!
 Make sure the correct sound card is selected (in the code)
 and make sure microphone input is turned up in volume control.

This code is what was used on my PC for the demonstration
video. This is the listenOnly.py file which will turn any audio
 detected from a sound card into data, optionally logging it
(if the last few lines are uncommented). This also works to
capture data for the bidirectional communication method,
described below on this website.

If this is running but no data is coming through, make sure the
microphone is selected as a recording device, the correct sound
card is selected, and the microphone volume is turned to high.

REQUIRED: To run this, you need to have the following installed:
-- Python 2.6
-- numpy for python 2.6
-- matplotlib for python 2.6
-- pyaudio for python 2.6
(other versions may work, but this is what I'm using)
"""
import numpy
import pyaudio
import matplotlib.pyplot as plt
import wave
import time

def listCards(dontAsk=True):
    p=pyaudio.PyAudio()
    print "SOUND CARDS:"
    for i in range(p.get_default_host_api_info()["deviceCount"]):
        if p.get_device_info_by_index(i)["maxInputChannels"]&gt;0:
                cardName = p.get_device_info_by_index(i)["name"]
                cardIndex = p.get_device_info_by_index(i)["index"]
                print "[%d] %s"%(cardIndex,cardName)
    if dontAsk: return
    return int(raw_input("CARD NUMBER TO USE:"))

cardID=1
listCards()
print "USING CARD:",cardID

rate=44100.0
sampleSize=1024

def data2vals(data):
    vals=numpy.array([])
    lastPeak=0
    for i in range(1,len(data)):
        if data[i]==True and data[i-1]==False:
            if lastPeak&gt;0: vals=numpy.append(vals,i-lastPeak)
            lastPeak=i
    return vals

def binary2dec(binary):
    binary=binary[:-1]
    dec=0
    s=""
    for i in range(len(binary)):
        dec=dec*2
        dec+=binary[i]
        s="%d"%binary[i]+s
    #print s,"=",dec #11111100101100000 = 3391
    return dec

def readVals(vals):
    if len(vals)&lt;7: return False
    vals2=[]
    aLow = min(vals[0:3])
    aMed = min(vals[3:6])
    aHigh = vals[6]
    thresh1=sum([aLow,aMed])/2+2
    thresh2=sum([aMed,aHigh])/2+2
    #print "tresholds:",thresh1,thresh2
    #print vals
    vals=vals[8:]
    binary=[]
    for i in range(len(vals)):
        if vals[i]&gt;thresh2:
            vals2.append(binary2dec(binary))
            binary=[]
        if vals[i]&gt;thresh1:binary=[1]+binary
        else:binary=[0]+binary
    vals2.append(binary2dec(binary))
    for i in range(len(vals2)):
        if vals2[i]==54321: return vals2[i+1:]
    return False

def playFile():
    chunk = 1024
    wf = wave.open("short_onenum.wav", 'rb')
    p = pyaudio.PyAudio()
    stream = p.open(format =
                    p.get_format_from_width(wf.getsampwidth()),
                    channels = wf.getnchannels(),
                    rate = wf.getframerate(),
                    output = True)
    data = wf.readframes(chunk)
    while data != '':
        stream.write(data)
        data = wf.readframes(chunk)
    stream.close()

def captureData():
    pyaud = pyaudio.PyAudio()
    stream = pyaud.open(format=pyaudio.paInt16,channels=1,
        rate = 44100,input_device_index=cardID,input=True,output=True)
    sample=numpy.array([])
    while True:
        sampleNew=numpy.fromstring(stream.read(sampleSize),dtype=numpy.int16)
        sampleNew=(sampleNew&lt;-25000)*1
        if True in sampleNew: sample=numpy.append(sample,sampleNew)
        else:
            if len(sample):
                stream.close()
                return sample
    stream.close()

tone_quiet=0

def buildNumber(num=123):

    if num&gt;255: print "NUMBER TOO HIGH!!!"
    #print num,'=',
    num+=1
    for i in [7,6,5,4,3,2,1,0]:
        if num&gt;2**i:one();num=num-2**i;#print"1",
        else: zero();#print"0",
    #print
    space()

def pulse():
    global data
    data+=[-30000]*10

def space():
    global data
    data+=[tone_quiet]*900
    pulse()

def one():
    global data
    data+=[tone_quiet]*600
    pulse()

def zero():
    global data
    data+=[tone_quiet]*300
    pulse()

def silence(msec=1000):
    global data
    data+=[tone_quiet]*int(41.1*msec)

data=[]
def sendAudio(numbers=[11,66,77]):
    global data
    data=[]
    silence(100)
    buildNumber(250)
    print "SENDING",
    for numba in numbers:
        buildNumber(numba)
        print numba,
    buildNumber(250)
    silence(100)
    data=numpy.array(data)
    data=-data
    data=data.tostring()
    print

    p = pyaudio.PyAudio()
    stream = p.open(rate=44100, channels=1, format=pyaudio.paInt16,
                    input_device_index=cardID, output=True)
    stream.write(data)
    stream.close()
    p.terminate()

i=0
while True:
    i+=1
    val=readVals(data2vals(captureData()))
    if val == False: continue
    line=""
    for item in val: line+=str(item)+","
    print i,line
    #f=open('log.csv','a')
    #f.write("%s\n"%line)
    #f.close()
</pre>
<h2>~ BIDIRECTIONAL SOLUTION ~</h2>
<p><b>What if we want to send data TO the microcontroller?</b> The solution is a little more complex, but quite doable.  Just add an extra wire to the sound card&#8217;s speaker output and attach it to PCINT0 (the highest level internal interrupt).  This is intended for advanced users, and if you&#8217;re doing this you probably are better off with USB or serial anyway! &#8230; but heck, why not do it as a proof of concept!</p>
<p><iframe width="480" height="390" src="http://www.youtube.com/embed/fhsYGRdwIaw" frameborder="0" allowfullscreen></iframe></p>
<p>Note that the USB sound card speaker output was not powerful enough to trigger the digital input pin of the AVR, so an inverting buffer was made from a single NPN transistor (2n3904). The hardware interrupt was attacked to the collector, and the collector was attached through +5V through a 220 ohm resistor.  The emitter was grounded.  The base was attached directly to the sound card output. I also tried running the sound card output through a small series capacitor (0.1uF) and biasing the base to ground through a 1Mohm resistor and it worked the same. Hardware, simple. Chip-side software&#8230; a little more complex.</p>
<p>### VIDEO ###</p>
<pre class="prettyprint python">
"""
This code is what was used on my PC for the
 demonstration video. The listenonly.py file
 (above on site) was also used without modification.
"""
import pyaudio
from struct import pack
from math import sin, pi
import wave
import random
import numpy
import time

RATE=44100
maxVol=2**15-1.0 #maximum amplitude
p = pyaudio.PyAudio()
stream = p.open(rate=44100, channels=1, format=pyaudio.paInt16,
		input_device_index=1, output=True)

def pulseZero():
    global wvData
    wvData+=pack('h', 0)*30
    wvData+=pack('h', maxVol)

def pulseOne():
    global wvData
    wvData+=pack('h', 0)*40
    wvData+=pack('h', maxVol)

def pulseSpace():
    global wvData
    wvData+=pack('h', 0)*50
    wvData+=pack('h', maxVol)

def buildNumber(num=123):
    if num&gt;255: print "NUMBER TOO HIGH!!!"
    num+=1
    for i in [7,6,5,4,3,2,1,0]:
        if num&gt;2**i:
            pulseOne()
            num=num-2**i
        else:
            pulseZero()

wvData=""
wvData+=pack('h', 0)*2000
pulseOne() #required before sending data

buildNumber(55)
buildNumber(66)
buildNumber(77)
buildNumber(123)

wvData+=pack('h', 0)*2000

while True:
	print "SENDING",
	stream.write(wvData)
	raw_input()
</pre>
<pre class="prettyprint c">
/*
This code is what was used on my AVR
microcontroller for the demonstration video
*/
#include &lt;avr/io.h&gt;
#include &lt;avr/delay.h&gt;
#include &lt;avr/interrupt.h&gt;

volatile long commandIncoming=0;
volatile char command1=0;
volatile char command2=0;
volatile char command3=0;
volatile char command4=0;
volatile char bitsGotten=0;

// timing thresholds are critical! Send pulses to the chip
// and have it report the time between them. Use this to
// determine the best threshold value for your application.
// The ones here must be changed if you run at a speed other
// than 1mhz or if you use different timings in PC software
#define thresh_low 100 // between this and the next
#define thresh_high 130 // is the range for a logical 'one'

// ######## OUTGOING AUDIO DATA #########
void solid(){
	_delay_ms(1); //LONG LOW
	pulse(1);pulse(1);pulse(1);pulse(3);pulse(3);
	pulse(3);pulse(5);pulse(5);// CALIBRATION PULSES
}
void pulse(char size){
	PORTA|=_BV(PA3);
	_delay_us(100);
	PORTA&#038;=~_BV(PA3);
	while (size){size--;_delay_us(100);}
}
void sendVal(unsigned long tosend){
	pulse(5); // send a space
	while (tosend){
		if (tosend&#038;1){pulse(3);} // send ONE
		else {pulse(1);} // send ZERO
		tosend=tosend&gt;&gt;1;
	}
}

// ######## INCOMING AUDIO DATA #########
// NOTE THAT INPUTS ARE NORMALLY *HIGH* AND DROP *LOW* FOR SIGNAL
SIGNAL (PCINT0_vect) { // audio input trigger
	TIMSK0|=(1&lt;&lt;TOIE1); //Overflow Interrupt Enable
	if (TCNT0&lt;10){return;} // seem too fast? ignore it!
	// Enable the following line to test custom timings
	//command1=command2;command2=command3;
	//command3=command4;command4=TCNT0;
	bitsGotten++;
	commandIncoming=commandIncoming*2; // shift left
	if (TCNT0&gt;thresh_low){commandIncoming++;} // make 1
	TCNT0=0;
}

ISR(TIM0_OVF_vect){ // TIMER OVERFLOW
	if (bitsGotten){sendStuff();}
}

void fillCommands(){
	command1=(char*)(commandIncoming&gt;&gt;24);
	command2=(char*)(commandIncoming&gt;&gt;16);
	command3=(char*)(commandIncoming&gt;&gt;8);
	command4=(char*)(commandIncoming);
}

void sendStuff(){
	TIMSK0=0; //Overflow Interrupt
	cli(); // disable interrupts!
	fillCommands();
	solid(); // start data transmissions with this
	sendVal(12345);
	sendVal(12345);
	sendVal(54321);
	sendVal(command1);
	sendVal(command2);
	sendVal(command3);
	sendVal(command4);
	sendVal(1234567890);
	pulse(1);
	bitsGotten=0;
	sei(); // enable interrupts again!
	TIMSK0|=(1&lt;&lt;TOIE1); //Overflow Interrupt
}

// ######## MAIN PROGRAM #########
int main(){

	DDRA|=_BV(PA2)|_BV(PA3);

	// SET UP FOR SOUND CARD INTERRUPT
	MCUCR = 0b00000010; // trigger interrupt on falling edge
	GIMSK = 0b00010000; // pin change interrupt enable 0
	GIFR =  0b00010000; // flag register, same as above
	PCMSK0 = (1&lt;&lt;PCINT0); // Set Pin to use (PCINT0)
	sei(); // enable global interrupts

	// SET UP 8-bit COUNTER
	TCCR0B|=0b00000010;
	//TCCR1B|=(1&lt;&lt;CS12)|(1&lt;&lt;CS10); // prescaler 1024
	TIMSK0|=(1&lt;&lt;TOIE1); //Enable Overflow Interrupt Enable
	TCNT0=0;//Initialize our varriable (set for 1/15th second?)

	// MAIN PROGRAM
	for (;;){}
	return 0;

}
</pre>
<p><b>In closing</b>, I&#8217;m tickled this works so well.  It&#8217;s funny to me that no one&#8217;s really done this before in the hobby field.  I&#8217;m sure I&#8217;m not the only one who wished there were an easy way to do this. I&#8217;m sure the process could be greatly improved, but this is a fun start. Wow, it&#8217;s late, I should get to bed. I have to treat patients tomorrow morning!</p>
<p><b>PS:</b> If you replicate this concept, let me know about it!  I&#8217;d love to see your project!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.SWHarden.com/blog/2011-07-09-sound-card-microcontrollerpc-communication/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Using Timers and Counters to Clock Seconds</title>
		<link>http://www.SWHarden.com/blog/2011-06-19-using-timers-and-counters-to-clock-seconds/</link>
		<comments>http://www.SWHarden.com/blog/2011-06-19-using-timers-and-counters-to-clock-seconds/#comments</comments>
		<pubDate>Mon, 20 Jun 2011 04:06:41 +0000</pubDate>
		<dc:creator>Scott</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[Circuitry]]></category>
		<category><![CDATA[Microcontrollers]]></category>

		<guid isPermaLink="false">http://www.SWHarden.com/blog/?p=2882</guid>
		<description><![CDATA[My current secret project involves cramming a bunch of features into a single microcontroller. The chip I chose to use is an ATMega48. The ATMega 48 is $1.40 each in small quantities and comes crammed packed with features. The chip will be quite busy performing many functions, but its main loop will be executed at [...]]]></description>
			<content:encoded><![CDATA[<p><b>My current secret project involves cramming a bunch of features into a single microcontroller.</b> The chip I chose to use is an <a href="http://www.swharden.com/blog/images/atmega48pinout.png" >ATMega48</a>. The ATMega 48 is $1.40 each in small quantities and comes crammed packed with features. The chip will be quite busy performing many functions, but its main loop will be executed at least every 50ms (required for USB, did I mention I&#8217;m bit-banging USB?!).  I desire to have a bit of RTC (real time clock) functionality in that I need to precisely measure seconds, although I don&#8217;t need to actually know the time or date. I desire to execute a function once per second, consuming a minimum of resources. The solution was quite simple, but I&#8217;m choosing to document it because it&#8217;s somewhat convoluted in its explanation elsewhere on the net.</p>
<p><b>In summary, the way I accomplished this</b> is using the built-in 16-bit timer (most AVRs have such a timer, including the ATTiny series). If I&#8217;m clocking the microcontroller at a known rate (determined by my selection of crystal, 12 MHz in my case), I can set the chip to continuously increment a register (timer1) and execute a function every time it overflows. Timer1 overflows at 2^16 (65,536).  I enabled a prescaler value of 256 so that it takes 256 clock pulses to increment the timer. 12MHz/256 = 46,875 Timer1 increments each second. Since Timer1 overflows at 65,536, if I initiate Timer1 at 18,661 (65,536-46,875), it will take 1 second exactly to overflow. Upon overflowing, I do something (maybe flip a LED on or off), and reset the Timer1 back to its starting value 18,661. Done! Without using an external RTC module or even an external crystal or asynchronous timer, we managed to execute a function every second on the second with minimal overhead, allowing the chip to do everything it wants in the rest of the time!</p>
<p><b>The following example</b> is a little more specific, executing a function exactly 15 times a second, and executing another function (to flash an LED) exactly every 1 second. It should be self explanatory:</p>
<pre class="prettyprint c">
// This function is called every second on the second
volatile int count; // this should be global
ISR(TIMER1_OVF_vect){
	TCNT1=62411;//Initialize our varriable (set for 1/15th second)
	count++; //increment 1/15th second counter
	if(count==15){
		statusTOGGLE(); // do your event (flash a LED in my case)
		count=0;//reset global variable
		}
	}
</pre>
<pre class="prettyprint c">
// This is for ATMega48, consult datasheet for variations for different chips
// place this just inside main(), before your primary loop
TCCR1B|=(1&lt;&lt;CS12);// prescaler 256
TIMSK1|=(1&lt;&lt;TOIE1); //Enable Overflow Interrupt Enable
TCNT1=62411;//Initialize our varriable (set for 1/15th second)
count=0; //Initialize a global variable
sei(); // enable interrupts
</pre>
<p><b>I&#8217;m having a lot of fun</b> spending time going through the datasheet of this chip. It has a lot of features, and some I didn&#8217;t really dig deeply into. Without giving away too much of my project, I&#8217;ll show some photos I&#8217;m excited to share. My project interfaces the PC through USB directly attached to 2 pins using no intermediate chips (wow!). The photos demonstrate various steps in the temperature measurement and calibration tests&#8230;<br />
<a href="http://www.SWHarden.com/blog/images/DSCN1367.JPG" onclick="javascript:urchinTracker ('/outbound/article/www.SWHarden.com');"><img src="http://www.SWHarden.com/blog/images/DSCN1367-525x393.jpg" alt="DSCN1367" title="DSCN1367" width="525" height="393" class="alignnone size-medium wp-image-2885" /></a><br />
<a href="http://www.SWHarden.com/blog/images/DSCN1372.JPG" onclick="javascript:urchinTracker ('/outbound/article/www.SWHarden.com');"><img src="http://www.SWHarden.com/blog/images/DSCN1372-525x393.jpg" alt="DSCN1372" title="DSCN1372" width="525" height="393" class="alignnone size-medium wp-image-2886" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.SWHarden.com/blog/2011-06-19-using-timers-and-counters-to-clock-seconds/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

