summaryrefslogtreecommitdiff
path: root/2004/n/asserv/src/main.c
blob: d3c90f497b4d1c0d65566e9f1fa0a079d593ee68 (plain)
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/* main.c */
/* APBTasserv - asservissement Robot 2004 {{{
 *
 * Copyright (C) 2003 Nicolas Schodet
 *
 * Robot APB Team/Efrei 2004.
 *	Web: http://assos.efrei.fr/robot/
 *	Mail: robot AT efrei DOT fr
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * }}} */
#include <18f442.h>
#include <stdlib.h>

/* Pattes
40 b7 : sens compteur gauche
39 b6 : libre
38 b5 : libre
37 b4 : sens compteur droit
36 b3 : libre
35 b2 : libre
34 b1 : libre
33 b0 : libre
32 Vdd
31 Vss
30 d7 : libre
29 d6 : libre
28 d5 : libre
27 d4 : libre
26 c7 : rcv
25 c6 : xmit
24 c5 : libre
23 c4 : libre
22 d3 : libre
21 d2 : libre

20 d1 : sens moteur gauche
19 d0 : sens moteur droit
18 c3 : libre
17 c2 : pwm motor gauche
16 c1 : pwm motor droit
15 c0 : compteur droit
14 osc2
13 osc1
12 Vss
11 Vdd
10 e2 : libre
9  e1 : libre
8  e0 : libre
7  a5 : libre
6  a4 : compteur gauche
5  a3 : libre
4  a2 : libre
3  a1 : libre
2  a0 : libre
1  /MCLR, Vpp

Libre: a      b        c        d        e
       0123-5 0123-56- ---345-- --234567 012
*/

/* Configuration g�n�rale. */
#fuses H4,WDT,WDT128,PUT,NOBROWNOUT,NOLVP
#use delay(clock=40000000)
#use rs232(baud=115200,xmit=PIN_C6,parity=N,rcv=PIN_C7,bits=8)
#priority EXT,RDA,TBE,TIMER2

#include "motor.c"
#include "serial.c"

/* Initialise le PIC. */
void
main_init (void)
{
    /* Configuration de la liaison s�rie. */
    setup_psp (PSP_DISABLED);
    /* Configuration de l'interface SPI : non utilis�e. */
    setup_spi (FALSE);
    /* Le timer0 est utilis� pour les pas du moteur gauche. */
    setup_timer_0 (RTCC_EXT_L_TO_H | RTCC_8_BIT | RTCC_DIV_1);
    /* Le timer1 est inutilis�. */
    setup_timer_1 (T1_DISABLED);
    /* Configuration du TIMER 2.
     * Tpwm = (PR2 + 1) * 4 * Tosc * TMR2_prescale
     * Fpwm = 20kHz
     * Tint2 = TMR2_postscale * Tpwm
     * Tint2 = 0,5ms
     */
    setup_timer_2 (T2_DIV_BY_4, 124, 10);
    /* Le timer3 est utilis� pour les pas du moteur droit. */
    setup_timer_3 (T3_EXTERNAL | T3_DIV_BY_1);
    /* D�sactivation de l'ADC. */
    setup_adc_ports (NO_ANALOGS);
    setup_adc (ADC_CLOCK_DIV_2);
    /* Configuration des registres pour un fonctionnement en PWM. */
    setup_ccp1 (CCP_PWM);
    setup_ccp2 (CCP_PWM);
    set_pwm1_duty (0);
    set_pwm2_duty (0);
}

/* Initialise les interruptions. */
void
irq_init (void)
{
    /* Configuration des interruptions activ�es. */
    enable_interrupts (INT_TIMER2);
    enable_interrupts (INT_RDA);
    disable_interrupts (INT_TBE);
    enable_interrupts (GLOBAL);
    enable_interrupts (INT_EXT);
}

void
main (void)
{
    delay_ms (20);
    main_init ();
    motor_init ();
    serial_init ();
    irq_init ();
    serial_send_rezet ();
    motor_main ();
}

#int_EXT
debug_isr ()
{
    int tosl, tosh, tosu;
#asm
    movf 0xffd,w
    movwf tosl
    movf 0xffe,w
    movwf tosh
    movf 0xfff,w
    movf tosu
#endasm
    putc ('!');
    putc (tosl);
    putc (tosh);
    putc (tosu);
    putc (CRLF);
}