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

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

View File

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

View File

@@ -0,0 +1,56 @@
/*
Direct-digital synthesis
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.