Page 1 of 1

AVR (Atmega328P) for starters

PostPosted: Fri Feb 08, 2013 11:23 pm
by Brian Griffin
We all know the Atmega328P is the mastermind behind all things Arduino (except the Due).

This time, it's not about Arduino or PIC. I'm tackling the AVR here, which seems to be popular in the USA and other countries.

One advantage is, the AVR is faster than the PIC16/18F. On a PIC16F with 4MHz clock, the effective time to excecute one instruction is just 1 microsecond. (clk/4) But on the AVR, it's 0.25 microsecond. I'd say it's 4 times faster. However, the chip's maximum frequency is 20MHz, so anything more than that is not guaranteed.

On the lower level side, the PIC16/18F has a W (double-u) register for storing temporary values. The one W can get very, very annoying when you have to do more complicated stuff on it. The AVR has 32 8-bit registers, from R0-R31, so you can hold some more values inside while working on a tough piece of calculation or work. Plus, it has a multiply instruction, and another ones to multiply fractional values (not floating point).

The more difficult part of the Atmega328P is the configuration bits. You need to set them in the AVR Studio prior to programming the thing. In the example below I'm using the onboard 1MHz oscillator.

Here is a sample code compiled with the AVR Studio 5.0 and WinAVR (latest version) installed.
CODE: SELECT_ALL_CODE
#define F_CPU 1000000UL

#include <avr/io.h>
#include <util/delay.h>

void delay_500ms() {
   TCNT1H = 0xFE;
   TCNT1L = 0x18;
   TCCR1A = 0b00000000;   // normal mode
   TCCR1B = 0b00000101;   // prescaler = 1024
   TIMSK1 = 0b00000000;   // disable timer1 interrupts
   while((TIFR1 & 0x01) != 1); // wait for timer1 interrupt flag to be up.
   TIFR1 = 0b00000001;      // clear timer1 interrupt (set the bit to clear it)
   TCCR1B = 0b00000000;   // shut off timer1   
}

int
main (void)
{
    DDRB = 0b00000001;
   
    while(1)
    {
        PORTB ^= 0b00000001;
        delay_500ms();
    }
}


The code toggles the LED on pin 14 for approximately 500ms. Similar to the PIC16/18F for the timers, they are quite straightforward.

Reference: http://www.micahcarrick.com/tutorials/avr-microcontroller-tutorial/getting-started.html

Re: AVR (Atmega328P) for starters

PostPosted: Sat Feb 09, 2013 10:54 am
by shahrul
Good info because I don't know about AVR. Many beginner now just know Arduino and not know the microcontroller side.

Re: AVR (Atmega328P) for starters

PostPosted: Mon Mar 11, 2013 9:49 pm
by Brian Griffin
On AVR series, the fuses (like PIC, it's called configuration bits), especially the DWEN bit, should not be touched.

When I was debugging the AVR in my free time, I had to click on it because to debug the microcontroller. Guess what - I can't revert it back to normal mode, which means, I can't reprogram it anymore.

Luckily I have an AVR Dragon, and this thing has to be reprogrammed manually using Parallel programming. And also, I will do it much later.

Re: AVR (Atmega328P) for starters

PostPosted: Tue Mar 12, 2013 2:25 pm
by robosang
Good sharing, I am one of many that uses Arduino without knowing the AVR. :mrgreen: