Hallo, ich habe einen code für eine Maschine ähnlich einer CNC-Fräse geschrieben darin gibt es jedoch probleme beim Timer0 und ich weiß einfach nich wo der fehler entsteht und wie ich ihn beheben soll. Main.c
1 | #include <avr/io.h> |
2 | #include <avr/interrupt.h> |
3 | #define F_CPU 1000000UL // 1 MHz
|
4 | #include <util/delay.h> |
5 | #include "Main.h" |
6 | |
7 | |
8 | |
9 | int main(void) |
10 | {
|
11 | //Timer 0 auf 1024 prescale
|
12 | TCCR0 = (1<<CS01 | 1<<CS02); |
13 | // Overflow Interrupt erlauben
|
14 | TIMSK |= (1<<TOIE0); |
15 | //interupts erlauben
|
16 | sei(); |
17 | //Port C als eingang definieren
|
18 | DDRC = 0x00; |
19 | DDRD = 0xFF; |
20 | Motor3 = 0; |
21 | //in Motor1 und 2 wird die anzahl der schritte pro tastendruck angegeben
|
22 | Motor2 = 0; |
23 | Motor1 = 0; |
24 | while (1) |
25 | {
|
26 | //Taster abfragen
|
27 | if (PINC0 == 1) |
28 | {
|
29 | Motor3 =+ 1; |
30 | }
|
31 | if (PINC1 == 1) |
32 | {
|
33 | Motor3 =- 1; |
34 | }
|
35 | if (PINC2 == 1) |
36 | {
|
37 | |
38 | for (uint8_t i = Motor1;i++;) |
39 | {
|
40 | if (M1 == 1) |
41 | {
|
42 | PORTD = (1<<PD0); |
43 | }
|
44 | if (M1 == 0) |
45 | {
|
46 | PORTD = (0<<PD0); |
47 | }
|
48 | PORTD = (1<<PD1); |
49 | _delay_ms(1); |
50 | PORTD = 0x00; |
51 | _delay_ms(1); |
52 | }
|
53 | }
|
54 | if (PINC3 == 1) |
55 | {
|
56 | for (uint8_t i = Motor1;i++;) |
57 | {
|
58 | if (M1 == 0) |
59 | {
|
60 | PORTD = (1<<PD0); |
61 | }
|
62 | if (M1 == 1) |
63 | {
|
64 | PORTD = (0<<PD0); |
65 | }
|
66 | PORTD = (1<<PD1); |
67 | _delay_ms(1); |
68 | PORTD = 0x00; |
69 | _delay_ms(1); |
70 | }
|
71 | }
|
72 | if (PINC4 == 1) |
73 | {
|
74 | for (uint8_t i = Motor2;i++;) |
75 | {
|
76 | if (M2 == 1) |
77 | {
|
78 | PORTD = (1<<PD2); |
79 | }
|
80 | if (M2 == 0) |
81 | {
|
82 | PORTD = (0<<PD2); |
83 | }
|
84 | PORTD = (1<<PD3); |
85 | _delay_ms(1); |
86 | PORTD = 0x00; |
87 | _delay_ms(1); |
88 | PORTD = 0x00 ; |
89 | }
|
90 | if (PINC5 == 1) |
91 | {
|
92 | for (uint8_t i = Motor2;i++;) |
93 | {
|
94 | if (M2 == 0) |
95 | {
|
96 | PORTD = (1<<PD2); |
97 | }
|
98 | if (M2 == 1) |
99 | {
|
100 | PORTD = (0<<PD2); |
101 | }
|
102 | PORTD = (1<<PD3); |
103 | _delay_ms(1); |
104 | PORTD = 0x00; |
105 | _delay_ms(1); |
106 | PORTD = 0x00; |
107 | }
|
108 | }
|
109 | }
|
110 | |
111 | }
|
112 | #ifndef TIMER0_OVF_vect
|
113 | #define TIMER0_OVF_vect TIMER0_OVF0_vect
|
114 | #endif
|
115 | ISR (TIMER0_OVF_vect) |
116 | {
|
117 | for (uint8_t i = Motor3; i++;) |
118 | {
|
119 | if (M3 == 1) |
120 | {
|
121 | PORTD = (1<<PD4); |
122 | }
|
123 | PORTD = (1<<PD5); |
124 | _delay_ms(1); |
125 | PORTD = 0x00; |
126 | _delay_ms(1); |
127 | }
|
128 | for (uint8_t i = Motor3; i++;) |
129 | {
|
130 | if (M3 == 1) |
131 | {
|
132 | PORTD = (0<<PD4); |
133 | }
|
134 | PORTD = (1<<PD5); |
135 | _delay_ms(1); |
136 | PORTD = 0x00; |
137 | _delay_ms(1); |
138 | }
|
139 | }
|
Und auch noch die Header Datei
1 | //Drehrichtungs einstellung
|
2 | #define M1 1
|
3 | #define M2 1
|
4 | #define M3 1
|
5 | unsigned int Motor1 = 0; |
6 | unsigned int Motor2 = 0; |
7 | unsigned int Motor3 = 0; |
ich hoffe ihr könnt mir helfen