You are on page 1of 79

MY EXPERIENCE IN PROGRAMMING AVR MICROCONTROLLER USING WINAVR/AVRGCC by BIBIN JOHN

Signatu re Not Verified

Bibin John

Digitally signed by Bibin John DN: cn=Bibin John, c=IN Date: 2006.04.28 11:08:52 Z Reason: I am the author of this document Location: Ernakulam

NOTICE This book is for newbies fo ! o" #$$in" AVR $i% o%on& o''e (sin" WinAVR. This is mainly for those who want to make robots using Atmel's AVR.Here i am writing this because I want to include troubleshooting tips and simple working examples for newbies. The main reason for choosing AVR microcontroller is because of its facilities and support from inux compilers and cheap cost programmers. Here i am using !inAVR and "#$ AVR$%$& programmer. All the codes in this book are tested with !inAVR'((). "efore starting this you should read my other books*it will be good+ because I refer to that books for some parts.All books are a,ailable on )#*oo " o(!+boo,sb)bibin I thank my teachers of -..IT and my colleagues Anil /.--0.1ra,een /umar Reddy2 -. #atish2Vigith -aurice2 3 Ra4ashekhar Reddy2Athul #ona2#an4o #ebastin . #o i feel this book can guide you through troubleshooting AVR microcontrollers. I thank my teachers of TH## -uttom2 Thodupu5ha from where i learned the ,ery basics of electronics.If you find any problems put a post in yahoo groupbooksbybibin. I am pro,iding this book free of cost. 6ou can use this book as a study material. I don't want my book to be used as a material for business without my permission. 6ou are not allowed to upload this book in any other sites. e7mail8n4bibin9yahoo.co.in website8www.geocities.com:n4bibin forums for AVR microcontrollers8 www.a,rfreaks.net www.a,rbeginners.net !ww.roboticsindia.net www.edaboard.com 6ahoo group8 a,rclub a,rbooks maga5ine8;ircuit ;ellar "I"I. 30H.

IN.EX !hy AVR<<<..................................................................................................= As a beginner what i need to know???....................................................= Softwares Required.....................................................................................= Hardwares Required...................................................................................= Difference between atmega32 and atmega32 .......................................= !"##$%! S#AR#"D.................................................................................) "#&S S#AR#...............................................................................................) -akefile.........................................................................................................> 'hat you had done in first program??...................................................?= (R)!RA**"R HARD'AR"...............................................................?= +)**)% "RR)RS $% A,R (R)!RA**$%!..............................'( #R)-. "SH))#$%!.............................................................................'? *)S# $*()R#A%# #$(.........................................................................''

FUSE BITS..................................................................................................'@

S"##$%!S /)R "0#"R%A +R1S#A A%D 2#A! D$SA. ".....') $*()R#A%# %)#"S )% /-S" .$#S..................................................'A

D$SA. $%! 2#A!................................................................................'A


D$SA. $%! 2#A! A%D -S$%! $%#"R%A 3*h4 + )+5..........'> $%#"R/A+$%! +D '$#H A,R...........................................................B( S)*" $*()R#A%# +)**A%DS -S"D /)R (R)!RA**$%!.............................................................................B@ 263D $%#"R/A+$%! '$#H A,R........................................................B= .$() AR S#"(("R *)#)R DR$,$%! -S$%! A,R.....................BC -%$() AR S#"(("R *)#)R $%#"R/A+$%!................................@? 5"1 "%+)D"R $%#"R/A+$%!789622:................................................@B 8;S"!*"%# D$S( A1 $%#"R/A+$%! '$#H A,R.........................@) *- #$( "0"D 8;S"!*"%# D$S( A1S.............................................@C $+ #"S#"R....................................................................................................@> -AR# of A,R..............................................................................................=? #roub<eshooting...............................................................................=? S"R$A ()R# )/ +)*(-#"R..............................................................=' .$)S S"##$%!S.........................................................................................=B S"R$A ()R# $%#"R/A+$%! '$#H +)*(-#"R..........................=@

H1("R #"R*$%A ................................................................................=C De=ice *anage +)* (roperties..........................................................)(

Testing your computer Serial Port................................................)?


S"R$A ()R# #R)-. "SH))#$%! A%D "RR)RS..................>2 S"R$A ()R# with +D D$S( A1....................................................)= $%#"R%A AD+ )/ A,R....................................................................)> AD+ with S"R$A ()R# and +D.....................................................A? #R)-. "SH))#$%!................................................................AB TIMERS......................................................................................................A@ w*i%* one s*o('/ I %*oose A&$e"#01 o A&$e"#01'.........................A@ IMPORTANT THINGS 2OR TROUBLESHOOTING33333333333333333333345 'in,s.............................................................................................................A)

W*) AVR666666 AVR microcontrollers are popular because of their inux support and their softwares like AVRD;; and AVR$%$&. If you start learning with any microcontroller the cost you ha,e to pay is the microcontroller costEsoftwareEprogrammer. If you compute this then AVR is the best. AtmegaC is a,ailable with Rs.A= and programmer cost is 4ust a printer cable costs Rs.@( with some wires and AVRD;; for inux and !inaVR'(() for !indows.It has facilities like inbuilt A$; which is not there in C(=?2 which will be ad,antageous for robotics. #oftware are easily a,ailable for other microcontrollers but they ha,e a limit of maximum '/ program memory. The pro4ect 'embedded ethernet' ha,e around ?(/ program memory usage while !inAVR ha,e no such limitations. The programmer cost of C(=? and pic are abo,e Rs.@((*as far as I know+. #ee http8::www.electricstuff.co.uk:pic,sa,r.html As # be"inne w*#& i nee/ &o ,now666 Here I am going to talk about microcontroller 4ust like a computer running on parallel port and a ; program. I am using the same examples. "asic thing you ha,e to know how to program the chip and writing programms for the chip. If you know C(C= then it will be easy to understand. Anyway I am writing codes in ; so that it will be easy to understand. Sof&w# es Re7(i e/8 !inAVR'(()2AVR$%$&*if you are using AVR#tudio+ www.wina,r.sourceforge.net atmegaB'2atmegaC datasheet*see in atmel.com+ 6ou better make logins in www.a,rfreaks.net so that when you ha,e doubt2 you can put it there. H# /w# es Re7(i e/8 atmegaB' or atmega C *atmegaB' costs from Rs.'(( to Rs.B=( and atmegaC from Rs.A= to Rs.?'(+ "readboard 1rinter 1ort cable #erial port cable ;rystal*B.)C)@-h5+ .iffe en%e be&ween #&$e"#01 #n/ #&$e"#01L8 "oth are same only difference in freFuency and ,oltage limitations. I prefer atmegaB' because of its low cost and easy a,ailability. Atmega?) also ser,e same purpose except its Glash is ?)/"*sufficient+ and difference in memory capacities. The remaining hardware portion are same for both.

GETTING STARTE. Girst you should reFuire a compiler which con,erts your program into the hex code of the a,r microcontroller. If you use ; for programming A,r then you can use !inAVR2 ;odeVision AVR2 Image;raft AVR2 "A#;0- AVR for programming in "A#I;2 AVR#tuidio for programming in assembly. "ut here I am talking about ; programming. I use !inaVR'(() for all these programms compilation. #ame code is ,alid for AVRD;; in inux. #econd reFuirement is a programmer which transfers the . Hex code*machine code for AVR+ into the chip. That is a programmer which burns the chip. I use "#$ programmer for that.

LET9S START Install !inaVR'(() to ; dri,e. Then you can see that your desktop has the following programs.

The main program we use is the programers .otepad. #o let's start with first program.

et's start with first program of blinking &$'s on all ports 0pen 1rogrammer's .otepad and type the program in Folder:blink progrm8bibin.c

#tore it in a folder named blink. How to compile it < Do to Tool7H-ake. H Imake.exeI all make.exe8 JJJ .o rule to make target Kall'. #top. H 1rocess &xit ;ode8 ' H Time Taken8 ((8((

.ow you ha,e to create -akefile to compile the ; program. 0pen

and do the following

<

=>

==

.ow sa,e the -akefile in the folder where your program is. "etter you use the -akefile in the code I had gi,en .ow open -akefile using .otepad and do the following.

=1

=0

Here I made ;1% freFuency to ?-h5 because when you buy the chip2 the default freFuency is ?-H5 with 3TAD enabled. They use internal R; oscillator to generate ?-h5. I will explain more about it in fuse settings

=?

W*#& )o( *#/ /one in fi s& ! o" #$66 See &*e /#&#s*ee& w*#& ..RA #n/ PORTA represents. $$R determine the direction of the specific pin and 10RT register ha,e the ,alue which should be sent to the port. #o here we had written a program to blink &$'s on all ports A2"2;2$. .ext step is to program the chip. #o we had to use a programmer to load the program into the microcontroller's flash. Here we are using "#$ programmer with AVR$%$& software as we ha,e set in -akefile. If you are using a different programmer2 then you ha,e to change the programmer options. PROGRAMMER HAR.WARE Here we are using "#$ programmer which is low cost about Rs.@(*1arallel 1ort cableEsome wires+.#ee the following link www.bsdhome.com:a,rdude I 4ust redrawn it here. 6ou buy a centronics printer cable which cost about Rs.@(

This is ,alid for all microcontrollers of AVR which are supported by AVR$%$& programmer. #ee how to do it for atmegaB'

=5

#ee my programmer.

=:

=4

Instead of using Vcc of supply you can use any pin on the data port*$(+ for Vcc of AVR.I am using breadboard instead of a ready made 1;" for programmer.#o the connections in the breadboard as shown

NOTE8 He e I #$ no& (sin" % )s&#' be%#(se I #$ &*in,in" &*#& )o( bo("*& # new #&$e"#01 o )o( # e *#@in" #n #&$e"#01 w*i%* is no& (sin" eA&e n#' % )s&#'Bno& /one f(se bi&sC3 If )o( $i% o%on& o''e *#/ /one f(se bi&s &*en )o( *#@e &o (se # ! o!e % )s&#'3 I wi'' eA!'#in #bo(& i& in 2(se Bi&s se%&ion3

=;

.ow go to Too's+D P o" #$

=<

COMMON ERRORS IN AVR PROGRAMMING 2i s& E o #@ /(/e8 AVR /e@i%e no& es!on/in" #@ /(/e8 ini&i#'iE#&ion f#i'e/- %F+= .o(b'e %*e%, %onne%&ions #n/ & ) #"#in- o (se +2 &o o@e i/e &*is %*e%,3 #@ /(/e /one3 T*#n, )o(3 $#,e3eAe8 GGG H! o" #$I E o = Se%on/ E o 8

1>

A small circuit2 but too many errors<<<<<<<. TROUBLESHOOTING ?. Girst check whether your parallel port is working or not using lpt.exe. 6ou can get more explanation on my pre,ious book7 -6 &L1&RI&.;& 0. 1ARA & 10RT I.T&RGA;I.D. '. #ee bios settings. "etter set for bidirectional mode2 but spp and ecp modes will also work. #ee the address of the parallel port*(xBAC+. #ee my bios settings in my pre,ious book 7 -6 &L1&RI&.;& 0. 1ARA & 10RT I.T&RGA;I.D.

1=

B. 6ou connected reset pin to external Vcc or gnd * disconnect it+ @. &xternal power supply may be off =. 1arallel port cable may be not fitted properly. ). #ee whether u interchanged -I#0 and -0#I A. #ee whether ur parallel port is working or not by using 1T.exe C. #ee for short7ckt between pins >. #ee for any loose connection in wires using ;ontinuity tester of multimeter ?(. #ome parallel ports won't work like that of Intel>?= because they ha,e special protection2 i don't know much about it. I ha,e tried parallel port interfacing on it but data port was not properly working. ??. If you are using soldered wires then see for any short circuit or loose connections in solderings ?'. Dround of power supply and parallel port should be short circuited. ?B. &xecute ins&#''J"i@eio3b#& for acessing ports .ow the main problems are 6ikesM 87 I ha,e faced it2 but after sometime i again programmed it*i might ha,e done some hardware repairs+2 it worked properly. This is mainly due to lack of power supply. Verification &rror87 This is due to loose contact with wires. If u keep wires 4ust touched with parallel port*once i tried with hand for connecting parallel port and wires to u;+2 then this occured. This occur when u remo,e crystal when programing*after doing fuse bits+ #o u check the following things87 ?. ;heck breadboard2 new breadboards ha,e this problem*some parts don't ha,e good connection+ '.;heck ur parallel port with lpt.exe B. #ee any loose contact. @. #ee the tips i said abo,e MOST IMPORTANT TIP A'w#)s see &*e @o'&#"e #& &*e V%% !in of &*e $i% o%on& o''e 3 I& s*o('/ no& be " e#&e &*#n 5V- if i& is " e#&e &*#n 5V &*en )o( $i% o%on& o''e wi'' "e& b( ne/3 See &*e V%% of &*e $i% o%on& o''e w*en )o( swi&%* on &*e !owe s(!!')3 T*e o&*e &*in" )o( *#@e &o e$e$be is #bo(& f(se bi&s3

11

If you are not working with !inAVR'(() and you are using AVR#tudio2 then you can use the same programmer. 0nly difference is that you ha,e to work in text mode. Install AVR$%$& then execute ins&#''J"i@eio3b#& for acessing ports. .ow change to the a,rdudeNbinH directory and do the following shown

Here f.hex is the hex file we had to load into the chip. If you installed !inAVR'(() then change the director to ;8N!inAVRNbinH and execute the abo,e command. #ee the AVR$%$& documentation for more about it.

Now %*e%, &*e ! o" #$ 'o#/e/ in&o &*e $e$o )3 Re$o@e &*e Rese& !in #n/ %onne%& Rese& !in &o VCC be%#(se &*e @#'(e #& Rese& !in is LOW b) /ef#('& #n/ %*i! is ese& B i #$ &#',in" #bo(& &*e !# #''e' !o & @o'&#"eC3 6ou can see that all ports except 10RT; pins ;'2;B2;@2;= are not blinking. This is because your chip's 3TAD has been enabled. If you want to use 10RT; pins further then you ha,e to disable 3TAD. This is done by using fuse bit settings.

10

2USE BITS
6ou ha,e to do fuse bits to disable 3TAD2lock the microcontroller2 change the clock freFuency of the microcontroller. #o let's study AVR$%$& in terminal mode. Girst enter in the terminal mode and see the fuses.#ee page '== of atmegaB' datasheet2 then you will get some ideas about fuse bits.

1?

%se 'part' command to display details about the microcontroller

15

SETTINGS 2OR EXTERNAL CRYSTAL AN. JTAG .ISABLE #ee below how I had done fuse settings for external crystal. 6ou can see some error occurred2 this is due to the lack of crystal. The connection in the crysal side should be proper with proper use of crystal and capacitors. Here I set the microcontroller for B7C-h5 crystal. ;ompare what i had done with that of in datasheet.

1:

IMPORTANT NOTES ON 2USE BITS8 ?. Always keep a screenshot fuse bits settings you do for future reference. '. $o Guse "it's at your own risk. #ee the link below http8::www.roboticsindia.com:modules.php<nameOGorumsPfileO,iewtopicP tO?@?'PpOA?'( B. $o remember the fuse settings. @. The crystal you use should be proper to produce the clock to the crystal. =. %se proper capacitors. I used ''pG2BBpG e,en ?=(pG. "ut better to use the prescribed ,alues in the datasheet. ). -ostly you will get problem after fuse bit settings2 so be thorough with fuse bits before you are doing. A. If you are doing fuse bits for external crystal2 remember that after doing fuse bits you can't e,en program chip without a proper crystal and capacitors.

.ISABLING JTAG
After disabling 3TAD you can use 10RT; properly. 1ins ;'2;B2;@2;= are properly used for other applications. i.e2 we got one port for other applications. !hen I brought my new microcontroller for the first time I thought that the microcontroller was not good because 10RT; pins are not working2 then I later came to know that it is due to 3TAD only.

14

#ee what happened when I remo,ed the R&#&T pin of the microcontroller . Here I downloaded blinking program to see whether 10RT; is working and I remo,ed the R&#&T pin connection with parallel port2 that is why the fuse read as (xff

1;

.ISABLING JTAG AN. USING INTERNAL ;M*E CLOCK 6ou can see that serial port baud rate depends on the clock. #o if you use ?-h5 clock then the maximum speed possible with less errors is @C((bps *.'Q error+. "ut for high baud rate cannot be used with ?-h5 internal clock2 so we ha,e to use C-h5 internal clock2 then you can use all baud rates with less error.

In the first case after changing the fuse bits I remo,ed the R&#&T pin to see whether I got C-h5. It worked according to the pre,ious program8 blink.c2 I got fast blinking. .on9& e$o@e &*e ! o" #$$e befo e )o( 'e#@e &*e &e $in#' $o/e of AVR.U.E.That is what happened in the first case.

1<

INTER2ACING LC. WITH AVR ;$ used8 JH.=:1A*see in my parallel port book for more about it+

This is the simple schematic without using backlight and contrast pins. Here control pins are connected to 10RT$ and datapins to 10RTA. #ee the figure below.

0>

0=

01

2o'/e 8'%/ :J7777777777777777777777777777777777777777777777777777777777777777 7777777777777;0..&;TI0. "&T!&&. ;$ A.$ AT-&DAB'77777777777777777 77777777777777777777777777777777777777777777777777777777777777777J: Rdefine $ATAS$$R $$RA Rdefine $ATAS10RT 10RTA Rdefine ;0.TR0 S$$R Rdefine ;0.TR0 S10RT Rdefine &nableS1in Rdefine Reg#electS1in @ Rdefine Read!riteS1in = Rdefine ;0.TR0 S-A#/ $$R$ 10RT$ ) (LA(

Here are the declaration I am using. Here ;0.TR0 S-A#/ is used so that remaining pins can be used for other purpose. ;hange the port settings here so that you can use same code for all ports. !hen you program you should program in such a way that port pins are not wasted and same program can be used irrespecti,e of ports. #o do this at the beginning of the program for better understanding of the program and portability. 1ractice this when you do programming. Rest program remain same as that I had gi,en my pre,ious book for parallel port.

00

SOME IMPORTANT COMMAN.S USE. 2OR PROGRAMMING 6ou might ha,e seen command S"V*a+ the eFui,alent of it is ?TTa 2 for this you go to !inaVR'(() directory $8N!inAVRNa,rNincludeNa,rH open the included files so that you can get more information for atmegaB' it is iomB'.h &g8 S"V*)+O (?(((((( #uppose if you want to set =th pin of 10RT$ then write 10RT$UOS"V*=+V Here we are using logic 0R to set a pin2 remaining pins remain same because according to logic operations AE?OA*0R gate+ and AE(OA. $o some examples then you will understand it better. #uppose if you want to clear @th bit in 10RT$ then write 10RT$POW*S"V*@++V W*S"V*@+ + O W*(((?((((+ O ???(???? This ,alue logic A.$ with 10RT$ and writing it to 10RT$ again will make that bit clear. Remember A.?OA and A.(O(

0?

L1<0. INTER2ACING WITH AVR '>B$ used as H7bridge to dri,e $; motor. It can dri,e a bipolar stepper motor. I ha,e explained more about in my first book for robotics. Here are the pictures for connecting '>B$ to AVR.

05

This figure shows connecting a motor to '>B$. Here I applied ?'V from adapter to the pinC of '>B$. I used a normal ?'V2'=(mA $; motor.

0:

:J7777777777777777777777777777777777777777777777777777777777777777 7777777777777;0..&;TI0. "&T!&&. '>B$ A.$ AT-&DAB'777777777777777 77777777777777777777777777777777777777777777777777777777777777777J: Rdefine '>B$S$$R $$R; Rdefine '>B$S10RT 10RT; Rdefine 1inS? ( Rdefine 1inS' ? Rdefine '>B$S-ask (x(B :J7777777777777777777777777777777777777777777777777777777777777777 7777777777777;0..&;TI0. "&T!&&. I.1%T A.$ AT-&DAB'777777777777777 77777777777777777777777777777777777777777777777777777777777777777J: Rdefine InputS$$R $$R$ Rdefine InputS1I. 1I.$ Rdefine I1inS? ) Rdefine I1inS' = Rdefine InputS-ask (x)( Here I am using 10RT; to connect to '>B$ and 10RT$ for input pins to test H7 bridge. '>B$S-ask2InputS-ask is used so that remaining pins of the port can be used for other purposes. :J7777777777777777777777777777777777777777777777777777777777777777 77777777777777777G%.;TI0.# T0 I.ITIA IX& 10RT#77777777777777777777 77777777777777777777777777777777777777777777777777777777777777777J: ,oid InitS1orts*,oid+ Y '>B$S$$RUO*S"V*1inS? +US"V*1inS'++V ::setting pins for output '>B$S10RTPOW*S"V*1inS? +US"V*1inS'++V::starting motor is 0ff InputS$$RPOW*S"V*=+US"V*)++V::making as input pins Z Here the '>B $$R is used for ouput and '>B$S10RT is set 0! to stop motor at the starting. aO**InputS1I.PS"V*I1inS?++HH*I1inS?++V bO**InputS1I.PS"V*I1inS'++HH*I1inS'++V These statements gi,e a2bO ( or ?. I used right shifting operation here. .ow you can dri,e a robot using '>B$.

04

BIPOLAR STEPPER MOTOR .RIVING USING AVR

0;

for more about stepper see my pre,ious books

0<

:J7777777777777777777777777777777777777777777777777777777777777777 7777777777777;0..&;TI0. "&T!&&. '>B$ A.$ AT-&DAB'777777777777777 77777777777777777777777777777777777777777777777777777777777777777J: Rdefine '>B$S$$R $$R; Rdefine '>B$S10RT 10RT; Rdefine 1inS? ( Rdefine 1inS' ? Rdefine 1inSB ' Rdefine 1inS@ B Rdefine '>B$S-ask (x(f Here are the connections used int steps[\OY?=2?B2?'2?@ZV the steps seFuence used for the bipolar stepper motors are shown in arrya. This will make @ steps. Repeat this seFuence again for continous motion. 2OL.ER8bi!o'# s&e!!e

?>

UNIPOLAR STEPPER MOTOR INTER2ACING Here I used power transistors instead of % .'((B. The .1. power transistors are used for dri,ing. The base of transistor are connected to the microcontroller ports and emitter grounded and the collector is connected to the points of the stepper motor. The common ends of both coils are connected to the VccO?'V *from adapter+ of the power supply. 2OL.ER8(ni!o'# s&e!!e

?=

Ha,e a closer look

:J7777777777777777777777777777777777777777777777777777777777777777 7777777777777;0..&;TI0. "&T!&&. '>B$ A.$ AT-&DAB'777777777777777 77777777777777777777777777777777777777777777777777777777777777777J: Rdefine '>B$S$$R $$R; Rdefine '>B$S10RT 10RT; Rdefine 1inS? ( Rdefine 1inS' ? Rdefine 1inSB ' Rdefine 1inS@ B Rdefine '>B$S-ask (x(f int steps[\OY>2B2)2?'ZV Here only steps changed2rest remain same. Remember to put the program in infinite loop for continuous rotation of motor.

?1

KEY ENCO.ER INTER2ACING

2OL.ER84?<11

?0

:J7777777777777777777777777777777777777777777777777777777777777777 7777777777777;0..&;TI0. "&T!&&. A@>'' A.$ AT-&DAB'777777777777777 77777777777777777777777777777777777777777777777777777777777777777J: Rdefine I;S$$R $$R; Rdefine I;S10RT 10RT; Rdefine I;S1I. 1I.; Rdefine 1inSA ( Rdefine 1inS" ? Rdefine 1inS; ' Rdefine 1inS$ B Rdefine 1inS$A @ Rdefine 1inS0& = Rdefine I;S-ask (x?( ::0& for output remaining for input Rdefine 0utputS$$R $$R$ Rdefine 0utputS10RT 10RT$ Rdefine 0utputS&nable I;S10RTPOWS"V*1inS0&+ ::for enabling the I; Here I am using a single port for connecting the pins of A@>''. 6ou can see that all pins are input except 0uput &nable. #o you ha,e to configure that pin for output and it should be acti,e low. I;S$$ROI;S-askV ::setting 0& pins for output2remaining for input Here I take input from 10RT; and outputting the key pressed to 10RT$. #ee the capacitor ,alues otherwise keydebouncing will occurs2 it should be ; and ?(; respecti,ely. #ee the datasheet for more reference. .ow let's see the keypressed on ;$. #o I am using the pre,ious program in the folder 'lcd' to a normal .c file after remo,ing the main*+ function. 1ut that .c file in same directory and send the ,alue to the ;$. "efore sending we should make it A#;II ,alue for displaying on ;$. '(' in hexadecimal corresponds to @C in A#;II. !e ha,e to do same with alphabets also. #ee the code2 you will understand better. #ee the diagram below for the circuit.

??

2OL.ER84?<11JLC.

?5

4+SEGMENT .ISPLAY INTER2ACING WITH AVR Here I am gi,ing how to connect A segment to AVR without using any decoder*A@@A+. I am using a common anode display. #o to turn on indi,idual &$'s you should apply 0DI; 0! to the other end of &$'s.

?:

:J7777777777777777777777777777777777777777777777777777777777777777 7777777777777;0..&;TI0. "&T!&&. A segment A.$ AT-&DAB'77777777777 77777777777777777777777777777777777777777777777777777777777777777J: Rdefine $ATAS$$R $$R; Rdefine $ATAS10RT 10RT; Rdefine 1inSa ( Rdefine 1inSb ? Rdefine 1inSc ' Rdefine 1inSd B Rdefine 1inSe @ Rdefine 1inSf = Rdefine 1inSg ) Rdefine 1inS$ec A Rdefine $ATAS-A#/ (LGG Here are the connections between Asegment and AVR. The pins are connected to the 10RT; and the indi,idual pins connections are shown abo,e. 2OL.ER8one4se" do Y $isplay*i+V iEEV if*iH>+ iO(V delaySms*=((+V Z while*?+V These statements run the A7segment from ( to > with a delay of =((ms. $isplay is used to display the ,alue on A7segment. Here I used two A7segment2 i showed in diagram2 but both are common and no need for one A7segment.

?4

MULTIPLEXE. 4+SEGMENT .ISPLAYS Here we use the techniFue of persistance of ,ision. i.e2 our eye cannot detect what is happening in ?:?) of a second. #o any happening within that time will be seen continous by our eye. This techniFue is used in tele,ision2 e,en used to program ;R0's for some games.

I am using A@?')2 tristate buffer to gi,e power supply to the Asegment. 6ou can use transistors in case of common cathode display. "ut here I am using common anoder displays. 2OL.ER81+4se" ?;

IC TESTER

Try to make one I; T&#T&R. In the pre,ious book I made one which reFuires atleast =7 ) I;'s. #uppose if you use a @( pin AVR then you can make it with a single AVR. The ad,antage is that the indi,idual pins can be set for input as well as output. There are B i:o ports which is connected directly to the I; to be tested. Gor each I; pin diagram will be different. That is we ha,e to configure those '@ pins as input port or output port as per the I; number. This can be di,ided into three categories2 $$R *data direction register in atmegaB'+2 data out and data in. !e are using only one port to communicate with computer. Therefore we ha,e got at max C pins. In that one nibble is used for data transfer only and the other nibble for determining the following things

?<

] 0f which port is the data in the most significant nibble of porta. That is portb lowest nibble2 portb highest nibble2 portc lowest nibble2 portc highest nibble2 portd lowest nibble or portd highest nibble. !hat kind of data is in the least significant nibble. That is ,alue to be stored in $$R *data direction register of atmegaB'+2 10RTA:":; *,alue to be outputted to data register of 10RT":;:$+ or 1I.":;:$ *,alue to be inputted from pins of 10RT":;:$+. .ow its clear that there are three combinations for what kind of data is a,ailable in most significant nibble. Also ) nibbles are to be controlled. #o total we need ?C different kind of data to be transferred through most significant nibble. Remaining number of bits to control this is only four. !ith four using usual con,ention only ?) combinations can be made. Gor this2 the strategy we implemented is as follows2 $(7$B of 10RTA is first four the least significant bits2 ie least significant nibble. .ow2 initially we will set this to ((((. The first bit which becomes high will decide what kind of data is in the bits $@7$A of 10RTA. #uppose first $B becomes high2 then the data in the bits $@7dA of 10RTA is $$R. #uppose first $' becomes high2 then the data in the bits $@7dA of 10RTA is 10RT":;:$. #uppose first $? becomes high2 then the data in the bits $@7dA of 10RTA is 1I.":;:$. After this what we want to know is2 of which nibble should the data in the bits $@7$A. This is determined by the remaining bits2 ie suppose first $? becomes high2 then the remaining bitw $B2$'2$( will determine that. If $B2$'2$( is (?(2 it means data in the bits $@7$A belongs to most significant nibble of 10RT"2 ie $@7$A of 10RT". And since first $? becomes high it implies 1I.":;:$. #o the ,alue in $@7$A of 10RTA is the ,alue from $@7$A of 10RT". In this way we can get and write ,alues to all '@ pins.

5>

UART of AVR

It is basically used for asynchronous communication2 a communication with B wires*Rx2Tx and ground+. #o first see the uart of AVR. #ee the datasheet for the registers in the %ART. #o let's go for the first program which tests your serial port. 2o'/e 8 se i#'= This program is meant for ?-H5 internal oscillator working AVR's. 3ust short circuit RL and TL *$(2$?+ of AVR and see the ,alue at 10RT;. If the ,alue at 10RT; is (x>> then your serial port is working fine. T o(b'es*oo&in" ?. #ee at ?-H5 '@(( bps the error is .'Q2 but if you go for >)(( bps then error is around C.BQ*refer datasheet baud rates+. '. 6ou should disable the 3TAD for acessing 10RT;2 see fuse bits. B. #hort circuit Rx and Tx. .ow you can confirm that your microcontroller uart is working fine.

5=

SERIAL PORT O2 COMPUTER

51

BIOS SETTINGS

Girst set the bios then only do any other operations like programming the microcontroller and serial port. #ee the 1ort numbers correctly. Then only use the ;0-?2 ;0-' or lpt?2lpt' repecti,ely. This is the most important step. AVR$%$& programmer sometimes won't work for some parallel port. It does not worked for I.T& >)=DV motherboard. #o see my pre,ious book on parallel port before doing these steps.

50

SERIAL PORT INTER2ACING WITH COMPUTER Gor interfacing AVR with serial port of cable you reFuire a serial port cable and max'B' or HI.'B'*le,el con,erter+. R#7'B' of the computer has 0DI; HIDH in the range of 7B to 7'=V* mostly around 7C.=V+ and 0DI; 0! around B to '=V *mostly C.=V+. !e had to con,ert it to 0DI; HIDH*=V+ and 0DI; 0!*(V+ of microcontroller2 so we use max'B' or you can use any other le,el con,erters. .ext is to get a serial port cable2if you are not getting a serial port cable2 then buy a serial mouse costs around Rs.A( and take the cable from it. "ut mouse serial port cable has only @ pins Rx2Tx2$T#2D.$2 so it can't be used for programmer's of some microcontrollers. "ut in our case we reFuire only B pins*Rx2Tx2D.$+. #o it is better to go for a serial cable2 I used one that of mouse.

5?

Gigure shows the connections with serial port of computer. 6ou can use a ?uG capacitor instead of ?(uG.

55

5:

54

HYPER TERMINAL Hyper Terminal is used to see the serial port data. It will show only the recei,ed data. All the data is show in A#;II. Do to #tartS1rogramsSAccessoriesS;ommunicatinSHyper Terminal. If you are using serial port for your internet you ha,e to disconnect it for acessing serial port. Run Hyper Terminal then some screen come for asking area codes2 put some garbage ,alue and press 0/ and in the next page press 0/2 then you will come to this page.

5;

If you use some %#" to R#'B' con,erter2 then I can't say that Hyper Terminal will work.

5<

#ee in the de,ice manager for the port settings. .e@i%e M#n#"e COM P o!e &ies

:>

Tes&in" )o( %o$!(&e Se i#' Po &8


#hort circuit Rx and Tx of your computer and type letters keeping hyperterminal working. If you can see what you are typing*i.e2 if you type 'a' then it is displayed as 'a' on the hyperterminal+ then you can infer that your serial port will work properly. $on't use ground and other pins2lea,e all other pins open and 4ust short circuit pin' and pinB*Rx and Tx+.

.ow connect serial port to max'B'2 then short circuit the Rx and Tx of max'B' ie pin ?? and pin ?' and type something in hyperterminal2 see what you are getting. If you are getting the alphabets you typed then your connections are proper. -ax'B' is working fine. "y default the TT output is =V. #ee the R#'B' output corresponding to =V input2 it should be mostly in the range 7CV to 7?(V. #ee these conditions properly otherwise your microcontroller will get burned because ,oltage greater than =V is damagerous to the microcontroller.

:=

2OL.ER8se i#'J%o$!(&e Here this code will send back next A#;II you type. #ee the error in baud rates for different clock freFuencies. Here I am using '@(( C . ? with ?-h5 internal crystal. If you want more speed then go for internal C-h5 crystal. "ut still error is around .'Q2 so if you want (Q error in baud rate go for crystal B.)C)@-h5 or crystals gi,en in the datasheet which is ha,ing (Q error in baud rate T #ns$i&B)&eBRe%ei@eB)&eBCL=CM 6ou can use terminal program inbuilt in !inL1 or "ray's terminal for serial port interfacing with computer. "ray's terminal is a free software with more options than hyperterminal. Hyperterminal won't show the transmitted data2 it shows only the recei,ed data. SERIAL PORT TROUBLESHOOTING AN. ERRORS8

!hen synchronisation is lost you will get xxxxxxxxxxxxxxxxx displayed on hyperterminal. #o be thorough about the micrcontroller clock and baud rate you set. This is the most important error occurs with serial port. If you use a ; program then you will get some different ,alues other than the ,alues you sent. Remember only one program can acess serial port at a time. #ometimes Turbo ; is ha,ing problem in acessing serial ports. I am attaching a ; program with it.

:1

#ee the connections

:0

:?

SERIAL PORT wi&* LC. .ISPLAY

2OL.ER8 se i#'!o &J'%/

:5

::

:4

::lcd initiali5ations InitS1orts*+V InitS cd*+V Init%ART* '= +V :J #et the baudrate to '@(( bps using a ?-H5 crystal J: for*VV+ :J Gore,er J: Y aORecei,e"yte*+V cdS#end*a+V Transmit"yte*a+V :J &cho the recei,ed character J: Z

Girst initialising ;$ ports and start initialising baud rates. Girst we recei,e the data from serial port and send it to ;$ first then it is again send back to serial port. "ut this process will be ha,ing some delays because of the delays inserted in the ;$ functions.

:;

INTERNAL A.C O2 AVR #ee the datasheet for the registers. AtmegB' has ?( bit A$; ha,ing two registers A$; and A$;H. "ut we are using it in C bit resolution2 so that error will be lowered. "ut there will always be noise error in the reading. I used -B=2 got error about .(?V. #ee e,en con,ersion is of ?( bit resolution and we are using C bits resolution*i.e2 first C bits lea,ing last two #"'s+. In C bit resolution mode we read A$;H. If you want to read ?( bits then use A$;*see in the include file2 you will understand better+. I used internal analog ,oltage reference of '.=) ,olt. Remember to use a capacitor in the AR&G pin2 use a .(?uG capacitor. The choice of capacitance will affect the reading. #ee in the datasheet how to a,oid noise better. I tried with .?uG and .(?uG capacitors. Here I am using -B= temperature sensor to A$; input.

:<

2OL.ER8'$J1+4se" #ee the problem with persistance of ,ision. Here the temperature ,aries with time and it is shown on &$ i.e2 it is shown a non decimal ,alue. A$;#RAUOS"V*A$#;+V ::start con,ersion while*M**A$;#RAPS"V*A$IG++HHA$IG++V return A$;HV Here I am using single con,ersion mode. I am waiting for the con,ersion to end and we are using C bit con,ersion mode. The C bit con,erted ,alue is there in A$;H register and we are returning the ,alues.

4>

A.C wi&* SERIAL PORT #n/ LC. ,oid InitSA$;*,oid+ Y A$-%LUO*S"V*A$ AR+ U S"V*R&G#?+U S"V*R&G#(++ V :: RG#? and RG#( are for Internal Reference ,oltage of '.=)V A.$ C "IT ;0.V&R#I0. A$-%LPOW*S"V*-%L(+US"V*-%L?+US"V*-%L'+US"V*-%LB+U S"V*-%L@++V ::#& &;TI.D ;HA..& ( A$;#RAUO*S"V*A$&.+ U S"V*A$1#'+ U S"V*A$1#(++ V ::normal con,ersion without interrupt A$;#RAPOW*S"V*A$#;+US"V*A$AT&+US"V*A$IG+US"V*A$I&+U S"V*A$1#?++V::clock:B' speed Z int ReadSA$;*,oid+ Y A$;#RAUOS"V*A$#;+V ::start con,ersion while*M**A$;#RAPS"V*A$IG++HHA$IG++V return A$;HV Z #ee the A$; declarations and initiali5ations.

4=

if*aMOb+ Y ::first delete the pre,ious ,alues for uart for*iO(ViT'ViEE+ Y Transmit"yte*del[i\+V Z ::for ;$ we reFuire initialiati5ation InitS cd*+V for*iO(ViT?'ViEE+ cdS#end*str[i\+V

4OaQ?(V kOa:?(V kOkQ?(V cdS#end**kE(xB(++V Transmit"yte**kE(xB(++V cdS#end**4E(xB(++V ::since both will display A#;II ,alues2 we con,ert decimal to A#;II Transmit"yte**4E(xB(++V bOaV ::for next ,alue ::delay sometime *?s+ otherwise ;$ display will run delay*?(((+V we are seeing for any change in the temperature reading2 if any change occurred then it is send it to the ;$ and serial port. !e are using '"A;/#1A;&' to delete the pre,ious ,alues in case of serial port. Here I am clearing ;$ and writing to it 2 but you can do other way by selecting the instruction register and mo,ing to location (xCc and (xCd to write the temperature on that locations.

41

TROUBLESHOOTING8 ?. Always remember about the fuse settings and freFuency of the crysal you are using. #et the baud rate according to it. '. #ee the baud rate errors. "etter to use a maximum error of .'Q2better don't go for baud rates of errors C.BQ. B. Refer datasheet for the baud rate before setting it. @. 1ut noise cancellation capacitance in the AR&G pin. =. #ee the Hyper Terminal settings2 about the port is it ;0-? or any other port2 see the bios settings.

40

TIMERS ,oid main*+ Y Init%ART*'=+V T;;R(UO*S"V*;#('+US"V*;#(?++V do Y Transmit"yte*T;.T(+V delaySms*=((+V Z while*?+V Z Here I am using external clock for running the timer. The clock is set falling edge. "ut if you keep T( pin open then the counter will run counting. "etter connect T( pin to Vcc or ground. Include ^serial.c_ for serial port transmission. W*i%* one s*o('/ I %*oose A&$e"#01 o A&$e"#01L66 I prefer AtmegaB'l because its low power consumption and cost. ;ost difference is around Rs.=( or more and second thing it works on low power which is ,ery ada,antegous in robotics because the power supply won't be consumed much. #uppose if you make a robot using some transistors and 01A-1's then the power consumption of that circuit will be more and motor will draw more current which will sometimes cause the control circuit to improper working. "ut atmegaB'l works from '.AV ` =V and with e,en low ,oltage. 6ou might ha,e noticed that it works with the ,oltage from the parallel port without connect any Vcc to the chip. After programming the chip2 switch off the adapter then also a,r works with the ,oltage from parallel port. This shows how less is the power reFuired to run the chip. This is the main ad,antage of using microcontroller instead of normal transistors and 01A-1# for robotics. I don't think clock freFuency does not matter much for us since atmegaB'l maximum freFuency is C-H5. The sensiti,ity of a sensor is in the range of milliseconds or ?(( u#ec not more than that and we are mainly processing the sensor signals2still we are getting C((( cycles for processing. This much time is more than enough for us.

4?

IMPORTANT THINGS TO REMEMBER 2OR MICROCONTROLLER TROUBLESHOOTING

?. Always see that the ,oltage *Vcc+ to the microcontroller should not be greater than =V. '. #ee your parallel port and serial port settings on "I0# before starting with microcontrollers. B. Always keep a screenshot of the fuse bit settings you are doing. @. -ain reasons which cause AVR to burn8 microcontroller ,oltageH=V fuse bits had done wrongly*lock bits especially+ =. "efore doing fuse bits2 be thorough with what you are doing. &nable the proper modules only. #uppose if you use external clock then before doing fuse bits you should ha,e a proper crystal. ). Always see the register settings. $on't waste a full port for the use of a single input or output. $o proper commands2mainly S"V*+V A. Apply proper noise cancellation. %se proper capacitance ,alues. C. #ee for the baud rate error corrections. "etter use maximum possible error to .'Q. >. #ee the breadboard problems. This is a serious problem. ?(. #ee my pre,ious books for more troubleshooting.

45

IR 'e/ i/en&ifi%#&ion http8::roboticsindia.net:modules.php<nameOGorumsPfileO,iewtopicPtO?@BC son# http8::www.robot7electronics.co.uk:htm:srf(@tech.htm http8::www.robot7electronics.co.uk:htm:srf(Ctech.shtml http8::ham4udo.com:sonar: http8::www.rdrop.com:Wcary:html:ultrasonic.html http8::www.leang.com:robotics:info:articles:minison:minison.html http8::www.robot7electronics.co.uk:htm:srf(@tech.htm http8::www.interF.or.4p:4apan:se7inoue:eSpic)S).htm

i @s son# http8::www.roboticsindia.net:modules.php<nameOGorumsPfileO,iewtopicPtO??C> s$s http8::www.riccibitti.com:tinyplanet:tinySarticle.htm s*# ! senso http8::www.blueink.com:; A##:physcom?:#harpD1'$?'.htm http8::neil.fraser.name:software:lpt: %o http8::www.roboticsindia.com:modules.php<nameO.ewsPfileOarticlePsidO??' '%/ http8::www.roboticsindia.com:modules.php<nameO.ewsPfileOprintPsidOA= http8::www.beyondlogic.org:parlcd:parlcd.htm http8::www.eio.com:lcdintro.htm http8::home.iae.nl:users:pouweha:lcd:lcd(.shtmlRhd@@AC( http8::ee.cle,ersoul.com:lcdSpro4ect.html

4:

s!ee%* http8::www.,bip.com:miscellaneous:#peech7Recognition.asp http8::roboticsindia.com:modules.php<nameO.ewsPfileOarticlePsidO??=PmodeOP orderO(PtholdO( 4se"$en& http8::www.iguanalabs.com:Asegment.htm "ene #' http8::homepage.hispeed.ch:peterfleury: #@ f e#,s (se ! oNe%&s http8::www.a,rfreaks.net:index.php<moduleOGreaksAcademyPfuncO,iew1ro4ectsP startnumOA) software http8::www.a,rfreaks.net:index.php<moduleOGreaksAcademyPfuncO,iew1ro4ectsP startnumOA) &#%*o$e&e http8::www.xdesignlabs.com:digitaltach:

e$o&e %on& o' http8::home.earthlink.net:Whenryarnold:pageBB.html http8::sc,.bu.edu:D;:shammi:ir: http8::www.ustr.net:infrared:sony.shtml http8::www.ustr.net:infrared:infrared?.shtml http8::www.ustr.net:

"oo/ 'in, http8::www.sbselektronik.com:de,reler.asp http8::www.geocities.com:stampic:links.htm

44

f(se bi&s http8::www.roboticsindia.com:modules.php<nameOGorumsPfileO,iewtopicPtO?@?' PpOA?'( http8::www.a,rfreaks.net:index.php<nameO1.php""'PfileO,iewtopicPpO'(ABC?R '(ABC? http8::www.a,rfreaks.net:index.php<nameO1.php""'PfileO,iewtopicPtOB)'B=

! o" #$$e s http8::www.tuxgraphics.org:electronics:'((=?(:article(=?(?.shtml http8::www.users.on.net:Wsymes:-egaB':;and-egaB'.htm

"oo/ &(&o i#'s http8::www.users.on.net:Wsymes:;withAVR:Introto;withAVR.htm\ www.a,rbeginners.net $$% %# / in&e f#%e http8::www.a,rrepository.com:samplecode:sdSbreakout:doc:index.html #@ /(/e ! ob'e$ http8::www.roboticsindia.net:modules.php<nameOGorumsPfileO,iewtopicPtO?B?C http8::www.roboticsindia.net:modules.php<nameOGorumsPfileO,iewtopicPtO?B'A http8::www.roboticsindia.net:modules.php<nameOGorumsPfileO,iewtopicPtO?BB' http8::www.roboticsindia.net:modules.php<nameOGorumsPfileO,iewtopicPtO?BB' http8::www.roboticsindia.net:modules.php<nameOGorumsPfileO,iewtopicPtO?'@?

4;

i http8::roboticsindia.com:modules.php<nameO.ewsPfileOarticlePsidOB=PmodeOPo rderO(PtholdO( http8::www.techgeek.com:subpage:index.phtml<topicO>>CPsubtopicO>>)PitemO?( ?A http8::www.acroname.com:robotics:info:articles:irlinear:irlinear.html http8::www.roboticsindia.net:modules.php<nameOGorumsPfileO,iewtopicPtO?@'A PpOA?C@

4<

You might also like