#include <avr/io.h>
#include <avr/interrupt.h>
unsigned int zx=0,zy=0;
void motorxR(unsigned int schr);
void motorxL(unsigned int schr);
int main()
{
DDRB=(1<<PB0)|(1<<PB1)|(1<<PB2)|(1<<PB3)|(1<<PB4)| (1<<PB5); // PIN0-5 als Ausgang für die Motoren definieren // PortB als Ausgang definieren
DDRC = 0; //PORTC als Eingang für PCINT MASK 1 definieren
// Timer 1 einstellen
//
// Modus 4:
// CTC , Top von OCR1A
//
// WGM13 WGM12 WGM11 WGM10
// 0 1 0 0
//
// Timer Vorteiler: 1
// CS12 CS11 CS10
// 0 0 1
//
// Steuerung des Ausgangsport: Set at BOTTOM, Clear at match
// COM1A1 COM1A0
// 0 1
// OCR1A=(fclk/2*N)-1 N: Vorteiler
TCCR1A = (1<<COM1A0) | (0<<WGM11);
TCCR1B = (0<<WGM13) | (1<<WGM12) | (1<<CS10);
OCR1A=18432;
//****************** Interrupt initialisieren *************************************
PCMSK1 = (1 << PCINT

|(1 << PCINT9)|(1 << PCINT10)|(1 << PCINT11)|(1 << PCINT12)|(1 << PCINT13); //Pin C0-5 für Pin Change Interrupt in Maske 1 setzen
PCICR |= (1 << PCIE1); //Pin Change Interrupt Control Register - PCIE3 setzen für PCINT30
//*********** TIMER1 OVF *****************
TIMSK1|=(1<<TOIE1);
TCNT1=30;
zx=0;
sei();
while (1) {}
}
ISR(TIMER1_OVF_vect)
{
zx++;
if(zx>100)
{
PORTB&=~(1<<PB0);}
TCNT1=0;
zx=0;
}
ISR(PCINT1_vect) //Interrupt Service Routine
{
if(!(PINC&(1<<PC0)))
{
motorxR(zx);
}
if(!(PINC&(1<<PC1)))
{
motorxL(zx);
}
}
void motorxR(unsigned int schr)
{
PORTB|=(1<<PB3);
PORTB|=(1<<PB0);
}
void motorxL(unsigned int schr)
{
PORTB&=~(1<<PB3);
PORTB|=(1<<PB0);
}
Lesezeichen