Gast
#1687545
Hallo Community,
Kann mir Bitte einer helfen den Fehler in meinem Code zu finden..
Ich möchte nach einer Sekunde auf meinem LCD anzeigen wie oft ein
Schaltvorgang stattgefunden hat. Mein Interrupt zählt nur eine variable
t hoch mehr nicht. Diese Variable wird dann in CheckRPM() mit 1
verglichen (1sek.) und die entsprechende Aktion ausgeführt.
Führe ich in diesem Zustand das Programm aus bekomme ich auf meinem LCD
unsaubere Werte angezeigt. Wenn ich in CheckRPM() die Zeile if (t==1)
auskommentiere funktioniert das Programm richtig. Für jeden
Schaltvorgang wird meine Variable Counter um eins erhöht.
Hat jemand nen Tipp?
Gruß R.
///////////////////////////////////////////////////
#include <avr/io.h>
#include "lcd-routines.h"
#include <math.h>
#include <stdlib.h>
#include <avr/interrupt.h>
#ifndef F_CPU
#define F_CPU 8000000UL
#endif
volatile uint8_t t,zustand,counter;
void CheckSwitch()
{
if ((zustand == 0) && (!(PINB & (1<<PB1)))) //switch on?
{
zustand = 1;
}
else if ((zustand == 1) && ((PINB & (1<<PB1)))) //switch off?
{
zustand = 2;
}
else if ((zustand == 2) && (!(PINB & (1<<PB1)))) //switch on?
{
counter++;
zustand = 0;
}
}
void CheckRPM()
{
if (t==1)
{
char Buffer[20];
itoa(counter, Buffer, 10);
set_cursor(0,1);
lcd_string(Buffer);
t=0; // reset timer
counter=0;
}
}
int main (void)
{
DDRB |= (0 << PB1);
PORTB |= (1 << PB1);
TCCR1B |= (1 << WGM12); // Configure timer 1 for CTC mode
TIMSK |= (1 << OCIE1A); // Enable CTC interrupt
TCCR1B |= (1 << CS12); // Start timer at Fcpu/256
OCR1A = 31249; // Set CTC compare value to 1s at 8MHz sei(); //
enable global interrupts / start timer
lcd_init();
for (;;)
{
CheckSwitch();
CheckRPM();
}
}
ISR(TIMER1_COMPA_vect)
{
t++; // count +1 for every interrupt at 1s
}
///////////////////////////////////////////////////