Started following some tutorial on att85 usi. Downloaded example code from make avr book.

This commit is contained in:
Dan
2022-09-20 01:08:01 -04:00
parent d0cbc0000e
commit 361a828c46
295 changed files with 68746 additions and 0 deletions

View File

@@ -0,0 +1,196 @@
##########------------------------------------------------------##########
########## Project-specific Details ##########
########## Check these every time you start a new project ##########
##########------------------------------------------------------##########
MCU = atmega168p
F_CPU = 8000000UL
BAUD = 9600UL
## Also try BAUD = 19200 or 38400 if you're feeling lucky.
## A directory for common include files and the simple USART library.
## If you move either the current folder or the Library folder, you'll
## need to change this path to match.
LIBDIR = ../../AVR-Programming-Library
##########------------------------------------------------------##########
########## Programmer Defaults ##########
########## Set up once, then forget about it ##########
########## (Can override. See bottom of file.) ##########
##########------------------------------------------------------##########
PROGRAMMER_TYPE = usbtiny
# extra arguments to avrdude: baud rate, chip type, -F flag, etc.
PROGRAMMER_ARGS =
##########------------------------------------------------------##########
########## Program Locations ##########
########## Won't need to change if they're in your PATH ##########
##########------------------------------------------------------##########
CC = avr-gcc
OBJCOPY = avr-objcopy
OBJDUMP = avr-objdump
AVRSIZE = avr-size
AVRDUDE = avrdude
##########------------------------------------------------------##########
########## Makefile Magic! ##########
########## Summary: ##########
########## We want a .hex file ##########
########## Compile source files into .elf ##########
########## Convert .elf file into .hex ##########
########## You shouldn't need to edit below. ##########
##########------------------------------------------------------##########
## The name of your project (without the .c)
# TARGET = blinkLED
## Or name it automatically after the enclosing directory
TARGET = $(lastword $(subst /, ,$(CURDIR)))
# Object files: will find all .c/.h files in current directory
# and in LIBDIR. If you have any other (sub-)directories with code,
# you can add them in to SOURCES below in the wildcard statement.
SOURCES=$(wildcard *.c $(LIBDIR)/*.c)
OBJECTS=$(SOURCES:.c=.o)
HEADERS=$(SOURCES:.c=.h)
## Compilation options, type man avr-gcc if you're curious.
CPPFLAGS = -DF_CPU=$(F_CPU) -DBAUD=$(BAUD) -I. -I$(LIBDIR)
CFLAGS = -Os -g -std=gnu99 -Wall
## Use short (8-bit) data types
CFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
## Splits up object files per function
CFLAGS += -ffunction-sections -fdata-sections
LDFLAGS = -Wl,-Map,$(TARGET).map
## Optional, but often ends up with smaller code
LDFLAGS += -Wl,--gc-sections
## Relax shrinks code even more, but makes disassembly messy
## LDFLAGS += -Wl,--relax
## LDFLAGS += -Wl,-u,vfprintf -lprintf_flt -lm ## for floating-point printf
## LDFLAGS += -Wl,-u,vfprintf -lprintf_min ## for smaller printf
TARGET_ARCH = -mmcu=$(MCU)
## Explicit pattern rules:
## To make .o files from .c files
%.o: %.c $(HEADERS) Makefile
$(CC) $(CFLAGS) $(CPPFLAGS) $(TARGET_ARCH) -c -o $@ $<;
$(TARGET).elf: $(OBJECTS)
$(CC) $(LDFLAGS) $(TARGET_ARCH) $^ $(LDLIBS) -o $@
%.hex: %.elf
$(OBJCOPY) -j .text -j .data -O ihex $< $@
%.eeprom: %.elf
$(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O ihex $< $@
%.lst: %.elf
$(OBJDUMP) -S $< > $@
## These targets don't have files named after them
.PHONY: all disassemble disasm eeprom size clean squeaky_clean flash fuses
all: $(TARGET).hex
debug:
@echo
@echo "Source files:" $(SOURCES)
@echo "MCU, F_CPU, BAUD:" $(MCU), $(F_CPU), $(BAUD)
@echo
# Optionally create listing file from .elf
# This creates approximate assembly-language equivalent of your code.
# Useful for debugging time-sensitive bits,
# or making sure the compiler does what you want.
disassemble: $(TARGET).lst
disasm: disassemble
# Optionally show how big the resulting program is
size: $(TARGET).elf
$(AVRSIZE) -C --mcu=$(MCU) $(TARGET).elf
clean:
rm -f $(TARGET).elf $(TARGET).hex $(TARGET).obj \
$(TARGET).o $(TARGET).d $(TARGET).eep $(TARGET).lst \
$(TARGET).lss $(TARGET).sym $(TARGET).map $(TARGET)~ \
$(TARGET).eeprom
squeaky_clean:
rm -f *.elf *.hex *.obj *.o *.d *.eep *.lst *.lss *.sym *.map *~ *.eeprom
##########------------------------------------------------------##########
########## Programmer-specific details ##########
########## Flashing code to AVR using avrdude ##########
##########------------------------------------------------------##########
flash: $(TARGET).hex
$(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -U flash:w:$<
## An alias
program: flash
flash_eeprom: $(TARGET).eeprom
$(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -U eeprom:w:$<
avrdude_terminal:
$(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -nt
## If you've got multiple programmers that you use,
## you can define them here so that it's easy to switch.
## To invoke, use something like `make flash_arduinoISP`
flash_usbtiny: PROGRAMMER_TYPE = usbtiny
flash_usbtiny: PROGRAMMER_ARGS = # USBTiny works with no further arguments
flash_usbtiny: flash
flash_usbasp: PROGRAMMER_TYPE = usbasp
flash_usbasp: PROGRAMMER_ARGS = # USBasp works with no further arguments
flash_usbasp: flash
flash_arduinoISP: PROGRAMMER_TYPE = avrisp
flash_arduinoISP: PROGRAMMER_ARGS = -b 19200 -P /dev/ttyACM0
## (for windows) flash_arduinoISP: PROGRAMMER_ARGS = -b 19200 -P com5
flash_arduinoISP: flash
flash_109: PROGRAMMER_TYPE = avr109
flash_109: PROGRAMMER_ARGS = -b 9600 -P /dev/ttyUSB0
flash_109: flash
##########------------------------------------------------------##########
########## Fuse settings and suitable defaults ##########
##########------------------------------------------------------##########
## Mega 48, 88, 168, 328 default values
LFUSE = 0x62
HFUSE = 0xdf
EFUSE = 0x00
## Generic
FUSE_STRING = -U lfuse:w:$(LFUSE):m -U hfuse:w:$(HFUSE):m -U efuse:w:$(EFUSE):m
fuses:
$(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) \
$(PROGRAMMER_ARGS) $(FUSE_STRING)
show_fuses:
$(AVRDUDE) -c $(PROGRAMMER_TYPE) -p $(MCU) $(PROGRAMMER_ARGS) -nv
## Called with no extra definitions, sets to defaults
set_default_fuses: FUSE_STRING = -U lfuse:w:$(LFUSE):m -U hfuse:w:$(HFUSE):m -U efuse:w:$(EFUSE):m
set_default_fuses: fuses
## Set the fuse byte for full-speed mode
## Note: can also be set in firmware for modern chips
set_fast_fuse: LFUSE = 0xE2
set_fast_fuse: FUSE_STRING = -U lfuse:w:$(LFUSE):m
set_fast_fuse: fuses
## Set the EESAVE fuse byte to preserve EEPROM across flashes
set_eeprom_save_fuse: HFUSE = 0xD7
set_eeprom_save_fuse: FUSE_STRING = -U hfuse:w:$(HFUSE):m
set_eeprom_save_fuse: fuses
## Clear the EESAVE fuse byte
clear_eeprom_save_fuse: FUSE_STRING = -U hfuse:w:$(HFUSE):m
clear_eeprom_save_fuse: fuses

View File

@@ -0,0 +1,56 @@
/* Direct-digital synthesis */
// ------- Preamble -------- //
#include <avr/io.h> /* Defines pins, ports, etc */
#include <util/delay.h> /* Functions to waste time */
#include <avr/power.h>
#include "pinDefines.h"
#include "fullSine.h"
static inline void initTimer0(void) {
TCCR0A |= (1 << COM0A1); /* PWM output on OCR0A */
SPEAKER_DDR |= (1 << SPEAKER); /* enable output on pin */
TCCR0A |= (1 << WGM00); /* Fast PWM mode */
TCCR0A |= (1 << WGM01); /* Fast PWM mode, pt.2 */
TCCR0B |= (1 << CS00); /* Clock with /1 prescaler */
}
int main(void) {
uint16_t accumulator = 0;
uint16_t accumulatorSteps = 880; /* approx 440 Hz */
uint8_t waveStep;
int8_t pwmValue;
// -------- Inits --------- //
clock_prescale_set(clock_div_1); /* CPU clock 8 MHz */
initTimer0();
BUTTON_PORT |= (1 << BUTTON); /* pullup on button */
// ------ Event loop ------ //
while (1) {
if (bit_is_clear(BUTTON_PIN, BUTTON)) {
SPEAKER_DDR |= (1 << SPEAKER); /* enable speaker */
accumulator += accumulatorSteps; /* advance accumulator */
waveStep = accumulator >> 8; /* which entry in lookup? */
pwmValue = fullSine[waveStep]; /* lookup voltage */
loop_until_bit_is_set(TIFR0, TOV0); /* wait for PWM cycle */
OCR0A = 128 + pwmValue; /* set new PWM value */
TIFR0 |= (1 << TOV0); /* reset PWM overflow bit */
}
else { /* button not pressed */
SPEAKER_DDR &= ~(1 << SPEAKER); /* disable speaker */
}
} /* End event loop */
return 0; /* This line is never reached */
}

View File

@@ -0,0 +1,34 @@
int8_t fullSaw15[256] = {
0, 1, 3, 4, 5, 6, 7, 7,
7, 7, 7, 7, 8, 9, 10, 12,
13, 15, 17, 18, 20, 21, 21, 22,
22, 22, 22, 22, 22, 23, 24, 25,
27, 29, 31, 33, 34, 35, 36, 36,
36, 36, 36, 36, 36, 37, 38, 39,
41, 43, 45, 47, 48, 50, 51, 51,
51, 51, 51, 51, 51, 51, 52, 53,
54, 56, 58, 61, 63, 64, 66, 66,
67, 66, 66, 65, 65, 65, 65, 66,
68, 70, 72, 75, 77, 79, 81, 82,
82, 82, 81, 80, 79, 78, 78, 78,
80, 82, 85, 88, 92, 95, 97, 99,
100, 99, 98, 95, 93, 90, 88, 88,
89, 91, 96, 101, 108, 115, 121, 125,
127, 125, 119, 108, 93, 74, 51, 26,
0, -27, -52, -75, -94, -109, -120, -126,
-128, -126, -122, -116, -109, -102, -97, -92,
-90, -89, -89, -91, -94, -96, -99, -100,
-101, -100, -98, -96, -93, -89, -86, -83,
-81, -79, -79, -79, -80, -81, -82, -83,
-83, -83, -82, -80, -78, -76, -73, -71,
-69, -67, -66, -66, -66, -66, -67, -67,
-68, -67, -67, -65, -64, -62, -59, -57,
-55, -54, -53, -52, -52, -52, -52, -52,
-52, -52, -52, -51, -49, -48, -46, -44,
-42, -40, -39, -38, -37, -37, -37, -37,
-37, -37, -37, -36, -35, -34, -32, -30,
-28, -26, -25, -24, -23, -23, -23, -23,
-23, -23, -22, -22, -21, -19, -18, -16,
-14, -13, -11, -10, -9, -8, -8, -8,
-8, -8, -8, -7, -6, -5, -4, -2
};

View File

@@ -0,0 +1,34 @@
int8_t fullSaw3[256] = {
0, 2, 4, 6, 8, 10, 12, 14,
16, 18, 20, 22, 23, 25, 27, 28,
29, 31, 32, 33, 34, 35, 35, 36,
37, 37, 38, 38, 38, 38, 39, 39,
39, 39, 39, 38, 38, 38, 38, 38,
38, 38, 38, 38, 38, 38, 38, 38,
39, 39, 40, 40, 41, 42, 43, 44,
45, 46, 47, 49, 51, 52, 54, 56,
58, 61, 63, 65, 68, 70, 73, 76,
79, 81, 84, 87, 90, 93, 96, 98,
101, 104, 106, 109, 111, 114, 116, 118,
120, 121, 123, 124, 125, 126, 126, 127,
127, 127, 126, 126, 125, 124, 122, 120,
118, 116, 113, 110, 107, 104, 100, 96,
92, 87, 83, 78, 72, 67, 62, 56,
50, 44, 38, 32, 25, 19, 12, 6,
0, -7, -13, -20, -26, -33, -39, -45,
-51, -57, -63, -68, -73, -79, -84, -88,
-93, -97, -101, -105, -108, -111, -114, -117,
-119, -121, -123, -125, -126, -127, -127, -128,
-128, -128, -127, -127, -126, -125, -124, -122,
-121, -119, -117, -115, -112, -110, -107, -105,
-102, -99, -97, -94, -91, -88, -85, -82,
-80, -77, -74, -71, -69, -66, -64, -62,
-59, -57, -55, -53, -52, -50, -48, -47,
-46, -45, -44, -43, -42, -41, -41, -40,
-40, -39, -39, -39, -39, -39, -39, -39,
-39, -39, -39, -39, -39, -39, -40, -40,
-40, -40, -40, -39, -39, -39, -39, -38,
-38, -37, -36, -36, -35, -34, -33, -32,
-30, -29, -28, -26, -24, -23, -21, -19,
-17, -15, -13, -11, -9, -7, -5, -3
};

View File

@@ -0,0 +1,34 @@
int8_t fullSaw7[256] = {
0, 1, 3, 5, 7, 8, 10, 11,
12, 13, 14, 15, 15, 15, 15, 16,
16, 16, 16, 16, 16, 16, 16, 17,
17, 18, 19, 20, 21, 23, 24, 26,
28, 30, 32, 34, 36, 38, 39, 41,
43, 44, 45, 46, 47, 47, 48, 48,
48, 48, 48, 48, 48, 47, 47, 47,
47, 47, 48, 49, 50, 51, 52, 54,
55, 57, 60, 62, 64, 66, 69, 71,
73, 75, 77, 79, 80, 81, 82, 82,
82, 82, 82, 81, 80, 80, 79, 78,
77, 76, 75, 75, 75, 76, 76, 78,
79, 81, 84, 87, 90, 94, 98, 102,
106, 110, 114, 117, 121, 123, 125, 127,
127, 127, 125, 122, 119, 114, 108, 101,
93, 84, 74, 63, 51, 39, 26, 13,
0, -14, -27, -40, -52, -64, -75, -85,
-94, -102, -109, -115, -120, -123, -126, -128,
-128, -128, -126, -124, -122, -118, -115, -111,
-107, -103, -99, -95, -91, -88, -85, -82,
-80, -79, -77, -77, -76, -76, -76, -77,
-78, -79, -80, -81, -81, -82, -83, -83,
-83, -83, -83, -82, -81, -80, -78, -76,
-74, -72, -70, -67, -65, -63, -61, -58,
-56, -55, -53, -52, -51, -50, -49, -48,
-48, -48, -48, -48, -49, -49, -49, -49,
-49, -49, -49, -48, -48, -47, -46, -45,
-44, -42, -40, -39, -37, -35, -33, -31,
-29, -27, -25, -24, -22, -21, -20, -19,
-18, -18, -17, -17, -17, -17, -17, -17,
-17, -17, -16, -16, -16, -16, -15, -14,
-13, -12, -11, -9, -8, -6, -4, -2
};

View File

@@ -0,0 +1,34 @@
int8_t fullSine[256] = {
0, 3, 6, 9, 12, 15, 18, 21,
24, 27, 30, 34, 37, 39, 42, 45,
48, 51, 54, 57, 60, 62, 65, 68,
70, 73, 75, 78, 80, 83, 85, 87,
90, 92, 94, 96, 98, 100, 102, 104,
106, 107, 109, 110, 112, 113, 115, 116,
117, 118, 120, 121, 122, 122, 123, 124,
125, 125, 126, 126, 126, 127, 127, 127,
127, 127, 127, 127, 126, 126, 126, 125,
125, 124, 123, 122, 122, 121, 120, 118,
117, 116, 115, 113, 112, 110, 109, 107,
106, 104, 102, 100, 98, 96, 94, 92,
90, 87, 85, 83, 80, 78, 75, 73,
70, 68, 65, 62, 60, 57, 54, 51,
48, 45, 42, 39, 37, 34, 30, 27,
24, 21, 18, 15, 12, 9, 6, 3,
0, -4, -7, -10, -13, -16, -19, -22,
-25, -28, -31, -35, -38, -40, -43, -46,
-49, -52, -55, -58, -61, -63, -66, -69,
-71, -74, -76, -79, -81, -84, -86, -88,
-91, -93, -95, -97, -99, -101, -103, -105,
-107, -108, -110, -111, -113, -114, -116, -117,
-118, -119, -121, -122, -123, -123, -124, -125,
-126, -126, -127, -127, -127, -128, -128, -128,
-128, -128, -128, -128, -127, -127, -127, -126,
-126, -125, -124, -123, -123, -122, -121, -119,
-118, -117, -116, -114, -113, -111, -110, -108,
-107, -105, -103, -101, -99, -97, -95, -93,
-91, -88, -86, -84, -81, -79, -76, -74,
-71, -69, -66, -63, -61, -58, -55, -52,
-49, -46, -43, -40, -38, -35, -31, -28,
-25, -22, -19, -16, -13, -10, -7, -4
};

View File

@@ -0,0 +1,34 @@
int8_t fullSquare15[256] = {
0, 1, 3, 4, 5, 6, 7, 7,
7, 7, 7, 7, 8, 9, 10, 12,
13, 15, 17, 18, 20, 21, 21, 22,
22, 22, 22, 22, 22, 23, 24, 25,
27, 29, 31, 33, 34, 35, 36, 36,
36, 36, 36, 36, 36, 37, 38, 39,
41, 43, 45, 47, 48, 50, 51, 51,
51, 51, 51, 51, 51, 51, 52, 53,
54, 56, 58, 61, 63, 64, 66, 66,
67, 66, 66, 65, 65, 65, 65, 66,
68, 70, 72, 75, 77, 79, 81, 82,
82, 82, 81, 80, 79, 78, 78, 78,
80, 82, 85, 88, 92, 95, 97, 99,
100, 99, 98, 95, 93, 90, 88, 88,
89, 91, 96, 101, 108, 115, 121, 125,
127, 125, 119, 108, 93, 74, 51, 26,
0, -27, -52, -75, -94, -109, -120, -126,
-128, -126, -122, -116, -109, -102, -97, -92,
-90, -89, -89, -91, -94, -96, -99, -100,
-101, -100, -98, -96, -93, -89, -86, -83,
-81, -79, -79, -79, -80, -81, -82, -83,
-83, -83, -82, -80, -78, -76, -73, -71,
-69, -67, -66, -66, -66, -66, -67, -67,
-68, -67, -67, -65, -64, -62, -59, -57,
-55, -54, -53, -52, -52, -52, -52, -52,
-52, -52, -52, -51, -49, -48, -46, -44,
-42, -40, -39, -38, -37, -37, -37, -37,
-37, -37, -37, -36, -35, -34, -32, -30,
-28, -26, -25, -24, -23, -23, -23, -23,
-23, -23, -22, -22, -21, -19, -18, -16,
-14, -13, -11, -10, -9, -8, -8, -8,
-8, -8, -8, -7, -6, -5, -4, -2
};

View File

@@ -0,0 +1,34 @@
int8_t fullSquare3[256] = {
0, 2, 4, 6, 8, 10, 12, 14,
16, 18, 20, 22, 23, 25, 27, 28,
29, 31, 32, 33, 34, 35, 35, 36,
37, 37, 38, 38, 38, 38, 39, 39,
39, 39, 39, 38, 38, 38, 38, 38,
38, 38, 38, 38, 38, 38, 38, 38,
39, 39, 40, 40, 41, 42, 43, 44,
45, 46, 47, 49, 51, 52, 54, 56,
58, 61, 63, 65, 68, 70, 73, 76,
79, 81, 84, 87, 90, 93, 96, 98,
101, 104, 106, 109, 111, 114, 116, 118,
120, 121, 123, 124, 125, 126, 126, 127,
127, 127, 126, 126, 125, 124, 122, 120,
118, 116, 113, 110, 107, 104, 100, 96,
92, 87, 83, 78, 72, 67, 62, 56,
50, 44, 38, 32, 25, 19, 12, 6,
0, -7, -13, -20, -26, -33, -39, -45,
-51, -57, -63, -68, -73, -79, -84, -88,
-93, -97, -101, -105, -108, -111, -114, -117,
-119, -121, -123, -125, -126, -127, -127, -128,
-128, -128, -127, -127, -126, -125, -124, -122,
-121, -119, -117, -115, -112, -110, -107, -105,
-102, -99, -97, -94, -91, -88, -85, -82,
-80, -77, -74, -71, -69, -66, -64, -62,
-59, -57, -55, -53, -52, -50, -48, -47,
-46, -45, -44, -43, -42, -41, -41, -40,
-40, -39, -39, -39, -39, -39, -39, -39,
-39, -39, -39, -39, -39, -39, -40, -40,
-40, -40, -40, -39, -39, -39, -39, -38,
-38, -37, -36, -36, -35, -34, -33, -32,
-30, -29, -28, -26, -24, -23, -21, -19,
-17, -15, -13, -11, -9, -7, -5, -3
};

View File

@@ -0,0 +1,34 @@
int8_t fullSquare7[256] = {
0, 1, 3, 5, 7, 8, 10, 11,
12, 13, 14, 15, 15, 15, 15, 16,
16, 16, 16, 16, 16, 16, 16, 17,
17, 18, 19, 20, 21, 23, 24, 26,
28, 30, 32, 34, 36, 38, 39, 41,
43, 44, 45, 46, 47, 47, 48, 48,
48, 48, 48, 48, 48, 47, 47, 47,
47, 47, 48, 49, 50, 51, 52, 54,
55, 57, 60, 62, 64, 66, 69, 71,
73, 75, 77, 79, 80, 81, 82, 82,
82, 82, 82, 81, 80, 80, 79, 78,
77, 76, 75, 75, 75, 76, 76, 78,
79, 81, 84, 87, 90, 94, 98, 102,
106, 110, 114, 117, 121, 123, 125, 127,
127, 127, 125, 122, 119, 114, 108, 101,
93, 84, 74, 63, 51, 39, 26, 13,
0, -14, -27, -40, -52, -64, -75, -85,
-94, -102, -109, -115, -120, -123, -126, -128,
-128, -128, -126, -124, -122, -118, -115, -111,
-107, -103, -99, -95, -91, -88, -85, -82,
-80, -79, -77, -77, -76, -76, -76, -77,
-78, -79, -80, -81, -81, -82, -83, -83,
-83, -83, -83, -82, -81, -80, -78, -76,
-74, -72, -70, -67, -65, -63, -61, -58,
-56, -55, -53, -52, -51, -50, -49, -48,
-48, -48, -48, -48, -49, -49, -49, -49,
-49, -49, -49, -48, -48, -47, -46, -45,
-44, -42, -40, -39, -37, -35, -33, -31,
-29, -27, -25, -24, -22, -21, -20, -19,
-18, -18, -17, -17, -17, -17, -17, -17,
-17, -17, -16, -16, -16, -16, -15, -14,
-13, -12, -11, -9, -8, -6, -4, -2
};

View File

@@ -0,0 +1,34 @@
int8_t fullTri15[256] = {
0, 1, 3, 4, 5, 6, 7, 7,
7, 7, 7, 7, 8, 9, 10, 12,
13, 15, 17, 18, 20, 21, 21, 22,
22, 22, 22, 22, 22, 23, 24, 25,
27, 29, 31, 33, 34, 35, 36, 36,
36, 36, 36, 36, 36, 37, 38, 39,
41, 43, 45, 47, 48, 50, 51, 51,
51, 51, 51, 51, 51, 51, 52, 53,
54, 56, 58, 61, 63, 64, 66, 66,
67, 66, 66, 65, 65, 65, 65, 66,
68, 70, 72, 75, 77, 79, 81, 82,
82, 82, 81, 80, 79, 78, 78, 78,
80, 82, 85, 88, 92, 95, 97, 99,
100, 99, 98, 95, 93, 90, 88, 88,
89, 91, 96, 101, 108, 115, 121, 125,
127, 125, 119, 108, 93, 74, 51, 26,
0, -27, -52, -75, -94, -109, -120, -126,
-128, -126, -122, -116, -109, -102, -97, -92,
-90, -89, -89, -91, -94, -96, -99, -100,
-101, -100, -98, -96, -93, -89, -86, -83,
-81, -79, -79, -79, -80, -81, -82, -83,
-83, -83, -82, -80, -78, -76, -73, -71,
-69, -67, -66, -66, -66, -66, -67, -67,
-68, -67, -67, -65, -64, -62, -59, -57,
-55, -54, -53, -52, -52, -52, -52, -52,
-52, -52, -52, -51, -49, -48, -46, -44,
-42, -40, -39, -38, -37, -37, -37, -37,
-37, -37, -37, -36, -35, -34, -32, -30,
-28, -26, -25, -24, -23, -23, -23, -23,
-23, -23, -22, -22, -21, -19, -18, -16,
-14, -13, -11, -10, -9, -8, -8, -8,
-8, -8, -8, -7, -6, -5, -4, -2
};

View File

@@ -0,0 +1,34 @@
int8_t fullTri3[256] = {
0, 2, 4, 6, 8, 10, 12, 14,
16, 18, 20, 22, 23, 25, 27, 28,
29, 31, 32, 33, 34, 35, 35, 36,
37, 37, 38, 38, 38, 38, 39, 39,
39, 39, 39, 38, 38, 38, 38, 38,
38, 38, 38, 38, 38, 38, 38, 38,
39, 39, 40, 40, 41, 42, 43, 44,
45, 46, 47, 49, 51, 52, 54, 56,
58, 61, 63, 65, 68, 70, 73, 76,
79, 81, 84, 87, 90, 93, 96, 98,
101, 104, 106, 109, 111, 114, 116, 118,
120, 121, 123, 124, 125, 126, 126, 127,
127, 127, 126, 126, 125, 124, 122, 120,
118, 116, 113, 110, 107, 104, 100, 96,
92, 87, 83, 78, 72, 67, 62, 56,
50, 44, 38, 32, 25, 19, 12, 6,
0, -7, -13, -20, -26, -33, -39, -45,
-51, -57, -63, -68, -73, -79, -84, -88,
-93, -97, -101, -105, -108, -111, -114, -117,
-119, -121, -123, -125, -126, -127, -127, -128,
-128, -128, -127, -127, -126, -125, -124, -122,
-121, -119, -117, -115, -112, -110, -107, -105,
-102, -99, -97, -94, -91, -88, -85, -82,
-80, -77, -74, -71, -69, -66, -64, -62,
-59, -57, -55, -53, -52, -50, -48, -47,
-46, -45, -44, -43, -42, -41, -41, -40,
-40, -39, -39, -39, -39, -39, -39, -39,
-39, -39, -39, -39, -39, -39, -40, -40,
-40, -40, -40, -39, -39, -39, -39, -38,
-38, -37, -36, -36, -35, -34, -33, -32,
-30, -29, -28, -26, -24, -23, -21, -19,
-17, -15, -13, -11, -9, -7, -5, -3
};

View File

@@ -0,0 +1,34 @@
int8_t fullTri7[256] = {
0, 1, 3, 5, 7, 8, 10, 11,
12, 13, 14, 15, 15, 15, 15, 16,
16, 16, 16, 16, 16, 16, 16, 17,
17, 18, 19, 20, 21, 23, 24, 26,
28, 30, 32, 34, 36, 38, 39, 41,
43, 44, 45, 46, 47, 47, 48, 48,
48, 48, 48, 48, 48, 47, 47, 47,
47, 47, 48, 49, 50, 51, 52, 54,
55, 57, 60, 62, 64, 66, 69, 71,
73, 75, 77, 79, 80, 81, 82, 82,
82, 82, 82, 81, 80, 80, 79, 78,
77, 76, 75, 75, 75, 76, 76, 78,
79, 81, 84, 87, 90, 94, 98, 102,
106, 110, 114, 117, 121, 123, 125, 127,
127, 127, 125, 122, 119, 114, 108, 101,
93, 84, 74, 63, 51, 39, 26, 13,
0, -14, -27, -40, -52, -64, -75, -85,
-94, -102, -109, -115, -120, -123, -126, -128,
-128, -128, -126, -124, -122, -118, -115, -111,
-107, -103, -99, -95, -91, -88, -85, -82,
-80, -79, -77, -77, -76, -76, -76, -77,
-78, -79, -80, -81, -81, -82, -83, -83,
-83, -83, -83, -82, -81, -80, -78, -76,
-74, -72, -70, -67, -65, -63, -61, -58,
-56, -55, -53, -52, -51, -50, -49, -48,
-48, -48, -48, -48, -49, -49, -49, -49,
-49, -49, -49, -48, -48, -47, -46, -45,
-44, -42, -40, -39, -37, -35, -33, -31,
-29, -27, -25, -24, -22, -21, -20, -19,
-18, -18, -17, -17, -17, -17, -17, -17,
-17, -17, -16, -16, -16, -16, -15, -14,
-13, -12, -11, -9, -8, -6, -4, -2
};

View File

@@ -0,0 +1,34 @@
int8_t fullTriangle[256] = {
0, 1, 3, 5, 7, 9, 11, 13,
15, 17, 19, 21, 23, 25, 27, 29,
31, 33, 35, 37, 39, 41, 43, 45,
47, 49, 51, 53, 55, 57, 59, 61,
63, 65, 67, 69, 71, 73, 75, 77,
79, 81, 83, 85, 87, 89, 91, 93,
95, 97, 99, 101, 103, 105, 107, 109,
111, 113, 115, 117, 119, 121, 123, 125,
127, 125, 123, 121, 119, 117, 115, 113,
111, 109, 107, 105, 103, 101, 99, 97,
95, 93, 91, 89, 87, 85, 83, 81,
79, 77, 75, 73, 71, 69, 67, 65,
63, 61, 59, 57, 55, 53, 51, 49,
47, 45, 43, 41, 39, 37, 35, 33,
31, 29, 27, 25, 23, 21, 19, 17,
15, 13, 11, 9, 7, 5, 3, 1,
0, -2, -4, -6, -8, -10, -12, -14,
-16, -18, -20, -22, -24, -26, -28, -30,
-32, -34, -36, -38, -40, -42, -44, -46,
-48, -50, -52, -54, -56, -58, -60, -62,
-64, -66, -68, -70, -72, -74, -76, -78,
-80, -82, -84, -86, -88, -90, -92, -94,
-96, -98, -100, -102, -104, -106, -108, -110,
-112, -114, -116, -118, -120, -122, -124, -126,
-128, -126, -124, -122, -120, -118, -116, -114,
-112, -110, -108, -106, -104, -102, -100, -98,
-96, -94, -92, -90, -88, -86, -84, -82,
-80, -78, -76, -74, -72, -70, -68, -66,
-64, -62, -60, -58, -56, -54, -52, -50,
-48, -46, -44, -42, -40, -38, -36, -34,
-32, -30, -28, -26, -24, -22, -20, -18,
-16, -14, -12, -10, -8, -6, -4, -2
};

View File

@@ -0,0 +1,101 @@
## This file generates headers with lookup tables for various waveforms
## Add your own.
import math
def phaseSteps(maxPhase, length=256):
steps = range(0, length)
steps = [1.0*x/length * 2.0*math.pi * (maxPhase/360.0) for x in steps]
return(steps)
def scaleAndRound(data, scale=255, signedInt=True):
data = [0.0+x-min(data) for x in data]
data = [1.0*x/max(data)*scale for x in data]
data = [int(round(x)) for x in data]
if signedInt:
data = [x-(scale+1)/2 for x in data]
return(data)
def makeSin(maxPhase, length=256):
sinus = [math.sin(x) for x in phaseSteps(maxPhase, length)]
return(sinus)
def prettyPrint(data, perLine = 8):
outString = ""
for i in range(len(data) / perLine):
strings = [str(x) for x in data[perLine*i:(perLine*i+perLine)]]
outString += "\t" + ", ".join(strings) + ",\n"
outString = outString[:-2] + "\n" # drop the final comma
return(outString)
def writeHeader(fileName, dataName, data, signedInt=True):
outfile = open(fileName, "w")
if signedInt:
outfile.write("int8_t {}[{:d}] = {{ \n".format(dataName, len(data)))
else:
outfile.write("uint8_t {}[{:d}] = {{ \n".format(dataName, len(data)))
outfile.write(prettyPrint(data))
outfile.write("};\n")
outfile.close()
def bandlimitedSawtooth(maxPhase, numberPartials, length=256):
wave = [0]*length
sign = 1.0
for k in range(1, numberPartials+1):
phases = phaseSteps(maxPhase*k, length)
for i in range(length):
wave[i] += sign * math.sin(phases[i]) / k
sign = sign * -1
return(wave)
def bandlimitedSquare(maxPhase, numberPartials, length=256):
wave = [0]*length
for k in range(1, numberPartials*2, 2):
phases = phaseSteps(maxPhase*k, length)
for i in range(length):
wave[i] += math.sin(phases[i]) / k
return(wave)
def bandlimitedTriangle(maxPhase, numberPartials, length=256):
wave = [0]*length
sign = 1.0
for k in range(1, numberPartials*2, 2):
phases = phaseSteps(maxPhase*k, length)
for i in range(length):
wave[i] += sign * math.sin(phases[i]) / k**2
sign = sign * -1
return(wave)
if __name__ == "__main__":
## Full-waves, full 256 bytes, 0-255 range
writeHeader("fullSine.h", 'fullSine', scaleAndRound(makeSin(360)))
triangleWave = range(0,64)
triangleWave.extend(range(64, -64, -1))
triangleWave.extend(range(-64, 0, 1))
triangleWave = scaleAndRound(triangleWave)
writeHeader("fullTriangle.h", 'fullTriangle', triangleWave)
for numberFrequencies in [3,7,15]:
saw = scaleAndRound(bandlimitedSawtooth(360, numberFrequencies))
writeHeader("fullSaw{}.h".format(numberFrequencies),
'fullSaw{}'.format(numberFrequencies), saw)
tri = scaleAndRound(bandlimitedTriangle(360, numberFrequencies))
writeHeader("fullTri{}.h".format(numberFrequencies),
'fullTri{}'.format(numberFrequencies), tri)
square = scaleAndRound(bandlimitedSquare(360, numberFrequencies))
writeHeader("fullSquare{}.h".format(numberFrequencies),
'fullSquare{}'.format(numberFrequencies), square)
## Note that if you define / use too many different waveforms,
## and you don't store them in PROGMEM in your AVR C routines,
## you might run the chip out of RAM, which causes strange and
## nearly impossible-to-diagnose glitches.
## So here we're breaking each waveform up into its own include file.
## There are ways of storing them all in program memory, and we'll
## see examples of that in later chapters.