Interface the Atmega 16/32 with the PC

May 18, 2009 at 20:19 (microcontroller, Uncategorized)

There are many ways to interface your microcontroller to computer , the most easiest way is using serial port and MAX-232.In this tutorial is about how to interface a ATMEGA32 to your computer using serial  port , max232 and programming the atmega32 using CodeVisionAVR.


ATMEGA32 is a 8-bit AVR microcontroller with 32K Bytes In-system Programmable Flash. In Atmega32pins pin no.14 and 15 are RxD and TxD resp. For basics of atmega32 click here


In computing, a serial port is a serial communication physical interface through which information transfers in or out one bit at a time.Throughout most of the history of personal computers, data transfer through serial ports connected the computer to devices such as terminals and various peripherals.

MAX232: As we know for serial port we require TIA/EIA-232-F voltage level i.e. it can accept ±30-V. MAX232 has a capacitive voltage generator to supply TIA/EIA-232-F voltage levels from a single 5-V supply. Each receiver converts TIA/EIA-232-F inputs to 5-V TTL/CMOS levels.

So, to interface the atmega32 to the computer we will be connecting atmega32 (pin no 14 & 15) to serial port via max232. We are using max232 because atmega32 works at TTL voltage level and for serial port we require other voltage level, max232 converts the TTL voltage level to the required voltage level.

Atmega32 will receive the signal from MAX232 and transmit to max232 which will be received by serial port. Do the following connections:

  1. connect pin no 2 (Tx) of serial port to Tx out of the MAX 232 IC (pin no 14)
  1. connect pin no 3 (Rx) of serial port to Rx in of the MAX 232 IC (pin no 13)
  2. connect pin no 14 (RxD) of Atmega to Rx out of MAX 232 IC (pin no 12)
  3. connect pin no 15 (TxD) of Atmega to Tx in of MAX 232 IC (pin no 11)
  4. Do other necessary connections like ground and Vcc.


  1. By selecting UART tab of the CodeWizardAVR , you can specify the UART configuration
  2. Specify the baud rate and communication parameter.
  3. Click on save, generate and exit.
  4. Write the code.
  5. Burn the program.
  6. Open the terminal in CodeVisionAVR TOOL—-à TERMINAL (shift + F5).
  7. Specify the baud rate and communication parameter.(ensure that you specify the same parameter which you have specify in step 2)
  8. In terminal you monitor the whole operation.


NOTE: instead of terminal of CodeVisionAVR you can use other software like DockLight.

It allows us to specify the following communication modes:

  • Asynchronous
  • Synchronous Master, with the UCSRC register’s UCPOL bit set to 0
  • Synchronous Master, with the UCSRC register’s UCPOL bit set to 1
  • Synchronous Slave, with the UCSRC register’s UCPOL bit set to 0
  • Synchronous Slave, with the UCSRC register’s UCPOL bit set to 1

The serial communication is realized using the Standard Input/Output Function getchar , gets , scanf , putchar , puts and printf.


If we want to write program in which, when button ‘a’ on keyboard is pressed then all the LED connected to PORTA should be ON , when button ‘s’ is pressed all LED should be OFF.

#include <mega32.h>

#include <stdio.h> // Standard Input/Output functions

Void main()




// USART initialization

// Communication Parameters: 8 Data, 1 Stop, No Parity

// USART Receiver: On

// USART Transmitter: On

// USART Mode: Asynchronous

// USART Baud Rate: 9600






while (1)


if (getchar() == ‘a’)




if (getchar() == ‘b’)






In this way you can interface your ATMEGA32 to your computer. In similar way you can control the motor (in semi-autonomous BOT).

u can also view this post @

………………….if you found this tutorial helpful or have any suggestions … do leave a comment.


Permalink 30 Comments

Which is better.. PIC or AVR ?

April 11, 2009 at 22:42 (microcontroller)

Ways in which PICs are better than AVRs

Instruction set/architecture (for Assembler users)
PIC good : Small instruction set means you can literally learn it in a day. Skip instructions can often simplify flow-control.

AVR bad : highly nonorthogonal instruction set – many operations can only be done on certain registers. Relative branch range can force code to be arranged in a non-logical order. No immediate XOR instruction. Illogical mixture of skip and branch flow-control instructions (Now if only they’d made STATUS one of the registers you could do skips on….!).

Clocks etc.

PICs (12Fxxx etc.) have more accurate calibrated RC oscillators.

Watchdog wakeup from sleep continues after SLEEP instruction – on AVR it causes a reset, which can complicate things.


Interrupt latency is constant – if you set up a regular timer interrupt on a PIC, it will occur at exactly the same interval every time. On the AVR you will get at least one cycle jitter.

I/O and bit-twiddly things
I/Os are the same as other file registers, so operations other than bit set/clear operations take less work – bits can be toggled using XORWF, multiple bits can be set or cleared simultaneously using ANDWF or IORWF, and you can do neat tricks like using an output port as a shift-register for really fast pattern generation. The latter is not often useful, but does allow neat stuff like video generation.

Ways in which AVRs are better than PICs.

Instruction set/architecture

AVR good : Add with carry, and compare with carry simplify multiple precision arithmetic. Good range of conditional branches.

PIC bad : Unconventional mnemonic names.. Only one pointer for indirection. Inefficient lookup tables. No add/subtract with carry, small stack. Paged architecture becomes a real pain on larger projetcs, although the PIC18 architecture addresses (pun intended!) many of the issues.

Clocks etc.
Wider range of RC oscillator speeds. PLL high-frequency IO clock on some parts for high-speed captre etc.

More instructions per oscillator clock, but as most instructions take 2 cycles, and more instructions are often necessary for IO intensive tasks, this advantage is 2:1 at best. Most embedded apps are not especially throughput-dependent. Also, crystals and resonators get bigger and harder to get much below about 3MHz, so the speed advantage over a 4MHz PIC can often not be a major issue.

Watchdog can be turned off by software.

TX and RX can be enabled seperately – useful for multi-processor comms on a common bus. High baudrates are possible from low clock frequencies.

Vectored interrupts are more efficient where there are multiple interrupt sources, and registers can be reserved for interrupt-only use to speed up context save for low latency.

Seperate PORT and PIN registers avoid read-modify-write issues with capacitively loaded pins. (Although has any AVR user never spent time wondering why their input port isn’t working because they used PORTx instead of PINx…? ).

And the winner is…..

If you’ve read the above, you’ll have realised that the answer is Neither of course, it all depends on what your requirements are, however that in almost all fields of enginering, there is simply no such thing as “Best”. The correct phrase is “Most appropriate for your application”

………………….if you found this tutorial helpful or have any suggestions … do leave a comment.

Permalink Leave a Comment

Basics of atmega16/32

February 8, 2009 at 23:36 (microcontroller)


Atmega16/32 is atmel product. they are of megaAVR series , they have 16Kb and 32Kb Flash memory resp. like other AVR uc they are ISP(in system programmable ) i.e u can program it without taking it out of the system.


  • 16/32 Kb In-System Self-Programmable Flash
  • 1024 Bytes EEPROM
  • Two 8-bit Timer/Counters with Separate Prescalers and Compare Modes
  • One 16-bit Timer/Counter with Separate Prescaler, Compare Mode, and Capture Mode
  • Four PWM Channels
  • Programmable Serial USART
  • Internal Calibrated RC Oscillator
  • Power-on Reset and Programmable Brown-out Detection
  • 32 Programmable I/O Lines

and many more…


As mentioned above it has 32 I/O lines that means it has 4 I/O ports(A,B,C,D).Every i/o port has 3 registers associated with each ports.These three registers are

  • DDRx(data direction register)
  • PINx
  • PORTx

x -A,B,C,D


This register configures data direction of port pins , means by using this register microcontroller decides whether this port will be used as input port or output port. In 8051 if we will declare any port as FF it becomes input port but in atmega it is opposite if we declare FF it becomes output port .

e.g To make all pins of  port D as input pins


If we want to make first 4 pins of portc as input and remaining 4 as output then


PINx register:

PIN(port IN) used to read data from port pins

e.g To read data from portA


Y=PINA  // data from port A will be stored in variable Y

PORTx register:

PORTx is used for two purpose-

  1. TO O/P DATA: when port is configured as output.when corresponding bits in DDR is set 1.
    DDRA=0b11111111x=0b10101010PORTA=x  // now content of port a will be AA
  2. To activate or deactivate pull up registers: This when u configure a port as input port. In input mode , when pull up is enabled , defaulted as 1. So, even if you dont connect anything to the pin and if you try to read it , it will read as 1. Now , when you drive pin to zero , only then it  will read as 0.


  • To make portA as i/p with pull-up enableDDRA=0b00000000
  • To make portA as tri-stated i/p

Using this basic knowledge we can easily write a program(I/O) for atmega.

u can also view this post on


………………….if you found this tutorial helpful or have any suggestions … do leave a comment.

Permalink 14 Comments

Programming atmega using USBasp

December 14, 2008 at 08:30 (microcontroller)

For burning a  program in a AVR uc using usb port instead of parallel or serial then USBasp is the best option. For circuit diagram of USBasp click here. following are the procedure for burning the hex file

  • Install the avrdude (winavr) ,it can be downloaded from click here

  • Download USBasp driver ,you can download it  from click here

  • Connect cable directly to USB port of computer .

  • Wait for windows information “New Hardware Found USBASP”. If device isnt detected, check the board for errors and reconnect again;

  • After device is detected Driver setup wizard opens. Select where USBASP driver is located. It is in directory you’ve downloaded with firmware. If you unpacked in D: drive, than driver should be in D:\usbasp.2006-12-29\bin\win-driver\ and press next:

  • there are many GUI available on net , using which u can burn the code , i think AVR8_Burn-O-Mat was the easiest one which can be downloaded from click here , dis GUI is self explanatory .

  • select the uc , specify the hex file

  • click on write , it will show msg flash successfully written.


  • click on verify to verify.
  • if got some error msg instead of flash succesfully written , these may be the reasons for the error
  1. avrdude: error: could not find USB device “USBasp” with vid=0x16c0 pid=0x5dc , reason may be

    “USBasp” is not Connect , check your connector or u haven’t installed the driver (install it) click here

2.   avrdude: error: program enable: target doesn’t answer. 1 , reason for dis error

  • Programmer isn’t connected with Target circuit
  • ISP Connector is bad connection
  • Target circuit power is OFF

3.   avrdude: verification error , first mismatch at byte 0x002a0x31 != 0xb1 , reason is verify error so try again.

4 .  if you got an error message “flash successfully written error while  verifying flash” the reason for this error is the power supply , Vcc which you are providing is less than the specified value 5 – 4.5 V .

……..if you found this tutorial helpful or have any suggestions … do leave a comment.

Permalink 16 Comments