summaryrefslogtreecommitdiff
path: root/n/accel/accel.c
diff options
context:
space:
mode:
authorburg2004-07-21 08:12:19 +0000
committerburg2004-07-21 08:12:19 +0000
commitb270fb1a79b18ee1b15613b46eedad20f0cb8901 (patch)
tree4019458041868974ab42dfbcb28fb50cbf7582d7 /n/accel/accel.c
parent924292823659b1e037188c608c795c80db42875e (diff)
Création du projet accelerometre
Diffstat (limited to 'n/accel/accel.c')
-rw-r--r--n/accel/accel.c155
1 files changed, 155 insertions, 0 deletions
diff --git a/n/accel/accel.c b/n/accel/accel.c
new file mode 100644
index 0000000..51404c3
--- /dev/null
+++ b/n/accel/accel.c
@@ -0,0 +1,155 @@
+/* accel.c */
+/* accel. {{{
+ *
+ * Copyright (C) 2004 Thomas Burg
+ *
+ * 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.
+ *
+ * Contact :
+ * Email: <burg@efrei.fr>
+ * }}} */
+
+/*
+ * Brochange :
+ * Xout <--> (ICP1)PB0
+ * Yout <--> (AIN0)PD6
+ *
+ */
+
+
+#include <avr/io.h>
+#include <inttypes.h>
+#include <avr/signal.h>
+#include <avr/interrupt.h>
+#include <string.h>
+#include <n/avr/rs232/rs232.h>
+#include "avrconfig.h"
+#include <n/avr/proto/proto.h>
+
+#define DEBUG
+
+#ifdef DEBUG
+#define debug_putc(x) rs232_putc (x)
+#else
+#define debug_putc(x)
+#endif
+
+/* definition de nouveau type necessaire à l'acquisition
+ * des données
+ * */
+
+enum T_etat {strt_at_Ta, rd_at_Tb, rd_at_Tc, rd_at_Td, calcul };
+int16_t Tb,Tc,Td;
+
+/* Variable Globale
+ */
+
+enum T_etat etat;
+
+/* Interruption input_capture */
+
+SIGNAL(SIG_INPUT_CAPTURE1)
+{
+ rs232_putc('i');
+ //code de l'interruption
+ switch (etat)
+ {
+ case strt_at_Ta :
+ debug_putc('A');
+ TCNT1 = 0; // remise de TCNT1 (counter 16bits)
+ TCCR1B &= ~_BV(ICES1); // trigge sur le front descendant
+ TIMSK |= 0x20; //autorise les interruptions Input Capture
+ etat = rd_at_Tb;
+ break;
+ case rd_at_Tb :
+ //debug_putc('B');
+ Tb = ICR1; // sauvegarde du registre Input Capture
+ etat = rd_at_Tc;
+ TIMSK &= ~0x20; //désactive les interruptions Input Capture
+ ACSR |= 0x04; //change d'entrée : analogue comparator
+ TCCR1B |= _BV(ICES1); //trigge sur le front montant
+ TIMSK |= 0x20; // Réactive les interruptions Input Capture
+ break;
+ case rd_at_Tc :
+ //debug_putc('C');
+ Tc = ICR1; // sauvegarde du registre Input Capture
+ TIMSK &= ~0x20; //désactive les interruptions Input Capture
+ TCCR1B &= ~_BV(ICES1); // trigge sur le front descendant
+ TIMSK |= 0x20; // Réactive les interruptions Input Capture
+ etat = rd_at_Td;
+ break;
+ case rd_at_Td :
+ debug_putc('D');
+ Td = ICR1; // sauvegarde du registre Input Capture
+ etat = calcul;
+ TIMSK &= ~0x20; //désactive les interruptions Input Capture
+ ACSR &= ~0x04; //change d'entrée : IPC
+ TCCR1B |= _BV(ICES1); //trigge sur le front montant
+ TIMSK |= 0x20; // Réactive les interruptions Input Capture
+ break;
+ default : debug_putc('L');
+ break; // Ce cas ne doit pas arriver
+ }
+}
+
+SIGNAL(SIG_OVERFLOW1)
+{
+ proto_send1('E',1); //erreur 1 Overflow timer/compteur1
+}
+
+void
+test_callback (uint8_t c, uint8_t argc, proto_arg_t argv[])
+{
+ proto_send (c, argc, argv);
+}
+
+int
+main (void)
+{
+ int16_t Tx,Ty,T2,T2_old;
+
+ //initialisation
+ rs232_init();
+ proto_init(test_callback,rs232_putc);
+
+ proto_send0('Z');
+
+ //initialisation du timer
+ TCCR1B = _BV(ICES1) | _BV(CS10);
+ TIMSK = _BV(TICIE1); // Input Capture Interrupt enable
+ sei ();
+ //Boucle
+ while(1)
+ {
+ if ( rs232_poll() )
+ {
+ proto_accept(rs232_getc());
+ }
+ // calcul des Tons
+ if (etat == calcul)
+ {
+ Tx = Tb; // Calcul des Ton
+ Ty = Td - Tc;
+ // Todo T2 ne doit pas être calculer tout le temps
+ T2 = (Td - ((Td - Tc)/2)) - Tb/2; //Calcul de T2
+ T2_old =T2;
+ //proto_send4 ('M', Tx >> 8, Tx, Ty >> 8, Ty); // envoie les 2 temps Ton
+ etat = strt_at_Ta; //remise dans l'état de capture
+ proto_send2 ('N', T2_old >>8, T2_old); // envoie la valeur T2
+ }
+
+ }
+ return 0;
+}