Hallo,
wir habe ein Code von unserem Lehrer bekommen, welches wir beenden
müssten aber das Problem ist, dass ich gar nichts vom Code verstehe.
Wäre einer so lieb und würde mir das ganze erklären und sagen was genau
jetzt noch fehlt? Wir haben als Unterlage, dies hier bekommen:
https://ww1.microchip.com/downloads/en/DeviceDoc/ATmega48A-PA-88A-PA-168A-PA-328-P-DS-DS40002061A.pdf
Code:
1 | /*
|
2 | * ErsteProj.c
|
3 | *
|
4 | * Created: 23.09.2019 13:04:48
|
5 | * Author : 20160306
|
6 | * Ziel: Einen Analogwert einlesen (alle Sekunde eine Wandlung), diesem dann digital an ein Terminalwindow schicken: USART, Timer, ADC
|
7 | */
|
8 |
|
9 | #include <avr/io.h>
|
10 | #include <avr/interrupt.h>
|
11 |
|
12 | volatile uint16_t wertADC=0;
|
13 | volatile uint8_t fertig=0;
|
14 |
|
15 | void init_timer(void)
|
16 | {
|
17 | TCCR1B |= (1<<WGM12) | (1<<CS12); //CTC Mode, Prescaler 256:62500
|
18 | OCR1A = 62499; //Um eins geringer als der Timer(!)
|
19 | OCR1B = 9999;
|
20 | TIMSK1 |= (1<<OCIE1A) | (1<<OCIE1B); // enable interrupt des Timers, auch für ADC
|
21 | DDRB |=(1<<5);
|
22 | }
|
23 |
|
24 | void init_usart(void)
|
25 | {
|
26 | UCSR0B |= (1<<RXCIE0) | (1<<TXCIE0) | (1<<TXEN0) | (1<<RXEN0); // Beide interrupts jeweilsreceive und interrupt activieren
|
27 | UBRR0 = 103;
|
28 | }
|
29 |
|
30 | void init_ADC(void)
|
31 | {
|
32 | ADCSRA |= (1<<ADEN) | (1<<ADIE) | (1<<ADPS2) | (1<<ADPS1) | (1<<ADPS0);
|
33 | ADCSRB |= (1<<ADTS2) | (1<<ADTS0);
|
34 | }
|
35 |
|
36 | void umwandeln(char txt[])
|
37 | {
|
38 | for (uint8_t i=4; i>0;i--)
|
39 | { if((wertADC%10)>=0) txt[i-1]=wertADC%10+48; else txt[i-1]=' ';
|
40 | wertADC=wertADC/10;
|
41 | }
|
42 | }
|
43 |
|
44 | void sendetext(char txt[])
|
45 | {
|
46 | for (uint8_t i=0; i<6;i++)
|
47 | {
|
48 | while((UCSR0A & (1<<UDRE0))==0);
|
49 | UDR0=txt[i];
|
50 | }
|
51 | }
|
52 |
|
53 | int main(void)
|
54 | {
|
55 | char text[6]={'0','0','0','0','\n','\r'};
|
56 |
|
57 | init_timer();
|
58 | init_usart();
|
59 | init_ADC();
|
60 | sei();
|
61 | /* Replace with your application code */
|
62 | while (1)
|
63 | {
|
64 | if(fertig==1)
|
65 | {
|
66 | umwandeln(text);
|
67 | sendetext(text);
|
68 | fertig=0;
|
69 | }
|
70 |
|
71 | }
|
72 | }
|
73 |
|
74 | ISR(USART_RX_vect)
|
75 | {
|
76 | //UDR0 = UDR0;
|
77 | }
|
78 |
|
79 | ISR(TIMER1_COMPA_vect)
|
80 | {
|
81 | //PORTB ^=(1<<5);
|
82 | }
|
83 |
|
84 | ISR(TIMER1_COMPB_vect)
|
85 | {
|
86 | PORTB ^= (1<<5);
|
87 | ADCSRA |= (1<<ADSC);
|
88 | }
|
89 |
|
90 | ISR(ADC_vect)
|
91 | {
|
92 | if(fertig==0)
|
93 | {
|
94 | wertADC=ADC; //UDR0 = 'A';
|
95 | fertig=1;
|
96 | }
|
97 | }
|
Mfg,
Marko