Hallo
ich habe Timer1 Probleme.Ich habe 2 Schalter, einer für Start/Stop ,andere für Schnell/Langsam.wenn ich einmal schnell/langsam Schalter betätigt alles in Ordnung aber wenn ich zweite mal betätigt dann motor hält.und warte ca. 1 minute dann funktioniert weiter.
Und wenn ich Timer2 benutze, dann funktioniert alle Funktinen richtig.Ich weiss nicht, warum so macht , warum wartet.Mein kleinere Frequenz soll 1 Hz aber mit Timer2 ist das nicht möglich. Kleinste Frequenz ist 4,39 Hz
(Fsignal=((18432000/8(CLKDIV))/(2*1024*(255+1))) ==>4,39HZ)
was soll ich machen?
ich habe Timer1 Probleme.Ich habe 2 Schalter, einer für Start/Stop ,andere für Schnell/Langsam.wenn ich einmal schnell/langsam Schalter betätigt alles in Ordnung aber wenn ich zweite mal betätigt dann motor hält.und warte ca. 1 minute dann funktioniert weiter.
Code:
#include <avr\io.h>
#include <inttypes.h>
#include <avr\interrupt.h>
#include <util/delay.h>
#include <stdbool.h>
volatile int i=0; //schrittanzahl
float u=0; //Umdrehung
float f_max=10; //wenn Schalter Schnell betätigt->Frequenz=10
float f_min=5; //wenn Schalter langsam betätigt->frequenz=5
void motor2();
void InitPorts()
{
DDRB =0b00000110; //PB1->richtung PB2 ->takt Ausgänge
DDRC &= ~(1<<DDC4); //PC4 Schnell/langsam schalter
PORTC |= (1<<PC4);
}
//Funktion für Schalter(gedrückt oder nicht)
bool check_button(volatile uint8_t *port, uint8_t pin, bool depressed){
if(!(*port&_BV(pin))){
_delay_ms(10);
if(!(*port&_BV(pin))){
if(depressed)while(!(*port&_BV(pin)));
return(true);
}
}
return(false);
}
void InitTimer1(void) //Timer initialisierung
{
TIFR1 =0b00000010; // Interrupt Request loeschen (sicherheitshalber)
TIMSK1 =0b00000010; // Enable Output Compare A Interrupt
TCCR1A =0b01000000;
TCCR1B =0b00001101; // Prescaler 1024, CTC Mode 4, Timer1 Start
OCR1A =((18432000/8)/(2*1024*f_max))-1; // CompareTime ist 200ms (18,432/900*1024*5MHz)
TCNT1 =0;
}
ISR (TIMER1_COMPA_vect)
{
(check_button(&PINC,PINC5,true)); //PC5 Start/Stop Schalter
i++;
motor2();
}
void motor2()
{
if (i<150)
{
PORTB^= (1<<PB2);
}
else if (i>=150)
{
PORTB^= (1<<PB1); //Richtung ändern
_delay_ms(5);
i=0;
u++;
if (u==6) //6 Umdrehung hält Motor
{
u=0;
loop_until_bit_is_clear (PINC,PC5); //start/stop schlater
}
}
}
int main()
{
sei();
InitPorts();
InitTimer1();
do{
if(check_button(&PINC,PINC4,false))
{
OCR1A =((18432000/8)/(2*1024*f_min))-1;
}
else
{
OCR1A =((18432000/8)/(2*1024*f_max))-1;
}
}
while(1);
}
(Fsignal=((18432000/8(CLKDIV))/(2*1024*(255+1))) ==>4,39HZ)
was soll ich machen?