Moteur.c 2.06 KB
#include <avr/io.h>
#include <util/delay.h>
#include "serial.h"
#include <avr/interrupt.h>

#define LED_ON 0x01
#define LED_OFF 0x02
#define MOVE_RIGHT 0x04
#define MOVE_LEFT 0x08
#define MOVE_STOP 0x10

//*******************************************
// Init of PWM signal deliverd on pin 9 (PB1)
//*******************************************
void init_pwm()
{
  // initialize
  cli();          // disable global interrupts


  //initialisation pwm sur broche 13 = PB1
  DDRB |= 0x02 ;
  // PD6 is now an output
  TCCR1A = (1 << WGM10) | (1 << COM1A1);
  // set none-inverting mode
  TCCR1B = (1 << WGM12) | (1 << CS10) |(1 << CS12);
  // set prescaler to 8 and starts PWM
  OCR1A= 0xFF ;

  DDRB |= (1 << DDB1)|(1 << DDB2);
  // PB1 and PB2 is now an output

  TCCR1B |= (1 << CS10);
  // START the timer with no prescaler

  sei();
}

//***********************************************************************
// Set of functions resulting movement to a servomotor connected on pin 9.
//***********************************************************************
void motor_right(){
  OCR1A = 0x80 ;
}

void motor_left(){
  OCR1A = 0x08 ;
}

void motor_stop(){
  OCR1A = 0xFF;
}

//**********************************
// Functions for the led management
//**********************************

void init_led(void){
  //Led must be placed on pin 7 (PD7)
  DDRD  |= 0x80 ;
  PORTD = 0x00 ;
}

void led_on(void){
  PORTD |= 0x80 ;
}

void led_off(void){
  PORTD &= 0x7F ;
}

int main(void)
{
  init_led();
  init_serial(9600); //on choisit une vitesse de 9600 bauds pour la transmission série
  uint8_t r ;
  init_pwm();
  while(1)
  {
	  r = get_serial();
    switch(r){
        case LED_ON:
          led_on();
          send_ack();
          break;
        case LED_OFF:
          led_off();
          send_ack();
          break;
        case MOVE_RIGHT:
          motor_right();
          send_ack();
          break;
        case MOVE_LEFT:
          motor_left();
          send_ack();
          break;
        case MOVE_STOP:
          motor_stop();
          send_ack();
          break;
        }
  }
return 0;
}