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,86 @@
/*
Direct-digital synthesis
ADSR Dynamic Volume Envelope Demo
*/
// ------- Preamble -------- //
#include "adsr.h" /* Defines, includes, and init functions */
int main(void) {
// -------- Inits --------- //
uint16_t accumulator = 0;
uint8_t volume = 0;
uint16_t noteClock = 0;
uint16_t tuningWord = C1;
uint8_t waveStep;
int16_t mixer=0;
char serialInput;
clock_prescale_set(clock_div_1); /* CPU clock 8 MHz */
initTimer0();
initUSART();
printString(" Serial Synth\r\n");
printString("Notes: asdfghjkl;'\r\n");
SPEAKER_DDR |= (1 << SPEAKER); /* speaker output */
// ------ Event loop ------ //
while (1) {
// Set PWM output
loop_until_bit_is_set(TIFR0, TOV0); /* wait for timer0 overflow */
OCR0A = 128 + (uint8_t) mixer;
TIFR0 |= (1 << TOV0); /* reset the overflow bit */
// Update the DDS
accumulator += tuningWord;
waveStep = accumulator >> 8;
mixer = fullTriangle[waveStep] * volume;
mixer = mixer >> 5;
/* Input processed here: check USART */
if (bit_is_set(UCSR0A, RXC0)) {
serialInput = UDR0; /* read in from USART */
tuningWord = lookupPitch(serialInput);
noteClock = 1;
}
/* Dynamic Volume stuff here */
if (noteClock) { /* if noteClock already running */
noteClock++;
if (noteClock < ATTACK_TIME) { /* attack */
/* wait until time to increase next step */
if (noteClock > ATTACK_RATE * volume) {
if (volume < 31) {
volume++;
}
}
}
else if (noteClock < DECAY_TIME) { /* decay */
if ((noteClock - ATTACK_TIME) >
(FULL_VOLUME - volume) * DECAY_RATE) {
if (volume > SUSTAIN_LEVEL) {
volume--;
}
}
}
else if (noteClock > RELEASE_TIME) { /* release */
if ((noteClock - RELEASE_TIME) >
(SUSTAIN_LEVEL - volume) * RELEASE_RATE) {
if (volume > 0) {
volume--;
}
else {
noteClock = 0;
}
}
}
}
} /* End event loop */
return 0; /* This line is never reached */
}

View File

@@ -0,0 +1,81 @@
/*Include file for ADSR synth project. */
#include <avr/io.h> /* Defines pins, ports, etc */
#include <util/delay.h> /* Functions to waste time */
#include <avr/power.h>
#include <avr/interrupt.h>
#include "pinDefines.h"
#include "macros.h"
#include "scale.h"
#include "fullSaw7.h" /* a 7-harmonic bandlimited sawtooth */
/* see generateWavetables.py if you're interested */
#include "fullTriangle.h"
#include "USART.h"
#define FULL_VOLUME 31 /* 5-bit volumes */
// Default envelope values (slightly percussive)
// Play around with these!
#define ATTACK_RATE 8 /* 0-255 */
#define DECAY_RATE 120 /* 0-255 */
#define SUSTAIN_LEVEL 25 /* 0-255 */
#define SUSTAIN_TIME 4000 /* 0-65535 */
#define RELEASE_RATE 200 /* 0-65535 */
// Compute these constants
#define ATTACK_TIME ATTACK_RATE * FULL_VOLUME
#define DECAY_TIME (ATTACK_TIME + (FULL_VOLUME-SUSTAIN_LEVEL) * DECAY_RATE)
#define RELEASE_TIME DECAY_TIME + SUSTAIN_TIME
// -------------- Init Routines --------------- //
static inline void initTimer0(void){
set_bit(TCCR0A, COM0A1); /* PWM output on OCR0A */
set_bit(SPEAKER_DDR, SPEAKER); /* enable output on pin */
set_bit(TCCR0A, WGM00); /* Fast PWM mode */
set_bit(TCCR0A, WGM01); /* Fast PWM mode, pt.2 */
set_bit(TCCR0B, CS00); /* Clock with /1 prescaler */
}
static inline void initLEDs(void){
uint8_t i;
LED_DDR = 0xff; /* All LEDs for diagnostics */
for (i=0; i<8; i++){
set_bit(LED_PORT, i);
_delay_ms(100);
clear_bit(LED_PORT, i);
}
}
static inline uint16_t lookupPitch(char i){
switch(i){
case 'a':
return(G1);
case 's':
return(A1);
case 'd':
return(B1);
case 'f':
return(C2);
case 'g':
return(D2);
case 'h':
return(E2);
case 'j':
return(F2);
case 'k':
return(G2);
case 'l':
return(A2);
case ';':
return(B2);
case '\'':
return(C3);
}
// Default value -- if press some other key
return(C1);
}

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 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,44 @@
## File to generate equal-tempered scale for use with DDS
import math # because music is math
scale = ['C', 'Cx', 'D', 'Dx', 'E', 'F', 'Fx', 'G', 'Gx', 'A', 'Ax', 'B']
def octave(baseLength):
pitches = [baseLength * math.exp(x*math.log(2)/12) for x in range(0, 12)]
pitches = [int(round(x)) for x in pitches]
return( zip(scale, pitches) )
def writeScaleHeader(basePitch, octaves, filename="scale.h"):
outfile = open(filename, "w")
outfile.write('''/*
Scales for use with DDS synthesis.
Aimed roughly at having A2 be at 440Hz,
when the chip is clocked at 8MHz and
using 8-bit resolution on the PWM.
Tune it by tweaking basePitch.
*/
''')
for i in range(0, octaves):
for note, pitch in octave(basePitch * 2**i):
if pitch < 30000:
noteString = note + str(i)
print("#define {:<5}{:>6}").format(noteString, pitch)
outfile.write("#define {:<5}{:>6}\n".format(noteString, pitch))
outfile.close()
######################################################################
if __name__ == "__main__":
writeScaleHeader(basePitch=130, octaves=5)

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.

View File

@@ -0,0 +1,72 @@
/*
Scales for use with DDS synthesis.
Aimed roughly at having A2 be at 440Hz,
when the chip is clocked at 8MHz and
using 8-bit resolution on the PWM.
Tune it if you'd like.
*/
#define C0 130
#define Cx0 138
#define D0 146
#define Dx0 155
#define E0 164
#define F0 174
#define Fx0 184
#define G0 195
#define Gx0 206
#define A0 219
#define Ax0 232
#define B0 245
#define C1 260
#define Cx1 275
#define D1 292
#define Dx1 309
#define E1 328
#define F1 347
#define Fx1 368
#define G1 390
#define Gx1 413
#define A1 437
#define Ax1 463
#define B1 491
#define C2 520
#define Cx2 551
#define D2 584
#define Dx2 618
#define E2 655
#define F2 694
#define Fx2 735
#define G2 779
#define Gx2 825
#define A2 875
#define Ax2 927
#define B2 982
#define C3 1040
#define Cx3 1102
#define D3 1167
#define Dx3 1237
#define E3 1310
#define F3 1388
#define Fx3 1471
#define G3 1558
#define Gx3 1651
#define A3 1749
#define Ax3 1853
#define B3 1963
#define C4 2080
#define Cx4 2204
#define D4 2335
#define Dx4 2474
#define E4 2621
#define F4 2776
#define Fx4 2942
#define G4 3116
#define Gx4 3302
#define A4 3498
#define Ax4 3706
#define B4 3927

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,78 @@
/*
* "Algorithmic Symphonies"
* When you combine modula arithmetic with a counter variable,
* you can get sequences that sound like 4-bit chiptunes.
* Note that we're just outputting numerical values
* directly as a voltage.
* Play around with these formulas defined here, or see others
* in the attached file "music_formula_collection.txt".
* */
// ------- 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 "macros.h"
static inline void initTimer0(void){
set_bit(TCCR0A, COM0A1); /* PWM output on OCR0A */
set_bit(SPEAKER_DDR, SPEAKER); /* enable output on pin */
set_bit(TCCR0A, WGM00); /* Fast PWM mode */
set_bit(TCCR0A, WGM01); /* Fast PWM mode, pt.2 */
set_bit(TCCR0B, CS00); /* Clock with /1 prescaler */
}
static inline void pollButton(void){
if (bit_is_clear(BUTTON_PIN, BUTTON)) {
set_bit(SPEAKER_DDR, SPEAKER); /* enable output on pin */
clear_bit(LED_PORT, LED0);
}
else {
clear_bit(SPEAKER_DDR, SPEAKER); /* disable output on pin */
set_bit(LED_PORT, LED0);
}
}
int main(void){
uint16_t t=0;
// -------- Inits --------- //
clock_prescale_set(clock_div_1); /* CPU clock 8 MHz */
initTimer0();
set_bit(BUTTON_PORT, BUTTON); /* pullup on button */
set_bit(LED_DDR, LED0); /* LED on for diagnostics */
set_bit(LED_PORT, LED0);
_delay_ms(100);
clear_bit(LED_PORT, LED0);
set_bit(SPEAKER_DDR, SPEAKER);
OCR0A = 210;
// ------ Event loop ------ //
while(1){
t++;
OCR0A = (uint8_t) ((t<<1)^((t<<1)+(t>>7)&t>>12))|t>>(4-(1^7&(t>>19)))|t>>7; /* "crowd" */
// OCR0A = (uint8_t) t; /* sawtooth */
// OCR0A = (uint8_t) t * (t >> 8); /* rising pitch + aliases all over */
// OCR0A = (uint8_t) t & (t >> 8); /* arpeggios */
// OCR0A = (uint8_t) t*( 42 & t >> 10 );
// OCR0A = (uint8_t) t*(t>>((t>>9)|(t>>8))&(63&(t>>4))); /* space invaders vs pong */
// OCR0A = (uint8_t) t*(t>>8*((t>>15)|(t>>8))&(20|(t>>19)*5>>t|(t>>3)));
// OCR0A = (uint8_t) (t*5&t>>7)|(t*3&t>>10);
// OCR0A = (uint8_t) (t*9 & t>>4 | t* 5 & t>> 7 | t * 3 & t/1024)-1;
// OCR0A = (uint8_t) (t>>5)|(t>>4)|((t%42)*(t>>4)|(0x15483113)-(t>>4))/(t>>16)^(t|(t>>4));
// OCR0A = (uint8_t) t*(( (t>>12) | (t>>8)) & (63 & (t>>4)) ); /* glitchy */
pollButton();
_delay_us(124); /* Aiming for 8kHz sample rate */
} /* End event loop */
return 0; /* This line is never reached */
}

View File

@@ -0,0 +1,320 @@
Collection of oneliner music formulas. Version 2011-10-18
I've tried to collect all the formulas in the related threads etc.
(excluding those that clearly sound like random first experiments or total
crap; when several variants are available, i've chosen the shortest one)
If you think I've missed something that should be here, please let me know.
====== 1ST ITERATION ======
// viznut 2011-09-18 http://www.youtube.com/watch?v=GtQdIYUtAHg
t*(((t>>12)|(t>>8))&(63&(t>>4)))
// tejeez 2011-09-18 http://www.youtube.com/watch?v=GtQdIYUtAHg
(t*(t>>5|t>>8))>>(t>>16)
// visy 2011-09-18 http://www.youtube.com/watch?v=GtQdIYUtAHg
t*(((t>>9)|(t>>13))&(25&(t>>6)))
// tejeez 2011-09-18 http://www.youtube.com/watch?v=GtQdIYUtAHg
t*(((t>>11)&(t>>8))&(123&(t>>3)))
// visy 2011-09-18 http://www.youtube.com/watch?v=GtQdIYUtAHg
t*(t>>8*((t>>15)|(t>>8))&(20|(t>>19)*5>>t|(t>>3)))
// tejeez 2011-09-18 http://www.youtube.com/watch?v=GtQdIYUtAHg
(-t&4095)*(255&t*(t&(t>>13)))>>12)+(127&t*(234&t>>8&t>>3)>>(3&t>>14))
// visy 2011-09-18 http://www.youtube.com/watch?v=GtQdIYUtAHg "Space Invaders vs Pong"
t*(t>>((t>>9)|(t>>8))&(63&(t>>4)))
====== 2ND ITERATION ======
// viznut 2011-09-30 http://www.youtube.com/watch?v=qlrs2Vorw2Y
(t>>6|t|t>>(t>>16))*10+((t>>11)&7)
// pyryp 2011-09-30 http://www.youtube.com/watch?v=qlrs2Vorw2Y
v=(v>>1)+(v>>4)+t*(((t>>16)|(t>>6))&(69&(t>>9)))
// red- 2011-09-30 http://www.youtube.com/watch?v=qlrs2Vorw2Y
(t|(t>>9|t>>7))*t&(t>>11|t>>9)
// miiro 2011-09-30 http://www.youtube.com/watch?v=qlrs2Vorw2Y
t*5&(t>>7)|t*3&(t*4>>10)
// viznut 2011-09-30 http://www.youtube.com/watch?v=qlrs2Vorw2Y (xpansive+varjohukka)
(t>>7|t|t>>6)*10+4*(t&t>>13|t>>6)
// skurk+raer 2011-09-30 http://www.youtube.com/watch?v=qlrs2Vorw2Y
((t&4096)?((t*(t^t%255)|(t>>4))>>1):(t>>3)|((t&8192)?t<<2:t))
// xpansive 2011-09-29 http://pouet.net/topic.php?which=8357&page=2 "Lost in Space"
((t*(t>>8|t>>9)&46&t>>8))^(t&t>>13|t>>6)
====== 3RD ITERATION ======
// viznut 2011-10-10 http://www.youtube.com/watch?v=tCRPUv8V22o
(t*5&t>>7)|(t*3&t>>10)
// bst 2011-10-10 http://www.youtube.com/watch?v=tCRPUv8V22o
(int)(t/1e7*t*t+t)%127|t>>4|t>>5|t%127+(t>>16)|t
// kb 2011-10-04 http://pouet.net/topic.php?which=8357&page=8 44kHz
((t/2*(15&(0x234568a0>>(t>>8&28))))|t/2>>(t>>11)^t>>12)+(t/16&t&24)
// viznut 2011-10-10 http://www.youtube.com/watch?v=tCRPUv8V22o
(t&t%255)-(t*3&t>>13&t>>6)
// droid 2011-10-10 http://www.youtube.com/watch?v=tCRPUv8V22o
t>>4|t&((t>>5)/(t>>7-(t>>15)&-t>>7-(t>>15)))
// ryg 2011-10-10 http://www.youtube.com/watch?v=tCRPUv8V22o 44.1 kHz
((t*("36364689"[t>>13&7]&15))/12&128)+(((((t>>12)^(t>>12)-2)%11*t)/4|t>>13)&127)
// stephth 2011-10-03 http://news.ycombinator.com/item?id=3063359
(t*9&t>>4|t*5&t>>7|t*3&t/1024)-1
// viznut+oasiz 2011-10-10 http://www.youtube.com/watch?v=tCRPUv8V22o "Dante's Inferno" short version
((t*(t>>12)&(201*t/100)&(199*t/100))&(t*(t>>14)&(t*301/100)&(t*399/100)))+((t*(t>>16)&(t*202/100)&(t*198/100))-(t*(t>>17)&(t*302/100)&(t*298/100)))
// viznut+oasiz 2011-10-10 http://www.youtube.com/watch?v=tCRPUv8V22o "Dante's Inferno" long version
((t*(t>>12)&(201*t/100)&(199*t/100))&(t*(t>>14)&(t*301/100)&(t*399/100)))+((t*(t>>16)&(t*202/100)&(t*198/100))-(t*(t>>18)&(t*302/100)&(t*298/100)))
// mu6k 2011-10-10 http://www.youtube.com/watch?v=tCRPUv8V22o "Long-line Theory", Chaos Theory cover, optimized by ryg, p01 et al., JS-only
w=t>>9,k=32,m=2048,a=1-t/m%1,d=(14*t*t^t)%m*a,y=[3,3,4.7,2][p=w/k&3]*t/4,h="IQNNNN!!]]!Q!IW]WQNN??!!W]WQNNN?".charCodeAt(w/2&15|p/3<<4)/33*t-t,s=y*.98%80+y%80+(w>>7&&a*((5*t%m*a&128)*(0x53232323>>w/4&1)+(d&127)*(0xa444c444>>w/4&1)*1.5+(d*w&1)+(h%k+h*1.99%k+h*.49%k+h*.97%k-64)*(4-a-a))),s*s>>14?127:s
// 216 2011-10-10 http://www.youtube.com/watch?v=tCRPUv8V22o
t*(t^t+(t>>15|1)^(t-1280^t)>>10)
// mu6k http://www.youtube.com/watch?v=tCRPUv8V22o 32.0 kHz
(3e3/(y=t&16383)&1)*35 +(x=t*"6689"[t>>16&3]/24&127)*y/4e4 +((t>>8^t>>10|t>>14|x)&63)
====== 0XA VIDEOS ======
// harism 2011-10-09 http://0xa.kuri.mu/2011/10/09/bitop-videos/
((t>>1%128)+20)*3*t>>14*t>>18
// tangent128 2011-10-09 http://0xa.kuri.mu/2011/10/09/bitop-videos/
t*(((t>>9)&10)|((t>>11)&24)^((t>>10)&15&(t>>15)))
// ultrageranium 2011-10-12 http://0xa.kuri.mu/2011/10/09/bitop-videos/
(t*t/256)&(t>>((t/1024)%16))^t%64*(0xC0D3DE4D69>>(t>>9&30)&t%32)*t>>18
====== ALL COLLECTED FORMULAS (length order) ======
// trivial minimum: plain sawtooth
t
// minimal sierpinski harmony
t&t>>8
// "the 42 melody", separately discovered by several people on irc etc
t*(42&t>>10)
// danharaj 2011-10-03 http://www.reddit.com/r/programming/comments/kyj77/algorithmic_symphonies_from_one_line_of_code_how/ "fractal trees", 216's version
t|t%255|t%257
// droid 2011-10-05 http://pouet.net/topic.php?which=8357&page=10
t>>6&1?t>>5:-t>>4
// Niklas_Roy 2011-10-14 http://countercomplex.blogspot.com/2011/10/algorithmic-symphonies-from-one-line-of.html
t*(t>>9|t>>13)&16
// krcko 2011-10-04 http://rafforum.rs/index.php/topic,123.0.html
(t&t>>12)*(t>>4|t>>8)
// viznut 2011-10-10 http://www.youtube.com/watch?v=tCRPUv8V22o
(t*5&t>>7)|(t*3&t>>10)
// tejeez 2011-09-18 http://www.youtube.com/watch?v=GtQdIYUtAHg
(t*(t>>5|t>>8))>>(t>>16)
// miiro 2011-09-30 http://www.youtube.com/watch?v=qlrs2Vorw2Y
t*5&(t>>7)|t*3&(t*4>>10)
// robert 2011-10-11 http://countercomplex.blogspot.com/2011/10/algorithmic-symphonies-from-one-line-of.html
(t>>13|t%24)&(t>>7|t%19)
// Niklas_Roy 2011-10-14 http://countercomplex.blogspot.com/2011/10/algorithmic-symphonies-from-one-line-of.html
(t*((t>>9|t>>13)&15))&129
// viznut 2011-10-10 http://www.youtube.com/watch?v=tCRPUv8V22o
(t&t%255)-(t*3&t>>13&t>>6)
// krcko 2011-10-04 http://rafforum.rs/index.php/topic,123.0.html
(t&t>>12)*(t>>4|t>>8)^t>>6
// blueberry 2011-10-05 http://pouet.net/topic.php?which=8357&page=12 11kHz
t*(((t>>9)^((t>>9)-1)^1)%13)
// rrola 2011-10-04 http://pouet.net/topic.php?which=8357&page=9 optimized by ryg
t*(0xCA98>>(t>>9&14)&15)|t>>8
// tonic 2011-10-01 http://pouet.net/topic.php?which=8357&page=5 "mr. arpeggiator playing a solo"
(t/8)>>(t>>9)*t/((t>>14&3)+4)
// FreeFull 2011-10-12 http://countercomplex.blogspot.com/2011/10/algorithmic-symphonies-from-one-line-of.html
(~t/100|(t*3))^(t*3&(t>>5))&t
// red- 2011-09-30 http://www.youtube.com/watch?v=qlrs2Vorw2Y
(t|(t>>9|t>>7))*t&(t>>11|t>>9)
// harism 2011-10-09 http://0xa.kuri.mu/2011/10/09/bitop-videos/
((t>>1%128)+20)*3*t>>14*t>>18
// droid 2011-10-04 http://pouet.net/topic.php?which=8357&page=9
t&(sin(t&t&3)*t>>5)/(t>>3&t>>6)
// viznut 2011-09-18 http://www.youtube.com/watch?v=GtQdIYUtAHg
t*(((t>>12)|(t>>8))&(63&(t>>4)))
// visy 2011-09-18 http://www.youtube.com/watch?v=GtQdIYUtAHg
t*(((t>>9)|(t>>13))&(25&(t>>6)))
// 216 2011-10-10 http://www.youtube.com/watch?v=tCRPUv8V22o
t*(t^t+(t>>15|1)^(t-1280^t)>>10)
// tejeez 2011-09-18 http://www.youtube.com/watch?v=GtQdIYUtAHg
t*(((t>>11)&(t>>8))&(123&(t>>3)))
// viznut 2011-09-30 http://www.youtube.com/watch?v=qlrs2Vorw2Y (xpansive+varjohukka)
(t>>7|t|t>>6)*10+4*(t&t>>13|t>>6)
// stephth 2011-10-03 http://news.ycombinator.com/item?id=3063359
(t*9&t>>4|t*5&t>>7|t*3&t/1024)-1
// visy 2011-09-18 http://www.youtube.com/watch?v=GtQdIYUtAHg "Space Invaders vs Pong"
t*(t>>((t>>9)|(t>>8))&(63&(t>>4)))
// viznut 2011-09-30 http://www.youtube.com/watch?v=qlrs2Vorw2Y
(t>>6|t|t>>(t>>16))*10+((t>>11)&7)
// yumeji 2011-10-04 http://pouet.net/topic.php?which=8357&page=9
(t>>1)*(0xbad2dea1>>(t>>13)&3)|t>>5
// ryg 2011-10-04 http://pouet.net/topic.php?which=8357&page=8
(t>>4)*(13&(0x8898a989>>(t>>11&30)))
// marmakoide 2011-10-04 http://pouet.net/topic.php?which=8357&page=8
(t>>(t&7))|(t<<(t&42))|(t>>7)|(t<<5)
// robert 2011-10-11 http://countercomplex.blogspot.com/2011/10/algorithmic-symphonies-from-one-line-of.html
(t>>7|t%45)&(t>>8|t%35)&(t>>11|t%20)
// lucasvb 2011-10-03 http://www.reddit.com/r/programming/comments/kyj77/algorithmic_symphonies_from_one_line_of_code_how/
(t>>6|t<<1)+(t>>5|t<<3|t>>3)|t>>2|t<<1
// bear @ celephais
t+(t&t^t>>6)-t*((t>>9)&(t%16?2:6)&t>>9)
// xpansive 2011-09-29 http://pouet.net/topic.php?which=8357&page=2 "Lost in Space"
((t*(t>>8|t>>9)&46&t>>8))^(t&t>>13|t>>6)
// rez 2011-10-05 http://pouet.net/topic.php?which=8357&page=11 js-only optimized by ryg
t*(1+"4451"[t>>13&3]/10)&t>>9+(t*0.003&3)
// marmakoide 2011-10-03 http://pouet.net/topic.php?which=8357&page=7 "Lemmings March"
(t>>5)|(t<<4)|((t&1023)^1981)|((t-67)>>4)
// droid 2011-10-04 http://pouet.net/topic.php?which=8357&page=9
t>>4|t&(t>>5)/(t>>7-(t>>15)&-t>>7-(t>>15))
// rez 2011-10-03 http://pouet.net/topic.php?which=8357&page=7
t*(t/256)-t*(t/255)+t*(t>>5|t>>6|t<<2&t>>1)
// viznut 2011-10-06 #countercomplex "moon scanner generalization", based on jaffa's formula
((t>>5&t)-(t>>5)+(t>>5&t))+(t*((t>>14)&14))
// viznut 2011-10-04 http://pouet.net/topic.php?which=8357&page=9
(t*((3+(1^t>>10&5))*(5+(3&t>>14))))>>(t>>8&3)
// droid 2011-10-10 http://www.youtube.com/watch?v=tCRPUv8V22o
t>>4|t&DIV((t>>5),(t>>7-(t>>15)&-t>>7-(t>>15)))
// pyryp 2011-09-30 http://www.youtube.com/watch?v=qlrs2Vorw2Y
v=(v>>1)+(v>>4)+t*(((t>>16)|(t>>6))&(69&(t>>9)))
// bst 2011-10-10 http://www.youtube.com/watch?v=tCRPUv8V22o
(int)(t/1e7*t*t+t)%127|t>>4|t>>5|t%127+(t>>16)|t
// tangent128 2011-10-09 http://0xa.kuri.mu/2011/10/09/bitop-videos/
t*(((t>>9)&10)|((t>>11)&24)^((t>>10)&15&(t>>15)))
// tejeez 2011-10-05 #countercomplex
(~t>>2)*((127&t*(7&t>>10))<(245&t*(2+(5&t>>14))))
// lokori 2011-10-04 #suomiscene
(t+(t>>2)|(t>>5))+(t>>3)|((t>>13)|(t>>7)|(t>>11))
// visy 2011-09-18 http://www.youtube.com/watch?v=GtQdIYUtAHg
t*(t>>8*((t>>15)|(t>>8))&(20|(t>>19)*5>>t|(t>>3)))
// Aaron_Krister_Johnson 2011-10-14 http://countercomplex.blogspot.com/2011/10/algorithmic-symphonies-from-one-line-of.html
(t>>4)|(t%10)|(((t%101)|(t>>14))&((t>>7)|(t*t%17)))
// jounim 2011-10-04 #suomiscene
((t&((t>>5)))+(t|((t>>7))))&(t>>6)|(t>>5)&(t*(t>>7))
// spikey 2011-10-04 #suomiscene based on jounim's formula
((t&((t>>23)))+(t|(t>>2)))&(t>>3)|(t>>5)&(t*(t>>7))
// akx 2011-10-05 http://twitter.com/#!/akx
(((((t*((t>>9|t>>13)&15))&255/15)*9)%(1<<7))<<2)%6<<4
// bst 2011-10-05 http://pouet.net/topic.php?which=8357&page=10
((t%42)*(t>>4)|(0x15483113)-(t>>4))/(t>>16)^(t|(t>>4))
// skurk 2011-10-04 http://pouet.net/topic.php?which=8357&page=8
t*(t>>((t&4096)?((t*t)/4096):(t/4096)))|(t<<(t/256))|(t>>4)
// skurk+raer 2011-09-30 http://www.youtube.com/watch?v=qlrs2Vorw2Y
((t&4096)?((t*(t^t%255)|(t>>4))>>1):(t>>3)|((t&8192)?t<<2:t))
// yumeji 2011-10-06 http://pouet.net/topic.php?which=8357&page=12 "badbeats & safe"
t*((0xbadbea75>>((t>>12)&30)&3)*0.25*(0x5afe5>>((t>>16)&28)&3))
// bst 2011-10-11 http://pouet.net/topic.php?which=8357&page=18
t>>16|((t>>4)%16)|((t>>4)%192)|(t*t%64)|(t*t%96)|(t>>16)*(t|t>>5)
// bear @ celephais
t>>6^t&37|t+(t^t>>11)-t*((t%24?2:6)&t>>11)^t<<1&(t&598?t>>4:t>>10)
// kb 2011-10-04 http://pouet.net/topic.php?which=8357&page=8 44kHz
((t/2*(15&(0x234568a0>>(t>>8&28))))|t/2>>(t>>11)^t>>12)+(t/16&t&24)
// bst 2011-10-05 http://pouet.net/topic.php?which=8357&page=12
(t>>5)|(t>>4)|((t%42)*(t>>4)|(0x15483113)-(t>>4))/(t>>16)^(t|(t>>4))
// tejeez 2011-09-18 http://www.youtube.com/watch?v=GtQdIYUtAHg
(-t&4095)*(255&t*(t&(t>>13)))>>12)+(127&t*(234&t>>8&t>>3)>>(3&t>>14))
// ultrageranium 2011-10-12 http://0xa.kuri.mu/2011/10/09/bitop-videos/
(t*t/256)&(t>>((t/1024)%16))^t%64*(0xC0D3DE4D69>>(t>>9&30)&t%32)*t>>18
// visy 2011-10-06 http://pouet.net/topic.php?which=8357&page=13
(t%25-(t>>2|t*15|t%227)-t>>3)|((t>>5)&(t<<5)*1663|(t>>3)%1544)/(t%17|t%2048)
// ryg 2011-10-10 http://www.youtube.com/watch?v=tCRPUv8V22o 44.1 kHz
((t*("36364689"[t>>13&7]&15))/12&128)+(((((t>>12)^(t>>12)-2)%11*t)/4|t>>13)&127)
// mu6k http://www.youtube.com/watch?v=tCRPUv8V22o 32.0 kHz
(3e3/(y=t&16383)&1)*35 +(x=t*"6689"[t>>16&3]/24&127)*y/4e4 +((t>>8^t>>10|t>>14|x)&63)
// Ola 2011-10-11 http://countercomplex.blogspot.com/2011/10/algorithmic-symphonies-from-one-line-of.html
((1-(((t+10)>>((t>>9)&((t>>14))))&(t>>4&-2)))*2)*(((t>>10)^((t+((t>>6)&127))>>10))&1)*32+128
// raer 2011-10-07 http://pouet.net/topic.php?which=8357&page=16 stereo 11kHz
L: ((t&4096)?((t*(t^t%255)|(t>>4))>>1):(t>>3)|((t&8192)?t<<2:t)) R: t*(((t>>9)^((t>>9)-1)^1)%13)
// ryg 2011-10-04 http://pouet.net/topic.php?which=8357&page=8
((t>>4)*(13&(0x8898a989>>(t>>11&30)))&255)+((((t>>9|(t>>2)|t>>8)*10+4*((t>>2)&t>>15|t>>8))&255)>>1)
// gasman 2011-10-05 http://pouet.net/topic.php?which=8357&page=12 js-only
(t<<3)*[8/9,1,9/8,6/5,4/3,3/2,0][[0xd2d2c8,0xce4088,0xca32c8,0x8e4009][t>>14&3]>>(0x3dbe4688>>((t>>10&15)>9?18:t>>10&15)*3&7)*3&7]
// a1k0n http://news.ycombinator.com/item?id=3063359 js-only
SS=function(s,o,r,p){var c=s.charCodeAt((t>>r)%p);return c==32?0:31&t*Math.pow(2,c/12-o)},SS("0 0 7 7 037:<<",6,10,32) + (t&4096?SS("037",4,8,3)*(4096-(t&4095))>>12 : 0)
// mu6k 2011-10-10 http://www.youtube.com/watch?v=tCRPUv8V22o "Long-line Theory", Chaos Theory cover, optimized by ryg, p01 et al., JS-only
w=t>>9,k=32,m=2048,a=1-t/m%1,d=(14*t*t^t)%m*a,y=[3,3,4.7,2][p=w/k&3]*t/4,h="IQNNNN!!]]!Q!IW]WQNN??!!W]WQNNN?".charCodeAt(w/2&15|p/3<<4)/33*t-t,s=y*.98%80+y%80+(w>>7&&a*((5*t%m*a&128)*(0x53232323>>w/4&1)+(d&127)*(0xa444c444>>w/4&1)*1.5+(d*w&1)+(h%k+h*1.99%k+h*.49%k+h*.97%k-64)*(4-a-a))),s*s>>14?127:s

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.

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,74 @@
/* Direct-digital synthesis */
// ------- Preamble -------- //
#include <avr/io.h> /* Defines pins, ports, etc */
#include <util/delay.h> /* Functions to waste time */
#include <avr/interrupt.h>
#include <avr/power.h>
#include "pinDefines.h"
#include "fullSine.h"
#include "USART.h"
volatile uint16_t accumulator;
volatile uint16_t tuningWord;
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 */
TIMSK0 |= (1 << TOIE0); /* Overflow interrupt */
sei(); /* Enable interrupt */
}
ISR(TIMER0_OVF_vect) {
accumulator += tuningWord; /* take tuningWord steps forward */
/* lookup and set output */
OCR0A = 128 + fullSine[(uint8_t) (accumulator >> 8)];
}
static inline void pollButton(void) {
if (bit_is_clear(BUTTON_PIN, BUTTON)) {
SPEAKER_DDR |= (1 << SPEAKER); /* enable output on pin */
LED_PORT &= ~(1 << LED0);
}
else {
SPEAKER_DDR &= ~(1 << SPEAKER); /* disable output on pin */
LED_PORT |= (1 << LED0);
}
}
int main(void) {
// -------- Inits --------- //
clock_prescale_set(clock_div_1); /* CPU clock 8 MHz */
initTimer0();
initUSART();
BUTTON_PORT |= (1 << BUTTON); /* pullup on button */
LED_DDR |= (1 << LED0); /* LED on for diagnostics */
LED_PORT |= (1 << LED0);
_delay_ms(100);
LED_PORT &= ~(1 << LED0);
SPEAKER_DDR |= (1 << SPEAKER);
tuningWord = 880;
transmitByte((uint8_t) tuningWord >> 2); /* approx value in Hz */
// ------ Event loop ------ //
while (1) {
pollButton();
} /* 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.

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 "fullSaw15.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 = fullSaw15[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.

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,85 @@
/* Direct-digital synthesis */
// ------- Preamble -------- //
#include <avr/io.h> /* Defines pins, ports, etc */
#include <util/delay.h> /* Functions to waste time */
#include <avr/interrupt.h>
#include <avr/power.h>
#include "pinDefines.h"
#include "macros.h"
#include "fullSine.h"
static inline void initTimer0(void){
set_bit(TCCR0A, COM0A1); /* PWM output on OCR0A */
set_bit(SPEAKER_DDR, SPEAKER); /* enable output on pin */
set_bit(TCCR0A, WGM00); /* Fast PWM mode */
set_bit(TCCR0A, WGM01); /* Fast PWM mode, pt.2 */
set_bit(TCCR0B, CS00); /* Clock with /1 prescaler */
}
static inline void pollButton(void){
if (bit_is_clear(BUTTON_PIN, BUTTON)) {
set_bit(SPEAKER_DDR, SPEAKER); /* enable output on pin */
clear_bit(LED_PORT, LED0);
}
else {
clear_bit(SPEAKER_DDR, SPEAKER); /* disable output on pin */
set_bit(LED_PORT, LED0);
}
}
static inline void initLED(void){
set_bit(LED_DDR, LED0); /* LED on for diagnostics */
set_bit(LED_PORT, LED0);
_delay_ms(100);
clear_bit(LED_PORT, LED0);
}
int main(void){
volatile uint16_t accumulator0;
volatile uint16_t accumulator1;
volatile uint16_t tuningWord0;
volatile uint16_t tuningWord1;
volatile uint16_t mixer;
// -------- Inits --------- //
clock_prescale_set(clock_div_1); /* CPU clock 8 MHz */
initLED();
initTimer0();
set_bit(BUTTON_PORT, BUTTON); /* pullup on button */
set_bit(SPEAKER_DDR, SPEAKER); /* speaker output */
tuningWord0 = 440*2; /* Dial tone frequencies */
tuningWord1 = 350*2;
// ------ Event loop ------ //
while(1){
loop_until_bit_is_set(TIFR0, TOV0); /* wait until overflow bit set */
set_bit(TIFR0, TOV0); /* writing set should reset... */
// Note that this bit has to be pretty fast... we have only 256 cycles to
// set the next value in OCR0A, or we may hear a glitch
accumulator0 += tuningWord0; /* take tuningWord steps forward */
accumulator1 += tuningWord1; /* take tuningWord steps forward */
mixer = fullSine[(uint8_t) (accumulator0 >> 8)]; /* add together */
mixer += fullSine[(uint8_t) (accumulator1 >> 8)];
OCR0A = 128 + (mixer >> 1); /* divide by 2, the fast way */
pollButton();
} /* End event loop */
return 0; /* This line is never reached */
}

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,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.

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
Phasing saw waves demo
*/
#include "fatSaw.h"
int main(void) {
uint16_t accumulators[NUMBER_OSCILLATORS];
uint8_t waveStep;
int16_t mixer = 0;
uint8_t i;
// -------- Inits --------- //
clock_prescale_set(clock_div_1); /* CPU clock 8 MHz */
initTimer0();
SPEAKER_DDR |= (1 << SPEAKER); /* speaker output */
LED_DDR |= (1 << LED0);
// Init all to same phase
for (i = 0; i < NUMBER_OSCILLATORS; i++) {
accumulators[i] = 0;
}
// ------ Event loop ------ //
while (1) {
/* Load in the PWM value when ready */
loop_until_bit_is_set(TIFR0, TOV0); /* wait until overflow bit set */
OCR0A = 128 + mixer; /* signed-integers need shifting up */
TIFR0 |= (1 << TOV0); /* re-set the overflow bit */
/* Update all accumulators, mix together */
mixer = 0;
for (i = 0; i < NUMBER_OSCILLATORS; i++) {
accumulators[i] += BASEPITCH;
waveStep = accumulators[i] >> 8;
// Add extra phase increment.
// Makes shifting overtones when
// different frequency components add, subtract
if (waveStep == 0) { /* roughly once per cycle */
accumulators[i] += PHASE_RATE * i; /* add extra phase */
}
mixer += fullSaw15[waveStep];
}
mixer = mixer >> OSCILLATOR_SHIFT;
/* Dividing by bitshift is very fast. */
} /* End event loop */
return 0; /* This line is never reached */
}

View File

@@ -0,0 +1,36 @@
// ------- Preamble -------- //
#include <avr/io.h> /* Defines pins, ports, etc */
#include <util/delay.h> /* Functions to waste time */
#include <avr/interrupt.h>
#include <avr/power.h>
#include "pinDefines.h"
#include "macros.h"
#include "fullSaw15.h"
#define BASEPITCH 220 /* in tuningWord steps, which are ~1/2 Hz */
#define PHASE_RATE 7 /* controls speed of phasing effect */
#define NUMBER_OSCILLATORS 4
/* 2 and 4 work just fine.
8 and 16 take too long to maintain our 31.25kHz sample rate
so the pitch shifts downwards and there's all sorts of aliasing.
If you're just after scary sounds, 8 and 16 are awesome, but you may want
to increase the BASEPITCH to compensate. */
#define OSCILLATOR_SHIFT 2
/* This is the number of bits to shift when volume mixing.
2**OSCILLATOR_SHIFT = NUMBER_OSCILLATORS
If you don't change this to match the number of oscillators, you'll
get clipping and digital distortion. */
static inline void initTimer0(void){
set_bit(TCCR0A, WGM00); /* Fast PWM mode */
set_bit(TCCR0A, WGM01); /* Fast PWM mode */
set_bit(TCCR0A, COM0A1); /* PWM output on OCR0A */
set_bit(SPEAKER_DDR, SPEAKER); /* enable output on pin */
set_bit(TCCR0B, CS00); /* Clock with /1 prescaler */
}

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,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.