This is the personal website of Garthee, who believes it is the perspiration not the perception that brings the success
Introduction to Microcontrollers Using AVR
This article is intended to introduce AVR, justification of its choice in general applications, and development of simple applications with AVR.
AVR is a Harvard Architecture, 8 bit, single chip micro controller based on RISC architecture, developed by two Norwegian students Alf-Egil Bogen and Vegard Wollan. Generally, it comes with plenty of program memory starting from 1KB ranging to enormous 256KB. Package size varies from tiny 8 pin to giant 100 pin.
Flash is used for program memory and EEPROM is available for data storage together with SRAM registers (working memory), all integrated in a single chip. It strictly follows the Harvard Architecture with RISC instruction set, hence single level pipeline is inherent. This means the next machine instruction is fetched as the current one is executing. Most instructions take just one or two clock cycles, making AVRs relatively fast among the eight-bit Microcontrollers.
Justification of its adoption
Important eye-catching feature in AVR is, it is built with the C code in mind; hence, pointer support is available hardware. In addition, it guarantees the efficient execution of C programs. AVR development KIT is available as DIY pack and together with AVR studio 4 (4.13 freely available), winAVR and ponyProg robust applications on interfacing and other microcontroller operations can be deployed.
When comparing with other microcontrollers it provides the easiness of PIC family, advanced debugging features (AVR studio 4) similar to of Visual Studio, inherent support for C programming (contrast to traditional Assembly programming) through open source AVR gcc compiler, availability of advanced features like Capture and Compare module, PWM module, USART communication, Watchdog timers, interrupts and excellent jumping instructions (in C code) even in tiny cheap models.
Deploying solutions through AVR Microcontrollers
The rest of the article assumes, user is in possession of the following software tools.
- Win AVR - http://winavr.sourceforge.net/
- AVR studio Setup - http://www.atmel.com/avrstudio
- User guide - http://instruct1.cit.cornell.edu/courses/ee476/AtmelStuff/doc1019.pdf
- AVR gcc reference - http://hubbard.engr.scu.edu/embedded/avr/doc/avr-libc/avr-libc-user-manual.pdf
The following programs are used to explain the operation, and can be tested only using the simulation in AVR studio, hence user will have to simulate the inputs at several instances as specified. When a development board is available (can be purchased as KIT) it is better to be utilized efficiently, and programs with I/O can be tested through USART. However, AVR studio offers the benefits of an Integrated Development Environment, and at the initial stages of a complex program, it will become handy specially in debugging through breaking into the program code.
Sample programs, in increasing difficulty level
/* Program 1 – Basics */
#include <avr/io.h>
int main (void)
{
/* let’s start with ports */
DDRA = 0xFF;
//make PortA output
/* Turn on Internal Pullup resiostors on all pins of PORTD as */
DDRD = 0x00;
/* make PortD input*/
PORTA = PIND;
/* trigger the change in PortA equal to PortD while PORTD becomes equal to '1' */
while (PORTA = PIND) ==0x1);
/* running lights for a while - time extended by double for loops */
int i; int j;
for (j =0; j<250; j++) {
for (i=0;i<250;i++)
/* do this for ever */
{
if(PORTC)
PORTC = PINC<<1;
else
PORTC = 0x01;
/* set Port C to value read */
}
}
}
/* End */
-----------------------------------------------------------------------------------------------------
/* Program 2 – Lower Intermediate */
/* Program explains how long jumps are used in place of short jumps i.e. goto statements */
#include <setjmp.h>
jmp_buf set;
int t;
int main (void)
{
t = 1; //start here with t=1
if (setjmp (set))
/* this will be entered only if it is returned from a jump */
{
t = 3;
}
if (t!=1)
/* execute of if the above error didn't happen */
smaplef();
t = 4;
/* we come here finally */
while(1);
}
void smaplef (void)
{
t = 2;
// ensure we came here
longjmp (set, 1);
/* error occurred lets return to the handler */
t = 5;
// we never came here
}
/* End */
-----------------------------------------------------------------------------------------------------
/* Program 3 – Upper Intermediate */
/* EEPROM writing */
int main (void)
{
uint8_t x;
const char *temp = "hello";
char t[20];
/* wait till it becomes ready*/
while (!eeprom_is_ready());
/*write single byte*/
eeprom_write_byte ((uint8_t*)0x10, (uint8_t)2);
/*read it back to check*/
x = eeprom_read_byte ((uint8_t*) 0x10);
/* wait till it becomes ready*/
while (!eeprom_is_ready());
/* write a block of bytes "hello"*/
eeprom_write_block (temp, (char *)0x10, strlen(temp));
/*read it back to check*/
eeprom_read_block(&t, (char*)0x10, strlen(temp));
}
/* End */
-----------------------------------------------------------------------------------------------------
/* Program 4 – Advanced */
#include <avr/io.h>
#include <stdio.h>
/* UART Baud rate calculation */
#define UART_CPU 4000000 // 4Mhz
#define UART_BAUD_RATE 9600 // baud rate
#define UART_BAUD_SELECT (UART_CPU/(UART_BAUD_RATE*16l)-1)
int main(void)
{
int n,i, tempV=0, tempold=0, count=0;
char temp[10];
/*use all pins on PortC for output */
DDRC= 0xFF;
/*enable USART transmission and reception*/
UCSRB = (1<<RXEN)|(1<<TXEN);
/* Set frame format: 8data, 2stop bit */
UCSRC = (1<<URSEL)|(1<<USBS)| (3<<UCSZ0);
UBRRL = ((u08)UART_BAUD_SELECT);
/*use AVCC as reference, right adjusted, ADC0 selected*/
ADMUX = _BV(REFS0);
/*enable ADC and set speed to 115.2KHz */
ADCSRA = (_BV(ADEN)|_BV(ADPS1)| _BV(ADPS0));
Ponyprog can be used to download the program through SPI port into the AVR micro controller, and write on the flash memory. Hyper terminal can be used to debug the program code, by receiving the signals sent through USART. However, Hyper terminal should be properly configured for Baud Rate and COM port selected. Usually Parity can be selected and flow control can be selected to be none.





