Kann mir jemand leicht verständlich erklären was das Programm in jeder Zeile macht und wie es funktioniert. Es soll die Leistung an dem Windrad messen und an ein LCD-Display weitergeben. Das alles mit Hilfe des ATMega32.
1 | #include <avr/io.h> |
2 | #include <avr/interrupt.h> |
3 | |
4 | |
5 | //----------------------------
|
6 | #ifndef F_CPU
|
7 | #define F_CPU 8000000UL
|
8 | #endif
|
9 | #define BAUD 9600UL
|
10 | #include <util/delay.h> |
11 | #include<string.h> |
12 | #include <stdlib.h> |
13 | #include <avr/io.h> |
14 | #include "lcd.h" |
15 | |
16 | void long_delay(uint16_t ms) { |
17 | for(; ms>0; ms--) _delay_ms(1); |
18 | }
|
19 | |
20 | /* USART-Init beim ATmega32 */
|
21 | |
22 | void uart_init(void) |
23 | {
|
24 | UBRRL = ((F_CPU)/(BAUD*16L))-1; |
25 | |
26 | UCSRB |= (1<<RXEN)|(1<<TXEN)|(1<<RXCIE); //Senden empfangen interrupt |
27 | UCSRC |= (1<<URSEL)|(1<<UCSZ1)|(1<<UCSZ0); // Asynchron 8N1 |
28 | }
|
29 | |
30 | |
31 | void putc(unsigned char data) |
32 | {
|
33 | |
34 | while(!(UCSRA & (1<<UDRE))){}; |
35 | UDR= data; |
36 | |
37 | }
|
38 | |
39 | |
40 | |
41 | void print(unsigned char *dat) |
42 | {
|
43 | int i; |
44 | int laenge; |
45 | laenge = strlen(dat); |
46 | for (i = 0; i < laenge; i++ ) |
47 | {
|
48 | putc(dat[i]); |
49 | }
|
50 | }
|
51 | |
52 | |
53 | |
54 | void init_ADC() |
55 | {
|
56 | /* konfig des ADC*/
|
57 | |
58 | ADCSRA |= (1<<ADEN) | (1<<ADSC) | (1<<ADATE) | (1<<ADIE) | (1<<ADPS2) | (1<<ADPS1); |
59 | sei(); |
60 | |
61 | }
|
62 | |
63 | int messwert = 0; |
64 | |
65 | ISR (ADC_vect) |
66 | {
|
67 | messwert = ADCW; |
68 | }
|
69 | |
70 | |
71 | long spannung (long wert) |
72 | {
|
73 | long help; |
74 | help = (long)50 *(long) wert; |
75 | help = help /(long) 1023; |
76 | return help; |
77 | }
|
78 | int main( void ) |
79 | {
|
80 | |
81 | |
82 | |
83 | uart_init(); |
84 | init_ADC(); |
85 | |
86 | /* Initialisiere Display, Cursor aus */
|
87 | lcd_init(LCD_DISP_ON); |
88 | |
89 | while(1) |
90 | {
|
91 | long sp; |
92 | long wert; |
93 | char buff[20]; |
94 | print("Hallo "); |
95 | wert = messwert; |
96 | sp = spannung (wert); |
97 | |
98 | print(ltoa(wert , buff, 10 )); |
99 | |
100 | print(" "); |
101 | print(ltoa(sp , buff, 10 )); |
102 | print(" "); |
103 | print(" "); |
104 | long_delay(1000); |
105 | |
106 | |
107 | |
108 | /* Loesche das LCD Display und Cursor auf 1 Zeile, 1 Spalte */
|
109 | lcd_clrscr(); |
110 | |
111 | /* String auf Display anzeigen */
|
112 | lcd_gotoxy(0,0); lcd_puts("Yarrak:"); |
113 | lcd_gotoxy(0,1); lcd_puts(26 cm); |
114 | |
115 | |
116 | |
117 | };
|
118 | |
119 | return 0; |
120 | }
|