From b270fb1a79b18ee1b15613b46eedad20f0cb8901 Mon Sep 17 00:00:00 2001 From: burg Date: Wed, 21 Jul 2004 08:12:19 +0000 Subject: Création du projet accelerometre --- n/accel/Makefile | 16 ++++++ n/accel/Makefile.avr | 100 +++++++++++++++++++++++++++++++++ n/accel/accel.c | 155 +++++++++++++++++++++++++++++++++++++++++++++++++++ n/accel/avrconfig.h | 60 ++++++++++++++++++++ 4 files changed, 331 insertions(+) create mode 100644 n/accel/Makefile create mode 100644 n/accel/Makefile.avr create mode 100644 n/accel/accel.c create mode 100644 n/accel/avrconfig.h (limited to 'n/accel') diff --git a/n/accel/Makefile b/n/accel/Makefile new file mode 100644 index 0000000..1c62445 --- /dev/null +++ b/n/accel/Makefile @@ -0,0 +1,16 @@ +PROGS =accel +SOURCES = accel.c +DOC = howto-avr.html +EXTRACTDOC = +MODULES = n/avr/proto n/avr/rs232 +CONFIGFILE = avrconfig.h +# atmega8, atmega8535, atmega128... +MCU_TARGET = atmega8 +# -O2 : speed +# -Os : size +OPTIMIZE = -O2 + +DEFS = +LIBS = + +include Makefile.avr diff --git a/n/accel/Makefile.avr b/n/accel/Makefile.avr new file mode 100644 index 0000000..01c9b7b --- /dev/null +++ b/n/accel/Makefile.avr @@ -0,0 +1,100 @@ +# Flags. {{{1 + +CC = avr-gcc + +CFLAGS = -g -Wall $(OPTIMIZE) -mmcu=$(MCU_TARGET) \ + $(if $(CONFIGFILE), $(CONFIGFILE:%=-include %)) +CPPFLAGS = $(DEFS) -Imodules +LDFLAGS = + +OBJCOPY = avr-objcopy +OBJDUMP = avr-objdump + +# Main rules. {{{1 + +CSOURCES = $(filter %.c, $(SOURCES)) +SSOURCES = $(filter %.s, $(SOURCES)) +OBJECTS = $(CSOURCES:%.c=%.o) $(SSOURCES:%.s=%.o) +ELFS = $(PROGS:%=%.elf) + +all: elf lst hex + +.PHONY: all clean elf lst doc text hex srec bin eeprom ehex esrec ebin + +# Rules for modules. {{{1 + +MODULESFILES = $(MODULES:%=modules/%/Makefile.module) + +include $(MODULESFILES) + +SOURCES += $(MODULESSOURCES) + +$(MODULESFILES): + mkdir -p modules + cd modules && cvs co $(@:modules/%/Makefile.module=%) + test -f $@ + +# General rules. {{{1 + +elf: $(PROGS:%=%.elf) +lst: $(PROGS:%=%.lst) + +$(PROGS:%=%.elf): $(OBJECTS) + $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS) + +%.lst: %.elf + $(OBJDUMP) -h -S $< > $@ + +clean: + rm -f *.o $(OBJECTS) $(PROGS:%=%.elf) *.lst *.map *.bak *~ + rm -f $(DOC) *.exd $(EXTRA_CLEAN_FILES) $(TEXTS) $(EEPROMS) + +# Rules for building the doc. {{{1 + +doc: $(DOC) + +%.html: %.txt %.exd + aft $< + +%.exd: $(EXTRACTDOC) + test -n "$^" && extractdoc $^ > $@ || true + +# Rules for building the .text rom images. {{{1 + +TEXTS = $(PROGS:%=%.hex) $(PROGS:%=%.bin) $(PROGS:%=%.srec) + +text: hex bin srec + +hex: $(PROGS:%=%.hex) +bin: $(PROGS:%=%.bin) +srec: $(PROGS:%=%.srec) + +%.hex: %.elf + $(OBJCOPY) -j .text -j .data -O ihex $< $@ + +%.srec: %.elf + $(OBJCOPY) -j .text -j .data -O srec $< $@ + +%.bin: %.elf + $(OBJCOPY) -j .text -j .data -O binary $< $@ + +# Rules for building the .eeprom rom images. {{{1 + +EEPROMS = $(PROGS:%=%_eeprom.hex) $(PROGS:%=%_eeprom.bin) \ + $(PROGS:%=%_eeprom.srec) + +eeprom: ehex ebin esrec + +ehex: $(PROGS:%=%_eeprom.hex) +ebin: $(PROGS:%=%_eeprom.bin) +esrec: $(PROGS:%=%_eeprom.srec) + +%_eeprom.hex: %.elf + $(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O ihex $< $@ + +%_eeprom.srec: %.elf + $(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O srec $< $@ + +%_eeprom.bin: %.elf + $(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O binary $< $@ + 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: + * }}} */ + +/* + * Brochange : + * Xout <--> (ICP1)PB0 + * Yout <--> (AIN0)PD6 + * + */ + + +#include +#include +#include +#include +#include +#include +#include "avrconfig.h" +#include + +#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; +} diff --git a/n/accel/avrconfig.h b/n/accel/avrconfig.h new file mode 100644 index 0000000..018f0c1 --- /dev/null +++ b/n/accel/avrconfig.h @@ -0,0 +1,60 @@ +#ifndef avrconfig_h +#define avrconfig_h +/* avrconfig.h - config file for avr projects. */ +/* n.avr.accel - Acceleration. {{{ + * + * 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: + * }}} */ + +/* global */ +/** AVR Frequency : 1000000, 1843200, 2000000, 3686400, 4000000, 7372800, + * 8000000, 11059200, 14745600, 16000000, 18432000, 20000000. */ +#define AC_FREQ 14745600 + +/* proto - Protocol module. */ +/** Maximum argument number. */ +#define AC_PROTO_MAX_ARGS 3 +/** Protocol arguments type. */ +#define AC_PROTO_ARG_TYPE uint8_t + +/* rs232 - RS232 Module. */ +/** Baudrate : 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, 76800, + * 115200, 230400, 250000, 500000, 1000000. */ +#define AC_RS232_BAUDRATE 115200 +/** Send mode : + * - POLLING : no interrupts; + * - RING : interrupts, ring buffer; + * - PACKETS : interrupts, by packets delimited by SOP and EOP + * (unimplemented). */ +#define AC_RS232_SEND_MODE POLLING +/** Recv mode, same as send mode. */ +#define AC_RS232_RECV_MODE POLLING +/** Character size : 5, 6, 7, 8, 9 (only 8 implemented). */ +#define AC_RS232_CHAR_SIZE 8 +/** Parity : ODD, EVEN, NONE. */ +#define AC_RS232_PARITY EVEN +/** Stop bits : 1, 2. */ +#define AC_RS232_STOP_BITS 1 +/** Send buffer size, should be power of 2 for RING mode. */ +#define AC_RS232_SEND_BUFFER_SIZE 32 +/** Recv buffer size, should be power of 2 for RING mode. */ +#define AC_RS232_RECV_BUFFER_SIZE 32 + +#endif /* avrconfig_h */ -- cgit v1.2.3