c-programma voor motorregeling bij motordriver.
/*
* motordrivers_versie_06.c
*
* Created: 19-10-2014 10:33:37
* Prototype (Madeleine) op Olimex AVR _40 met Atmega32.
* aansluitingen:
*
* MotorA:
* input_1 PB7 pin8
* input_2 PB1 pin2
* enable_M1 PB2 pin3
* MotorB
* input3 PB3 pin4
* input4 PB5 pin6
* enable_M2 PB6 pin7
* PowerLed PB0 pin1 (default). Olimex AVR_40
* Button PB4 pin5 (default). Olimex AVR_40
* Int0 PD2 pin16
* Programma werkt.
* Afgeleid van code 13.1 uit Microcontrollers en de taal C
* Doel: motoren met langere tussenperiodes laten draaien: vooruit en achteruit.
*/
#include
#include
unsigned interruptcount = 0;
unsigned togglecount = 0;
unsigned vooruit = 1;
unsigned running = 0;
void mot1(char command);
void mot2(char command);
/*
ISR(INT0_vect)
{
if (running == 0)
{
TCCR0 = _BV(CS01) | _BV(CS00); // normal mode, OC not used, prescaling is 64
TCNT0 = 6; // timer 0 counts 256 - 6 = 250 prescaled clockcycles
TIMSK = _BV(TOIE0); // enable timer 0 interrupt overflow
running = 1;
}
else
{
TIMSK = ~(_BV(TOIE0)); // disable timer 0 interrupt overflow
mot1('S');
mot2('S');
running = 0;
}
}
*/
ISR(TIMER0_OVF_vect)
{
TCNT0 = 6; // it counts 256-6 prescaled clockcycles
if (++interruptcount == 125) // toggles only once of 125 interrupts
{
interruptcount = 0;
togglecount = togglecount +1;
};
if (togglecount == 10)
{
if (vooruit == 1){
mot1('R');
mot2('R');
vooruit = 0 ;
}
else {
mot1('S');
mot2('S');
vooruit = 1;
}
togglecount = 0;
}
}
int main(void)
{
DDRB = 0xFF; // set PORTB for output. Hier staan de leds op.En de motoren
/* PORTB = 102; //01100110 */
mot1('F');
mot2('F');
vooruit = 0 ;
TCCR0 = _BV(CS01) | _BV(CS00); // normal mode, OC not used, prescaling is 64
TCNT0 = 6; // timer 0 counts 256 - 6 = 250 prescaled clockcycles
TIMSK = _BV(TOIE0); // enable timer 0 interrupt overflow
/*
GICR = _BV(INT0); // enable INT0
MCUCR = _BV(ISC01); // falling edge
*/
sei();
while(1) // do nothing
{
}
}
void mot1(char command)
{
switch (command)
{
case 'R' : PORTB &= ~_BV(7);PORTB |= _BV(1);PORTB &=~_BV(2); // Reverse
break;
case 'F' : PORTB |=_BV(7);PORTB &= ~_BV(1);PORTB &=~_BV(2); //Forward
break;
case 'S' : PORTB |= _BV(2);// Stop
break;
case 'B' : PORTB &=~_BV(7);PORTB &=~_BV(1);PORTB &=~_BV(2); // Brake
break;
}
}
void mot2(char command)
{
switch (command)
{
case 'R' : PORTB &= ~_BV(3);PORTB |= _BV(5);PORTB &=~_BV(6); //Reverse
break;
case 'F' : PORTB |=_BV(3);PORTB &= ~_BV(5);PORTB &=~_BV(6); //Forward
break;
case 'S' : PORTB |=_BV(6); //Stop
break;
case 'B' : PORTB &=~_BV(3);PORTB &=~_BV(5);PORTB &=~_BV(6); //Brake
break;
}
}
-->