Started following some tutorial on att85 usi. Downloaded example code from make avr book.
This commit is contained in:
196
Make AVR Examples/Chapter13_Advanced-PWM-Tricks/adsr/Makefile
Normal file
196
Make AVR Examples/Chapter13_Advanced-PWM-Tricks/adsr/Makefile
Normal 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
|
||||
86
Make AVR Examples/Chapter13_Advanced-PWM-Tricks/adsr/adsr.c
Normal file
86
Make AVR Examples/Chapter13_Advanced-PWM-Tricks/adsr/adsr.c
Normal 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 */
|
||||
}
|
||||
81
Make AVR Examples/Chapter13_Advanced-PWM-Tricks/adsr/adsr.h
Normal file
81
Make AVR Examples/Chapter13_Advanced-PWM-Tricks/adsr/adsr.h
Normal 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);
|
||||
}
|
||||
|
||||
@@ -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
|
||||
};
|
||||
@@ -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
|
||||
};
|
||||
@@ -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
|
||||
};
|
||||
@@ -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
|
||||
};
|
||||
@@ -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
|
||||
};
|
||||
@@ -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
|
||||
};
|
||||
@@ -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
|
||||
};
|
||||
@@ -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)
|
||||
|
||||
|
||||
@@ -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.
|
||||
|
||||
72
Make AVR Examples/Chapter13_Advanced-PWM-Tricks/adsr/scale.h
Normal file
72
Make AVR Examples/Chapter13_Advanced-PWM-Tricks/adsr/scale.h
Normal 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
|
||||
Reference in New Issue
Block a user