Blame view

Moteur/Moteur.c 2.06 KB
686fbdf0   dmohamed   UPDATE - Mise à j...
1
  #include <avr/io.h>
41f6cbfd   dmohamed   first commit
2
  #include <util/delay.h>
686fbdf0   dmohamed   UPDATE - Mise à j...
3
4
5
6
7
8
9
10
11
12
13
14
15
  #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()
41f6cbfd   dmohamed   first commit
16
  {
686fbdf0   dmohamed   UPDATE - Mise à j...
17
18
    // initialize
    cli();          // disable global interrupts
41f6cbfd   dmohamed   first commit
19
  
41f6cbfd   dmohamed   first commit
20
  
686fbdf0   dmohamed   UPDATE - Mise à j...
21
22
23
24
25
26
27
28
    //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 ;
41f6cbfd   dmohamed   first commit
29
  
686fbdf0   dmohamed   UPDATE - Mise à j...
30
31
    DDRB |= (1 << DDB1)|(1 << DDB2);
    // PB1 and PB2 is now an output
41f6cbfd   dmohamed   first commit
32
  
686fbdf0   dmohamed   UPDATE - Mise à j...
33
34
    TCCR1B |= (1 << CS10);
    // START the timer with no prescaler
41f6cbfd   dmohamed   first commit
35
  
686fbdf0   dmohamed   UPDATE - Mise à j...
36
    sei();
41f6cbfd   dmohamed   first commit
37
38
  }
  
686fbdf0   dmohamed   UPDATE - Mise à j...
39
40
41
42
43
  //***********************************************************************
  // Set of functions resulting movement to a servomotor connected on pin 9.
  //***********************************************************************
  void motor_right(){
    OCR1A = 0x80 ;
41f6cbfd   dmohamed   first commit
44
45
  }
  
686fbdf0   dmohamed   UPDATE - Mise à j...
46
47
  void motor_left(){
    OCR1A = 0x08 ;
41f6cbfd   dmohamed   first commit
48
49
  }
  
686fbdf0   dmohamed   UPDATE - Mise à j...
50
51
  void motor_stop(){
    OCR1A = 0xFF;
41f6cbfd   dmohamed   first commit
52
53
  }
  
686fbdf0   dmohamed   UPDATE - Mise à j...
54
55
56
  //**********************************
  // Functions for the led management
  //**********************************
41f6cbfd   dmohamed   first commit
57
  
686fbdf0   dmohamed   UPDATE - Mise à j...
58
59
60
61
  void init_led(void){
    //Led must be placed on pin 7 (PD7)
    DDRD  |= 0x80 ;
    PORTD = 0x00 ;
41f6cbfd   dmohamed   first commit
62
  }
41f6cbfd   dmohamed   first commit
63
  
686fbdf0   dmohamed   UPDATE - Mise à j...
64
65
  void led_on(void){
    PORTD |= 0x80 ;
41f6cbfd   dmohamed   first commit
66
67
  }
  
686fbdf0   dmohamed   UPDATE - Mise à j...
68
69
70
  void led_off(void){
    PORTD &= 0x7F ;
  }
41f6cbfd   dmohamed   first commit
71
72
73
  
  int main(void)
  {
686fbdf0   dmohamed   UPDATE - Mise à j...
74
    init_led();
41f6cbfd   dmohamed   first commit
75
    init_serial(9600); //on choisit une vitesse de 9600 bauds pour la transmission série
686fbdf0   dmohamed   UPDATE - Mise à j...
76
77
    uint8_t r ;
    init_pwm();
41f6cbfd   dmohamed   first commit
78
    while(1)
686fbdf0   dmohamed   UPDATE - Mise à j...
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
    {
  	  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;
          }
    }
41f6cbfd   dmohamed   first commit
104
105
  return 0;
  }