- 追加された行はこの色です。
- 削除された行はこの色です。
[[FrontPage]]
2008/10/20 からのアクセス回数 &counter;
#contents
AVRlibを使った最初の例は、RS232C経由でデバッグプリントを出力する時に
役に立つUART通信です。
** ブレッドボートの回路 [#zd056844]
使用するチップは、DIP28ピンのAtmega88です。
ATmega88を選択した理由は、SPOTの外部CPUとして使用されているからです。
#ref(8.jpg);
のように結線します。EXO-3は12MHzを取得するために使用します。
従って、lower fuseは、0xc0です。
AE-UM232RのVOUTは使用せず、GNDのみをAtmega88のGNDと接続しました。
** メインプログラム [#o212dbb0]
最初はなんでも、サンプルのコピーからはじめます。
UARTでもそれにならって、avrlib/examples/rprintfをコピーして使いました。
不要な部分をすべて削除して、必要最低限にしたuarttest.cは、以下の通りです。
#pre{{
#include "global.h" // include our global settings
#include "uart.h" // include uart function library
#include "rprintf.h" // include printf function library
int main(void)
{
uartInit();
uartSetBaudRate(9600);
rprintfInit(uartSendByte);
rprintf("Hello World\n");
return 0;
}
}}
となります。
- uartInitでUARTを初期化
- uartSetBaudRateでボーレートの設定
- rprintfInitでByte出力関数を渡す
- rprintfは、printfの代わり
*** makefileの変更 [#sa500762]
makefileの変更は、
- MCUをatmega88
- TRGをuartetst
にします。
最終的なmakefileは以下のようになります。
#pre{{
MCU = atmega88
TRG = uarttest
#put your C sourcefiles here
SRC = $(AVRLIB)/buffer.c $(AVRLIB)/uart.c $(AVRLIB)/rprintf.c $(TRG).c
#put additional assembler source file here
ASRC =
#additional libraries and object files to link
LIB =
#additional includes to compile
INC =
#assembler flags
ASFLAGS = -Wa, -gstabs
#compiler flags
CPFLAGS = -g -Os -Wall -Wstrict-prototypes -I$(AVRLIB) -Wa,-ahlms=$(<:.c=.lst)
#linker flags
LDFLAGS = -Wl,-Map=$(TRG).map,--cref
########### you should not need to change the following line #############
include $(AVRLIB)/make/avrproj_make
###### dependecies, add any dependencies you need here ###################
buffer.o : buffer.c buffer.h
uart.o : uart.c uart.h global.h
uart2.o : uart2.c uart2.h global.h
rprintf.o : rprintf.c rprintf.h
a2d.o : a2d.c a2d.h
timer.o : timer.c timer.h global.h
pulse.o : pulse.c pulse.h timer.h global.h
lcd.o : lcd.c lcd.h global.h
i2c.o : i2c.c i2c.h global.h
spi.o : spi.c spi.h global.h
swpwm.o : swpwm.c swpwm.h global.h
servo.o : servo.c servo.h global.h
swuart.o : swuart.c swuart.h global.h
tsip.o : tsip.c tsip.h global.h
nmea.o : nmea.c nmea.h global.h
vt100.o : vt100.c vt100.h global.h
gps.o : gps.c gps.h global.h
$(TRG).o : $(TRG).c global.h
avrdude: $(TRG).hex
avrdude -c avrispmkII -P usb -p $(MCU) -U flash:w:$(TRG).hex
lfuse:
avrdude -c avrispmkII -P usb -p $(MCU) -u -U lfuse:w:0xc0:m
}}
** 動作確認 [#h5c80641]
ターミナルからjermを起動し、電源を入れると
#pre{{
$ jerm -b 9600 -p none -s 1 -d 8 /dev/cu.usbserial-A5002yHm
途中省略
Hello World
}}
と出力されました。
とても簡単にUART通信ができました。
** コメント [#h05b5080]
この記事は、
#vote(おもしろかった,そうでもない,わかりずらい)
#vote(おもしろかった[1],そうでもない[0],わかりずらい[0])
皆様のご意見、ご希望をお待ちしております。
#comment_kcaptcha