AVR - Programming:

You have read concepts and required basic theory in previous sessions.  Now, we are moving towards practial programming of an AVR micro-controller.  A Personal Computer or laptop, which can run atleast Office programs/tools (like word / spread sheets etc.) is essential for programming AVR micro-controller.

For, testing and practice, you may select any AVR micro-controller.  But, due to availability of four ports, having most of the communication types, more ADC channels and having ISP (In-System Programming) pins of programming port at one place (ease and convenience in soldering on PCB), ATMEGA16 is selected for explanation.

 

The actual ATMEGA16/32 micro-controller board developed for testing and pinouts of ATMEAGA 16/32 are shown here for conenience.  Many ATMEAGA 16/32 development boards are available in the market, but, making a board for yourself, makes you easily understand concepts of working, more space for practice and proto-typing in future.  The most important advantage is more flexibility, while developing your own project.

ATMEGA16 PINOUTS.png

Programming of AVR micro-controller has two parts.

1) Software or Coding

2) Hardware or programmer.

ATMEGA16 board2.jpg

Software or Coding:

You may use any programming software available, by downloading from internet.  Most of the softwares are free (or limited or GPA agreement) to download and program for personal or educational use.  The program logic and sequence are almost same for all softwares, but,  syntax of the statement(s) may vary.

Here, we use AVR Studio 4 (uses C-language for coding, which is easy to understand for starters) which is easy to install, understand and requires less disk space.  You have to download the AVR Studio 4 software and tool chain package  Then install both the softwares on your system (PC) consecutively as per your OS (Operating System).  Once you are comfortable and understand the programming, you may use next versions or other softwares for programming AVR.

Link to AVR softwares download page (courtacy Microchip):

https://www.microchip.com/mplab/avr-support/avr-and-sam-downloads-archive

Using AVR Studio:

Once installed AVR Studio 4,  click the shrotcut for AVR Studio 4, available on the desktop. Then, follow the 4 steps as shown below:

AVRS4_Step_1.jpg
AVRS4_Step_2.jpg
AVRS4_Step_3.jpg
AVRS4_Step_4.jpg

Now, start writing the code in C-language, where space is provided.  Once, your code typed, then select "Build All" from "Build" menu on the top.  Don't forget to save the project and files.  After compiling the source code (written in C-language), the output files will be generated.  Out of which the .HEX file is to be written (or burn) to the AVR micro-controller using AVR programmer, which is explained below.  The .HEX file is available at project-path\project-name\default\project-name.HEX

AVRS4_Step_5.jpg
AVRS4_Step_6.jpg
AVRS4_Step_7.jpg

Hardware or Programmer:

AVR programmer is required, to write the HEX code generated by the AVR Studio to the flash memory of the AVR micro controller.  AVR may be programmed many ways like, Parallel programming, JTAG, ISP etc., You may use any programmer available in the market.  Most of the programmers use USB port for programming.  

For new programmers,  USB-ISP programmer is suggested for its simplicity, availability and uses less number of pins of micro-controller while programming.  These programmers come with USB connector one side and 6 or 10 pin connector for ISP port on the other side with a small module / board is connected in between. The requied drivers and programming software (IDE) matching to the hardware has to be installed on the system (PC).

The pinouts / orientation of three types of headers available are shown below.  The first one being widely available, I feel the third one is more comfortable for programming AVR in ISP mode.  The header connections may be modified as 6 pin straight model (like third one), using 6 pin relimate connector with wires. The reason for straight header is, out of 6 pins, most of the AVR contans atleast 3 pins in series and matches the sequence of the MOSI-MISO-SCK.

For the ATMEGA 16/32, all the six pins matches the pinouts/sequence of the third ISP header.

ISP_headers_3.png
AVR_ISP_PRGR2.jpg

Using the USB Programmer:

When you purchase any AVR programmer, an installation software is supplied along with it or supplier provides link to download the required software.  Otherwise, many USB programmers are available on-line to download, like AVRDUDE, WINAVR, USBASP, BURN-O-MAT, PROGISP, PONYPROG etc..  These are simple and easy to install and use softwares for programming AVR micro controllers.  

 

One of the AVR programmer (IDE) software, which is easy to install and use is PROGISP is explained here.  Once downloaded and installed the PROGISP, then run executable file (progisp.exe) or the other software downloaded and installed by you. 

 

Change the port to USB, if required or indicated in installation procedure of the particular programmer.  Press calibration button once, if available in the menu and let the software check and match the programming frequiencies.  For some softwares, these steps may be optional or not required at all.

For any AVR programmer, first, select the type of AVR micro controller is to be programmed (eg: ATMEGA16 or ATTINY13 etc.).  Then select "Load Flash" or equivalent option from the menu.  Now, select the HEX file from your computer, which was generated by the AVR Studio (on compiling and building the code).  The HEX code will be loaded to the programmers buffer memory.  

Now, press "Erase Chip" and "Write Flash", consecutively.  Most of the AVR programming softwares, Erase the Chip before Writting the Flash to AVR by default.  Then, a progress bar or status is displayed, while programming the AVR micro-controller.  Once, the HEX code is written/burnt to the AVR micro controller, the status window shows "Success" message or "Error" message with error code.

Once, the HEX code is written to the AVR micro-controller, then the AVR starts working as per the HEX code loaded to it.  Now, disconnect the AVR programmer hearder from the project AVR board.   In case of error, go through the error message and correct the settings or code, if any.  Now, try writting the code to the AVR micro-controller once again.

progisp-scr-3_SMALL.jpg

SUMMARY:

The above explained process is to be done initially.  Once, installed all softwares and drivers as required, then, every time write your code with your own logic using AVR Studio and create HEX code.  Then write/burn the HEX code to the AVR micro controller using the AVR programmer.

 

That's all.   BEST OF LUCK.