EmbDev.net

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


von micro u. (Guest)


Rate this post
useful
not useful
Hi
im trying to get used to avr studio.
the program shut turn a led on and off in a period of, lets say 1 
second.
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.

Questions:
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 
order.

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
1
/*
2
3
********************************************************************
4
5
AVR Studio - Program for Test board (myAVR MK2)
6
7
********************************************************************
8
9
File    :  
10
11
User    :  
12
Datum    :  
13
Version    :  
14
15
********************************************************************
16
17
Function  :
18
19
********************************************************************
20
21
call input EXAMPLE
22
  if(!(PIND & 0x04)){  
23
24
25
*/
26
27
// HARDWARE SETTINGS & LIBRARYS
28
//      BEWARE! ORDER MUST BE KEPT AS IT IS
29
#include <avr/io.h>        // Header for AVR (type of uC)
30
31
#ifndef  F_CPU          // must be written before delay.h
32
#define  F_CPU 1000UL      // loop frequency
33
#endif
34
35
#include <stdio.h>        // standard library
36
//#include <stdint.h>        // standardized data types
37
#include <util/delay.h>      // time delay
38
//#include <avr/interrupt.h>     // interrupt????
39
                //#include <windows.h>
40
//#include <avr/sleep.h>                
41
42
// PROTOTYPE
43
void initPorts(void);
44
int testing(void);
45
46
// STRUCTURES
47
48
49
// GLOBAL VARIABLES
50
int counter = 0;
51
int time = 5000;
52
int loop = 0;
53
54
55
56
//******* MAIN *************************************************************
57
58
int main (void){
59
  
60
  initPorts();      // initialize ports
61
  testing();
62
    
63
  //return 0;        // not needed because of void
64
}
65
    
66
//******* TESTING *************************************************************
67
68
int testing(void){
69
  
70
  loop = 1;
71
  
72
  while(loop==1){
73
    
74
    counter++;
75
    //_delay_ms(5000);
76
    
77
    if(counter>=32767){        // prevent overflow
78
      counter=0;
79
    }
80
    else if(counter%2 == 0){    // even
81
      PORTB = 0x01;
82
    } else {            // odd
83
      PORTB = 0x00;
84
    }
85
  }
86
  
87
}  
88
  
89
90
//******* INI *************************************************************
91
92
void initPorts(void){      
93
  
94
  // definitions I/O
95
  DDRB = 0xFF;      //  define port B as output
96
  DDRD = 0x00;      //  define port D as input
97
98
  // activate intern pull-up resistors
99
  PORTD = 0xFC;      //  bites 2 to 7
100
}

von Sebastian W. (sebastian_w29)


Rate this post
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

von micro u. (Guest)


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

von chakri c. (chakri)


Attached files:

Rate this post
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.

von xmh (Guest)


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

Please log in before posting. Registration is free and takes only a minute.
Existing account
Do you have a Google/GoogleMail account? No registration required!
Log in with Google account
No account? Register here.