Using the UART for RS232 communication to a host PCDr Nathan Scott & Dr Hiroyuki Kagawa July 2002 You should now have an AVR set up on a breadboard. In this section I will lead you through the steps to establish RS232 communication with a host PC (for example a Windows(TM) computer). This is a very useful technique because it means you can communicate with your AVR using a terminal program such as HyperTerminal or VersaTerm. This means you can use your Windows keyboard to control what the AVR does, and see output from the AVR on your computer screen. |
ホストPCとRS232で通信するためのUARTの使い方ブレッドボードにこれまでやったようにAVRを取り付けておく。ここでは, RS232でパソコン(例えばWindows(TM) computer)と通信する方法について学ぶ。これは非常に役立つ技術であり,HyperTerminalやVersaTermのようなターミナルソフトを使ってAVRと通信できるようになる。WindowsパソコンのキーボードによってAVRに何をさせるかを制御したり,AVRからの出力をコンピュータ画面に表示したりできることを意味している。 |
RS232 vs TTL/CMOSRS232 is an old communication standard, invented in 1969. It has a few peculiarities that we have to cope with. In particular the signal in an RS232 cable is sent as pulses of +12V and -12V. This is not inherently compatible with the voltages in a modern microprocessor such as the AVR, which are 0V and 5V (CMOS levels). In other words, the AVR can't just plug into an RS232 port on a Windows(TM) computer, it would not work and it might damage the AVR. Fortunately we do not have to build an adapting circuit, we can just buy one. The one I have been using is the MAX232 from Maxim Co. There is a data sheet in my collection, however you may find it difficult to locate the section of the data sheet that is about the MAX232, so here is the most important diagram: |
RS232とTTL/CMOSRS232は1969年に発明された非常に古い通信方式であり,いくつかの制約がある。特に,RS232ケーブル中の信号電圧は+12Vと-12Vのパルスとして伝送される。これは,AVRのような最近のマイクロプロセッサーにおける電圧(CMOSレベルなら0Vと5V)とは本質的に互換性がない。言い換えれば,Windows(TM)コンピュータのRS232ポートに接続しただけではAVRが動作せず,AVRを破壊する可能性がある。 RS232を使うためには,都合のいいことに変換回路を作成する必要はなく,ひとつの部品を購入するだけでよい。マキシム社のMAX232を使うのも一つの方法である。データシート集の中からMAX232に関するものを見つけづらいかもしれないので,ここでは最も重要である概略図を示す。 |

Figure 1 MAX232 pinout diagram, from the data sheet.
図1 MAX232のピン配置図(データシートを参照)
|
For a long time I thought the "232" in the name "MAX232" referred to RS232. However it seems this is just a coincidence. Maxim Co. make many adapter chips for RS232 voltages e.g. MAX220, MAX231 etc. The MAX232 chip has a very useful feature. It generates the +12V and -12V RS232 line levels from a single +5V supply using internal charge pumps. A charge pump is a clever circuit that can create a high voltage from a low voltage. This function is also sometimes called a "voltage inverter". The charge pumps require some external capacitors which are shown in Figure 1.
|
長い間,「MAX232」の「232」は「RS232」に関係していると思っていたが,どうも偶然の一致だったようである。マキシム社はRS232用のチップ(例えばMAX220, MAX231など)を作っている。 MAX232チップはとても便利な機能をもっている。内臓の昇圧器を使って+5V単一電源から+12Vと-12VのRS232レベルの電圧を生成する。昇圧器は低い電圧から高い電圧を生成できる巧妙な電子回路である。この機能は「変圧器」と呼ばれることもある。昇圧器には図1のようにいくつかの外部コンデンサーが必要になる。
|
RS232 plug for host computerThe RS232 communication standard has been implemented several times over the years using different physical plugs. We will use one of the most common, the 9-pin version, but for the curious, here are some others. Note that a female plug is one with holes in it rather than little pins. |
ホストコンピュータと接続するためのRS232プラグ
|

Figure 2: 9-pin RS232 plug, MAX232 adapter and AVR wiring diagram for RS232 serial communications
図2 RS232シリアル通信のための9ピンRS232プラグ,MAX232アダプターおよびAVRの配線図
|
|
Crystal speedWhen I first used the AVR I always bought an 8MHz crystal, since that was the maximum speed. However I soon found that 8MHz is not always the best choice. The AVR generates the baud rate for the UART meaning the number of bits sent or received per second by dividing the main crystal speed. There is a register called UBRR that contains the dividing integer (well not exactly but this definition will do for now). It follows that only certain baud rates can be generated from a given crystal speed. The usual RS232 baud rates are 2400 bits per second (baud) So we have to try to achieve one of these speeds. A small amount of error is acceptable but if the AVR generates a baud rate that is more than 1% different from one of those rates, there are going to be communication problems. The data sheet for the AVR shows how to calculate the baud rate from the crystal speed and the UBRR ratio. There is also a helpful table (table 25) that gives pre-calculated values for UBRR. If an 8MHz crystal is used, the only standard baud rate that we can generate (with acceptable error) is 38400. That is OK but is a bit limiting. What if you want some other speed? As usual, someone has already thought of this problem. The solution is to choose the right crystal frequency. If you can obtain a 7.3728 MHz crystal, you can generate any standard baud rate. Unfortunately the usual hobby electronics stores do not normally carry this speed and you may need to buy this part from a professional shop such as RS components or Farnell. |
水晶発振子の周波数
2400 bps (ボー) であり,上記のいづれかの速度に設定する必要がある。多少の誤差は許容されるが,1%以上になると通信トラブルを生じる。
|
Software for AVRThe AVR is a general purpose microprocessor and, like a human baby, it is very smart but does not initially know much. It has hardware to support fast communication (called a UART) but we have to write a small amount of software to read and write data to the UART. |
AVRのためのソフトウェア
|
/*
Code for a crash course in AVR programming
Example "rs232.c"
Dr Nathan Scott, July 2002
This program shows how to use interrupt-driven UART code
to establish RS232 communication with a Windows(TM) host
*/
#include "environment.h"
#include "delay.h"
#include "uart.h"
// define names for important numbers.
// It is easier to work with the names.
#define LED_pin 0
void prompt(char p)
{
putch('\r'); putch('\n'); putch(p);
}
void Idle()
// This is the code which is called while we are waiting for a character
// to arrive from the terminal host. It means that nothing in particular
// is happening on the RS232 line and we can use this time to do something
// else.
// Do not do lengthy processing here, return as soon as you can, so that
// data sent from the terminal is not lost.
{
}
void ObeyCommand(char c)
{
char i;
switch (c)
{
case '0': // turn LED off
putstr("off\r\n");
PORTC &= ~BIT(LED_pin);
break;
case '1': // turn LED on
putstr("on\r\n");
PORTC |= BIT(LED_pin);
break;
case '?': // about
putstr("AVR ready\r\n");
// flash the LED a few times
for (i = 0; i < 3; i++)
{
PORTC |= BIT(LED_pin);
DelayMs(20);
PORTC &= ~BIT(LED_pin);
DelayMs(20);
}
break;
} // end switch
}
void main()
{
char c = 0;
// set up hardware UART for serial communications. This is where we
// specify the baud rate. See uart.h for more info.
InitUART(UBRR38400);
// set up control over PORTC so we can use it to flash the LED
PORTC = 0; // put a definite start value into the register for PORTC
// - it's best not to leave this to chance
DDRC |= BIT(LED_pin); // set the direction of one pin of PORTC to
// output mode so it can drive the LED.
// initialisation is done, it is time to "flip the switch" by
// setting the Global Interrupt Enable flag
SEI();
// from this point on, an interrupt can happen at any time.
// show a welcome string on the terminal
ObeyCommand('?'); // show "about" string
while (c != 'x') // note that we can leave the "endless" loop!
{
prompt('>');
c = ReceiveByte(); // calls Idle() while nothing is happening
putch(c); // echo (send back to the terminal)
putch(' '); // separate response from echoed command
ObeyCommand(c);
}
putstr("exit\r\n");
// OK, so what happens here? Run the program and find out!
}
|
Figure 3 Example program that uses the interrupt-driven UART library and can respond to some commands from the host
図3 プログラム例(割込み駆動UARTライブラリの利用とホストからのコマンドへの対応)
|
|
Exercises
|
演習
|