Commit
d2f14e3cf13a8f716641a372b4769ae883b30e26
Authored by
pfrison
Debut programme arduino principal
| @@ -0,0 +1,41 @@ |
| @@ -0,0 +1,41 @@ |
| |
1
| +#include <avr/io.h> |
| |
2
| +#include <util/delay.h> |
| |
3
| + |
| |
4
| +#define STEP_GAUCHE 0x10 // pin 7 |
| |
5
| +#define DIR_GAUCHE 0x08 // pin 6 |
| |
6
| +#define STEP_DROIT 0x20 // pin 12 |
| |
7
| +#define DIR_DROIT 0x10 // pin 11 |
| |
8
| + |
| |
9
| +const uint8_t deltaL[] = {}; |
| |
10
| +const uint8_t delaiL[] = {}; |
| |
11
| +const uint8_t deltaR[] = {}; |
| |
12
| +const uint8_t delaiR[] = {}; |
| |
13
| + |
| |
14
| +#define GAUCHE 0 |
| |
15
| +#define DROIT 1 |
| |
16
| + |
| |
17
| +void init_deplacements(void){ |
| |
18
| + DDRH |= STEP_GAUCHE | DIR_GAUCHE; |
| |
19
| + DDRB |= STEP_DROIT | DIR_DROIT; |
| |
20
| +} |
| |
21
| + |
| |
22
| +uint16_t* deplacement_commande_suivante(uint16_t num){ |
| |
23
| + return {deltaL[num], delaiL[num], deltaR[num], delaiR[num]}; |
| |
24
| +} |
| |
25
| + |
| |
26
| +void deplacement_moteur_gauche(uint16_t delta, uint16_t delai){ deplacement_moteur(GAUCHE, delta, delai); } |
| |
27
| +void deplacement_moteur_droit(uint16_t delta, uint16_t delai){ deplacement_moteur(DROIT, delta, delai); } |
| |
28
| +static void deplacement_moteur(uint8_t mot, uint16_t delta, uint16_t delai){ |
| |
29
| + for(uint16_t i=0; i<delta; i++){ |
| |
30
| + if (mot == GAUCHE) |
| |
31
| + PORTH |= STEP_GAUCHE; |
| |
32
| + else if (mot == DROIT) |
| |
33
| + PORTB |= STEP_DROIT; |
| |
34
| + _delay_ms(delai); |
| |
35
| + if (mot == GAUCHE) |
| |
36
| + PORTH &= ~STEP_GAUCHE; |
| |
37
| + else if (mot == DROIT) |
| |
38
| + PORTB &= ~STEP_DROIT; |
| |
39
| + _delay_ms(delai); |
| |
40
| + } |
| |
41
| +} |
| @@ -0,0 +1,30 @@ |
| @@ -0,0 +1,30 @@ |
| |
1
| +#ifndef DEPLACEMENT |
| |
2
| +#define DEPLACEMENT |
| |
3
| + |
| |
4
| +void init_deplacements(void){ |
| |
5
| + DDRH |= STEP_GAUCHE | DIR_GAUCHE; |
| |
6
| + DDRB |= STEP_DROIT | DIR_DROIT; |
| |
7
| +} |
| |
8
| + |
| |
9
| +uint16_t* deplacement_commande_suivante(uint16_t num){ |
| |
10
| + return {deltaL[num], delaiL[num], deltaR[num], delaiR[num]}; |
| |
11
| +} |
| |
12
| + |
| |
13
| +void deplacement_moteur_gauche(uint16_t delta, uint16_t delai){ deplacement_moteur(GAUCHE, delta, delai); } |
| |
14
| +void deplacement_moteur_droit(uint16_t delta, uint16_t delai){ deplacement_moteur(DROIT, delta, delai); } |
| |
15
| +static void deplacement_moteur(uint8_t mot, uint16_t delta, uint16_t delai){ |
| |
16
| + for(uint16_t i=0; i<delta; i++){ |
| |
17
| + if (mot == GAUCHE) |
| |
18
| + PORTH |= STEP_GAUCHE; |
| |
19
| + else if (mot == DROIT) |
| |
20
| + PORTB |= STEP_DROIT; |
| |
21
| + _delay_ms(delai); |
| |
22
| + if (mot == GAUCHE) |
| |
23
| + PORTH &= ~STEP_GAUCHE; |
| |
24
| + else if (mot == DROIT) |
| |
25
| + PORTB &= ~STEP_DROIT; |
| |
26
| + _delay_ms(delai); |
| |
27
| + } |
| |
28
| +} |
| |
29
| + |
| |
30
| +#endif |
0
| \ No newline at end of file |
31
| \ No newline at end of file |
| @@ -0,0 +1,80 @@ |
| @@ -0,0 +1,80 @@ |
| |
1
| +#include <avr/io.h> |
| |
2
| +#include <avr/interrupt.h> |
| |
3
| +#include <util/delay.h> |
| |
4
| + |
| |
5
| +#include "serial.h" |
| |
6
| +#include "main.h" |
| |
7
| + |
| |
8
| +#define NB_TICK 104 //1563 |
| |
9
| + |
| |
10
| +/** ----- Taches ----- */ |
| |
11
| +void task_deplacement(void){ |
| |
12
| + |
| |
13
| +} |
| |
14
| +void task_print_b(void){ |
| |
15
| + while(1){ |
| |
16
| + send_serial('b'); |
| |
17
| + _delay_ms(700); |
| |
18
| + } |
| |
19
| +} |
| |
20
| + |
| |
21
| +/** ----- Ordonnancement ----- */ |
| |
22
| +struct task{ |
| |
23
| + uint16_t sp_vise; |
| |
24
| +}; |
| |
25
| +uint8_t cpt = 0; |
| |
26
| +uint8_t premier_lancement = 0; |
| |
27
| +struct task deplacement = {0x0300}; |
| |
28
| +struct task print_b = {0x0500}; |
| |
29
| + |
| |
30
| +// Démarre le timer pour l'ordonnancement |
| |
31
| +void init_timer(){ |
| |
32
| + TCCR1B |= _BV(WGM12); // CTC mode with value in OCR1A |
| |
33
| + TCCR1B |= _BV(CS12); // CS12 = 1; CS11 = 1; CS10 =1 => CLK/1024 prescaler |
| |
34
| + TCCR1B |= _BV(CS10); |
| |
35
| + OCR1A = NB_TICK; |
| |
36
| + TIMSK1 |= _BV(OCIE1A); |
| |
37
| +} |
| |
38
| + |
| |
39
| +// Changement de contexte |
| |
40
| +ISR(TIMER1_COMPA_vect){ |
| |
41
| + if(premier_lancement == 0){ |
| |
42
| + premier_lancement++; |
| |
43
| + sei(); |
| |
44
| + SP = deplacement.sp_vise; |
| |
45
| + task_deplacement(); |
| |
46
| + }else if(premier_lancement == 1){ |
| |
47
| + SAVE_CONTEXT(); |
| |
48
| + deplacement.sp_vise = SP; |
| |
49
| + premier_lancement++; |
| |
50
| + sei(); |
| |
51
| + SP = print_b.sp_vise; |
| |
52
| + task_print_b(); |
| |
53
| + }else{ |
| |
54
| + if(cpt==0){ |
| |
55
| + SAVE_CONTEXT(); |
| |
56
| + print_b.sp_vise = SP; |
| |
57
| + SP = deplacement.sp_vise; |
| |
58
| + RESTORE_CONTEXT(); |
| |
59
| + cpt++; |
| |
60
| + }else if(cpt==1){ |
| |
61
| + SAVE_CONTEXT(); |
| |
62
| + deplacement.sp_vise = SP; |
| |
63
| + SP = print_b.sp_vise; |
| |
64
| + RESTORE_CONTEXT(); |
| |
65
| + cpt = 0; |
| |
66
| + } |
| |
67
| + } |
| |
68
| + sei(); |
| |
69
| +} |
| |
70
| + |
| |
71
| +int main(void){ |
| |
72
| + // Initialisation |
| |
73
| + init_timer(); |
| |
74
| + init_serial(9600); |
| |
75
| + sei(); |
| |
76
| + |
| |
77
| + while(1){} |
| |
78
| + return 0; |
| |
79
| +} |
| |
80
| + |
| @@ -0,0 +1,82 @@ |
| @@ -0,0 +1,82 @@ |
| |
1
| +#ifndef MAIN |
| |
2
| +#define MAIN |
| |
3
| + |
| |
4
| +#define SAVE_CONTEXT() \ |
| |
5
| +asm volatile ( \ |
| |
6
| +"push r0 \n\t" \ |
| |
7
| +"in r0, __SREG__ \n\t" \ |
| |
8
| +"cli \n\t" \ |
| |
9
| +"push r0 \n\t" \ |
| |
10
| +"push r1 \n\t" \ |
| |
11
| +"clr r1 \n\t" \ |
| |
12
| +"push r2 \n\t" \ |
| |
13
| +"push r3 \n\t" \ |
| |
14
| +"push r4 \n\t" \ |
| |
15
| +"push r5 \n\t" \ |
| |
16
| +"push r6 \n\t" \ |
| |
17
| +"push r7 \n\t" \ |
| |
18
| +"push r8 \n\t" \ |
| |
19
| +"push r9 \n\t" \ |
| |
20
| +"push r10 \n\t" \ |
| |
21
| +"push r11 \n\t" \ |
| |
22
| +"push r12 \n\t" \ |
| |
23
| +"push r13 \n\t" \ |
| |
24
| +"push r14 \n\t" \ |
| |
25
| +"push r15 \n\t" \ |
| |
26
| +"push r16 \n\t" \ |
| |
27
| +"push r17 \n\t" \ |
| |
28
| +"push r18 \n\t" \ |
| |
29
| +"push r19 \n\t" \ |
| |
30
| +"push r20 \n\t" \ |
| |
31
| +"push r21 \n\t" \ |
| |
32
| +"push r22 \n\t" \ |
| |
33
| +"push r23 \n\t" \ |
| |
34
| +"push r24 \n\t" \ |
| |
35
| +"push r25 \n\t" \ |
| |
36
| +"push r26 \n\t" \ |
| |
37
| +"push r27 \n\t" \ |
| |
38
| +"push r28 \n\t" \ |
| |
39
| +"push r29 \n\t" \ |
| |
40
| +"push r30 \n\t" \ |
| |
41
| +"push r31 \n\t" \ |
| |
42
| +); |
| |
43
| + |
| |
44
| +#define RESTORE_CONTEXT() \ |
| |
45
| +asm volatile ( \ |
| |
46
| +"pop r31 \n\t" \ |
| |
47
| +"pop r30 \n\t" \ |
| |
48
| +"pop r29 \n\t" \ |
| |
49
| +"pop r28 \n\t" \ |
| |
50
| +"pop r27 \n\t" \ |
| |
51
| +"pop r26 \n\t" \ |
| |
52
| +"pop r25 \n\t" \ |
| |
53
| +"pop r24 \n\t" \ |
| |
54
| +"pop r23 \n\t" \ |
| |
55
| +"pop r22 \n\t" \ |
| |
56
| +"pop r21 \n\t" \ |
| |
57
| +"pop r20 \n\t" \ |
| |
58
| +"pop r19 \n\t" \ |
| |
59
| +"pop r18 \n\t" \ |
| |
60
| +"pop r17 \n\t" \ |
| |
61
| +"pop r16 \n\t" \ |
| |
62
| +"pop r15 \n\t" \ |
| |
63
| +"pop r14 \n\t" \ |
| |
64
| +"pop r13 \n\t" \ |
| |
65
| +"pop r12 \n\t" \ |
| |
66
| +"pop r11 \n\t" \ |
| |
67
| +"pop r10 \n\t" \ |
| |
68
| +"pop r9 \n\t" \ |
| |
69
| +"pop r8 \n\t" \ |
| |
70
| +"pop r7 \n\t" \ |
| |
71
| +"pop r6 \n\t" \ |
| |
72
| +"pop r5 \n\t" \ |
| |
73
| +"pop r4 \n\t" \ |
| |
74
| +"pop r3 \n\t" \ |
| |
75
| +"pop r2 \n\t" \ |
| |
76
| +"pop r1 \n\t" \ |
| |
77
| +"pop r0 \n\t" \ |
| |
78
| +"out __SREG__, r0 \n\t" \ |
| |
79
| +"pop r0 \n\t" \ |
| |
80
| +); |
| |
81
| + |
| |
82
| +#endif |
0
| \ No newline at end of file |
83
| \ No newline at end of file |
| @@ -0,0 +1,36 @@ |
| @@ -0,0 +1,36 @@ |
| |
1
| +export CC = avr-gcc |
| |
2
| + |
| |
3
| +export MCU = atmega2560 |
| |
4
| +export TARGET_ARCH = -mmcu=$(MCU) |
| |
5
| + |
| |
6
| +export CFLAGS = -Wall -I. -DF_CPU=16000000 -Os #-g |
| |
7
| +export LDFLAGS = -g $(TARGET_ARCH) -lm -Wl,--gc-sections # -Os |
| |
8
| + |
| |
9
| +TARGET = servoControl |
| |
10
| +TERM = /dev/ttyACM0 |
| |
11
| +CPPFLAGS = -mmcu=$(MCU) |
| |
12
| +PGMERISP = -c wiring -b 115200 -P $(TERM) -D |
| |
13
| +export DUDE = /usr/bin/avrdude -F -v -p $(MCU) |
| |
14
| + |
| |
15
| +C_SRC = $(wildcard *.c) |
| |
16
| +OBJS = $(C_SRC:.c=.o) |
| |
17
| + |
| |
18
| +all: $(TARGET).hex |
| |
19
| + |
| |
20
| +clean: |
| |
21
| + rm -f *.o *.hex *.elf |
| |
22
| + |
| |
23
| +%.o:%.c |
| |
24
| + $(CC) -c $(CPPFLAGS) $(CFLAGS) $< -o $@ |
| |
25
| + |
| |
26
| +$(TARGET).elf: $(OBJS) |
| |
27
| + $(CC) $(LDFLAGS) -o $@ $(OBJS) |
| |
28
| + |
| |
29
| +$(TARGET).hex: $(TARGET).elf |
| |
30
| + avr-objcopy -j .text -j .data -O ihex $(TARGET).elf $(TARGET).hex |
| |
31
| + avr-objcopy -j .eeprom --set-section-flags=.eeprom="alloc,load" --change-section-lma .eeprom=0 -O ihex $(TARGET).elf eeprom.hex |
| |
32
| + |
| |
33
| +upload: $(TARGET).hex |
| |
34
| + stty -F $(TERM) hupcl # reset |
| |
35
| + $(DUDE) $(PGMERISP) -U flash:w:$(TARGET).hex |
| |
36
| + make clean |
| @@ -0,0 +1,18 @@ |
| @@ -0,0 +1,18 @@ |
| |
1
| +#include <avr/io.h> |
| |
2
| + |
| |
3
| +#define CPU_FREQ 16000000L |
| |
4
| + |
| |
5
| +void init_serial(int speed){ |
| |
6
| + UBRR0 = CPU_FREQ/(((unsigned long int)speed)<<4)-1; //Set baud rate |
| |
7
| + UCSR0B = (1<<TXEN0 | 1<<RXEN0); //Enable transmitter & receiver |
| |
8
| + UCSR0C = (1<<UCSZ01 | 1<<UCSZ00); //Set 8 bits character and 1 stop bit |
| |
9
| + UCSR0A &= ~(1 << U2X0); //Set off UART baud doubler |
| |
10
| +} |
| |
11
| +void send_serial(unsigned char c){ |
| |
12
| + loop_until_bit_is_set(UCSR0A, UDRE0); |
| |
13
| + UDR0 = c; |
| |
14
| +} |
| |
15
| +unsigned char get_serial(void){ |
| |
16
| + loop_until_bit_is_set(UCSR0A, RXC0); |
| |
17
| + return UDR0; |
| |
18
| +} |
| @@ -0,0 +1,8 @@ |
| @@ -0,0 +1,8 @@ |
| |
1
| +#ifndef SERIAL |
| |
2
| +#define SERIAL |
| |
3
| + |
| |
4
| +void init_serial(int speed); |
| |
5
| +void send_serial(unsigned char c); |
| |
6
| +unsigned char get_serial(void); |
| |
7
| + |
| |
8
| +#endif |
0
| \ No newline at end of file |
9
| \ No newline at end of file |