Hey Leute...
Ich bin dabei ein kleines Programm zu schreiben und folgendes Problem
liegt vor:
Ich habe einen Timer für allgemeine Zählfunktionen eingerichtet.
Leider musste ich feststellen, dass das abfragen des Wertes nicht
funktioniert.
Nach einigen hin und her fand ich heraus, dass die Variable "zaehler"
wie von selbst auf 0 gesetzt wird, sobald der wert > 7 ist.
Das selbe passiert auch wenn ich den wert auf 50 setze und hochzählen
lasse, dann zählt er bis 60 danach kommt die 0.
Der Code ist mittlerweile soweit reduziert, dass der Timer0 die Variable
"zaehler" hochzählt und das ganze weiter unten nur mal abgefragt wird.
Trotzdem das selbe Problem...
Kann mir jemand verraten worin der Fehler liegt?
Hier mein Code:
1 | /*
|
2 | * LED_Badezimmer.cpp
|
3 | *
|
4 | * Created: 05.09.2015 15:49:07
|
5 | * Author: Alex
|
6 | */
|
7 |
|
8 | /*
|
9 | PD2 = Taster Interrupt0
|
10 | PB0 = Relais
|
11 | PB1 = Transistor PWM Timer/Counter
|
12 | 1sek. Timer/Counter autom. abschaltung nach 2std ->120min ->7200sek.
|
13 | */
|
14 |
|
15 |
|
16 | #include <avr/io.h>
|
17 | #include <avr/interrupt.h>
|
18 |
|
19 | void entprellung (void);
|
20 |
|
21 | bool interrupt;
|
22 | bool Taster_use;
|
23 |
|
24 | volatile uint8_t zaehler;
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | ISR (INT0_vect)
|
30 | {
|
31 | interrupt = 1;
|
32 |
|
33 | }
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 | void write16bitregister (uint16_t m_OCR1) //OCR1A max. 10bit ->1023!
|
40 | {
|
41 |
|
42 | unsigned char sreg;
|
43 |
|
44 | sreg = SREG;
|
45 |
|
46 | cli();
|
47 |
|
48 | OCR1AH = (m_OCR1>>8);
|
49 |
|
50 | OCR1AL = (m_OCR1 & 0x0FF);
|
51 |
|
52 |
|
53 | SREG = sreg;
|
54 | sei();
|
55 |
|
56 | }
|
57 |
|
58 |
|
59 |
|
60 | ISR(TIMER0_OVF_vect)
|
61 | {
|
62 | zaehler++;
|
63 |
|
64 | }
|
65 |
|
66 |
|
67 | int main(void)
|
68 | {
|
69 |
|
70 | MCUCR |= (1<<ISC01)|(1<<ISC00);
|
71 | GICR |= (1<<INT0);
|
72 | GIFR |= (1<<INTF0);
|
73 |
|
74 | TCCR1A |= (1<<COM1A1)|(1<<WGM12)|(1<<WGM11)|(1<<WGM10);
|
75 | TCCR1B |= (1<<CS10);
|
76 | TIMSK |= (1<<OCIE1A)|(1<<TOIE1);
|
77 |
|
78 | DDRB |= (1<<PB0)|(1<<PB1);
|
79 |
|
80 |
|
81 |
|
82 |
|
83 | TCCR0 |= (1<<CS00);
|
84 | TIMSK |= (1<<TOIE0);
|
85 |
|
86 |
|
87 |
|
88 |
|
89 | uint16_t soll;
|
90 | uint16_t ist;
|
91 |
|
92 |
|
93 | sei();
|
94 | while(1)
|
95 | {
|
96 |
|
97 | entprellung();
|
98 | if (Taster_use == 1)
|
99 | {
|
100 | /* write16bitregister(20);*/
|
101 | soll = 1023;
|
102 | PORTB |= (1<<PB0);
|
103 | }
|
104 | else
|
105 | {
|
106 | /* write16bitregister(0);*/
|
107 | soll = 0;
|
108 | PORTB &= ~(1<<PB0);
|
109 | }
|
110 |
|
111 | if(zaehler == 100)
|
112 | {
|
113 | ist++;
|
114 | write16bitregister(20);
|
115 | zaehler = 0;
|
116 | }
|
117 | }
|
118 | }
|
119 |
|
120 |
|
121 | void entprellung (void)
|
122 | {
|
123 |
|
124 | bool entprell;
|
125 | uint16_t time1;
|
126 |
|
127 |
|
128 | if (interrupt == 1 && entprell == 0)
|
129 | {
|
130 | Taster_use ^= 1;
|
131 | entprell = 1;
|
132 | interrupt = 0;
|
133 | }
|
134 | if(interrupt == 1 && entprell == 1)
|
135 | {
|
136 | interrupt = 0;
|
137 | }
|
138 | if (entprell == 1)
|
139 | {
|
140 | time1++;
|
141 | }
|
142 | if(time1 > 20000)
|
143 | {
|
144 | entprell = 0;
|
145 | time1 = 0;
|
146 | }
|
147 | }
|
Programmieren tue ich einen ATmega8 mit AVR Studio 6.2.9200.0 - Service
Pack 2.
Vielen Dank für eure Mühe!
MfG Alex