Kasutaja tarvikud

Lehe tööriistad


kursused:roboti_algkursused:2017.1:pwm

PWM mootorite juhtimine

main.c

/*
 * PWM main.c
 *
 */
 
#include <avr/io.h>
#include <util/delay.h>
#include "motors.h"
 
int main(void)
{
   motors_init();
   int i=0;
 
   while (1)
   {
      for(i=-1000; i<=1000; i++)
      {
         motors_set(i, i);
         1_delay_ms(5);
      }
      for(i=1000; i>=-1000; i--)
      {
         motors_set(i, i);
         _delay_ms(5);
      }
    }
}

motors.h

/*
 * motors.h
 *
 */
 
 
#ifndef MOTORS_H_
#define MOTORS_H_
 
 
#include <avr/io.h>
#define AIN1 PB0
#define AIN2 PB1 //OC1A
#define BIN1 PB2 //OC1B
#define BIN2 PB3
 
void motors_init(void);
 
void motors_set(int16_t left, int16_t right);
 
#endif

motors.c

/*
 * motors.c
 *
 */
 
#include "motors.h"
 
void motors_init(void)
{
   DDRB|=(1<<DDB0)|(1<<DDB1)|(1<<DDB2)|(1<<DDB3);
 
   TCCR1A|=(1<<COM1A1|1<<COM1B1);
 
   TCCR1A|=1<<WGM11;
   TCCR1B|=(1<<WGM12) | (1<<WGM13);
 
   TCCR1B|=(1<<CS10);
 
   ICR1 = 1000;
}
 
void motors_set(int16_t left, int16_t right)
{
  if(right>=0)
  {
    PORTB|=(1<<BIN2);
    OCR1B = right;
  }
  else
  {
    PORTB &=~(1<<BIN2);
    OCR1B = (-1)*right;
  }
 
  if(left>=0)
  {
    PORTB|=(1<<AIN1);
    OCR1A = left;
  }
  else
  {
    PORTB &=~(1<<AIN1);
    OCR1A = (-1)*left;
  }
}
kursused/roboti_algkursused/2017.1/pwm.txt · Viimati muutnud: 2017/05/01 16:36 persoon maarjo.mass