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 for programming AVR microcontroller using 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 Linux compilers and cheap cost programmers. Here i am using WinAVR and BSD AVRDUDE programmer. All the codes in this book are tested with WinAVR2006. Before starting this you should read my other books(it will be good) because I refer to that books for some parts.All books are available on yahoo group-booksbybibin I thank my teachers of MNNIT and my colleagues Anil K.M,O.Praveen Kumar Reddy, M. Satish,Vigith Maurice, J Rajashekhar Reddy,Athul Sona,Sanjo Sebastin . So i feel this book can guide you through troubleshooting AVR microcontrollers. I thank my teachers of THSS Muttom, Thodupuzha from where i learned the very basics of electronics.If you find any problems put a post in yahoo groupbooksbybibin. I am providing this book free of cost. You 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. You are not allowed to upload this book in any other sites. e-mail:njbibin@yahoo.co.in website:www.geocities.com/njbibin forums for AVR microcontrollers: www.avrfreaks.net www.avrbeginners.net Www.roboticsindia.net www.edaboard.com Yahoo group: avrclub avrbooks magazine:Circuit Cellar BIBIN JOHN

INDEX Why AVR???..................................................................................................5 As a beginner what i need to know???....................................................5 Softwares Required.....................................................................................5 Hardwares Required...................................................................................5 Difference between atmega32 and atmega32L.......................................5 GETTING STARTED.................................................................................6 LET'S START...............................................................................................6 Makefile.........................................................................................................9 What you had done in first program??...................................................15 PROGRAMMER HARDWARE...............................................................15 COMMON ERRORS IN AVR PROGRAMMING..............................20 TROUBLESHOOTING.............................................................................21 MOST IMPORTANT TIP.........................................................................22

FUSE BITS..................................................................................................24

SETTINGS FOR EXTERNAL CRYSTAL AND JTAG DISABLE.....26 IMPORTANT NOTES ON FUSE BITS..................................................27

DISABLING JTAG................................................................................27
DISABLING JTAG AND USING INTERNAL 8Mhz CLOCK..........29 INTERFACING LCD WITH AVR...........................................................30 SOME IMPORTANT COMMANDS USED FOR PROGRAMMING.............................................................................34 L293D INTERFACING WITH AVR........................................................35 BIPOLAR STEPPER MOTOR DRIVING USING AVR.....................38 UNIPOLAR STEPPER MOTOR INTERFACING................................41 KEY ENCODER INTERFACING(74922)................................................43 7-SEGMENT DISPLAY INTERFACING WITH AVR.........................46 MULTIPLEXED 7-SEGMENT DISPLAYS.............................................48 IC TESTER....................................................................................................49 UART of AVR..............................................................................................51 Troubleshooting...............................................................................51 SERIAL PORT OF COMPUTER..............................................................52 BIOS SETTINGS.........................................................................................53 SERIAL PORT INTERFACING WITH COMPUTER..........................54

.................................................................................................................................62 SERIAL PORT with LCD DISPLAY..........................................................................................71 TROUBLESHOOTING.........60 Testing your computer Serial Port..65 INTERNAL ADC OF AVR............................74 IMPORTANT THINGS FOR TROUBLESHOOTING...................61 SERIAL PORT TROUBLESHOOTING AND ERRORS............................HYPER TERMINAL..........................58 Device Manage COM Properties.....................................................74 which one should I choose Atmega32 or Atmega32l.......................................73 TIMERS..................................................................76 ...........................................................................75 links...................................................................69 ADC with SERIAL PORT and LCD....................................................................

The programmer cost of 8051 and pic are above Rs. The project 'embedded ethernet' have around 10K program memory usage while WinAVR have no such limitations. you can put it there. Anyway I am writing codes in C so that it will be easy to understand. Atmega16 also serve same purpose except its Flash is 16KB(sufficient) and difference in memory capacities. Softwares Required: WinAVR2006. See http://www.75 and programmer cost is just a printer cable costs Rs. Software are easily available for other microcontrollers but they have a limit of maximum 2K program memory.120) Breadboard Printer Port cable Serial port cable Crystal(3.atmega8 datasheet(see in atmel. If you start learning with any microcontroller the cost you have to pay is the microcontroller cost+software+programmer.net atmega32. I am using the same examples.uk/picvsavr.AVRDUDE(if you are using AVRStudio) www.Why AVR?????? AVR microcontrollers are popular because of their Linux support and their softwares like AVRGCC and AVRDUDE.It has facilities like inbuilt ADC which is not there in 8051. If you compute this then AVR is the best.com) You better make logins in www.html As a beginner what i need to know??? Here I am going to talk about microcontroller just like a computer running on parallel port and a C program.avrfreaks. The remaining hardware portion are same for both.net so that when you have doubt. Basic thing you have to know how to program the chip and writing programms for the chip. which will be advantageous for robotics.40 with some wires and AVRGCC for Linux and WinaVR2006 for Windows. 5 . Hardwares Required: atmega32 or atmega 8 (atmega32 costs from Rs.75 to Rs. I prefer atmega32L because of its low cost and easy availability.electricstuff. Atmega8 is available with Rs.350 and atmega8 from Rs. If you know 8085 then it will be easy to understand.6864Mhz) Difference between atmega32 and atmega32L: Both are same only difference in frequency and voltage limitations.200 to Rs.winavr.co.400(as far as I know).sourceforge.

Hex code(machine code for AVR) into the chip. 6 . So let's start with first program.GETTING STARTED First you should require a compiler which converts your program into the hex code of the avr microcontroller. BASCOM AVR for programming in BASIC. AVRStuidio for programming in assembly. I use WinaVR2006 for all these programms compilation. LET'S START Install WinaVR2006 to C drive. Same code is valid for AVRGCC in Linux. Then you can see that your desktop has the following programs. If you use C for programming Avr then you can use WinAVR. The main program we use is the programers Notepad. I use BSD programmer for that. That is a programmer which burns the chip. But here I am talking about C programming. ImageCraft AVR. CodeVision AVR. Second requirement is a programmer which transfers the .

7 .

Stop.exe" all make. > "make.exe: *** No rule to make target `all'. How to compile it ? Go to Tool->Make.c Store it in a folder named blink. > Process Exit Code: 2 > Time Taken: 00:00 8 .Let's start with first program of blinking LED's on all ports Open Programmer's Notepad and type the program in Folder:blink progrm:bibin.

Open and do the following 9 .Now you have to create Makefile to compile the C program.

10 .

11 .

12 . Better you use the Makefile in the code I had given Now open Makefile using Notepad and do the following.Now save the Makefile in the folder where your program is.

13 .

Here I made CPU frequency to 1Mhz because when you buy the chip. They use internal RC oscillator to generate 1Mhz. I will explain more about it in fuse settings 14 . the default frequency is 1MHz with JTAG enabled.

So here we had written a program to blink LED's on all ports A. So we had to use a programmer to load the program into the microcontroller's flash. You buy a centronics printer cable which cost about Rs.40(Parallel Port cable+some wires). If you are using a different programmer. DDR determine the direction of the specific pin and PORT register have the value which should be sent to the port. Here we are using BSD programmer with AVRDUDE software as we have set in Makefile. then you have to change the programmer options.B.What you had done in first program?? See the datasheet what DDRA and PORTA represents.40 This is valid for all microcontrollers of AVR which are supported by AVRDUDE programmer.bsdhome.D. See how to do it for atmega32 15 .com/avrdude I just redrawn it here. Next step is to program the chip. PROGRAMMER HARDWARE Here we are using BSD programmer which is low cost about Rs.C.See the following link www.

16 .See my programmer.

17 .

I will explain about it in Fuse Bits section. If your microcontroller had done fuse bits then you have to use a proper crystal.So the connections in the breadboard as shown NOTE: Here I am not using crystal because I am thinking that you bought a new atmega32 or you are having an atmega32 which is not using external crystal(not done fuse bits).I am using breadboard instead of a ready made PCB for programmer. 18 .Instead of using Vcc of supply you can use any pin on the data port(D0) for Vcc of AVR.

Now go to Tools-> Program 19 .

exe: *** [program] Error 1 Second Error: 20 .COMMON ERRORS IN AVR PROGRAMMING First Error avrdude: AVR device not responding avrdude: initialization failed. make. Thank you. avrdude done. rc=-1 Double check connections and try again. or use -F to override this check.

MY EXPERIENCE ON PARALLEL PORT INTERFACING. Better set for bidirectional mode. but too many errors???????.A small circuit. 2.exe. 21 . TROUBLESHOOTING 1. You can get more explanation on my previous book. See bios settings. See the address of the parallel port(0x378).MY EXPERIENCE ON PARALLEL PORT INTERFACING. First check whether your parallel port is working or not using lpt. See my bios settings in my previous book . but spp and ecp modes will also work.

4. Some parallel ports won't work like that of Intel915 because they have special protection. See for short-ckt between pins 9. Check breadboard. The other thing you have to remember is about fuse bits. Execute install_giveio. This occur when u remove crystal when programing(after doing fuse bits) So u check the following things:1. Verification Error:. 13. 11. then this occured. You connected reset pin to external Vcc or gnd ( disconnect it) 4.exe 8. Ground of power supply and parallel port should be short circuited. If you are using soldered wires then see for any short circuit or loose connections in solderings 12. if it is greater than 5V then your microcontroller will get burned. See the Vcc of the microcontroller when you switch on the power supply. This is mainly due to lack of power supply. i don't know much about it. Parallel port cable may be not fitted properly. It should not be greater than 5V.Check ur parallel port with lpt. but after sometime i again programmed it(i might have done some hardware repairs). See for any loose connection in wires using Continuity tester of multimeter 10. If u keep wires just touched with parallel port(once i tried with hand for connecting parallel port and wires to uC).3.exe 3. See any loose contact.This is due to loose contact with wires.I have faced it.bat for acessing ports Now the main problems are Yikes! :. it worked properly. 22 . 6. See the tips i said above MOST IMPORTANT TIP Always see the voltage at the Vcc pin of the microcontroller. I have tried parallel port interfacing on it but data port was not properly working. new breadboards have this problem(some parts don't have good connection) 2. See whether u interchanged MISO and MOSI 7. External power supply may be off 5. See whether ur parallel port is working or not by using LPT.

C3.C4.hex is the hex file we had to load into the chip. Now change to the avrdude\bin> directory and do the following shown Here f. Only difference is that you have to work in text mode. Install AVRDUDE then execute install_giveio. You can see that all ports except PORTC pins C2.If you are not working with WinAVR2006 and you are using AVRStudio. Now check the program loaded into the memory. This is done by using fuse bit settings. Remove the Reset pin and connect Reset pin to VCC because the value at Reset pin is LOW by default and chip is reset ( i am talking about the parallel port voltage).bat for acessing ports. If you want to use PORTC pins further then you have to disable JTAG. 23 . If you installed WinAVR2006 then change the director to C:\WinAVR\bin> and execute the above command. then you can use the same programmer.C5 are not blinking. This is because your chip's JTAG has been enabled. See the AVRDUDE documentation for more about it.

So let's study AVRDUDE in terminal mode.See page 255 of atmega32 datasheet. First enter in the terminal mode and see the fuses. 24 . change the clock frequency of the microcontroller. then you will get some ideas about fuse bits.lock the microcontroller.FUSE BITS You have to do fuse bits to disable JTAG.

Use 'part' command to display details about the microcontroller 25 .

Here I set the microcontroller for 3-8Mhz crystal. this is due to the lack of crystal. You can see some error occurred.SETTINGS FOR EXTERNAL CRYSTAL AND JTAG DISABLE See below how I had done fuse settings for external crystal. The connection in the crysal side should be proper with proper use of crystal and capacitors. 26 . Compare what i had done with that of in datasheet.

C3. we got one port for other applications. 4. I used 22pF.e.php?name=Forums&file=viewtopic& t=1412&p=7120 3. But better to use the prescribed values in the datasheet. i. Do Fuse Bit's at your own risk. Use proper capacitors.roboticsindia.IMPORTANT NOTES ON FUSE BITS: 1. so be thorough with fuse bits before you are doing. 2.C5 are properly used for other applications. The crystal you use should be proper to produce the clock to the crystal. Do remember the fuse settings. When I brought my new microcontroller for the first time I thought that the microcontroller was not good because PORTC pins are not working. remember that after doing fuse bits you can't even program chip without a proper crystal and capacitors. Mostly you will get problem after fuse bit settings. Pins C2. If you are doing fuse bits for external crystal.com/modules. 6. then I later came to know that it is due to JTAG only. DISABLING JTAG After disabling JTAG you can use PORTC properly. 27 .33pF even 150pF. Always keep a screenshot fuse bits settings you do for future reference. 5.C4. See the link below http://www. 7.

See what happened when I removed the RESET pin of the microcontroller . Here I downloaded blinking program to see whether PORTC is working and I removed the RESET pin connection with parallel port. that is why the fuse read as 0xff 28 .

So if you use 1Mhz clock then the maximum speed possible with less errors is 4800bps (.That is what happened in the first case. I got fast blinking. But for high baud rate cannot be used with 1Mhz internal clock.DISABLING JTAG AND USING INTERNAL 8Mhz CLOCK You can see that serial port baud rate depends on the clock.c. In the first case after changing the fuse bits I removed the RESET pin to see whether I got 8Mhz. so we have to use 8Mhz internal clock. Don't remove the programmer before you leave the terminal mode of AVRDUDE. 29 . It worked according to the previous program: blink.2% error). then you can use all baud rates with less error.

Here control pins are connected to PORTD and datapins to PORTA.INTERFACING LCD WITH AVR LCD used: JHD162A(see in my parallel port book for more about it) This is the simple schematic without using backlight and contrast pins. See the figure below. 30 .

31 .

32 .

Change the port settings here so that you can use same code for all ports. Rest program remain same as that I had given my previous book for parallel port. 33 . Here CONTROL_MASK is used so that remaining pins can be used for other purpose. When you program you should program in such a way that port pins are not wasted and same program can be used irrespective of ports. So do this at the beginning of the program for better understanding of the program and portability.Folder:lcd /*----------------------------------------------------------------------------CONNECTION BETWEEN LCD AND ATMEGA32---------------------------------------------------------------------------------*/ #define DATA_DDR DDRA #define DATA_PORT PORTA #define CONTROL_DDR #define CONTROL_PORT #define Enable_Pin #define RegSelect_Pin 4 #define ReadWrite_Pin 5 #define CONTROL_MASK DDRD PORTD 6 0X70 Here are the declaration I am using. Practice this when you do programming.

Remember A.0=0 34 . for this you go to WinaVR2006 directory D:\WinAVR\avr\include\avr> open the included files so that you can get more information for atmega32 it is iom32.h Eg: _BV(6)= 01000000 Suppose if you want to set 5th pin of PORTD then write PORTD|=_BV(5). remaining pins remain same because according to logic operations A+1=A(OR gate) and A+0=A.SOME IMPORTANT COMMANDS USED FOR PROGRAMMING You might have seen command _BV(a) the equivalent of it is 1<<a . Do some examples then you will understand it better. Suppose if you want to clear 4th bit in PORTD then write PORTD&=~(_BV(4)). Here we are using logic OR to set a pin. ~(_BV(4) ) = ~(00010000) = 11101111 This value logic AND with PORTD and writing it to PORTD again will make that bit clear.1=A and A.

I have explained more about in my first book for robotics. 35 . It can drive a bipolar stepper motor. Here are the pictures for connecting L293D to AVR.L293D INTERFACING WITH AVR L293D used as H-bridge to drive DC motor.

Here I applied 12V from adapter to the pin8 of L293D.This figure shows connecting a motor to L293D. 36 .250mA DC motor. I used a normal 12V.

Input_Mask is used so that remaining pins of the port can be used for other purposes./*----------------------------------------------------------------------------CONNECTION BETWEEN L293D AND ATMEGA32-------------------------------------------------------------------------------*/ #define L293D_DDR DDRC #define L293D_PORT PORTC #define Pin_1 0 #define Pin_2 1 #define L293D_Mask 0x03 /*----------------------------------------------------------------------------CONNECTION BETWEEN INPUT AND ATMEGA32-------------------------------------------------------------------------------*/ #define Input_DDR DDRD #define Input_PIN PIND #define IPin_1 6 #define IPin_2 5 #define Input_Mask 0x60 Here I am using PORTC to connect to L293D and PORTD for input pins to test Hbridge. b=((Input_PIN&_BV(IPin_2))>>(IPin_2)).b= 0 or 1. L293D_Mask. //setting pins for output L293D_PORT&=~(_BV(Pin_1 )|_BV(Pin_2)). I used right shifting operation here. /*--------------------------------------------------------------------------------FUNCTIONS TO INITIALIZE PORTS------------------------------------------------------------------------------------*/ void Init_Ports(void) { L293D_DDR|=(_BV(Pin_1 )|_BV(Pin_2)). Now you can drive a robot using L293D.//starting motor is Off Input_DDR&=~(_BV(5)|_BV(6)). These statements give a. a=((Input_PIN&_BV(IPin_1))>>(IPin_1)). 37 .//making as input pins } Here the L293 DDR is used for ouput and L293D_PORT is set LOW to stop motor at the starting.

BIPOLAR STEPPER MOTOR DRIVING USING AVR 38 .

for more about stepper see my previous books 39 .

13. FOLDER:bipolarstepper 40 . Repeat this sequence again for continous motion.14}. the steps sequence used for the bipolar stepper motors are shown in arrya. This will make 4 steps./*----------------------------------------------------------------------------CONNECTION BETWEEN L293D AND ATMEGA32-------------------------------------------------------------------------------*/ #define L293D_DDR DDRC #define L293D_PORT PORTC #define Pin_1 0 #define Pin_2 1 #define Pin_3 2 #define Pin_4 3 #define L293D_Mask 0x0f Here are the connections used int steps[]={15.12.

The common ends of both coils are connected to the Vcc=12V (from adapter) of the power supply. The NPN power transistors are used for driving. 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. FOLDER:unipolarstepper 41 .UNIPOLAR STEPPER MOTOR INTERFACING Here I used power transistors instead of ULN2003.

12}.rest remain same.Have a closer look /*----------------------------------------------------------------------------CONNECTION BETWEEN L293D AND ATMEGA32-------------------------------------------------------------------------------*/ #define L293D_DDR DDRC #define L293D_PORT PORTC #define Pin_1 0 #define Pin_2 1 #define Pin_3 2 #define Pin_4 3 #define L293D_Mask 0x0f int steps[]={9. Here only steps changed. Remember to put the program in infinite loop for continuous rotation of motor.3.6. 42 .

KEY ENCODER INTERFACING FOLDER:74922 43 .

See the datasheet for more reference. So you have to configure that pin for output and it should be active low. //setting OE pins for output.c file in same directory and send the value to the LCD. Before sending we should make it ASCII value for displaying on LCD./*----------------------------------------------------------------------------CONNECTION BETWEEN 74922 AND ATMEGA32-------------------------------------------------------------------------------*/ #define IC_DDR DDRC #define IC_PORT PORTC #define IC_PIN PINC #define Pin_A 0 #define Pin_B 1 #define Pin_C 2 #define Pin_D 3 #define Pin_DA 4 #define Pin_OE 5 #define IC_Mask 0x10 //OE for output remaining for input #define Output_DDR DDRD #define Output_PORT PORTD #define Output_Enable IC_PORT&=~_BV(Pin_OE) //for enabling the IC Here I am using a single port for connecting the pins of 74922.c file after removing the main() function. We have to do same with alphabets also. You can see that all pins are input except Ouput Enable. you will understand better. See the diagram below for the circuit. it should be C and 10C respectively. 44 . See the capacitor values otherwise keydebouncing will occurs. '0' in hexadecimal corresponds to 48 in ASCII. So I am using the previous program in the folder 'lcd' to a normal . IC_DDR=IC_Mask.remaining for input Here I take input from PORTC and outputting the key pressed to PORTD. Now let's see the keypressed on LCD. Put that . See the code.

FOLDER:74922_LCD 45 .

7-SEGMENT DISPLAY INTERFACING WITH AVR Here I am giving how to connect 7 segment to AVR without using any decoder(7447). I am using a common anode display. So to turn on individual LED's you should apply LOGIC LOW to the other end of LED's. 46 .

} while(1). if(i>9) i=0. 47 . but both are common and no need for one 7-segment. Here I used two 7-segment. Display is used to display the value on 7-segment. i++. delay_ms(500). These statements run the 7-segment from 0 to 9 with a delay of 500ms./*----------------------------------------------------------------------------CONNECTION BETWEEN 7 segment AND ATMEGA32---------------------------------------------------------------------------*/ #define DATA_DDR DDRC #define DATA_PORT PORTC #define Pin_a 0 #define Pin_b 1 #define Pin_c 2 #define Pin_d 3 #define Pin_e 4 #define Pin_f 5 #define Pin_g 6 #define Pin_Dec 7 #define DATA_MASK 0XFF Here are the connections between 7segment and AVR. The pins are connected to the PORTC and the individual pins connections are shown above. i showed in diagram. FOLDER:one7seg do { Display(i).

But here I am using common anoder displays.MULTIPLEXED 7-SEGMENT DISPLAYS Here we use the technique of persistance of vision. You can use transistors in case of common cathode display. our eye cannot detect what is happening in 1/16 of a second. FOLDER:2-7seg 48 . even used to program CRO's for some games. So any happening within that time will be seen continous by our eye. This technique is used in television.e. i. I am using 74126. tristate buffer to give power supply to the 7segment.

data out and data in. This can be divided into three categories. The advantage is that the individual pins can be set for input as well as output. For each IC pin diagram will be different. We are using only one port to communicate with computer. Suppose if you use a 40 pin AVR then you can make it with a single AVR.IC TESTER Try to make one IC TESTER. Therefore we have got at max 8 pins. That is we have to configure those 24 pins as input port or output port as per the IC number. DDR (data direction register in atmega32). There are 3 i/o ports which is connected directly to the IC to be tested. In that one nibble is used for data transfer only and the other nibble for determining the following things 49 . In the previous book I made one which requires atleast 56 IC's.

D0-D3 of PORTA is first four the least significant bits. then the data in the bits D4-d7 of PORTA is DDR.D2. Suppose first D3 becomes high. portb highest nibble. of which nibble should the data in the bits D4-D7. This is determined by the remaining bits. portc lowest nibble. Remaining number of bits to control this is only four. Suppose first D2 becomes high. PORTA/B/C (value to be outputted to data register of PORTB/C/D) or PINB/C/D (value to be inputted from pins of PORTB/C/D). So total we need 18 different kind of data to be transferred through most significant nibble. Now its clear that there are three combinations for what kind of data is available in most significant nibble.D0 is 010.D2.D0 will determine that. then the remaining bitw D3. Now. And since first D1 becomes high it implies PINB/C/D. Also 6 nibbles are to be controlled. then the data in the bits D4-d7 of PORTA is PINB/C/D. If D3. With four using usual convention only 16 combinations can be made. That is value to be stored in DDR (data direction register of atmega32). ie suppose first D1 becomes high. 50 . it means data in the bits D4-D7 belongs to most significant nibble of PORTB. Suppose first D1 becomes high. ie least significant nibble. the strategy we implemented is as follows. So the value in D4-D7 of PORTA is the value from D4-D7 of PORTB. In this way we can get and write values to all 24 pins. The first bit which becomes high will decide what kind of data is in the bits D4-D7 of PORTA. portd lowest nibble or portd highest nibble. initially we will set this to 0000. then the data in the bits D4-d7 of PORTA is PORTB/C/D. For this.· Of which port is the data in the most significant nibble of porta. After this what we want to know is. portc highest nibble. That is portb lowest nibble. What kind of data is in the least significant nibble. ie D4-D7 of PORTB.

see fuse bits. If the value at PORTC is 0x99 then your serial port is working fine. See the datasheet for the registers in the UART. Folder: serial1 This program is meant for 1MHz internal oscillator working AVR's. Just short circuit RX and TX (D0. 2. 51 . Now you can confirm that your microcontroller uart is working fine.D1) of AVR and see the value at PORTC. a communication with 3 wires(Rx. You should disable the JTAG for acessing PORTC. So let's go for the first program which tests your serial port. 3.UART of AVR It is basically used for asynchronous communication. See at 1MHz 2400 bps the error is .3%(refer datasheet baud rates). Short circuit Rx and Tx.Tx and ground). but if you go for 9600 bps then error is around 8. So first see the uart of AVR. Troubleshooting 1.2%.

SERIAL PORT OF COMPUTER 52 .

AVRDUDE programmer sometimes won't work for some parallel port.BIOS SETTINGS First set the bios then only do any other operations like programming the microcontroller and serial port. 53 . It does not worked for INTEL965GV motherboard. This is the most important step.lpt2 repectively. Then only use the COM1. COM2 or lpt1. So see my previous book on parallel port before doing these steps. See the Port numbers correctly.

70 and take the cable from it. But mouse serial port cable has only 4 pins Rx. Next is to get a serial port cable.if you are not getting a serial port cable. then buy a serial mouse costs around Rs.SERIAL PORT INTERFACING WITH COMPUTER For interfacing AVR with serial port of cable you require a serial port cable and max232 or HIN232(level converter). So it is better to go for a serial cable.Tx. so it can't be used for programmer's of some microcontrollers.GND.Tx.GND). I used one that of mouse. But in our case we require only 3 pins(Rx.5V) and LOGIC LOW around 3 to 25V (mostly 8. We had to convert it to LOGIC HIGH(5V) and LOGIC LOW(0V) of microcontroller. 54 . so we use max232 or you can use any other level converters.DTS.5V). RS-232 of the computer has LOGIC HIGH in the range of -3 to -25V( mostly around -8.

Figure shows the connections with serial port of computer. 55 . You can use a 1uF capacitor instead of 10uF.

56 .

57 .

It will show only the received data. then you will come to this page.HYPER TERMINAL Hyper Terminal is used to see the serial port data. Run Hyper Terminal then some screen come for asking area codes. If you are using serial port for your internet you have to disconnect it for acessing serial port. 58 . All the data is show in ASCII. put some garbage value and press OK and in the next page press OK. Go to Start_Programs_Accessories_Communicatin_Hyper Terminal.

then I can't say that Hyper Terminal will work. 59 .If you use some USB to RS232 converter.

Device Manage COM Properties 60 .See in the device manager for the port settings.

If you are getting the alphabets you typed then your connections are proper.Testing your computer Serial Port: Short circuit Rx and Tx of your computer and type letters keeping hyperterminal working. By default the TTL output is 5V. Max232 is working fine. then short circuit the Rx and Tx of max232 ie pin 11 and pin 12 and type something in hyperterminal. if you type 'a' then it is displayed as 'a' on the hyperterminal) then you can infer that your serial port will work properly. Now connect serial port to max232. it should be mostly in the range -8V to -10V. If you can see what you are typing(i. See the RS232 output corresponding to 5V input.e.leave all other pins open and just short circuit pin2 and pin3(Rx and Tx). Don't use ground and other pins. 61 . See these conditions properly otherwise your microcontroller will get burned because voltage greater than 5V is damagerous to the microcontroller. see what you are getting.

it shows only the received data. I am attaching a C program with it. Here I am using 2400 8 N 1 with 1Mhz internal crystal. So be thorough about the micrcontroller clock and baud rate you set. This is the most important error occurs with serial port.2%. Hyperterminal won't show the transmitted data. If you use a C program then you will get some different values other than the values you sent. Remember only one program can acess serial port at a time. But still error is around . SERIAL PORT TROUBLESHOOTING AND ERRORS: When synchronisation is lost you will get xxxxxxxxxxxxxxxxx displayed on hyperterminal. 62 . Bray's terminal is a free software with more options than hyperterminal. You can use terminal program inbuilt in WinXP or Bray's terminal for serial port interfacing with computer. See the error in baud rates for different clock frequencies. If you want more speed then go for internal 8Mhz crystal.FOLDER:serial_computer Here this code will send back next ASCII you type. so if you want 0% error in baud rate go for crystal 3.6864Mhz or crystals given in the datasheet which is having 0% error in baud rate TransmitByte(ReceiveByte()+1). Sometimes Turbo C is having problem in acessing serial ports.

See the connections 63 .

64 .

SERIAL PORT with LCD DISPLAY

FOLDER: serialport_lcd

65

66

67

First we receive the data from serial port and send it to LCD first then it is again send back to serial port. /* Set the baudrate to 2400 bps using a 1MHz crystal */ for(..//lcd initializations Init_Ports(). But this process will be having some delays because of the delays inserted in the LCD functions. Init_Lcd(). Lcd_Send(a).) /* Forever */ { a=ReceiveByte(). InitUART( 25 ). /* Echo the received character */ } First initialising LCD ports and start initialising baud rates. 68 . TransmitByte(a).

I used internal analog voltage reference of 2. use a .e.1uF and . But we are using it in 8 bit resolution. But there will always be noise error in the reading. If you want to read 10 bits then use ADC(see in the include file.01V.56 volt. I tried with . Remember to use a capacitor in the AREF pin. got error about . Atmeg32 has 10 bit ADC having two registers ADCL and ADCH. I used LM35. In 8 bit resolution mode we read ADCH. first 8 bits leaving last two LSB's). 69 .INTERNAL ADC OF AVR See the datasheet for the registers. See in the datasheet how to avoid noise better.01uF capacitor. See even conversion is of 10 bit resolution and we are using 8 bits resolution(i. The choice of capacitance will affect the reading. so that error will be lowered. Here I am using LM35 temperature sensor to ADC input. you will understand better).01uF capacitors.

70 . //start conversion while(!((ADCSRA&_BV(ADIF))>>ADIF)).e.FOLDER:lm_2-7seg See the problem with persistance of vision. The 8 bit converted value is there in ADCH register and we are returning the values. ADCSRA|=_BV(ADSC). Here the temperature varies with time and it is shown on LED i. I am waiting for the conversion to end and we are using 8 bit conversion mode. Here I am using single conversion mode. it is shown a non decimal value. return ADCH.

//clock/32 speed } int Read_ADC(void) { ADCSRA|=_BV(ADSC).ADC with SERIAL PORT and LCD void Init_ADC(void) { ADMUX|=(_BV(ADLAR) | _BV(REFS1)| _BV(REFS0)) . //normal conversion without interrupt ADCSRA&=~(_BV(ADSC)|_BV(ADATE)|_BV(ADIF)|_BV(ADIE)| _BV(ADPS1)).56V AND 8 BIT CONVERSION ADMUX&=~(_BV(MUX0)|_BV(MUX1)|_BV(MUX2)|_BV(MUX3)| _BV(MUX4)). //SELECTING CHANNEL0 ADCSRA|=(_BV(ADEN) | _BV(ADPS2) | _BV(ADPS0)) . 71 . //start conversion while(!((ADCSRA&_BV(ADIF))>>ADIF)). return ADCH. } See the ADC declarations and initializations. // RFS1 and RFS0 are for Internal Reference voltage of 2.

i<2. if any change occurred then it is send it to the LCD and serial port.i++) Lcd_Send(str[i]). for(i=0. //for next value //delay sometime (1s) otherwise LCD display will run delay(1000). we convert decimal to ASCII TransmitByte((j+0x30)). 72 . Lcd_Send((j+0x30)). TransmitByte((k+0x30)).i++) { TransmitByte(del[i]). we are seeing for any change in the temperature reading. } //for LCD we require initialiatization Init_Lcd().i<12. Lcd_Send((k+0x30)). //since both will display ASCII values. We are using 'BACKSPACE' to delete the previous values in case of serial port. Here I am clearing LCD and writing to it . but you can do other way by selecting the instruction register and moving to location 0x8c and 0x8d to write the temperature on that locations. k=k%10. k=a/10. j=a%10. b=a.if(a!=b) { //first delete the previous values for uart for(i=0.

Refer datasheet for the baud rate before setting it. 3. See the Hyper Terminal settings.TROUBLESHOOTING: 1. Better to use a maximum error of . 4. 5. 73 . see the bios settings.3%. about the port is it COM1 or any other port. Put noise cancellation capacitance in the AREF pin. See the baud rate errors.2%.better don't go for baud rates of errors 8. 2. Set the baud rate according to it. Always remember about the fuse settings and frequency of the crysal you are using.

} Here I am using external clock for running the timer. } while(1). The sensitivity of a sensor is in the range of milliseconds or 100 uSec not more than that and we are mainly processing the sensor signals. 74 .TIMERS void main() { InitUART(25). TCCR0|=(_BV(CS02)|_BV(CS01)). do { TransmitByte(TCNT0). But atmega32l works from 2. You might have noticed that it works with the voltage from the parallel port without connect any Vcc to the chip. I don't think clock frequency does not matter much for us since atmega32l maximum frequency is 8MHz.c” for serial port transmission. Cost difference is around Rs. This is the main advantage of using microcontroller instead of normal transistors and OPAMPS for robotics. After programming the chip. Suppose if you make a robot using some transistors and OPAMP'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.still we are getting 8000 cycles for processing.7V – 5V and with even low voltage.50 or more and second thing it works on low power which is very adavantegous in robotics because the power supply won't be consumed much. The clock is set falling edge. Include “serial. This shows how less is the power required to run the chip. Which one should I choose Atmega32 or Atmega32L?? I prefer Atmega32l because its low power consumption and cost. switch off the adapter then also avr works with the voltage from parallel port. Better connect T0 pin to Vcc or ground. This much time is more than enough for us. But if you keep T0 pin open then the counter will run counting. delay_ms(500).

IMPORTANT THINGS TO REMEMBER FOR MICROCONTROLLER TROUBLESHOOTING 1. be thorough with what you are doing. Apply proper noise cancellation. Suppose if you use external clock then before doing fuse bits you should have a proper crystal. Always keep a screenshot of the fuse bit settings you are doing. See for the baud rate error corrections.2%. 75 . Before doing fuse bits. 2. Use proper capacitance values. Don't waste a full port for the use of a single input or output. 4. Better use maximum possible error to . 6. See your parallel port and serial port settings on BIOS before starting with microcontrollers. See my previous books for more troubleshooting. Do proper commands. This is a serious problem. 9. Always see that the voltage (Vcc) to the microcontroller should not be greater than 5V. Enable the proper modules only. Always see the register settings. See the breadboard problems. 7.mainly _BV(). 8. Main reasons which cause AVR to burn: microcontroller voltage>5V fuse bits had done wrongly(lock bits especially) 5. 3. 10.

php?name=News&file=article&sid=112 lcd http://www.eio.co.com/lcd_project.htm http://www.php?name=Forums&file=viewtopic&t=1438 sonar http://www.htm sharp sensor http://www.roboticsindia.com/modules.html 76 .html http://www.shtml http://hamjudo.co.htm http://home.co.roboticsindia.iae.nl/users/pouweha/lcd/lcd0.riccibitti.cleversoul.shtml#hd44780 http://ee.uk/htm/srf08tech.leang.htm http://www.name/software/lpt/ cro http://www.htm http://neil.htm http://www.or.php?name=News&file=print&sid=75 http://www.blueink.beyondlogic.com/robotics/info/articles/minison/minison.com/CLASS/physcom1/SharpGP2D12.htm ir vs sonar http://www.robot-electronics.com/sonar/ http://www.uk/htm/srf04tech.uk/htm/srf04tech.php?name=Forums&file=viewtopic&t=1189 sms http://www.com/lcdintro.com/tinyplanet/tiny_article.robot-electronics.interq.rdrop.IR led identification http://roboticsindia.jp/japan/se-inoue/e_pic6_6.org/parlcd/parlcd.robot-electronics.roboticsindia.net/modules.fraser.com/modules.com/~cary/html/ultrasonic.html http://www.net/modules.

com/devreler.net/infrared/infrared1.vbip.htm 77 .htm general http://homepage.speech http://www.html http://scv.asp http://roboticsindia.shtml http://www.hispeed.ustr.php?module=FreaksAcademy&func=viewProjects& startnum=76 software http://www.edu/GC/shammi/ir/ http://www.com/modules.com/digitaltach/ remote control http://home.com/stampic/links.ch/peterfleury/ avrfreaks user projects http://www.php?name=News&file=article&sid=115&mode=& order=0&thold=0 7segment http://www.php?module=FreaksAcademy&func=viewProjects& startnum=76 tachometer http://www.ustr.com/7segment.geocities.sbselektronik.net/index.shtml http://www.xdesignlabs.ustr.avrfreaks.avrfreaks.bu.earthlink.iguanalabs.com/miscellaneous/Speech-Recognition.asp http://www.net/~henryarnold/page33.net/infrared/sony.net/ good link http://www.net/index.

roboticsindia.net/~symes/Mega32/CandMega32.php?name=Forums&file=viewtopic&t=1318 http://www.net/modules.avrfreaks.net/~symes/CwithAVR/IntrotoCwithAVR.net/modules.shtml http://www.roboticsindia.users.htm good tutorials http://www.php?name=Forums&file=viewtopic&t=1327 http://www.php?name=PNphpBB2&file=viewtopic&t=36235 programmers http://www.net/index.php?name=Forums&file=viewtopic&t=1412 &p=7120 http://www.net/modules.net/modules.on.users.fuse bits http://www.avrfreaks.net/index.roboticsindia.roboticsindia.php?name=PNphpBB2&file=viewtopic&p=207381# 207381 http://www.avrrepository.net/modules.roboticsindia.tuxgraphics.html avrdude problem http://www.htm] www.on.com/samplecode/sd_breakout/doc/index.php?name=Forums&file=viewtopic&t=1332 http://www.com/modules.php?name=Forums&file=viewtopic&t=1241 78 .org/electronics/200510/article05101.roboticsindia.php?name=Forums&file=viewtopic&t=1332 http://www.net mmc card interface http://www.avrbeginners.

html http://www.roboticsindia.php?name=News&file=article&sid=35&mode=&o rder=0&thold=0 http://www.php?name=Forums&file=viewtopic&t=1427 &p=7184 79 .com/robotics/info/articles/irlinear/irlinear.techgeek.phtml?topic=998&subtopic=996&item=10 17 http://www.ir http://roboticsindia.net/modules.com/modules.com/subpage/index.acroname.

Sign up to vote on this title
UsefulNot useful