Add voltmeter1

This commit is contained in:
Michael Hayes
2013-05-24 05:39:35 +00:00
parent cd2e6f4460
commit 1320cad49c
79 changed files with 849 additions and 106 deletions
+70
View File
@@ -0,0 +1,70 @@
# File: Makefile
# Author: M. P. Hayes, UCECE
# Date: 12 Sep 2010
# Descr: Makefile for voltmeter1
# Definitions.
CC = avr-gcc
CFLAGS = -mmcu=atmega32u2 -Os -Wall -Wstrict-prototypes -Wextra -g -I. -I../../drivers -I../../fonts -I../../drivers/avr -I../../utils
OBJCOPY = avr-objcopy
SIZE = avr-size
DEL = rm
# Default target.
all: voltmeter1.out
# Compile: create object files from C source files.
voltmeter1.o: voltmeter1.c ../../drivers/avr/system.h ../../drivers/avr/timer.h ../../drivers/display.h ../../fonts/font3x5_1.h ../../utils/font.h ../../utils/task.h ../../utils/tinygl.h adc.h
$(CC) -c $(CFLAGS) $< -o $@
adc.o: adc.c ../../drivers/avr/delay.h ../../drivers/avr/pio.h ../../drivers/avr/system.h adc.h
$(CC) -c $(CFLAGS) $< -o $@
pio.o: ../../drivers/avr/pio.c ../../drivers/avr/pio.h ../../drivers/avr/system.h
$(CC) -c $(CFLAGS) $< -o $@
system.o: ../../drivers/avr/system.c ../../drivers/avr/system.h
$(CC) -c $(CFLAGS) $< -o $@
timer.o: ../../drivers/avr/timer.c ../../drivers/avr/system.h ../../drivers/avr/timer.h
$(CC) -c $(CFLAGS) $< -o $@
display.o: ../../drivers/display.c ../../drivers/avr/system.h ../../drivers/display.h ../../drivers/ledmat.h
$(CC) -c $(CFLAGS) $< -o $@
ledmat.o: ../../drivers/ledmat.c ../../drivers/avr/pio.h ../../drivers/avr/system.h ../../drivers/ledmat.h
$(CC) -c $(CFLAGS) $< -o $@
font.o: ../../utils/font.c ../../drivers/avr/system.h ../../utils/font.h
$(CC) -c $(CFLAGS) $< -o $@
task.o: ../../utils/task.c ../../drivers/avr/system.h ../../drivers/avr/timer.h ../../utils/task.h
$(CC) -c $(CFLAGS) $< -o $@
tinygl.o: ../../utils/tinygl.c ../../drivers/avr/system.h ../../drivers/display.h ../../utils/font.h ../../utils/tinygl.h
$(CC) -c $(CFLAGS) $< -o $@
# Link: create output file (executable) from object files.
voltmeter1.out: voltmeter1.o adc.o pio.o system.o timer.o display.o ledmat.o font.o task.o tinygl.o
$(CC) $(CFLAGS) $^ -o $@ -lm
$(SIZE) $@
# Target: clean project.
.PHONY: clean
clean:
-$(DEL) *.o *.out *.hex
# Target: program project.
.PHONY: program
program: voltmeter1.out
$(OBJCOPY) -O ihex voltmeter1.out voltmeter1.hex
dfu-programmer atmega32u2 erase; dfu-programmer atmega32u2 flash voltmeter1.hex; dfu-programmer atmega32u2 start
+64
View File
@@ -0,0 +1,64 @@
# File: Makefile
# Author: M. P. Hayes, UCECE
# Date: 11 Sep 2010
# Descr: Makefile for voltmeter1
CC = gcc
CFLAGS = -Wall -Wstrict-prototypes -Wextra -g -I. -I../../drivers/test -I../../drivers -I../../fonts -I../../utils
DEL = rm
# Default target.
all: voltmeter1
# Compile: create object files from C source files.
voltmeter1-test.o: voltmeter1.c ../../drivers/display.h ../../drivers/test/system.h ../../drivers/test/timer.h ../../fonts/font3x5_1.h ../../utils/font.h ../../utils/task.h ../../utils/tinygl.h adc.h
$(CC) -c $(CFLAGS) $< -o $@
adc-test.o: adc.c ../../drivers/test/avrtest.h ../../drivers/test/delay.h ../../drivers/test/pio.h ../../drivers/test/system.h adc.h
$(CC) -c $(CFLAGS) $< -o $@
display-test.o: ../../drivers/display.c ../../drivers/display.h ../../drivers/ledmat.h ../../drivers/test/system.h
$(CC) -c $(CFLAGS) $< -o $@
ledmat-test.o: ../../drivers/ledmat.c ../../drivers/ledmat.h ../../drivers/test/avrtest.h ../../drivers/test/pio.h ../../drivers/test/system.h
$(CC) -c $(CFLAGS) $< -o $@
mgetkey-test.o: ../../drivers/test/mgetkey.c ../../drivers/test/mgetkey.h
$(CC) -c $(CFLAGS) $< -o $@
pio-test.o: ../../drivers/test/pio.c ../../drivers/test/avrtest.h ../../drivers/test/pio.h ../../drivers/test/system.h
$(CC) -c $(CFLAGS) $< -o $@
system-test.o: ../../drivers/test/system.c ../../drivers/test/avrtest.h ../../drivers/test/mgetkey.h ../../drivers/test/pio.h ../../drivers/test/system.h
$(CC) -c $(CFLAGS) $< -o $@
timer-test.o: ../../drivers/test/timer.c ../../drivers/test/system.h ../../drivers/test/timer.h
$(CC) -c $(CFLAGS) $< -o $@
font-test.o: ../../utils/font.c ../../drivers/test/system.h ../../utils/font.h
$(CC) -c $(CFLAGS) $< -o $@
task-test.o: ../../utils/task.c ../../drivers/test/system.h ../../drivers/test/timer.h ../../utils/task.h
$(CC) -c $(CFLAGS) $< -o $@
tinygl-test.o: ../../utils/tinygl.c ../../drivers/display.h ../../drivers/test/system.h ../../utils/font.h ../../utils/tinygl.h
$(CC) -c $(CFLAGS) $< -o $@
# Link: create executable file from object files.
voltmeter1: voltmeter1-test.o adc-test.o display-test.o ledmat-test.o mgetkey-test.o pio-test.o system-test.o timer-test.o font-test.o task-test.o tinygl-test.o
$(CC) $(CFLAGS) $^ -o $@ -lrt
# Clean: delete derived files.
.PHONY: clean
clean:
-$(DEL) voltmeter1 voltmeter1-test.o adc-test.o display-test.o ledmat-test.o mgetkey-test.o pio-test.o system-test.o timer-test.o font-test.o task-test.o tinygl-test.o
+104
View File
@@ -0,0 +1,104 @@
#include "avr/io.h"
#include "pio.h"
#include "delay.h"
#include "adc.h"
/**
This requires a capacitor connected from AIN0 (P1-3) to ground
(P1-1) and a resistor connected from AIN0 (P1-3) to PD5 (P1-7).
The unknown input voltage is applied to AIN3 (P1-6).
It operates by applying a fixed voltage from PD5 (5 V, assuming the
UCFK4 is powered from USB) to the capacitor and timing how long it
takes for the capacitor voltage to exceed the unknown voltage using
the comparator.
Here's the typical usage for two channels.:
adc_init ();
adc_enable (ADC_CHANNEL1);
adc_enable (ADC_CHANNEL2);
adc_measure (ADC_CHANNEL1);
wait until capacitor discharged
adc_measure (ADC_CHANNEL2);
Note adc_measure blocks until the voltage on the capacitor exceeds
the voltage being measured. adc_measure cannot be called again
until the capacitor is discharged. This could be greatly sped up
by using another PIO directly connected to the capacitor. Better
still, since PD1 shares the same pin as AIN0, it can be configured
as a low output to more rapidly discharge the capacitor.
From the IBIS file it appears that a PIO can sink/source 60 mA
(typ) at 25 deg. C. The datasheet says that the maximum PIO DC
current is 40 mA.
*/
#ifndef ADC_CHARGE_PIO
#define ADC_CHARGE_PIO PIO_DEFINE(PORT_D, 5)
#endif
#ifndef ADC_CAPACITANCE
#define ADC_CAPACITANCE 4.7e-9
#endif
#ifndef ADC_RESISTANCE
#define ADC_RESISTANCE 37e3
#endif
#define ADC_TIME_CONSTANT_US (ADC_CAPACITANCE * ADC_RESISTANCE * 1e6)
/* This is not defined in io32u2.h */
#ifndef ACMUX
#define ACMUX _SFR_MEM8(0x7D)
#endif
void adc_init(void)
{
pio_config_set(ADC_CHARGE_PIO, PIO_OUTPUT_LOW);
/* Disable digital input for AIN0. */
DIDR1 |= BIT(0);
/* By default the analog comparator is enabled but let's enable it. */
ACSR &= BIT(7);
}
void adc_enable(uint8_t adc_channel)
{
/* Disable digital input for selected channel. */
DIDR1 |= BIT(adc_channel);
}
void adc_disable(uint8_t adc_channel)
{
/* Enable digital input for selected channel. */
DIDR1 &= ~BIT(adc_channel);
}
uint8_t adc_measure(uint8_t adc_channel)
{
uint8_t count;
count = 0;
/* Select desired channel. */
ACMUX = adc_channel - 1;
pio_output_high (ADC_CHARGE_PIO);
while (! (ACSR & BIT(ACO)))
count++;
pio_output_low (ADC_CHARGE_PIO);
return count;
}
+17
View File
@@ -0,0 +1,17 @@
#ifndef ADC_H
#define ADC_H
#include "system.h"
void adc_init(void);
void adc_enable(uint8_t adc_channel);
void adc_disable(uint8_t adc_channel);
uint8_t adc_measure(uint8_t adc_channel);
#endif
+45
View File
@@ -0,0 +1,45 @@
# File: Makefile
# Author: M. P. Hayes, UCECE
# Date: 11 Sep 2010
# Descr: Makefile for voltmeter1 docs
# The scripts used to make the graphs require the program dot; this
# is part of the graphviz package.
DEL = rm
all: file_dependencies.pdf module_dependencies.pdf makefile_dependencies.pdf build_dependencies.pdf callgraph.pdf
file_dependencies.pdf: files.d
../../../etc/graphdeps.py $< --out $@
module_dependencies.pdf: modules.d
../../../etc/graphdeps.py $< --modules --rotate --out $@
makefile_dependencies.pdf: ../Makefile
../../../etc/graphdeps.py $< --out $@
build_dependencies.pdf: ../Makefile
../../../etc/graphdeps.py $< --out $@ --showops
callgraph.pdf: callgraph.d
../../../etc/graphdeps.py --calls --modules $< --out $@ --showops
files.d: ../Makefile
(cd ..; ../../etc/makemake.py --relpath --files . . ../../drivers ../../drivers/avr ../../utils ../../extra --exclude system.h > doc/files.d)
modules.d: ../Makefile
(cd ..; ../../etc/makemake.py --relpath --modules . . ../../drivers ../../drivers/avr ../../utils ../../extra --exclude system > doc/modules.d)
callgraph.d: ../Makefile
(cd ..; ../../etc/makemake.py --cc="avr-gcc" --cflags="-Os -mmcu=atmega32u2" --relpath --calls . . ../../drivers ../../drivers/avr ../../utils ../../extra --exclude system.h > doc/callgraph.d)
# Clean: delete derived files.
.PHONY: clean
clean:
-$(DEL) *.d *.pdf
+16
View File
@@ -0,0 +1,16 @@
Running make in this directory will generate a number of PDF graphs.
In the callgraph, the arrows means "calls". In the dependency graphs,
the arrows means "requires" (or "depends upon").
callgraph.pdf This shows the callgraph, i.e., what functions each
function in the program calls.
module_dependencies.pdf This shows the dependencies between the modules.
file_dependencies.pdf This shows the dependencies between the files.
makefile_dependencies.pdf This shows the dependencies required by make when
building the program.
build_dependencies.pdf This is like makefile_dependencies.pdf but shows
the operations performed to generate the new file.
callgraph.d This shows the callgraph in text format.
files.d This shows the file dependencies in text format.
modules.d This shows the module dependencies in text format.
Binary file not shown.
+80
View File
@@ -0,0 +1,80 @@
system_clock_init@system.c:
system_watchdog_timer_init@system.c:
system_init@system.c: system_clock_init system_watchdog_timer_init
pio_config_set@pio.c:
ledmat_init@ledmat.c: pio_config_set pio_config_set
display_clear@display.c:
display_init@display.c: ledmat_init display_clear
tinygl_text_speed_set@tinygl.c:
tinygl_clear@tinygl.c: display_clear
tinygl_init@tinygl.c: display_init tinygl_text_speed_set tinygl_clear
tinygl_font_set@tinygl.c:
tinygl_text_mode_set@tinygl.c:
tinygl_text_dir_set@tinygl.c:
display_task_init@voltmeter1.c: tinygl_init tinygl_font_set tinygl_text_mode_set tinygl_text_dir_set
adc_init@adc.c: pio_config_set
adc_enable@adc.c:
adc_task_init@voltmeter1.c: adc_init adc_enable
timer_init@timer.c:
timer_get@timer.c:
timer_wait_until@timer.c: timer_get
task_schedule@task.c: timer_init timer_get timer_wait_until timer_get
tinygl_point@tinygl.c:
font_pixel_get@font.c:
tinygl_font_pixel_get@tinygl.c: font_pixel_get
display_pixel_set@display.c:
tinygl_draw_point@tinygl.c: display_pixel_set
tinygl_draw_char@tinygl.c: tinygl_point tinygl_font_pixel_get tinygl_draw_point tinygl_point tinygl_draw_point tinygl_point tinygl_font_pixel_get tinygl_draw_point tinygl_point tinygl_draw_point
tinygl_draw_string@tinygl.c: tinygl_draw_char
tinygl_text_advance@tinygl.c: tinygl_draw_string tinygl_draw_string
pio_output_high@pio.c:
pio_output_low@pio.c:
ledmat_display_column@ledmat.c: pio_output_high pio_output_low pio_output_high pio_output_low
display_update@display.c: ledmat_display_column
tinygl_update@tinygl.c: tinygl_text_advance display_update
display_task@voltmeter1.c: tinygl_update
adc_measure@adc.c: pio_output_high pio_output_low
tinygl_draw_message@tinygl.c: strncpy strlen
tinygl_text@tinygl.c: tinygl_draw_message
adc_task@voltmeter1.c: adc_measure tinygl_text
main@voltmeter1.c: system_init display_task_init adc_task_init task_schedule @display_task @adc_task
Binary file not shown.
Binary file not shown.
+62
View File
@@ -0,0 +1,62 @@
../../drivers/avr/timer.h:
../../drivers/avr/timer.c: ../../drivers/avr/timer.h
../../drivers/avr/timer.o: ../../drivers/avr/timer.c
../../drivers/avr/system.c:
../../drivers/avr/system.o: ../../drivers/avr/system.c
../../utils/task.h: ../../drivers/avr/timer.h
../../drivers/display.h:
../../utils/font.h:
../../utils/tinygl.h: ../../drivers/display.h ../../utils/font.h
adc.h:
../../fonts/font3x5_1.h: ../../utils/font.h
voltmeter1.c: ../../utils/task.h ../../utils/tinygl.h adc.h ../../fonts/font3x5_1.h
voltmeter1.o: voltmeter1.c
../../utils/font.c: ../../utils/font.h
../../utils/font.o: ../../utils/font.c
../../drivers/avr/pio.h:
../../drivers/avr/delay.h:
adc.c: ../../drivers/avr/pio.h ../../drivers/avr/delay.h adc.h
adc.o: adc.c
../../drivers/avr/pio.c: ../../drivers/avr/pio.h
../../drivers/avr/pio.o: ../../drivers/avr/pio.c
../../drivers/ledmat.h:
../../drivers/display.c: ../../drivers/ledmat.h ../../drivers/display.h
../../drivers/display.o: ../../drivers/display.c
../../drivers/ledmat.c: ../../drivers/avr/pio.h ../../drivers/ledmat.h
../../drivers/ledmat.o: ../../drivers/ledmat.c
../../utils/tinygl.c: ../../utils/tinygl.h ../../drivers/display.h ../../utils/font.h
../../utils/tinygl.o: ../../utils/tinygl.c
../../utils/task.c: ../../utils/task.h ../../drivers/avr/timer.h
../../utils/task.o: ../../utils/task.c
voltmeter1.out: ../../drivers/avr/timer.o ../../drivers/avr/system.o voltmeter1.o ../../utils/font.o adc.o ../../drivers/avr/pio.o ../../drivers/display.o ../../drivers/ledmat.o ../../utils/tinygl.o ../../utils/task.o
Binary file not shown.
Binary file not shown.
+18
View File
@@ -0,0 +1,18 @@
timer:
task: timer
pio:
ledmat: pio
display: ledmat
font:
tinygl: display font
adc: pio
voltmeter1: task tinygl adc
+76
View File
@@ -0,0 +1,76 @@
/** @file voltmeter1.c
@author M. P. Hayes, UCECE
@date 1 February 2013
@brief Simple message display program
@defgroup voltmeter1 Simple voltmeter
*/
#include "system.h"
#include "task.h"
#include "tinygl.h"
#include "adc.h"
#include "../fonts/font3x5_1.h"
/* Define polling rates in Hz. */
#define DISPLAY_TASK_RATE 250
#define ADC_TASK_RATE 1
#define ADC_AIN 3
static void adc_task_init (void)
{
adc_init ();
adc_enable (ADC_AIN);
}
static void adc_task (__unused__ void *data)
{
uint8_t count;
char str[3];
count = adc_measure (ADC_AIN);
str[0] = ((count / 10) % 10) + '0';
str[1] = (count % 10) + '0';
str[2] = 0;
tinygl_text (str);
}
static void display_task_init (void)
{
tinygl_init (DISPLAY_TASK_RATE);
tinygl_font_set (&font3x5_1);
tinygl_text_mode_set (TINYGL_TEXT_MODE_STEP);
tinygl_text_dir_set (TINYGL_TEXT_DIR_ROTATE);
}
static void display_task (__unused__ void *data)
{
tinygl_update ();
}
int main (void)
{
task_t tasks[] =
{
{.func = display_task, .period = TASK_RATE / DISPLAY_TASK_RATE},
{.func = adc_task, .period = TASK_RATE / ADC_TASK_RATE},
};
system_init ();
display_task_init ();
adc_task_init ();
task_schedule (tasks, ARRAY_SIZE (tasks));
return 0;
}