Moteur.c
2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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
104
105
#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;
}