Files
SyncHome/trunk/workspace/00_Lib/Serial/Rs-232 Serial on Atmega 48, 88, 168.htm
topicchi 0b6afad8fe
2023-09-22 06:03:48 +00:00

213 lines
13 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<!-- saved from url=(0078)http://tom-itx.dyndns.org:81/~webpage/how_to/atmega168/mega168_rs232_index.php -->
<HTML lang=en xml:lang="en" xmlns="http://www.w3.org/1999/xhtml"><HEAD><TITLE>Rs-232 Serial on Atmega 48, 88, 168</TITLE>
<META content="text/html; charset=utf-8" http-equiv=Content-Type><!-- -->
<META name=Keywords
content="PS3, PSGroove, LUFA, article,articles,atmega,atmega32,atmega48,atmega88,atmega168,atmega128,atmel,atxmega,attiny,attiny10,avr,avrisp,avrispmkii,avrisp mkii,avrispmk2,basic,beginner,beginner's guide,bot,demo,electronic,electronics,howto,how to,isp,learn,lessons,manual,mega,mega32,mega48,mega88,mega168,mega128,mkii,mk2,noob,pdi,primer,program,programmer,robot,robotics,samples,school,source,source code,strr_ukr,bravekit,tpi,tiny,tiny10,rst, rstdisbl, fuse recovery,tips,tom_l, tom_itx,tutorial,tutorials,usbtiny,usbtinymkii,usbtiny mkii,usb tiny mkii,usbtiny mk2, usb tiny mk2,usb,xmega,scope,toaster,oven,toaster oven,sherline"><LINK
rel=stylesheet type=text/css
href="Rs-232%20Serial%20on%20Atmega%2048,%2088,%20168-Dateien/style-layout.css"><LINK
rel=stylesheet type=text/css
href="Rs-232%20Serial%20on%20Atmega%2048,%2088,%20168-Dateien/style-presentation.css"
media=screen>
<META name=GENERATOR content="MSHTML 8.00.7601.18283"></HEAD>
<BODY>
<DIV id=outer><!-- INCLUDE THE HEADER .PHP FILE HERE -->
<DIV id=hdr><!-- <font size="+1 font-family: Arial"><big><big>MY PROJECTS</big></big> &nbsp; &nbsp; &nbsp; &nbsp;</font> -->
<DIV align=center><FONT size=6>MY PROJECTS</FONT></DIV>
<DIV align=center><FONT size=4>robotics - electronics - machining</FONT></DIV><!-- <div align="center"> <font size="4em font-family: Arial align="center" >robotics - electronics - machining</font></div> --></DIV><!-- INCLUDE THE TOP BAR .PHP FILE HERE -->
<DIV id=bar>&nbsp;<SPAN
style="PADDING-BOTTOM: 5px; PADDING-LEFT: 5px; PADDING-RIGHT: 5px; FONT-SIZE: 11px; PADDING-TOP: 5px">
Wednesday November 27th 2013 06:53:31 AM<BR></SPAN></DIV><!-- end bar -->
<DIV id=bodyblock align=right>
<DIV id=l-col align=left><!-- INCLUDE THE TEMPERATURE .PHP FILE HERE --><!-- <div id="l-col" align="left"> -->
<DIV align=center><!-- <font size="1pt" align="center" color="white">Outdoor: --><FONT
color=white size=1>Outdoor: 32.0 (F) / 0.0 (C) <BR>&nbsp;&nbsp;&nbsp;Indoor:
32.0 (F) / 0.0 (C) </FONT></DIV><!-- </div> -->
<H4 align=center>How To</H4>
<UL class=cats>
<LI onclick="location='/~webpage/index.php'"><A class=cats
title="My Project Page"
href="http://tom-itx.dyndns.org:81/~webpage/index.php">Main HomePage</A></LI>
<LI></LI>
<LI
onclick="location='/~webpage/how_to/atmega168/mega168_howto_main_index.php'"><A
class=cats title="How To Series"
href="http://tom-itx.dyndns.org:81/~webpage/how_to/atmega168/mega168_howto_main_index.php">Main
HowTo Page</A></LI>
<LI
onclick="location='/~webpage/how_to/atmega168/mega168_led_blink_delay_index.php'"><A
class=cats title="Atmega 168 led blink using delay"
href="http://tom-itx.dyndns.org:81/~webpage/how_to/atmega168/mega168_led_blink_delay_index.php">LED
Blink - Delay</A></LI>
<LI
onclick="location='/~webpage/how_to/atmega168/mega168_led_blink_int_index.php'"><A
class=cats title="Atmega 168 led blink using interrupt"
href="http://tom-itx.dyndns.org:81/~webpage/how_to/atmega168/mega168_led_blink_int_index.php">LED
Blink - Int.</A></LI>
<LI onclick="location='/~webpage/how_to/atmega168/mega168_pwm_index.php'"><A
class=cats title="Atmega 168 PWM"
href="http://tom-itx.dyndns.org:81/~webpage/how_to/atmega168/mega168_pwm_index.php">PWM
Output</A></LI>
<LI onclick="location='/~webpage/how_to/atmega168/mega168_rs232_index.php'"><A
class=cats title="Atmega 168 RS232"
href="http://tom-itx.dyndns.org:81/~webpage/how_to/atmega168/mega168_rs232_index.php">RS232
Output</A></LI>
<LI onclick="location='/~webpage/how_to/atmega168/mega168_adc_index.php'"><A
class=cats title="Atmega 168 ADC"
href="http://tom-itx.dyndns.org:81/~webpage/how_to/atmega168/mega168_adc_index.php">A/D
Conversion</A></LI>
<LI onclick="location='/~webpage/how_to/atmega168/coming_soon_index.php'"><A
class=cats title="Atmega 168 PWM Control"
href="http://tom-itx.dyndns.org:81/~webpage/how_to/atmega168/coming_soon_index.php">A/D
PWM Control</A></LI>
<LI></LI>
<LI onclick="location='/~webpage/how_to/atmega168/using_avrdude_index.php'"><A
class=cats title="Using Avrdude"
href="http://tom-itx.dyndns.org:81/~webpage/how_to/atmega168/using_avrdude_index.php">Using
Avrdude</A></LI></UL>
<H4 align=center>Links</H4>
<UL class=cats>
<LI onclick="location='/~webpage/avr/c_bits/bits_index.php'"><A class=cats
title="C Bit Manipulation"
href="http://tom-itx.dyndns.org:81/~webpage/avr/c_bits/bits_index.php">C Bit
Manipulation</A></LI>
<LI onclick="location='/~webpage/avr_links_index.php'"><A class=cats
title="AVR Links"
href="http://tom-itx.dyndns.org:81/~webpage/avr_links_index.php">AVR
Info</A></LI>
<LI onclick="location='/~webpage/pcboard_links_index.php'"><A class=cats
title="PC Board Related Links"
href="http://tom-itx.dyndns.org:81/~webpage/pcboard_links_index.php">Making PC
Boards</A></LI>
<LI onclick="location='/~webpage/robotic_links_index.php'"><A class=cats
title="Robotic Related Sites"
href="http://tom-itx.dyndns.org:81/~webpage/robotic_links_index.php">Robotic
Sites</A></LI></UL>
<P><!-- <a href="http://validator.w3.org/check?uri=referer"><img
src="http://www.w3.org/Icons/valid-xhtml10-blue"
alt="Valid XHTML 1.0 Transitional" height="31" width="88" /></a> --></P></DIV><!-- end l-col -->
<DIV id=cont>
<H2 class=title align=center>Rs-232 Serial on Atmega 48, 88, 168</H2>
<P>Things you will need:</P>
<TABLE
style="TEXT-ALIGN: left; WIDTH: 90%; MARGIN-LEFT: auto; MARGIN-RIGHT: auto"
border=0>
<TBODY>
<TR>
<TD width="25%"><A
href="Rs-232%20Serial%20on%20Atmega%2048,%2088,%20168-Dateien/breadboard_basic_side.jpg"><IMG
style="BORDER-BOTTOM: 0px solid; BORDER-LEFT: 0px solid; WIDTH: 130px; HEIGHT: 101px; BORDER-TOP: 0px solid; BORDER-RIGHT: 0px solid"
alt=""
src="Rs-232%20Serial%20on%20Atmega%2048,%2088,%20168-Dateien/breadboard_basic_side.jpg"
width=2202 height=1698></A><A
href="http://tom-itx.dyndns.org:81/~webpage/how_to/atmega168/walwart_switching.jpg"><BR></A></TD>
<TD style="WIDTH: 25%">The breadboard setup as shown on the main page</TD>
<TD width="25%"><A
href="Rs-232%20Serial%20on%20Atmega%2048,%2088,%20168-Dateien/rs232_dongle.jpg"><IMG
style="WIDTH: 138px; HEIGHT: 100px" border=2 alt=""
src="Rs-232%20Serial%20on%20Atmega%2048,%2088,%20168-Dateien/rs232_dongle.jpg"></A></TD>
<TD width="25%">A RS-232 level converter</TD></TR>
<TR>
<TD width="25%"></TD>
<TD width="25%"></TD>
<TD width="25%"></TD>
<TD width="25%"></TD></TR></TBODY></TABLE>* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * *
<P>If a RS-232 dongle isn't available, wire up a MAX232 next to the Atmega chip
on the breadboard following the schematic below. For that you will need a MAX232
chip, 5 1uf capacitors and a female DB9 connector.</P>
<P style="TEXT-ALIGN: center"><A
href="Rs-232%20Serial%20on%20Atmega%2048,%2088,%20168-Dateien/rs232_sch.png"><IMG
style="BORDER-BOTTOM: 0px solid; BORDER-LEFT: 0px solid; WIDTH: 372px; HEIGHT: 279px; BORDER-TOP: 0px solid; BORDER-RIGHT: 0px solid"
alt=""
src="Rs-232%20Serial%20on%20Atmega%2048,%2088,%20168-Dateien/rs232_sch.png"></A></P>
<P>1) Connect the +5v and GND leads from the RS232 dongle to the breadboard
regulated supply.&nbsp;</P>
<P>2) Connect the TX wire from the RS232 dongle to the RX (pin2) of the
Atmega.</P>
<P>3) Connect the RX wire from the RS232 dongle to the TX(pin3) of the
Atmega.</P>
<P>The basic setup should look similar to the images below.</P>
<P>&nbsp;&nbsp;<A
href="Rs-232%20Serial%20on%20Atmega%2048,%2088,%20168-Dateien/rs232_side.jpg"><IMG
style="BORDER-BOTTOM: 0px solid; BORDER-LEFT: 0px solid; WIDTH: 265px; HEIGHT: 145px; BORDER-TOP: 0px solid; BORDER-RIGHT: 0px solid"
alt=""
src="Rs-232%20Serial%20on%20Atmega%2048,%2088,%20168-Dateien/rs232_side.jpg"></A>&nbsp;&nbsp;
<A
href="Rs-232%20Serial%20on%20Atmega%2048,%2088,%20168-Dateien/rs232_top.jpg"><IMG
style="BORDER-BOTTOM: 0px solid; BORDER-LEFT: 0px solid; WIDTH: 240px; HEIGHT: 145px; BORDER-TOP: 0px solid; BORDER-RIGHT: 0px solid"
alt=""
src="Rs-232%20Serial%20on%20Atmega%2048,%2088,%20168-Dateien/rs232_top.jpg"></A></P>
<P>The ISP programmer hookup can be seen at the rear.</P>
<P>4) Download the&nbsp;sample code and load it into the Atmega using your
favorite download software. The specifics of programming the Atmega are not
covered here since there are several different ways to do it.</P><BR>
<P>5) Program the fuse settings as follows: <BR>lfuse = 0x62, hfuse = 0xDF,
efuse = 0xF9</P>
<P>6) Connect the MAX232 to the serial port on your pc and start up a terminal
program with the settings of 2400 8 N 1 and power up the Atmega.</P>
<P>You should see this on your terminal if it was successful:</P>
<P style="TEXT-ALIGN: center"><A
href="Rs-232%20Serial%20on%20Atmega%2048,%2088,%20168-Dateien/hyperterm.jpg"><IMG
style="BORDER-BOTTOM: 0px solid; BORDER-LEFT: 0px solid; WIDTH: 430px; HEIGHT: 283px; BORDER-TOP: 0px solid; BORDER-RIGHT: 0px solid"
alt=""
src="Rs-232%20Serial%20on%20Atmega%2048,%2088,%20168-Dateien/hyperterm.jpg"></A></P>
<P>C Example Code Section</P>
<BLOCKQUOTE><SPAN class=c5><!-- *********************************** -->/*
RS232 Test<BR>&nbsp;For an Atmega 48 88 or 168<BR><BR>Atmega168 DIP TX PD1
(pin3) <BR>Atmega168 DIP RX PD0 (pin2) */<BR><BR>#define F_CPU <SPAN
class=c2>1000000UL</SPAN> <BR>#define UBRR_1200 <SPAN class=c2>51</SPAN>
<BR>#define UBRR_2400 <SPAN class=c2>25</SPAN> <SPAN class=c3>&nbsp; &nbsp; //
for 1Mhz </SPAN><BR><BR>// #define UBRR_2400 <SPAN class=c2>207</SPAN> <SPAN
class=c3>// for 8Mhz with .2% error</SPAN> <BR>// #define UBRR_9600 <SPAN
class=c2>51</SPAN> <SPAN class=c3>&nbsp;// for 8Mhz with .2% error
</SPAN><BR>// #define UBRR_19200 <SPAN class=c2>25</SPAN> <SPAN class=c3>//
for 8Mhz with .2% error</SPAN> <BR><BR><SPAN class=c1>#include</SPAN>
&lt;avr/io.h&gt;<BR><SPAN class=c1>#include</SPAN>
&lt;util/delay.h&gt;<BR><BR><SPAN class=c3>/* Initializes the USART (RS232
interface) */</SPAN><BR>&nbsp;<BR><SPAN class=c2>void</SPAN> USART_init(
unsigned int ubrr ) <BR>{ <BR>UBRR0H = (unsigned char)(ubrr&gt;&gt;<SPAN
class=c2>8</SPAN>);<BR>UBRR0L = (unsigned char)ubrr; <BR>UCSR0B = (<SPAN
class=c2>1</SPAN> &lt;&lt; TXEN0);&nbsp;&nbsp;&nbsp; <SPAN class=c3>&nbsp;//
Enable RX, TX &amp; RX interrupt</SPAN> <BR>UCSR0C = (<SPAN class=c2>3</SPAN>
&lt;&lt; UCSZ00);&nbsp;&nbsp; <SPAN class=c3>&nbsp;//asynchronous 8 N
1</SPAN><BR>} <BR><BR><SPAN class=c3>/* Send some data to the serial port
*/</SPAN><BR><BR><SPAN class=c2>void</SPAN> USART_tx_string( char *data
)<BR>{<BR><SPAN class=c1>while</SPAN> ((*data != '\<SPAN
class=c2>0</SPAN>'))<BR>&nbsp; &nbsp;{<BR>&nbsp; &nbsp; &nbsp; while (!(UCSR0A
&amp; (<SPAN class=c2>1</SPAN> &lt;&lt;UDRE0)));<BR>&nbsp;&nbsp; &nbsp;
&nbsp;UDR0 = *data;<BR>&nbsp;&nbsp; &nbsp;
&nbsp;data++;<BR>&nbsp;&nbsp;&nbsp;}&nbsp; &nbsp;<BR>}<BR><BR><SPAN
class=c3>/*&nbsp; MAIN */</SPAN><BR><BR><SPAN class=c2>int</SPAN>
main(void)<BR>{<BR><BR>USART_init(UBRR_2400);<BR><BR>_delay_ms(<SPAN
class=c2>250</SPAN>);<BR>USART_tx_string("Connected!\r\n");<BR>_delay_ms(<SPAN
class=c2>250</SPAN>);<BR><BR>while (<SPAN class=c2>1</SPAN>)<SPAN
class=c3>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp; &nbsp;// do&nbsp;until finished or broken</SPAN><BR>&nbsp;
&nbsp;{<BR>&nbsp;&nbsp; &nbsp;
&nbsp;USART_tx_string("U");&nbsp;&nbsp;&nbsp;<SPAN class=c3>//show me the
test</SPAN><BR>&nbsp;&nbsp; &nbsp; &nbsp;_delay_ms(<SPAN
class=c2>250</SPAN>);&nbsp;&nbsp; &nbsp;<SPAN class=c3>&nbsp;&nbsp;&nbsp;
&nbsp;// wait .25 seconds</SPAN><BR>&nbsp;&nbsp; &nbsp;
&nbsp;USART_tx_string("T");<BR>&nbsp;&nbsp; &nbsp; &nbsp;_delay_ms(<SPAN
class=c2>250</SPAN>);&nbsp;&nbsp; &nbsp;<SPAN class=c3>&nbsp;&nbsp;&nbsp;
&nbsp;// wait .25 seconds</SPAN><BR>&nbsp;&nbsp; }
<BR>}&nbsp;<BR></SPAN></BLOCKQUOTE>
<P><A href="http://tom-itx.dyndns.org:81/~webpage/avr/rs232_test/">C Source code
for this project.</A></P><BR><BR></DIV><!-- cont --></DIV><!-- end bodyblock --><!-- INCLUDE THE FOOTER .PHP FILE HERE -->
<DIV id=ftr align=center>Copyright © Tom_L 2009-2012</DIV></DIV><!-- outer -->
<SCRIPT type=text/javascript>
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-19811816-1']);
_gaq.push(['_trackPageview']);
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</SCRIPT>
</BODY></HTML>