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 | }
|