From a3e326954c6cc15003986e9c81d1a36e58d500e7 Mon Sep 17 00:00:00 2001 From: burg Date: Tue, 22 Mar 2005 08:43:24 +0000 Subject: Ajout du Module ADC (non testé) --- n/avr/adc/Makefile | 16 ++++++++ n/avr/adc/Makefile.avr | 100 +++++++++++++++++++++++++++++++++++++++++++++++++ n/avr/adc/README | 1 + n/avr/adc/adc.c | 98 ++++++++++++++++++++++++++++++++++++++++++++++++ n/avr/adc/adc.h | 48 ++++++++++++++++++++++++ n/avr/adc/adc.txt | 15 ++++++++ n/avr/adc/avrconfig.h | 63 +++++++++++++++++++++++++++++++ n/avr/adc/test_adc.c | 65 ++++++++++++++++++++++++++++++++ 8 files changed, 406 insertions(+) create mode 100644 n/avr/adc/Makefile create mode 100644 n/avr/adc/Makefile.avr create mode 100644 n/avr/adc/README create mode 100644 n/avr/adc/adc.c create mode 100644 n/avr/adc/adc.h create mode 100644 n/avr/adc/adc.txt create mode 100644 n/avr/adc/avrconfig.h create mode 100644 n/avr/adc/test_adc.c (limited to 'n') diff --git a/n/avr/adc/Makefile b/n/avr/adc/Makefile new file mode 100644 index 0000000..3dd2ee6 --- /dev/null +++ b/n/avr/adc/Makefile @@ -0,0 +1,16 @@ +PROGS = test_adc +SOURCES = test_adc.c adc.c +DOC = adc.html +EXTRACTDOC = adc.c avrconfig.h +MODULES = n/avr/proto n/avr/rs232 n/avr/utils +CONFIGFILE = avrconfig.h +# atmega8, atmega8535, atmega128... +MCU_TARGET = atmega8535 +# -O2 : speed +# -Os : size +OPTIMIZE = -O2 + +DEFS = +LIBS = + +include Makefile.avr diff --git a/n/avr/adc/Makefile.avr b/n/avr/adc/Makefile.avr new file mode 100644 index 0000000..01c9b7b --- /dev/null +++ b/n/avr/adc/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/avr/adc/README b/n/avr/adc/README new file mode 100644 index 0000000..1fd657a --- /dev/null +++ b/n/avr/adc/README @@ -0,0 +1 @@ +n.avr.adc - AVR ADC Module. diff --git a/n/avr/adc/adc.c b/n/avr/adc/adc.c new file mode 100644 index 0000000..d7b3581 --- /dev/null +++ b/n/avr/adc/adc.c @@ -0,0 +1,98 @@ +/* adc.c */ +/* n.avr.adc - AVR ADC Module. {{{ + * + * Copyright (C) 2005 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: + * }}} */ +#include "adc.h" + +#include +#include + +/* Tested AVR check. */ +#if defined (__AVR_ATmega8__) +#elif defined (__AVR_ATmega8535__) +#elif defined (__AVR_ATmega128__) +#else +# warning "adc: not tested on this chip." +#endif + + +/* +AutoDec */ +/* -AutoDec */ + +/* CONF: + * 0x40 AVCC with external capacitor at AREF pin + * 0xC0 Internal 2.56V Voltage Reference with external capacitor at AREF + * pin + */ + +#define CONF 0xC0 + +/** Initialise adc. */ +void +adc_init (void) +{ + ADMUX = CONF; +#if defined (__AVR_ATmega8535__) + ADCSRA = ADEN | 0x7; +#else + ADCSR = ADEN | 0x7; +#endif +} + +/** Choose and start mesure on adc line */ +void +adc_start (uint8_t d) +{ + // Choose adc + ADMUX = CONF | ( d & 0x07); + /* ADEN active l'adc + * ADSC demarre la mesure */ +#if defined (__AVR_ATmega8535__) + ADCSRA |= ADSC; +#else + ADCSR |= ADSC; +#endif +} + +/** check on finish mesure */ +uint8_t +adc_checkf (void) +{ + // return != than zero when convertion is complete +#if defined (__AVR_ATmega8535__) + return ADCSRA & ADIF; +#else + return ADCSR & ADIF; +#endif +} + +/** Read mesure */ +uint16_t +adc_read (void) +{ +#if defined (__AVR_ATmega8535__) + ADCSRA &= ~ADIF; +#else + ADCSR &= ~ADIF; +#endif + return ADCW; +} + diff --git a/n/avr/adc/adc.h b/n/avr/adc/adc.h new file mode 100644 index 0000000..57df73a --- /dev/null +++ b/n/avr/adc/adc.h @@ -0,0 +1,48 @@ +#ifndef adc_h +#define adc_h +/* adc.h */ +/* n.avr.adc - AVR ADC Module. {{{ + * + * Copyright (C) 2005 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: + * }}} */ + +#include + +/* +AutoDec */ + +/** Initialise adc. */ +void +adc_init (void); + +/** choose and start mesure on adc line */ +void +adc_start (uint8_t d); + +/** check on finish mesure */ +uint8_t +adc_checkf (void); + +/** read mesure */ +uint16_t +adc_read (void); + +/* -AutoDec */ + +#endif /* adc_h */ diff --git a/n/avr/adc/adc.txt b/n/avr/adc/adc.txt new file mode 100644 index 0000000..a024aa4 --- /dev/null +++ b/n/avr/adc/adc.txt @@ -0,0 +1,15 @@ +*Title: Module AVR ADC +*Author: Tb + +* Utilisation + +Rien de plus simple. Appeler la fonction |adc_init| au démarrage, puis +|adc_start| pour commencer une mesure. Cette fonction nécessite un +parametre uint8_t d dont seul les 3 premiers bits servent à désigner +l'adc. +|adc_checkf| renvoit un uint8_t different de zero lorsque la mesure est +fini. +|adc_read| renvoit un uint16_t avec la valeur de la mesure. +* Doc + +*File: adc.exd diff --git a/n/avr/adc/avrconfig.h b/n/avr/adc/avrconfig.h new file mode 100644 index 0000000..ec66267 --- /dev/null +++ b/n/avr/adc/avrconfig.h @@ -0,0 +1,63 @@ +#ifndef avrconfig_h +#define avrconfig_h +/* avrconfig.h - config file for avr projects. */ +/* n.avr.proto - Protocol Module. {{{ + * + * Copyright (C) 2004 Nicolas Schodet + * + * 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 : + * Web: http://perso.efrei.fr/~schodet/ + * Email: + * }}} */ + +/* global */ +/** AVR Frequency : 1000000, 1843200, 2000000, 3686400, 4000000, 7372800, + * 8000000, 11059200, 14745600, 16000000, 18432000, 20000000. */ +#define AC_FREQ 14745600 + +/* 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. */ +#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 +/** Select serial port (0 or 1). */ +#define AC_RS232_PORT 0 + +/* proto - Protocol module. */ +/** Maximum argument size. */ +#define AC_PROTO_ARGS_MAX_SIZE 8 +/** Callback function name. */ +#define AC_PROTO_CALLBACK proto_callback +/** Putchar function name. */ +#define AC_PROTO_PUTC rs232_putc + +#endif /* avrconfig_h */ diff --git a/n/avr/adc/test_adc.c b/n/avr/adc/test_adc.c new file mode 100644 index 0000000..6e7d420 --- /dev/null +++ b/n/avr/adc/test_adc.c @@ -0,0 +1,65 @@ +/* test_adc.c */ +/* {{{ + * + * Copyright (C) 2005 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: + * }}} */ +#include +#include + +#include "adc.h" + +/* +AutoDec */ +/* -AutoDec */ + +void +proto_callback (uint8_t cmd, uint8_t size, uint8_t *args) +{ + proto_send (cmd, size, args); +} + +void +proto_putc (uint8_t c) +{ + rs232_putc (c); +} + +int +main (void) +{ + rs232_init (); + rs232_putc ('!'); + rs232_putc ('z'); + rs232_putc ('\r'); + + adc_init(); + while (1) + { + /* Démarre une acquisition de mesure sur l'adc 0 */ + adc_start(0x00); + + /* Attente active sur adc jusqu'a ce que la valeur soit disponible + * */ + while (adc_checkf !=0); + + /*Lit la valeur puis l'envoit */ + proto_send1w('m',adc_read()); + } + +} -- cgit v1.2.3