Forum: µC & Digital Electronics AVR Studio - ATmega8 - Beginner

Author: micro u. (Guest)
Posted on:

Rate this post
-1 useful
not useful
im trying to get used to avr studio.
the program shut turn a led on and off in a period of, lets say 1 
i got now two possible ways, to achieve this function:
first, im gonna define f_cpu 1 per second and let the led change every 
loop. or, 2nd, im gonna let the frequency of the cpu as it is and sleep 
every loop 1 second.
but both ways seem not to work at all.

1.) where can i get information about the order of including librarys 
and definitions about my hardware. i was told to be carefully about the 

2.) is the way im defining my f_cpu correct? can i freely choose between 
1Hz and a bit over 3MHz??

3.) when i choose to use the _delay_ms(5000) function, how does this 
function work? if i say _delay_ms(5000);, does the program wait there 
for 5 seconds and then keep going??

4.) any  ideas for improvement in this program?

thanks for your help


AVR Studio - Program for Test board (myAVR MK2)


File    :  

User    :  
Datum    :  
Version    :  


Function  :


call input EXAMPLE
  if(!(PIND & 0x04)){  


#include <avr/io.h>        // Header for AVR (type of uC)

#ifndef  F_CPU          // must be written before delay.h
#define  F_CPU 1000UL      // loop frequency

#include <stdio.h>        // standard library
//#include <stdint.h>        // standardized data types
#include <util/delay.h>      // time delay
//#include <avr/interrupt.h>     // interrupt????
                //#include <windows.h>
//#include <avr/sleep.h>                

void initPorts(void);
int testing(void);


int counter = 0;
int time = 5000;
int loop = 0;

//******* MAIN *************************************************************

int main (void){
  initPorts();      // initialize ports
  //return 0;        // not needed because of void
//******* TESTING *************************************************************

int testing(void){
  loop = 1;
    if(counter>=32767){        // prevent overflow
    else if(counter%2 == 0){    // even
      PORTB = 0x01;
    } else {            // odd
      PORTB = 0x00;

//******* INI *************************************************************

void initPorts(void){      
  // definitions I/O
  DDRB = 0xFF;      //  define port B as output
  DDRD = 0x00;      //  define port D as input

  // activate intern pull-up resistors
  PORTD = 0xFC;      //  bites 2 to 7

Author: Sebastian W. (sebastian_w29)
Posted on:

Rate this post
0 useful
not useful
Hello Luk,

if you define F_CPU as 1000UL, then <util/delay.h> assumes that your CPU 
would execute only 1000 instructions per second. When calling 
_delay_ms(5000) the procedure will therefore return after 5000 CPU 
cycles. But as your CPU runs actually much much faster it will thus 
return as well much much faster.

You should therefore set F_CPU to the real frequency of your CPU. If 
your CPU uses a 16MHz quartz, for instance, you should set F_CPU to 
16000000UL. Then _delay_ms(5000) will return only after 5000ms as you 
would want it to.

Regarding your program and possible improvements:

The order of header file inclusion is usually not so very important. 
There might be exceptions, but I would not know any. However, you should 
include only those header files that you also need. For instance, for 
your little program above you do not need <stdio.h>.

You can avoid the overflow logic by declaring counter as unsigned int 
and letting it overflow deliberately. No harm done later as it smoothly 
goes odd 65535 -> even 0.

time should probable be declared as const int, it will then take away no 
memory at all. time should also be used.

loop does not need to be global. In fact, loop is completely 
superfluous, you can just use while (1) { ...}

You should probably define the pin position of your led in the header of 
the program and not in the guts of the testing() procedure. For 
instance, const unsigned char LED = 0x01; and then later PORTB = LED;

Have fun, Sebastian

Author: micro u. (Guest)
Posted on:

Rate this post
0 useful
not useful
thank you very much =)

Author: chakri chakri (chakri)
Posted on:
Attached files:

Rate this post
0 useful
not useful
hello sir,i am beginner for ATmega8.
i am using AVR studio4 for ATmega8.
while build the program i get a error
kumar.c:3: *** missing separator.  Stop.
Build failed with 1 errors and 0 warnings...
but there is need of separator.
some times it shows at main function.
plz give the solution for my problem.

Author: xmh (Guest)
Posted on:

Rate this post
0 useful
not useful
Hi chakri,
I guess you should remove declaration of internal functions.


Entering an e-mail address is optional. If you want to receive reply notifications by e-mail, please log in.

Rules — please read before posting

  • Post long source code as attachment, not in the text
  • Posting advertisements is forbidden.

Formatting options

  • [c]C code[/c]
  • [avrasm]AVR assembler code[/avrasm]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]

Bild automatisch verkleinern, falls nötig
Note: the original post is older than 6 months. Please don't ask any new questions in this thread, but start a new one.