c-programma voor motorregeling bij motordriver.
  1.  
  2.  
  3.        
  4.             /*
  5.             * motordrivers_versie_06.c
  6.             *
  7.             * Created: 19-10-2014 10:33:37
  8.             * Prototype (Madeleine) op Olimex AVR _40 met Atmega32.
  9.             * aansluitingen:
  10.             *
  11.             * MotorA:
  12.             * input_1   PB7     pin8
  13.             * input_2   PB1     pin2
  14.             * enable_M1 PB2     pin3
  15.            
  16.             * MotorB
  17.             * input3    PB3     pin4
  18.             * input4    PB5     pin6
  19.             * enable_M2 PB6     pin7
  20.            
  21.             * PowerLed  PB0     pin1 (default). Olimex AVR_40
  22.             * Button    PB4     pin5 (default). Olimex AVR_40
  23.            
  24.             * Int0              PD2 pin16
  25.            
  26.             * Programma werkt.
  27.             * Afgeleid van code 13.1 uit Microcontrollers en de taal C
  28.             * Doel: motoren met langere tussenperiodes laten draaien: vooruit en achteruit.
  29.             */
  30.            
  31.             #include
  32.             #include
  33.            
  34.             unsigned interruptcount = 0;
  35.             unsigned togglecount = 0;
  36.             unsigned vooruit = 1;
  37.             unsigned running = 0;
  38.             void mot1(char command);
  39.             void mot2(char command);
  40.             /*
  41.             ISR(INT0_vect)
  42.             {
  43.                 if (running == 0)
  44.                 {
  45.                     TCCR0 = _BV(CS01) | _BV(CS00);      // normal mode, OC not used, prescaling is 64
  46.                     TCNT0 = 6;                  // timer 0 counts 256 - 6 = 250 prescaled clockcycles
  47.                     TIMSK = _BV(TOIE0);         // enable timer 0 interrupt overflow
  48.                     running = 1;
  49.                 }
  50.                 else
  51.                 {
  52.                     TIMSK = ~(_BV(TOIE0));              // disable timer 0 interrupt overflow
  53.                     mot1('S');
  54.                     mot2('S');
  55.                     running = 0;
  56.                    
  57.                 }
  58.             }
  59.             */ 
  60.             ISR(TIMER0_OVF_vect)
  61.             {
  62.                
  63.                 TCNT0 = 6;              // it counts 256-6 prescaled clockcycles
  64.                 if (++interruptcount == 125)    // toggles only once of 125 interrupts
  65.                 {
  66.                     interruptcount = 0;
  67.                     togglecount = togglecount +1;
  68.                 };
  69.                 if (togglecount == 10)
  70.                 {
  71.                     if (vooruit == 1){
  72.                         mot1('R');
  73.                         mot2('R');
  74.                         vooruit = 0 ;
  75.                         }
  76.                     else {
  77.                         mot1('S');
  78.                         mot2('S');
  79.                         vooruit = 1;
  80.                     }
  81.                    
  82.                     togglecount = 0;
  83.                 }
  84.                    
  85.             }
  86.             int main(void)
  87.             {
  88.                 DDRB = 0xFF;       // set PORTB for output. Hier staan de leds op.En de motoren
  89.                 /*      PORTB = 102; //01100110 */
  90.                 mot1('F');
  91.                 mot2('F');
  92.                 vooruit = 0 ;
  93.                
  94.                 TCCR0 = _BV(CS01) | _BV(CS00);  // normal mode, OC not used, prescaling is 64
  95.                 TCNT0 = 6;                      // timer 0 counts 256 - 6 = 250 prescaled clockcycles
  96.                 TIMSK = _BV(TOIE0);             // enable timer 0 interrupt overflow
  97.            
  98.             /* 
  99.                 GICR = _BV(INT0); // enable INT0
  100.                 MCUCR = _BV(ISC01); // falling edge
  101.             */ 
  102.                 sei();
  103.                 while(1)                                // do nothing
  104.                 {
  105.                    
  106.                 }
  107.             }
  108.             void mot1(char command)
  109.             {
  110.                 switch (command)
  111.                 {
  112.                     case 'R' : PORTB &= ~_BV(7);PORTB |= _BV(1);PORTB &=~_BV(2); // Reverse
  113.                     break;
  114.                     case 'F' : PORTB |=_BV(7);PORTB &= ~_BV(1);PORTB &=~_BV(2); //Forward
  115.                     break;
  116.                     case 'S' : PORTB |= _BV(2);// Stop
  117.                     break;
  118.                     case 'B' : PORTB &=~_BV(7);PORTB &=~_BV(1);PORTB &=~_BV(2); // Brake
  119.                     break;
  120.                 }
  121.             }
  122.             void mot2(char command)
  123.             {
  124.                 switch (command)
  125.                 {
  126.                     case 'R' : PORTB &= ~_BV(3);PORTB |= _BV(5);PORTB &=~_BV(6); //Reverse
  127.                     break;
  128.                     case 'F' : PORTB |=_BV(3);PORTB &= ~_BV(5);PORTB &=~_BV(6); //Forward
  129.                     break;
  130.                     case 'S' : PORTB |=_BV(6); //Stop
  131.                     break;
  132.                     case 'B' : PORTB &=~_BV(3);PORTB &=~_BV(5);PORTB &=~_BV(6); //Brake
  133.                     break;
  134.                 }
  135.             }  
  136.        
  137.  
  138.  
-->