AVR (Atmega328P) for starters

Discussion about project that uses other controller, or non controller based project such as using logic gates, FPGA, ARM, Mechanical ....etc. Welcome to post it here

AVR (Atmega328P) for starters

Postby Brian Griffin » Fri Feb 08, 2013 11:23 pm

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
PIC - UIC00B from Cytron (replacement for my broken PICKit 2), Pickit 3, MikroC for PIC
dsPIC - MikroC for dsPIC, mikromedia board (dsPIC33)
AVR - AVR Dragon
Parallax - Prop tool
User avatar
Brian Griffin
Enthusiast
 
Posts: 403
Joined: Mon Jan 17, 2011 9:36 am

Re: AVR (Atmega328P) for starters

Postby shahrul » Sat Feb 09, 2013 10:54 am

Good info because I don't know about AVR. Many beginner now just know Arduino and not know the microcontroller side.
User avatar
shahrul
Professional
 
Posts: 812
Joined: Sat May 16, 2009 9:54 pm
Location: Selangor

Re: AVR (Atmega328P) for starters

Postby Brian Griffin » Mon Mar 11, 2013 9:49 pm

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.
PIC - UIC00B from Cytron (replacement for my broken PICKit 2), Pickit 3, MikroC for PIC
dsPIC - MikroC for dsPIC, mikromedia board (dsPIC33)
AVR - AVR Dragon
Parallax - Prop tool
User avatar
Brian Griffin
Enthusiast
 
Posts: 403
Joined: Mon Jan 17, 2011 9:36 am

Re: AVR (Atmega328P) for starters

Postby robosang » Tue Mar 12, 2013 2:25 pm

Good sharing, I am one of many that uses Arduino without knowing the AVR. :mrgreen:
robosang
Expert
 
Posts: 1239
Joined: Wed Jun 10, 2009 5:37 pm


Return to Misc Project

Who is online

Users browsing this forum: No registered users and 10 guests

cron