Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Atmega8

Status
Not open for further replies.
Hello everybody!

Can anyone help me with a small project, to light LED for 1 minute and then turn it off. All I want is to know how to initialize ports of atmega8 as output using C language. I’m new on AVR and C language. I have been using PIC microcontrollers and assembly language and I can write this program perfectly if I was to use PIC microcontroller and assembly language. You can check on my previous posts that I am now familiar with PIC microcontrollers and assembly.

I like knowing different things and extending my knowledge, I would like to know about AVR and C language.
 
What is the toolchain and development environment you are using?

I recommend the avr-gcc compiler and AVR Libc library. If you are using windows, you can download and install WinAVR and both the avr-gcc and the library are set up for you.
Browse WinAVR Files on SourceForge.net

The WinAVR integrates with AVR studio, which is a fairly good IDE for setting up projects and debugging. You can get AVR studio free from Atmel. The disassembler in AVR studio is handy when coding in C. Sometimes you really have to get into the details of what is going on.
Atmel Products - AVR Solutions - AVR Studio 4

If you are using AVR Libc (which I strongly recommend). Then a very important source of information and examples is the AVR Libc documentation.
AVR Libc Home Page

Check out the example projects at the AVR Libc documentation site: avr-libc: Demo projects
 
I think I am confused between AVR and atmega8. I know that Atmage8 is a microcontroller like PIC microcontroller. What is AVR?
Can you have a look at these two projects? The first one I did it myself and the second one is done by a friend of mine who needs help. The two projects are similar but different when it comes to microcontrollers used and programming languages. I am interested in helping as it will make me learn or know more.

First project
Its aim is to turn motor 90 degree Celsius, clockwise. I used assembly language and PIC 16F877A, ULN2803 and motor stepper. I used the Mplab IDE v8.30 to compile my code to hex file.

Second project (not done by me)
Its aim is to turn motor clockwise for one revolution. atmega8, C language, ULN2803 and motor stepper are used. The project does not perform its aim and I want to make it perform its aim that’s why I am interested to know C language and atmega8. Which compiler must I use for C language?

I used proteus to test the circuits.
 

Attachments

  • First project.zip
    16.6 KB · Views: 442
  • second project.zip
    9.6 KB · Views: 381
AVR is a family of microcontrollers from Atmel. Atmega8 is one of the microcontrollers in this family. Take a look at this sticky topic: https://www.electro-tech-online.com/threads/what-is-an-avr.96858/

I would use the avr-gcc compiler. With avr-gcc and AVR Libc you have a great toolchain to work with all of the devices in AVR family (ATmega, ATtiny, Xmega, etc.).
Of course if you can invest some money in compilers and simulators etc. There are few choices out there for you. I don't know much about the commercial compilers and tools.

I don't know what compiler the second project is written for. I was able to run the proteus simulation successfully. The motor turns but does not stop after one revolution because the code is executed over and over again. You have to stop running the two for-loops after the outer for-loop finishes.

Code:
while (1)
{
 // Place your code here


    for (i = 0; i<=49; i++)    //it requires 50 x 4 steps to complete one revolution
    {
         for (j= 0; j<=3; j++)
         {
             PORTB= step[j];        //PortB = 1001,1100,0110,0011
             delay_ms(3);

         }
    }

 // At this point the code jumps back to the first for-loop and starts
 // turning the motor again
 
 // Do something to prevent this.. like:

    // infinite loop
    while(1);
 

}

It is not good style to stop the code like that. You'll have to reset the microcontroller to turn the motor again. But that fix does the job.. which is to stop the motor after one revolution.
 
Last edited:
To switch an port as output the according bit is setting to 1.

DDRC=0b00010001; // switch the Port 0 and the port 4 of the output bank C as Output the other Pins are used as inputs

To set an, as output defined, pin to 1 you'll set the according PORT bit

PORTC=0b00000001; // set the port 0 of portbank "C" to 1 the other output ports of "C" will be driven to 0

When you'll set a Port of an, as input defined, Port to 1, the internel pullups will be activated ( Not availible on all Ports of all AVR Microcontrollers.

To read the Level from an as input defined Port you will use the PINx Command

values=PINC; //Read the levels from the whole Port "C" into the variable "values"

The easyest way to generate delays is to use the delay_ms command.
delay_ms(1000); // Generate a delay of nearly 1 second

The problem is, the controller can't do nothing other while waiting.
The better way is to use a internal timer, and measure the time in the according timer interrupt.
So the controller isn't blocked for other threats.
 
Last edited:
The easyest way to generate delays is to use the delay_ms command.
delay_ms(1000); // Generate a delay of nearly 1 second

What is the library (and compiler) you are referring to? The use of I/O pins is pretty much the same with all libraries, but things like delays and how you define an Interrupt Service Routine can be quite different. It is important to know what the development environment is (compiler, libraries etc.).
 
Last edited:
I'll use CodeVision AVR with the included librarys.
mega8.h, stdlib.h, stdio.h and delay.h

I'm not shure, but i think AVR GCC will use the delay_ms command too.

In the help topics of the used compiler should be found, how to create a delay.

Interrupt usage is very different between the "C" compilers - Thats right.
In CodeVision e.g. it's:
// Timer 1 output compare A interrupt service routine
interrupt [TIM1_COMPA] void timer1_compa_isr(void)
{
// Your Code
}

You'll have a look at the manual, how to insert an interrupt routine.

The first step is to initialize the according register of the wanted interrupt.

In CodeVision you can create the basic interupt code by using the "Automatic Program Generator"
 
Last edited:
I'm not shure, but i think AVR GCC will use the delay_ms command too.
In the help topics of the used compiler should be found, how to create a delay.

It depends on the library, not the compiler. For example there are two delay functions in AVR Libc library: _delay_ms() and _delay_us()

I know you probably know what is the difference between a library and a compiler. I just want to be precise with these things.
 
It depends on the library, not the compiler.
Thats right, but you can't use a AVR GCC library with an CodeVision Compiler.
That wouldn't work.
I think most users will use the included files.
Only for special funktions or external chips it's needful to use an external library.

Theese librarys must be written for the used compiler too.
To covert such an library for another compiler could be pretty work.
 
I recommend the avr-gcc compiler and AVR Libc library. If you are using windows, you can download and install WinAVR and both the avr-gcc and the library are set up for you.
Browse WinAVR Files on SourceForge.net

Hi
I have download and installed winavr-20100110.I have not seen avr-gcc compiler can you give me the link to it
I have copied a C code from Cprogramming.com Tutorial: Functions and paste it on my programmers notepad. I wanted to run the code but I cannot see how I can run it. Or can you give me the best compiler for compilling c. The tutorils which I have been reading does not tell me which compiler I must use.
 

Attachments

  • Untitled.jpg
    Untitled.jpg
    88.9 KB · Views: 498
I have download and installed winavr-20100110.I have not seen avr-gcc compiler can you give me the link to it

avr-gcc and AVR Libc comes with the winavr package.. so you have both of them installed to your system.

Download and install AVR Studio 4 from Atmel: Atmel Products - AVR Solutions - AVR Studio 4
It is the one that is labeled: AVR Studio 4.18 (build 684) (116 MB, updated 11/09)

Here is a tutorial to get started with AVR Studio: https://www.electro-tech-online.com/custompdfs/2010/07/Getting-started-C-programming-Atmel-AVR.pdf
You can find lots of good tutorials with google.

Even if you use AVR Studio for creating projects, compiling, debugging, uploading code etc.. you can still use your favorite editor to write code. I use UltraEdit to write code and AVR Studio to manage the toolchain.
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top