Docstoc

AVR

Document Sample
AVR Powered By Docstoc
					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

HYPER TERMINAL................................................................................58 Device Manage COM Properties..........................................................60

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

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

5

GETTING STARTED First you should require a compiler which converts your program into the hex code of the avr microcontroller. If you use C for programming Avr then you can use WinAVR, CodeVision AVR, ImageCraft AVR, BASCOM AVR for programming in BASIC, AVRStuidio for programming in assembly. But here I am talking about C programming. I use WinaVR2006 for all these programms compilation. Same code is valid for AVRGCC in Linux. Second requirement 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 BSD programmer for that.

LET'S START Install WinaVR2006 to C drive. Then you can see that your desktop has the following programs.

The main program we use is the programers Notepad. So let's start with first program.

6

7

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.c

Store it in a folder named blink. How to compile it ? Go to Tool->Make. > "make.exe" all make.exe: *** No rule to make target `all'. Stop. > Process Exit Code: 2 > Time Taken: 00:00

8

Now you have to create Makefile to compile the C program. Open

and do the following

9

10

11

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

12

13

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

14

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

This is valid for all microcontrollers of AVR which are supported by AVRDUDE programmer. See how to do it for atmega32

15

See my programmer.

16

17

Instead of using Vcc of supply you can use any pin on the data port(D0) for Vcc of AVR.I am using breadboard instead of a ready made PCB for programmer.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). If your microcontroller had done fuse bits then you have to use a proper crystal. I will explain about it in Fuse Bits section.

18

Now go to Tools-> Program

19

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

20

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

21

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

22

If you are not working with WinAVR2006 and you are using AVRStudio, then you can use the same programmer. Only difference is that you have to work in text mode. Install AVRDUDE then execute install_giveio.bat for acessing ports. Now change to the avrdude\bin> directory and do the following shown

Here f.hex is the hex file we had to load into the chip. If you installed WinAVR2006 then change the director to C:\WinAVR\bin> and execute the above command. See the AVRDUDE documentation for more about it.

Now check the program loaded into the memory. 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). You can see that all ports except PORTC pins C2,C3,C4,C5 are not blinking. This is because your chip's JTAG has been enabled. If you want to use PORTC pins further then you have to disable JTAG. This is done by using fuse bit settings.

23

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

24

Use 'part' command to display details about the microcontroller

25

SETTINGS FOR EXTERNAL CRYSTAL AND JTAG DISABLE See below how I had done fuse settings for external crystal. You can see some error occurred, 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 3-8Mhz crystal. Compare what i had done with that of in datasheet.

26

IMPORTANT NOTES ON FUSE BITS: 1. Always keep a screenshot fuse bits settings you do for future reference. 2. Do Fuse Bit's at your own risk. See the link below http://www.roboticsindia.com/modules.php?name=Forums&file=viewtopic& t=1412&p=7120 3. Do remember the fuse settings. 4. The crystal you use should be proper to produce the clock to the crystal. 5. Use proper capacitors. I used 22pF,33pF even 150pF. But better to use the prescribed values in the datasheet. 6. Mostly you will get problem after fuse bit settings, so be thorough with fuse bits before you are doing. 7. If you are doing fuse bits for external crystal, remember that after doing fuse bits you can't even program chip without a proper crystal and capacitors.

DISABLING JTAG
After disabling JTAG you can use PORTC properly. Pins C2,C3,C4,C5 are properly used for other applications. i.e, we got one port for other applications. When I brought my new microcontroller for the first time I thought that the microcontroller was not good because PORTC pins are not working, then I later came to know that it is due to JTAG only.

27

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

DISABLING JTAG AND USING INTERNAL 8Mhz CLOCK You can see that serial port baud rate depends on the clock. So if you use 1Mhz clock then the maximum speed possible with less errors is 4800bps (.2% error). But for high baud rate cannot be used with 1Mhz internal clock, so we have to use 8Mhz internal clock, then you can use all baud rates with less error.

In the first case after changing the fuse bits I removed the RESET pin to see whether I got 8Mhz. It worked according to the previous program: blink.c, I got fast blinking. Don't remove the programmer before you leave the terminal mode of AVRDUDE.That is what happened in the first case.

29

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. Here control pins are connected to PORTD and datapins to PORTA. See the figure below.

30

31

32

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. Here CONTROL_MASK is used so that remaining pins can be used for other purpose. Change the port settings here so that you can use same code for all ports. 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. Practice this when you do programming. Rest program remain same as that I had given my previous book for parallel port.

33

SOME IMPORTANT COMMANDS USED FOR PROGRAMMING You might have seen command _BV(a) the equivalent of it is 1<<a , 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); Here we are using logic OR to set a pin, remaining pins remain same because according to logic operations A+1=A(OR gate) and A+0=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)); ~(_BV(4) ) = ~(00010000) = 11101111 This value logic AND with PORTD and writing it to PORTD again will make that bit clear. Remember A.1=A and A.0=0

34

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

35

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

36

/*----------------------------------------------------------------------------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. L293D_Mask,Input_Mask is used so that remaining pins of the port can be used for other purposes. /*--------------------------------------------------------------------------------FUNCTIONS TO INITIALIZE PORTS------------------------------------------------------------------------------------*/ void Init_Ports(void) { L293D_DDR|=(_BV(Pin_1 )|_BV(Pin_2)); //setting pins for output L293D_PORT&=~(_BV(Pin_1 )|_BV(Pin_2));//starting motor is Off Input_DDR&=~(_BV(5)|_BV(6));//making as input pins } Here the L293 DDR is used for ouput and L293D_PORT is set LOW to stop motor at the starting. a=((Input_PIN&_BV(IPin_1))>>(IPin_1)); b=((Input_PIN&_BV(IPin_2))>>(IPin_2)); These statements give a,b= 0 or 1. I used right shifting operation here. Now you can drive a robot using L293D.

37

BIPOLAR STEPPER MOTOR DRIVING USING AVR

38

for more about stepper see my previous books

39

/*----------------------------------------------------------------------------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,13,12,14}; the steps sequence used for the bipolar stepper motors are shown in arrya. This will make 4 steps. Repeat this sequence again for continous motion. FOLDER:bipolarstepper

40

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

41

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,3,6,12}; Here only steps changed,rest remain same. Remember to put the program in infinite loop for continuous rotation of motor.

42

KEY ENCODER INTERFACING

FOLDER:74922

43

/*----------------------------------------------------------------------------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. You can see that all pins are input except Ouput Enable. So you have to configure that pin for output and it should be active low. IC_DDR=IC_Mask; //setting OE pins for output,remaining for input Here I take input from PORTC and outputting the key pressed to PORTD. See the capacitor values otherwise keydebouncing will occurs, it should be C and 10C respectively. See the datasheet for more reference. Now let's see the keypressed on LCD. So I am using the previous program in the folder 'lcd' to a normal .c file after removing the main() function. Put that .c file in same directory and send the value to the LCD. Before sending we should make it ASCII value for displaying on LCD. '0' in hexadecimal corresponds to 48 in ASCII. We have to do same with alphabets also. See the code, you will understand better. See the diagram below for the circuit.

44

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

/*----------------------------------------------------------------------------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. FOLDER:one7seg do { Display(i); i++; if(i>9) i=0; delay_ms(500); } while(1); These statements run the 7-segment from 0 to 9 with a delay of 500ms. Display is used to display the value on 7-segment. Here I used two 7-segment, i showed in diagram, but both are common and no need for one 7-segment.

47

MULTIPLEXED 7-SEGMENT DISPLAYS Here we use the technique of persistance of vision. i.e, our eye cannot detect what is happening in 1/16 of a second. So any happening within that time will be seen continous by our eye. This technique is used in television, even used to program CRO's for some games.

I am using 74126, tristate buffer to give power supply to the 7segment. You can use transistors in case of common cathode display. But here I am using common anoder displays. FOLDER:2-7seg 48

IC TESTER

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

49

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

50

UART of AVR

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

51

SERIAL PORT OF COMPUTER

52

BIOS SETTINGS

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

53

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). RS-232 of the computer has LOGIC HIGH in the range of -3 to -25V( mostly around -8.5V) and LOGIC LOW around 3 to 25V (mostly 8.5V). We had to convert it to LOGIC HIGH(5V) and LOGIC LOW(0V) of microcontroller, so we use max232 or you can use any other level converters. 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.70 and take the cable from it. But mouse serial port cable has only 4 pins Rx,Tx,DTS,GND, so it can't be used for programmer's of some microcontrollers. But in our case we require only 3 pins(Rx,Tx,GND). So it is better to go for a serial cable, I used one that of mouse.

54

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

55

56

57

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

58

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

59

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

60

Testing your computer Serial Port:
Short circuit Rx and Tx of your computer and type letters keeping hyperterminal working. If you can see what you are typing(i.e, if you type 'a' then it is displayed as 'a' on the hyperterminal) then you can infer that your serial port will work properly. Don't use ground and other pins,leave all other pins open and just short circuit pin2 and pin3(Rx and Tx).

Now connect serial port to max232, then short circuit the Rx and Tx of max232 ie pin 11 and pin 12 and type something in hyperterminal, see what you are getting. If you are getting the alphabets you typed then your connections are proper. Max232 is working fine. By default the TTL output is 5V. See the RS232 output corresponding to 5V input, it should be mostly in the range -8V to -10V. See these conditions properly otherwise your microcontroller will get burned because voltage greater than 5V is damagerous to the microcontroller.

61

FOLDER:serial_computer Here this code will send back next ASCII you type. See the error in baud rates for different clock frequencies. Here I am using 2400 8 N 1 with 1Mhz internal crystal. If you want more speed then go for internal 8Mhz crystal. But still error is around .2%, 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); You can use terminal program inbuilt in WinXP or Bray's terminal for serial port interfacing with computer. Bray's terminal is a free software with more options than hyperterminal. Hyperterminal won't show the transmitted data, it shows only the received data. SERIAL PORT TROUBLESHOOTING AND ERRORS:

When synchronisation is lost you will get xxxxxxxxxxxxxxxxx displayed on hyperterminal. So 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 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. Sometimes Turbo C is having problem in acessing serial ports. I am attaching a C program with it.

62

See the connections

63

64

SERIAL PORT with LCD DISPLAY

FOLDER: serialport_lcd

65

66

67

//lcd initializations Init_Ports(); Init_Lcd(); InitUART( 25 ); /* Set the baudrate to 2400 bps using a 1MHz crystal */ for(;;) /* Forever */ { a=ReceiveByte(); Lcd_Send(a); TransmitByte(a); /* Echo the received character */ }

First initialising LCD ports and start initialising baud rates. First we receive the data from serial port and send it to LCD first then it is again send back to serial port. But this process will be having some delays because of the delays inserted in the LCD functions.

68

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

69

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

70

ADC with SERIAL PORT and LCD void Init_ADC(void) { ADMUX|=(_BV(ADLAR) | _BV(REFS1)| _BV(REFS0)) ; // RFS1 and RFS0 are for Internal Reference voltage of 2.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)) ; //normal conversion without interrupt ADCSRA&=~(_BV(ADSC)|_BV(ADATE)|_BV(ADIF)|_BV(ADIE)| _BV(ADPS1));//clock/32 speed } int Read_ADC(void) { ADCSRA|=_BV(ADSC); //start conversion while(!((ADCSRA&_BV(ADIF))>>ADIF)); return ADCH; } See the ADC declarations and initializations.

71

if(a!=b) { //first delete the previous values for uart for(i=0;i<2;i++) { TransmitByte(del[i]); } //for LCD we require initialiatization Init_Lcd(); for(i=0;i<12;i++) Lcd_Send(str[i]);

j=a%10; k=a/10; k=k%10; Lcd_Send((k+0x30)); TransmitByte((k+0x30)); Lcd_Send((j+0x30)); //since both will display ASCII values, we convert decimal to ASCII TransmitByte((j+0x30)); b=a; //for next value //delay sometime (1s) otherwise LCD display will run delay(1000); we are seeing for any change in the temperature reading, if any change occurred then it is send it to the LCD and serial port. 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.

72

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

73

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

74

IMPORTANT THINGS TO REMEMBER FOR MICROCONTROLLER TROUBLESHOOTING

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

75

IR led identification http://roboticsindia.net/modules.php?name=Forums&file=viewtopic&t=1438 sonar http://www.robot-electronics.co.uk/htm/srf04tech.htm http://www.robot-electronics.co.uk/htm/srf08tech.shtml http://hamjudo.com/sonar/ http://www.rdrop.com/~cary/html/ultrasonic.html http://www.leang.com/robotics/info/articles/minison/minison.html http://www.robot-electronics.co.uk/htm/srf04tech.htm http://www.interq.or.jp/japan/se-inoue/e_pic6_6.htm

ir vs sonar http://www.roboticsindia.net/modules.php?name=Forums&file=viewtopic&t=1189 sms http://www.riccibitti.com/tinyplanet/tiny_article.htm sharp sensor http://www.blueink.com/CLASS/physcom1/SharpGP2D12.htm http://neil.fraser.name/software/lpt/ cro http://www.roboticsindia.com/modules.php?name=News&file=article&sid=112 lcd http://www.roboticsindia.com/modules.php?name=News&file=print&sid=75 http://www.beyondlogic.org/parlcd/parlcd.htm http://www.eio.com/lcdintro.htm http://home.iae.nl/users/pouweha/lcd/lcd0.shtml#hd44780 http://ee.cleversoul.com/lcd_project.html

76

speech http://www.vbip.com/miscellaneous/Speech-Recognition.asp http://roboticsindia.com/modules.php?name=News&file=article&sid=115&mode=& order=0&thold=0 7segment http://www.iguanalabs.com/7segment.htm general http://homepage.hispeed.ch/peterfleury/ avrfreaks user projects http://www.avrfreaks.net/index.php?module=FreaksAcademy&func=viewProjects& startnum=76 software http://www.avrfreaks.net/index.php?module=FreaksAcademy&func=viewProjects& startnum=76 tachometer http://www.xdesignlabs.com/digitaltach/

remote control http://home.earthlink.net/~henryarnold/page33.html http://scv.bu.edu/GC/shammi/ir/ http://www.ustr.net/infrared/sony.shtml http://www.ustr.net/infrared/infrared1.shtml http://www.ustr.net/

good link http://www.sbselektronik.com/devreler.asp http://www.geocities.com/stampic/links.htm

77

fuse bits http://www.roboticsindia.com/modules.php?name=Forums&file=viewtopic&t=1412 &p=7120 http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&p=207381# 207381 http://www.avrfreaks.net/index.php?name=PNphpBB2&file=viewtopic&t=36235

programmers http://www.tuxgraphics.org/electronics/200510/article05101.shtml http://www.users.on.net/~symes/Mega32/CandMega32.htm

good tutorials http://www.users.on.net/~symes/CwithAVR/IntrotoCwithAVR.htm] www.avrbeginners.net mmc card interface http://www.avrrepository.com/samplecode/sd_breakout/doc/index.html avrdude problem http://www.roboticsindia.net/modules.php?name=Forums&file=viewtopic&t=1318 http://www.roboticsindia.net/modules.php?name=Forums&file=viewtopic&t=1327 http://www.roboticsindia.net/modules.php?name=Forums&file=viewtopic&t=1332 http://www.roboticsindia.net/modules.php?name=Forums&file=viewtopic&t=1332 http://www.roboticsindia.net/modules.php?name=Forums&file=viewtopic&t=1241

78

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

79


				
DOCUMENT INFO
Shared By:
Categories:
Stats:
views:14817
posted:11/7/2008
language:English
pages:79