diff --git a/Make AVR Examples/AVR-Programming-Library/.gitignore b/Make AVR Examples/AVR-Programming-Library/.gitignore new file mode 100644 index 0000000..df3d5dd --- /dev/null +++ b/Make AVR Examples/AVR-Programming-Library/.gitignore @@ -0,0 +1,2 @@ +binaryMacroDemo.c + diff --git a/Make AVR Examples/AVR-Programming-Library/Makefile b/Make AVR Examples/AVR-Programming-Library/Makefile new file mode 100644 index 0000000..3d9e544 --- /dev/null +++ b/Make AVR Examples/AVR-Programming-Library/Makefile @@ -0,0 +1,196 @@ + +##########------------------------------------------------------########## +########## Project-specific Details ########## +########## Check these every time you start a new project ########## +##########------------------------------------------------------########## + +MCU = atmega168p +F_CPU = 1000000UL +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 diff --git a/Make AVR Examples/AVR-Programming-Library/README.md b/Make AVR Examples/AVR-Programming-Library/README.md new file mode 100644 index 0000000..1bed580 --- /dev/null +++ b/Make AVR Examples/AVR-Programming-Library/README.md @@ -0,0 +1,30 @@ +AVR-Programming-Library +======================= + +Files that are re-used throughout the book *Make: AVR Programming* + +* **pinDefines.h** + Includes all of the pin definitions for the projects in *Make: AVR Programming*. When you + need to know what to hook up where, have a look here. + +* **USART.c** and **USART.h** + These are the main utility files used for serial input/output in the book. They're not + particularly clever, but they have a tiny memory and resource footprint and get you + up and running very quickly. + +* **binaryMacro.h** + If you're using a compiler other than a recent AVR-GCC, it may not support + native binary digits (e.g. 0b00111010). If so, this include file has a pre-processor + macro that will let you enter them similarly: B8(01010101). + +* **macros.h** + This file includes pre-processor macro definitions for bit-twiddling: + set_bit, clear_bit, and toggle_bit. Useful for those days when you're feeling + lazy, or have forgotten your bit-wise logic operations. + +* **portpins.h** + This is a recent version of the portpins.h file included with the Arduino environment. + It includes the AVR pin definitions of the form PB1 and similar. The version included + with Arduino is very old -- you'll want to replace it with this one if you want to write + in C using the Arduino IDE. Or you can include this file directly in your code. + diff --git a/Make AVR Examples/AVR-Programming-Library/USART.c b/Make AVR Examples/AVR-Programming-Library/USART.c new file mode 100644 index 0000000..7e8139a --- /dev/null +++ b/Make AVR Examples/AVR-Programming-Library/USART.c @@ -0,0 +1,137 @@ + +/* + Quick and dirty functions that make serial communications work. + + Note that receiveByte() blocks -- it sits and waits _forever_ for + a byte to come in. If you're doing anything that's more interesting, + you'll want to implement this with interrupts. + + initUSART requires BAUDRATE to be defined in order to calculate + the bit-rate multiplier. 9600 is a reasonable default. + + May not work with some of the older chips: + Tiny2313, Mega8, Mega16, Mega32 have different pin macros + If you're using these chips, see (e.g.) iom8.h for how it's done. + These old chips don't specify UDR0 vs UDR1. + Correspondingly, the macros will just be defined as UDR. +*/ + +#include +#include "USART.h" +#include + +void initUSART(void) { /* requires BAUD */ + UBRR0H = UBRRH_VALUE; /* defined in setbaud.h */ + UBRR0L = UBRRL_VALUE; +#if USE_2X + UCSR0A |= (1 << U2X0); +#else + UCSR0A &= ~(1 << U2X0); +#endif + /* Enable USART transmitter/receiver */ + UCSR0B = (1 << TXEN0) | (1 << RXEN0); + UCSR0C = (1 << UCSZ01) | (1 << UCSZ00); /* 8 data bits, 1 stop bit */ +} + + +void transmitByte(uint8_t data) { + /* Wait for empty transmit buffer */ + loop_until_bit_is_set(UCSR0A, UDRE0); + UDR0 = data; /* send data */ +} + +uint8_t receiveByte(void) { + loop_until_bit_is_set(UCSR0A, RXC0); /* Wait for incoming data */ + return UDR0; /* return register value */ +} + + + /* Here are a bunch of useful printing commands */ + +void printString(const char myString[]) { + uint8_t i = 0; + while (myString[i]) { + transmitByte(myString[i]); + i++; + } +} + +void readString(char myString[], uint8_t maxLength) { + char response; + uint8_t i; + i = 0; + while (i < (maxLength - 1)) { /* prevent over-runs */ + response = receiveByte(); + transmitByte(response); /* echo */ + if (response == '\r') { /* enter marks the end */ + break; + } + else { + myString[i] = response; /* add in a letter */ + i++; + } + } + myString[i] = 0; /* terminal NULL character */ +} + +void printByte(uint8_t byte) { + /* Converts a byte to a string of decimal text, sends it */ + transmitByte('0' + (byte / 100)); /* Hundreds */ + transmitByte('0' + ((byte / 10) % 10)); /* Tens */ + transmitByte('0' + (byte % 10)); /* Ones */ +} + +void printWord(uint16_t word) { + transmitByte('0' + (word / 10000)); /* Ten-thousands */ + transmitByte('0' + ((word / 1000) % 10)); /* Thousands */ + transmitByte('0' + ((word / 100) % 10)); /* Hundreds */ + transmitByte('0' + ((word / 10) % 10)); /* Tens */ + transmitByte('0' + (word % 10)); /* Ones */ +} + +void printBinaryByte(uint8_t byte) { + /* Prints out a byte as a series of 1's and 0's */ + uint8_t bit; + for (bit = 7; bit < 255; bit--) { + if (bit_is_set(byte, bit)) + transmitByte('1'); + else + transmitByte('0'); + } +} + +char nibbleToHexCharacter(uint8_t nibble) { + /* Converts 4 bits into hexadecimal */ + if (nibble < 10) { + return ('0' + nibble); + } + else { + return ('A' + nibble - 10); + } +} + +void printHexByte(uint8_t byte) { + /* Prints a byte as its hexadecimal equivalent */ + uint8_t nibble; + nibble = (byte & 0b11110000) >> 4; + transmitByte(nibbleToHexCharacter(nibble)); + nibble = byte & 0b00001111; + transmitByte(nibbleToHexCharacter(nibble)); +} + +uint8_t getNumber(void) { + // Gets a numerical 0-255 from the serial port. + // Converts from string to number. + char hundreds = '0'; + char tens = '0'; + char ones = '0'; + char thisChar = '0'; + do { /* shift over */ + hundreds = tens; + tens = ones; + ones = thisChar; + thisChar = receiveByte(); /* get a new character */ + transmitByte(thisChar); /* echo */ + } while (thisChar != '\r'); /* until type return */ + return (100 * (hundreds - '0') + 10 * (tens - '0') + ones - '0'); +} diff --git a/Make AVR Examples/AVR-Programming-Library/USART.h b/Make AVR Examples/AVR-Programming-Library/USART.h new file mode 100644 index 0000000..8d02081 --- /dev/null +++ b/Make AVR Examples/AVR-Programming-Library/USART.h @@ -0,0 +1,45 @@ +/* Functions to initialize, send, receive over USART + + initUSART requires BAUD to be defined in order to calculate + the bit-rate multiplier. + */ + +#ifndef BAUD /* if not defined in Makefile... */ +#define BAUD 9600 /* set a safe default baud rate */ +#endif + + /* These are defined for convenience */ +#define USART_HAS_DATA bit_is_set(UCSR0A, RXC0) +#define USART_READY bit_is_set(UCSR0A, UDRE0) + +/* Takes the defined BAUD and F_CPU, + calculates the bit-clock multiplier, + and configures the hardware USART */ +void initUSART(void); + +/* Blocking transmit and receive functions. + When you call receiveByte() your program will hang until + data comes through. We'll improve on this later. */ +void transmitByte(uint8_t data); +uint8_t receiveByte(void); + +void printString(const char myString[]); + /* Utility function to transmit an entire string from RAM */ +void readString(char myString[], uint8_t maxLength); +/* Define a string variable, pass it to this function + The string will contain whatever you typed over serial */ + +void printByte(uint8_t byte); + /* Prints a byte out as its 3-digit ascii equivalent */ +void printWord(uint16_t word); + /* Prints a word (16-bits) out as its 5-digit ascii equivalent */ + +void printBinaryByte(uint8_t byte); + /* Prints a byte out in 1s and 0s */ +char nibbleToHex(uint8_t nibble); +char nibbleToHexCharacter(uint8_t nibble); +void printHexByte(uint8_t byte); + /* Prints a byte out in hexadecimal */ +uint8_t getNumber(void); +/* takes in up to three ascii digits, + converts them to a byte when press enter */ diff --git a/Make AVR Examples/AVR-Programming-Library/binaryMacro.h b/Make AVR Examples/AVR-Programming-Library/binaryMacro.h new file mode 100644 index 0000000..9dff3c1 --- /dev/null +++ b/Make AVR Examples/AVR-Programming-Library/binaryMacro.h @@ -0,0 +1,45 @@ +/* Binary constant generator macro +By Tom Torfs - donated to the public domain +*/ + +/* All macro's evaluate to compile-time constants */ + +/* *** helper macros *** / + +/* turn a numeric literal into a hex constant +(avoids problems with leading zeroes) +8-bit constants max value 0x11111111, always fits in unsigned long +*/ +#define HEX__(n) 0x##n##LU + +/* 8-bit conversion function */ +#define B8__(x) ((x&0x0000000FLU)?1:0) \ ++((x&0x000000F0LU)?2:0) \ ++((x&0x00000F00LU)?4:0) \ ++((x&0x0000F000LU)?8:0) \ ++((x&0x000F0000LU)?16:0) \ ++((x&0x00F00000LU)?32:0) \ ++((x&0x0F000000LU)?64:0) \ ++((x&0xF0000000LU)?128:0) + +/* *** user macros *** / + +/* for upto 8-bit binary constants */ +#define B8(d) ((unsigned char)B8__(HEX__(d))) + +/* for upto 16-bit binary constants, MSB first */ +#define B16(dmsb,dlsb) (((unsigned short)B8(dmsb)<<8) \ ++ B8(dlsb)) + +/* for upto 32-bit binary constants, MSB first */ +#define B32(dmsb,db2,db3,dlsb) (((unsigned long)B8(dmsb)<<24) \ ++ ((unsigned long)B8(db2)<<16) \ ++ ((unsigned long)B8(db3)<<8) \ ++ B8(dlsb)) + +/* Sample usage: +B8(01010101) = 85 +B16(10101010,01010101) = 43605 +B32(10000000,11111111,10101010,01010101) = 2164238933 +*/ + diff --git a/Make AVR Examples/AVR-Programming-Library/macros.h b/Make AVR Examples/AVR-Programming-Library/macros.h new file mode 100644 index 0000000..7987b22 --- /dev/null +++ b/Make AVR Examples/AVR-Programming-Library/macros.h @@ -0,0 +1,26 @@ + +/* Standard Macros */ +/* You can totally get by without these, but why? */ + +/* Make sure we've already got io / sfr / pindefs loaded */ +#ifndef _AVR_IO_H_ +#include +#endif + +/* Reminder: the following useful bit-twiddling macros are + always included in avr/sfr_defs.h, which is called from + avr/io.h + + bit_is_set(sfr, bit) + bit_is_clear(sfr, bit) + loop_until_bit_is_set(sfr, bit) + loop_until_bit_is_clear(sfr, bit) + +*/ + +/* Define up the full complement of bit-twiddling macros */ +#define BV(bit) (1 << bit) +#define set_bit(sfr, bit) (_SFR_BYTE(sfr) |= BV(bit)) // old sbi() +#define clear_bit(sfr, bit) (_SFR_BYTE(sfr) &= ~BV(bit)) // old cbi() +#define toggle_bit(sfr, bit) (_SFR_BYTE(sfr) ^= BV(bit)) + diff --git a/Make AVR Examples/AVR-Programming-Library/pinDefines.h b/Make AVR Examples/AVR-Programming-Library/pinDefines.h new file mode 100644 index 0000000..36cd2f0 --- /dev/null +++ b/Make AVR Examples/AVR-Programming-Library/pinDefines.h @@ -0,0 +1,91 @@ +// --------------- +// Pin Defines +// --------------- + +#define LED_PORT PORTB +#define LED_PIN PINB +#define LED_DDR DDRB + +#define LED0 PB0 +#define LED1 PB1 +#define LED2 PB2 +#define LED3 PB3 +#define LED4 PB4 +#define LED5 PB5 +#define LED6 PB6 +#define LED7 PB7 + +#define BUTTON_PORT PORTD +#define BUTTON_PIN PIND +#define BUTTON_DDR DDRD + +#define BUTTON PD2 +#define BUTTON2 PD3 +#define BUTTON3 PD4 + +#define SPEAKER PD6 /* OC0A */ +#define SPEAKER_PORT PORTD +#define SPEAKER_PIN PIND +#define SPEAKER_DDR DDRD + +#define ANTENNA PD5 /* OC0B */ +#define ANTENNA_PORT PORTD +#define ANTENNA_PIN PIND +#define ANTENNA_DDR DDRD + +#define MODULATION PD3 /* OC2B */ +#define MODULATION_PORT PORTD +#define MODULATION_PIN PIND +#define MODULATION_DDR DDRD + +#define LIGHT_SENSOR PC0 /* ADC0 */ +#define LIGHT_SENSOR_PORT PORTC +#define LIGHT_SENSOR_PIN PINC +#define LIGHT_SENSOR_DDR DDRC + +#define CAP_SENSOR PC1 /* ADC1 */ +#define CAP_SENSOR_PORT PORTC +#define CAP_SENSOR_PIN PINC +#define CAP_SENSOR_DDR DDRC + +#define PIEZO PC2 /* ADC2 */ +#define PIEZO_PORT PORTC +#define PIEZO_PIN PINC +#define PIEZO_DDR DDRC + +#define POT PC3 /* ADC3 */ +#define POT_PORT PORTC +#define POT_PIN PINC +#define POT_DDR DDRC + +// SPI and I2C serial mode defines + +#define SPI_SS PB2 +#define SPI_SS_PORT PORTB +#define SPI_SS_PIN PINB +#define SPI_SS_DDR DDRB + +#define SPI_MOSI PB3 +#define SPI_MOSI_PORT PORTB +#define SPI_MOSI_PIN PINB +#define SPI_MOSI_DDR DDRB + +#define SPI_MISO PB4 +#define SPI_MISO_PORT PORTB +#define SPI_MISO_PIN PINB +#define SPI_MISO_DDR DDRB + +#define SPI_SCK PB5 +#define SPI_SCK_PORT PORTB +#define SPI_SCK_PIN PINB +#define SPI_SCK_DDR DDRB + +#define I2C_SDA PC4 +#define I2C_SDA_PORT PORTC +#define I2C_SDA_PIN PINC +#define I2C_SDA_DDR DDRC + +#define I2C_SCL PC5 +#define I2C_SCL_PORT PORTC +#define I2C_SCL_PIN PINC +#define I2C_SCL_DDR DDRC diff --git a/Make AVR Examples/AVR-Programming-Library/portpins.h b/Make AVR Examples/AVR-Programming-Library/portpins.h new file mode 100644 index 0000000..6a24830 --- /dev/null +++ b/Make AVR Examples/AVR-Programming-Library/portpins.h @@ -0,0 +1,549 @@ +/* Copyright (c) 2003 Theodore A. Roth + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the + distribution. + + * Neither the name of the copyright holders nor the names of + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. */ + +/* $Id: portpins.h 1936 2009-03-19 22:19:26Z arcanum $ */ + +#ifndef _AVR_PORTPINS_H_ +#define _AVR_PORTPINS_H_ 1 + +/* This file should only be included from , never directly. */ + +#ifndef _AVR_IO_H_ +# error "Include instead of this file." +#endif + +/* Define Generic PORTn, DDn, and PINn values. */ + +/* Port Data Register (generic) */ +#define PORT7 7 +#define PORT6 6 +#define PORT5 5 +#define PORT4 4 +#define PORT3 3 +#define PORT2 2 +#define PORT1 1 +#define PORT0 0 + +/* Port Data Direction Register (generic) */ +#define DD7 7 +#define DD6 6 +#define DD5 5 +#define DD4 4 +#define DD3 3 +#define DD2 2 +#define DD1 1 +#define DD0 0 + +/* Port Input Pins (generic) */ +#define PIN7 7 +#define PIN6 6 +#define PIN5 5 +#define PIN4 4 +#define PIN3 3 +#define PIN2 2 +#define PIN1 1 +#define PIN0 0 + +/* Define PORTxn an Pxn values for all possible port pins if not defined already by io.h. */ + +/* PORT A */ + +#if defined(PA0) && !defined(PORTA0) +# define PORTA0 PA0 +#elif defined(PORTA0) && !defined(PA0) +# define PA0 PORTA0 +#endif +#if defined(PA1) && !defined(PORTA1) +# define PORTA1 PA1 +#elif defined(PORTA1) && !defined(PA1) +# define PA1 PORTA1 +#endif +#if defined(PA2) && !defined(PORTA2) +# define PORTA2 PA2 +#elif defined(PORTA2) && !defined(PA2) +# define PA2 PORTA2 +#endif +#if defined(PA3) && !defined(PORTA3) +# define PORTA3 PA3 +#elif defined(PORTA3) && !defined(PA3) +# define PA3 PORTA3 +#endif +#if defined(PA4) && !defined(PORTA4) +# define PORTA4 PA4 +#elif defined(PORTA4) && !defined(PA4) +# define PA4 PORTA4 +#endif +#if defined(PA5) && !defined(PORTA5) +# define PORTA5 PA5 +#elif defined(PORTA5) && !defined(PA5) +# define PA5 PORTA5 +#endif +#if defined(PA6) && !defined(PORTA6) +# define PORTA6 PA6 +#elif defined(PORTA6) && !defined(PA6) +# define PA6 PORTA6 +#endif +#if defined(PA7) && !defined(PORTA7) +# define PORTA7 PA7 +#elif defined(PORTA7) && !defined(PA7) +# define PA7 PORTA7 +#endif + +/* PORT B */ + +#if defined(PB0) && !defined(PORTB0) +# define PORTB0 PB0 +#elif defined(PORTB0) && !defined(PB0) +# define PB0 PORTB0 +#endif +#if defined(PB1) && !defined(PORTB1) +# define PORTB1 PB1 +#elif defined(PORTB1) && !defined(PB1) +# define PB1 PORTB1 +#endif +#if defined(PB2) && !defined(PORTB2) +# define PORTB2 PB2 +#elif defined(PORTB2) && !defined(PB2) +# define PB2 PORTB2 +#endif +#if defined(PB3) && !defined(PORTB3) +# define PORTB3 PB3 +#elif defined(PORTB3) && !defined(PB3) +# define PB3 PORTB3 +#endif +#if defined(PB4) && !defined(PORTB4) +# define PORTB4 PB4 +#elif defined(PORTB4) && !defined(PB4) +# define PB4 PORTB4 +#endif +#if defined(PB5) && !defined(PORTB5) +# define PORTB5 PB5 +#elif defined(PORTB5) && !defined(PB5) +# define PB5 PORTB5 +#endif +#if defined(PB6) && !defined(PORTB6) +# define PORTB6 PB6 +#elif defined(PORTB6) && !defined(PB6) +# define PB6 PORTB6 +#endif +#if defined(PB7) && !defined(PORTB7) +# define PORTB7 PB7 +#elif defined(PORTB7) && !defined(PB7) +# define PB7 PORTB7 +#endif + +/* PORT C */ + +#if defined(PC0) && !defined(PORTC0) +# define PORTC0 PC0 +#elif defined(PORTC0) && !defined(PC0) +# define PC0 PORTC0 +#endif +#if defined(PC1) && !defined(PORTC1) +# define PORTC1 PC1 +#elif defined(PORTC1) && !defined(PC1) +# define PC1 PORTC1 +#endif +#if defined(PC2) && !defined(PORTC2) +# define PORTC2 PC2 +#elif defined(PORTC2) && !defined(PC2) +# define PC2 PORTC2 +#endif +#if defined(PC3) && !defined(PORTC3) +# define PORTC3 PC3 +#elif defined(PORTC3) && !defined(PC3) +# define PC3 PORTC3 +#endif +#if defined(PC4) && !defined(PORTC4) +# define PORTC4 PC4 +#elif defined(PORTC4) && !defined(PC4) +# define PC4 PORTC4 +#endif +#if defined(PC5) && !defined(PORTC5) +# define PORTC5 PC5 +#elif defined(PORTC5) && !defined(PC5) +# define PC5 PORTC5 +#endif +#if defined(PC6) && !defined(PORTC6) +# define PORTC6 PC6 +#elif defined(PORTC6) && !defined(PC6) +# define PC6 PORTC6 +#endif +#if defined(PC7) && !defined(PORTC7) +# define PORTC7 PC7 +#elif defined(PORTC7) && !defined(PC7) +# define PC7 PORTC7 +#endif + +/* PORT D */ + +#if defined(PD0) && !defined(PORTD0) +# define PORTD0 PD0 +#elif defined(PORTD0) && !defined(PD0) +# define PD0 PORTD0 +#endif +#if defined(PD1) && !defined(PORTD1) +# define PORTD1 PD1 +#elif defined(PORTD1) && !defined(PD1) +# define PD1 PORTD1 +#endif +#if defined(PD2) && !defined(PORTD2) +# define PORTD2 PD2 +#elif defined(PORTD2) && !defined(PD2) +# define PD2 PORTD2 +#endif +#if defined(PD3) && !defined(PORTD3) +# define PORTD3 PD3 +#elif defined(PORTD3) && !defined(PD3) +# define PD3 PORTD3 +#endif +#if defined(PD4) && !defined(PORTD4) +# define PORTD4 PD4 +#elif defined(PORTD4) && !defined(PD4) +# define PD4 PORTD4 +#endif +#if defined(PD5) && !defined(PORTD5) +# define PORTD5 PD5 +#elif defined(PORTD5) && !defined(PD5) +# define PD5 PORTD5 +#endif +#if defined(PD6) && !defined(PORTD6) +# define PORTD6 PD6 +#elif defined(PORTD6) && !defined(PD6) +# define PD6 PORTD6 +#endif +#if defined(PD7) && !defined(PORTD7) +# define PORTD7 PD7 +#elif defined(PORTD7) && !defined(PD7) +# define PD7 PORTD7 +#endif + +/* PORT E */ + +#if defined(PE0) && !defined(PORTE0) +# define PORTE0 PE0 +#elif defined(PORTE0) && !defined(PE0) +# define PE0 PORTE0 +#endif +#if defined(PE1) && !defined(PORTE1) +# define PORTE1 PE1 +#elif defined(PORTE1) && !defined(PE1) +# define PE1 PORTE1 +#endif +#if defined(PE2) && !defined(PORTE2) +# define PORTE2 PE2 +#elif defined(PORTE2) && !defined(PE2) +# define PE2 PORTE2 +#endif +#if defined(PE3) && !defined(PORTE3) +# define PORTE3 PE3 +#elif defined(PORTE3) && !defined(PE3) +# define PE3 PORTE3 +#endif +#if defined(PE4) && !defined(PORTE4) +# define PORTE4 PE4 +#elif defined(PORTE4) && !defined(PE4) +# define PE4 PORTE4 +#endif +#if defined(PE5) && !defined(PORTE5) +# define PORTE5 PE5 +#elif defined(PORTE5) && !defined(PE5) +# define PE5 PORTE5 +#endif +#if defined(PE6) && !defined(PORTE6) +# define PORTE6 PE6 +#elif defined(PORTE6) && !defined(PE6) +# define PE6 PORTE6 +#endif +#if defined(PE7) && !defined(PORTE7) +# define PORTE7 PE7 +#elif defined(PORTE7) && !defined(PE7) +# define PE7 PORTE7 +#endif + +/* PORT F */ + +#if defined(PF0) && !defined(PORTF0) +# define PORTF0 PF0 +#elif defined(PORTF0) && !defined(PF0) +# define PF0 PORTF0 +#endif +#if defined(PF1) && !defined(PORTF1) +# define PORTF1 PF1 +#elif defined(PORTF1) && !defined(PF1) +# define PF1 PORTF1 +#endif +#if defined(PF2) && !defined(PORTF2) +# define PORTF2 PF2 +#elif defined(PORTF2) && !defined(PF2) +# define PF2 PORTF2 +#endif +#if defined(PF3) && !defined(PORTF3) +# define PORTF3 PF3 +#elif defined(PORTF3) && !defined(PF3) +# define PF3 PORTF3 +#endif +#if defined(PF4) && !defined(PORTF4) +# define PORTF4 PF4 +#elif defined(PORTF4) && !defined(PF4) +# define PF4 PORTF4 +#endif +#if defined(PF5) && !defined(PORTF5) +# define PORTF5 PF5 +#elif defined(PORTF5) && !defined(PF5) +# define PF5 PORTF5 +#endif +#if defined(PF6) && !defined(PORTF6) +# define PORTF6 PF6 +#elif defined(PORTF6) && !defined(PF6) +# define PF6 PORTF6 +#endif +#if defined(PF7) && !defined(PORTF7) +# define PORTF7 PF7 +#elif defined(PORTF7) && !defined(PF7) +# define PF7 PORTF7 +#endif + +/* PORT G */ + +#if defined(PG0) && !defined(PORTG0) +# define PORTG0 PG0 +#elif defined(PORTG0) && !defined(PG0) +# define PG0 PORTG0 +#endif +#if defined(PG1) && !defined(PORTG1) +# define PORTG1 PG1 +#elif defined(PORTG1) && !defined(PG1) +# define PG1 PORTG1 +#endif +#if defined(PG2) && !defined(PORTG2) +# define PORTG2 PG2 +#elif defined(PORTG2) && !defined(PG2) +# define PG2 PORTG2 +#endif +#if defined(PG3) && !defined(PORTG3) +# define PORTG3 PG3 +#elif defined(PORTG3) && !defined(PG3) +# define PG3 PORTG3 +#endif +#if defined(PG4) && !defined(PORTG4) +# define PORTG4 PG4 +#elif defined(PORTG4) && !defined(PG4) +# define PG4 PORTG4 +#endif +#if defined(PG5) && !defined(PORTG5) +# define PORTG5 PG5 +#elif defined(PORTG5) && !defined(PG5) +# define PG5 PORTG5 +#endif +#if defined(PG6) && !defined(PORTG6) +# define PORTG6 PG6 +#elif defined(PORTG6) && !defined(PG6) +# define PG6 PORTG6 +#endif +#if defined(PG7) && !defined(PORTG7) +# define PORTG7 PG7 +#elif defined(PORTG7) && !defined(PG7) +# define PG7 PORTG7 +#endif + +/* PORT H */ + +#if defined(PH0) && !defined(PORTH0) +# define PORTH0 PH0 +#elif defined(PORTH0) && !defined(PH0) +# define PH0 PORTH0 +#endif +#if defined(PH1) && !defined(PORTH1) +# define PORTH1 PH1 +#elif defined(PORTH1) && !defined(PH1) +# define PH1 PORTH1 +#endif +#if defined(PH2) && !defined(PORTH2) +# define PORTH2 PH2 +#elif defined(PORTH2) && !defined(PH2) +# define PH2 PORTH2 +#endif +#if defined(PH3) && !defined(PORTH3) +# define PORTH3 PH3 +#elif defined(PORTH3) && !defined(PH3) +# define PH3 PORTH3 +#endif +#if defined(PH4) && !defined(PORTH4) +# define PORTH4 PH4 +#elif defined(PORTH4) && !defined(PH4) +# define PH4 PORTH4 +#endif +#if defined(PH5) && !defined(PORTH5) +# define PORTH5 PH5 +#elif defined(PORTH5) && !defined(PH5) +# define PH5 PORTH5 +#endif +#if defined(PH6) && !defined(PORTH6) +# define PORTH6 PH6 +#elif defined(PORTH6) && !defined(PH6) +# define PH6 PORTH6 +#endif +#if defined(PH7) && !defined(PORTH7) +# define PORTH7 PH7 +#elif defined(PORTH7) && !defined(PH7) +# define PH7 PORTH7 +#endif + +/* PORT J */ + +#if defined(PJ0) && !defined(PORTJ0) +# define PORTJ0 PJ0 +#elif defined(PORTJ0) && !defined(PJ0) +# define PJ0 PORTJ0 +#endif +#if defined(PJ1) && !defined(PORTJ1) +# define PORTJ1 PJ1 +#elif defined(PORTJ1) && !defined(PJ1) +# define PJ1 PORTJ1 +#endif +#if defined(PJ2) && !defined(PORTJ2) +# define PORTJ2 PJ2 +#elif defined(PORTJ2) && !defined(PJ2) +# define PJ2 PORTJ2 +#endif +#if defined(PJ3) && !defined(PORTJ3) +# define PORTJ3 PJ3 +#elif defined(PORTJ3) && !defined(PJ3) +# define PJ3 PORTJ3 +#endif +#if defined(PJ4) && !defined(PORTJ4) +# define PORTJ4 PJ4 +#elif defined(PORTJ4) && !defined(PJ4) +# define PJ4 PORTJ4 +#endif +#if defined(PJ5) && !defined(PORTJ5) +# define PORTJ5 PJ5 +#elif defined(PORTJ5) && !defined(PJ5) +# define PJ5 PORTJ5 +#endif +#if defined(PJ6) && !defined(PORTJ6) +# define PORTJ6 PJ6 +#elif defined(PORTJ6) && !defined(PJ6) +# define PJ6 PORTJ6 +#endif +#if defined(PJ7) && !defined(PORTJ7) +# define PORTJ7 PJ7 +#elif defined(PORTJ7) && !defined(PJ7) +# define PJ7 PORTJ7 +#endif + +/* PORT K */ + +#if defined(PK0) && !defined(PORTK0) +# define PORTK0 PK0 +#elif defined(PORTK0) && !defined(PK0) +# define PK0 PORTK0 +#endif +#if defined(PK1) && !defined(PORTK1) +# define PORTK1 PK1 +#elif defined(PORTK1) && !defined(PK1) +# define PK1 PORTK1 +#endif +#if defined(PK2) && !defined(PORTK2) +# define PORTK2 PK2 +#elif defined(PORTK2) && !defined(PK2) +# define PK2 PORTK2 +#endif +#if defined(PK3) && !defined(PORTK3) +# define PORTK3 PK3 +#elif defined(PORTK3) && !defined(PK3) +# define PK3 PORTK3 +#endif +#if defined(PK4) && !defined(PORTK4) +# define PORTK4 PK4 +#elif defined(PORTK4) && !defined(PK4) +# define PK4 PORTK4 +#endif +#if defined(PK5) && !defined(PORTK5) +# define PORTK5 PK5 +#elif defined(PORTK5) && !defined(PK5) +# define PK5 PORTK5 +#endif +#if defined(PK6) && !defined(PORTK6) +# define PORTK6 PK6 +#elif defined(PORTK6) && !defined(PK6) +# define PK6 PORTK6 +#endif +#if defined(PK7) && !defined(PORTK7) +# define PORTK7 PK7 +#elif defined(PORTK7) && !defined(PK7) +# define PK7 PORTK7 +#endif + +/* PORT L */ + +#if defined(PL0) && !defined(PORTL0) +# define PORTL0 PL0 +#elif defined(PORTL0) && !defined(PL0) +# define PL0 PORTL0 +#endif +#if defined(PL1) && !defined(PORTL1) +# define PORTL1 PL1 +#elif defined(PORTL1) && !defined(PL1) +# define PL1 PORTL1 +#endif +#if defined(PL2) && !defined(PORTL2) +# define PORTL2 PL2 +#elif defined(PORTL2) && !defined(PL2) +# define PL2 PORTL2 +#endif +#if defined(PL3) && !defined(PORTL3) +# define PORTL3 PL3 +#elif defined(PORTL3) && !defined(PL3) +# define PL3 PORTL3 +#endif +#if defined(PL4) && !defined(PORTL4) +# define PORTL4 PL4 +#elif defined(PORTL4) && !defined(PL4) +# define PL4 PORTL4 +#endif +#if defined(PL5) && !defined(PORTL5) +# define PORTL5 PL5 +#elif defined(PORTL5) && !defined(PL5) +# define PL5 PORTL5 +#endif +#if defined(PL6) && !defined(PORTL6) +# define PORTL6 PL6 +#elif defined(PORTL6) && !defined(PL6) +# define PL6 PORTL6 +#endif +#if defined(PL7) && !defined(PORTL7) +# define PORTL7 PL7 +#elif defined(PORTL7) && !defined(PL7) +# define PL7 PORTL7 +#endif + +#endif /* _AVR_PORTPINS_H_ */ diff --git a/Make AVR Examples/Chapter02_Programming-AVRs/blinkLED/Makefile b/Make AVR Examples/Chapter02_Programming-AVRs/blinkLED/Makefile new file mode 100644 index 0000000..3d9e544 --- /dev/null +++ b/Make AVR Examples/Chapter02_Programming-AVRs/blinkLED/Makefile @@ -0,0 +1,196 @@ + +##########------------------------------------------------------########## +########## Project-specific Details ########## +########## Check these every time you start a new project ########## +##########------------------------------------------------------########## + +MCU = atmega168p +F_CPU = 1000000UL +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 diff --git a/Make AVR Examples/Chapter02_Programming-AVRs/blinkLED/blinkLED.c b/Make AVR Examples/Chapter02_Programming-AVRs/blinkLED/blinkLED.c new file mode 100644 index 0000000..46a5e54 --- /dev/null +++ b/Make AVR Examples/Chapter02_Programming-AVRs/blinkLED/blinkLED.c @@ -0,0 +1,26 @@ + /* Blinker Demo */ + +// ------- Preamble -------- // +#include /* Defines pins, ports, etc */ +#include /* Functions to waste time */ + + +int main(void) { + + // -------- Inits --------- // + DDRB |= 0b00000001; /* Data Direction Register B: + writing a one to the bit + enables output. */ + + // ------ Event loop ------ // + while (1) { + + PORTB = 0b00000001; /* Turn on first LED bit/pin in PORTB */ + _delay_ms(1000); /* wait */ + + PORTB = 0b00000000; /* Turn off all B pins, including LED */ + _delay_ms(1000); /* wait */ + + } /* End event loop */ + return 0; /* This line is never reached */ +} diff --git a/Make AVR Examples/Chapter02_Programming-AVRs/blinkLED_AVR_style/Makefile b/Make AVR Examples/Chapter02_Programming-AVRs/blinkLED_AVR_style/Makefile new file mode 100644 index 0000000..3d9e544 --- /dev/null +++ b/Make AVR Examples/Chapter02_Programming-AVRs/blinkLED_AVR_style/Makefile @@ -0,0 +1,196 @@ + +##########------------------------------------------------------########## +########## Project-specific Details ########## +########## Check these every time you start a new project ########## +##########------------------------------------------------------########## + +MCU = atmega168p +F_CPU = 1000000UL +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 diff --git a/Make AVR Examples/Chapter02_Programming-AVRs/blinkLED_AVR_style/blinkLED_AVRStyle.c b/Make AVR Examples/Chapter02_Programming-AVRs/blinkLED_AVR_style/blinkLED_AVRStyle.c new file mode 100644 index 0000000..42d2e16 --- /dev/null +++ b/Make AVR Examples/Chapter02_Programming-AVRs/blinkLED_AVR_style/blinkLED_AVRStyle.c @@ -0,0 +1,32 @@ + /* Blinker Demo II */ + +#include +#include + +#define LED PB0 +#define LED_DDR DDRB +#define LED_PORT PORTB + +#define DELAYTIME 200 + +#define setBit(sfr, bit) (_SFR_BYTE(sfr) |= (1 << bit)) +#define clearBit(sfr, bit) (_SFR_BYTE(sfr) &= ~(1 << bit)) +#define toggleBit(sfr, bit) (_SFR_BYTE(sfr) ^= (1 << bit)) + +int main(void) { + + // Init + setBit(LED_DDR, LED); /* set LED pin for output */ + + // Mainloop + while (1) { + + setBit(LED_PORT, LED); + _delay_ms(DELAYTIME); + + clearBit(LED_PORT, LED); + _delay_ms(DELAYTIME); + + } + return 0; /* end mainloop */ +} diff --git a/Make AVR Examples/Chapter03_Digital-Output/povToy/Makefile b/Make AVR Examples/Chapter03_Digital-Output/povToy/Makefile new file mode 100644 index 0000000..3d9e544 --- /dev/null +++ b/Make AVR Examples/Chapter03_Digital-Output/povToy/Makefile @@ -0,0 +1,196 @@ + +##########------------------------------------------------------########## +########## Project-specific Details ########## +########## Check these every time you start a new project ########## +##########------------------------------------------------------########## + +MCU = atmega168p +F_CPU = 1000000UL +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 diff --git a/Make AVR Examples/Chapter03_Digital-Output/povToy/povToy.c b/Make AVR Examples/Chapter03_Digital-Output/povToy/povToy.c new file mode 100644 index 0000000..88410ec --- /dev/null +++ b/Make AVR Examples/Chapter03_Digital-Output/povToy/povToy.c @@ -0,0 +1,34 @@ +// POV toy demo framework // + +// ------- Preamble -------- // +#include +#include + +// -------- Functions --------- // +void POVDisplay(uint8_t oneByte) { + PORTB = oneByte; + _delay_ms(2); +} + +int main(void) { + // -------- Inits --------- // + DDRB = 0xff; /* Set up all of LED pins for output */ + // ------ Event loop ------ // + while (1) { /* mainloop */ + POVDisplay(0b00001110); + POVDisplay(0b00011000); + POVDisplay(0b10111101); + POVDisplay(0b01110110); + POVDisplay(0b00111100); + POVDisplay(0b00111100); + POVDisplay(0b00111100); + POVDisplay(0b01110110); + POVDisplay(0b10111101); + POVDisplay(0b00011000); + POVDisplay(0b00001110); + + PORTB = 0; + _delay_ms(10); + } /* end mainloop */ + return 0; +} diff --git a/Make AVR Examples/Chapter03_Digital-Output/povToy_1up/1up.c b/Make AVR Examples/Chapter03_Digital-Output/povToy_1up/1up.c new file mode 100644 index 0000000..5a1f25a --- /dev/null +++ b/Make AVR Examples/Chapter03_Digital-Output/povToy_1up/1up.c @@ -0,0 +1,59 @@ +/* + POV demo by Patrick Roanhouse +*/ + +#include +#include +#define DELAYTIME 2 /* ms */ + +uint8_t Star1UP[] = { + 0b10000100, + 0b01101100, + 0b01111110, + 0b00011111, + 0b01111110, + 0b01101100, + 0b10000100, + 0b00000000, + 0b00000000, + 0b00000000, + 0b10000010, + 0b11111111, + 0b11111111, + 0b10000000, + 0b00000000, + 0b00000000, + 0b01111111, + 0b11111111, + 0b10000000, + 0b11111111, + 0b01111111, + 0b00000000, + 0b00000000, + 0b11111111, + 0b11111111, + 0b00110011, + 0b00110011, + 0b00011110, + 0b00011110, +}; + + +int main(void) { + uint8_t i; + + DDRB = 0xff; /* all output */ + + while (1) { /* mainloop */ + for (i = 0; i < sizeof(Star1UP); i++) { + /* sizeof(Star1UP) returns the number of bytes in our array, */ + PORTB = Star1UP[i]; + _delay_ms(DELAYTIME); + } + + PORTB = 0; /* blank for gap between repetitions */ + _delay_ms(5 * DELAYTIME); + + } /* end mainloop */ + return 0; +} diff --git a/Make AVR Examples/Chapter03_Digital-Output/povToy_1up/Makefile b/Make AVR Examples/Chapter03_Digital-Output/povToy_1up/Makefile new file mode 100644 index 0000000..3d9e544 --- /dev/null +++ b/Make AVR Examples/Chapter03_Digital-Output/povToy_1up/Makefile @@ -0,0 +1,196 @@ + +##########------------------------------------------------------########## +########## Project-specific Details ########## +########## Check these every time you start a new project ########## +##########------------------------------------------------------########## + +MCU = atmega168p +F_CPU = 1000000UL +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 diff --git a/Make AVR Examples/Chapter03_Digital-Output/povToy_cylonEyes/Makefile b/Make AVR Examples/Chapter03_Digital-Output/povToy_cylonEyes/Makefile new file mode 100644 index 0000000..3d9e544 --- /dev/null +++ b/Make AVR Examples/Chapter03_Digital-Output/povToy_cylonEyes/Makefile @@ -0,0 +1,196 @@ + +##########------------------------------------------------------########## +########## Project-specific Details ########## +########## Check these every time you start a new project ########## +##########------------------------------------------------------########## + +MCU = atmega168p +F_CPU = 1000000UL +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 diff --git a/Make AVR Examples/Chapter03_Digital-Output/povToy_cylonEyes/cylonEyes_POV.c b/Make AVR Examples/Chapter03_Digital-Output/povToy_cylonEyes/cylonEyes_POV.c new file mode 100644 index 0000000..588efa6 --- /dev/null +++ b/Make AVR Examples/Chapter03_Digital-Output/povToy_cylonEyes/cylonEyes_POV.c @@ -0,0 +1,34 @@ +/* + +Cylon eyes actually turns out to be a decent POV routine. +The only change here is in DELAYTIME. + + */ + + +#include /* Defines pins, ports, etc */ +#include /* Functions to waste time */ + +#define DELAYTIME 2 /* milliseconds */ + +int main(void) { + uint8_t i=0; + DDRB = 0b11111111; /* Data Direction Register B: all on */ + + while (1) { + + while (i < 7) { + PORTB = (1 << i); /* illuminate only i'th pin */ + _delay_ms(DELAYTIME); /* wait */ + i = i + 1; /* move to the next LED */ + } + + while (i > 0) { + PORTB = (1 << i); /* illuminate only i'th pin */ + _delay_ms(DELAYTIME); /* wait */ + i = i - 1; /* move to the previous LED */ + } + } + + return 0; +} diff --git a/Make AVR Examples/Chapter03_Digital-Output/povToy_invaders/Makefile b/Make AVR Examples/Chapter03_Digital-Output/povToy_invaders/Makefile new file mode 100644 index 0000000..3d9e544 --- /dev/null +++ b/Make AVR Examples/Chapter03_Digital-Output/povToy_invaders/Makefile @@ -0,0 +1,196 @@ + +##########------------------------------------------------------########## +########## Project-specific Details ########## +########## Check these every time you start a new project ########## +##########------------------------------------------------------########## + +MCU = atmega168p +F_CPU = 1000000UL +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 diff --git a/Make AVR Examples/Chapter03_Digital-Output/povToy_invaders/invaders.c b/Make AVR Examples/Chapter03_Digital-Output/povToy_invaders/invaders.c new file mode 100644 index 0000000..99586a7 --- /dev/null +++ b/Make AVR Examples/Chapter03_Digital-Output/povToy_invaders/invaders.c @@ -0,0 +1,70 @@ +/* + Space Invaders POV demo by Elliot Williams + This version stores the displays in arrays, + loops over their elements in a function. +*/ + +#include +#include +#define DELAYTIME 2 /* ms */ + +#define LED_PORT PORTB +#define LED_DDR DDRB + +uint8_t invaderData1[] = { + 0b01110000, + 0b00011000, + 0b11111101, + 0b10110110, + 0b00111100, + 0b00111100, + 0b00111100, + 0b10110110, + 0b11111101, + 0b00011000, + 0b01110000 +}; + +uint8_t invaderData2[] = { + 0b00001110, + 0b00011000, + 0b10111101, + 0b01110110, + 0b00111100, + 0b00111100, + 0b00111100, + 0b01110110, + 0b10111101, + 0b00011000, + 0b00001110 +}; + + +void pause(void){ + uint8_t i; + for (i=0; i<5; i++){ + _delay_ms(DELAYTIME); + } +} + +void POVDisplay(uint8_t povData[], uint8_t numberRows) { + uint8_t i; + for (i = 0; i < numberRows; ++i) { + LED_PORT = povData[i]; + _delay_ms(DELAYTIME); + } + pause(); +} + +int main(void) { + + LED_DDR = 0xff; /* set all LEDs to output */ + + while (1) { /* mainloop */ + + POVDisplay(invaderData1, sizeof(invaderData1)); + POVDisplay(invaderData2, sizeof(invaderData2)); + + } /* end mainloop */ + return 0; +} diff --git a/Make AVR Examples/Chapter04_Bit-Twiddling/cylonEyes/Makefile b/Make AVR Examples/Chapter04_Bit-Twiddling/cylonEyes/Makefile new file mode 100644 index 0000000..3d9e544 --- /dev/null +++ b/Make AVR Examples/Chapter04_Bit-Twiddling/cylonEyes/Makefile @@ -0,0 +1,196 @@ + +##########------------------------------------------------------########## +########## Project-specific Details ########## +########## Check these every time you start a new project ########## +##########------------------------------------------------------########## + +MCU = atmega168p +F_CPU = 1000000UL +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 diff --git a/Make AVR Examples/Chapter04_Bit-Twiddling/cylonEyes/cylonEyes.c b/Make AVR Examples/Chapter04_Bit-Twiddling/cylonEyes/cylonEyes.c new file mode 100644 index 0000000..d60f293 --- /dev/null +++ b/Make AVR Examples/Chapter04_Bit-Twiddling/cylonEyes/cylonEyes.c @@ -0,0 +1,36 @@ + /* Cylon Eyes */ + +// ------- Preamble -------- // +#include /* Defines pins, ports, etc */ +#include /* Functions to waste time */ + +#define DELAYTIME 85 /* milliseconds */ +#define LED_PORT PORTB +#define LED_PIN PINB +#define LED_DDR DDRB + +int main(void) { + + // -------- Inits --------- // + uint8_t i=0; + LED_DDR = 0xff; /* Data Direction Register B: + all set up for output */ + + // ------ Event loop ------ // + while (1) { + + while (i < 7) { + LED_PORT = (1 << i); /* illuminate only i'th pin */ + _delay_ms(DELAYTIME); /* wait */ + i = i + 1; /* move to the next LED */ + } + + while (i > 0) { + LED_PORT = (1 << i); /* illuminate only i'th pin */ + _delay_ms(DELAYTIME); /* wait */ + i = i - 1; /* move to the previous LED */ + } + + } /* End event loop */ + return 0; +} diff --git a/Make AVR Examples/Chapter04_Bit-Twiddling/cylonEyes_16LFSR/16LFSR.c b/Make AVR Examples/Chapter04_Bit-Twiddling/cylonEyes_16LFSR/16LFSR.c new file mode 100644 index 0000000..64b987a --- /dev/null +++ b/Make AVR Examples/Chapter04_Bit-Twiddling/cylonEyes_16LFSR/16LFSR.c @@ -0,0 +1,71 @@ +// ------- Preamble -------- // +#include +#include + +#define DELAY 100 /* ms */ + +static inline uint8_t LFSR8_step(uint8_t random); +static inline uint16_t LFSR16(uint16_t random); + +int main(void) { + + // -------- Inits --------- // + + DDRB = 0xff; /* output on all LEDs */ + uint16_t random = 123; /* can't init to 0, any other is ok */ + + // ------ Event loop ------ // + while (1) { + + /* Display and output */ + random = LFSR16(random); + PORTB = (random >> 8); + _delay_ms(DELAY); + + } /* End event loop */ + return 0; +} + + +// ----------------- LFSR Routines ---------------- // + +inline uint8_t LFSR8_step(uint8_t random) { + /* + Takes an 8-bit number, takes one step in a () LFSR. + [3, 4, 5, 7] is the set of taps for 8-bits that goes through + the whole cycle before repeating. + + If you're really serious about randomness, you'll want a different + algorithm. In fact, this is a great demo of how "predictable" + the "pseudo-random" sequence is. + + Note that this is not a very efficient way to code this up, + but it's meant mostly for teaching and is plenty fast + because the compiler does an OK job with it. + */ + uint8_t tap1, tap2, tap3, tap4; + uint8_t newBit; + tap1 = 1 & (random >> 3); + tap2 = 1 & (random >> 4); + tap3 = 1 & (random >> 5); + tap4 = 1 & (random >> 7); + + newBit = tap1 ^ tap2 ^ tap3 ^ tap4; + random = ((random << 1) | newBit); + return (random); +} + + +inline uint16_t LFSR16(uint16_t random) { + // 3, 12, 14, 15 are the maximal taps for 16 bits. + uint16_t tap1, tap2, tap3, tap4; + uint16_t newBit; + tap1 = 1 & (random >> 3); + tap2 = 1 & (random >> 12); + tap3 = 1 & (random >> 14); + tap4 = 1 & (random >> 15); + + newBit = tap1 ^ tap2 ^ tap3 ^ tap4; + random = ((random << 1) | newBit); + return (random); +} diff --git a/Make AVR Examples/Chapter04_Bit-Twiddling/cylonEyes_16LFSR/Makefile b/Make AVR Examples/Chapter04_Bit-Twiddling/cylonEyes_16LFSR/Makefile new file mode 100644 index 0000000..3d9e544 --- /dev/null +++ b/Make AVR Examples/Chapter04_Bit-Twiddling/cylonEyes_16LFSR/Makefile @@ -0,0 +1,196 @@ + +##########------------------------------------------------------########## +########## Project-specific Details ########## +########## Check these every time you start a new project ########## +##########------------------------------------------------------########## + +MCU = atmega168p +F_CPU = 1000000UL +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 diff --git a/Make AVR Examples/Chapter04_Bit-Twiddling/cylonEyes_8LFSR/8LFSR.c b/Make AVR Examples/Chapter04_Bit-Twiddling/cylonEyes_8LFSR/8LFSR.c new file mode 100644 index 0000000..aa33648 --- /dev/null +++ b/Make AVR Examples/Chapter04_Bit-Twiddling/cylonEyes_8LFSR/8LFSR.c @@ -0,0 +1,70 @@ +// ------- Preamble -------- // +#include +#include + +#define DELAY 100 /* ms */ + +static inline uint8_t LFSR8_step(uint8_t random); + +int main(void) { + + // -------- Inits --------- // + + DDRB = 0xff; /* output on all LEDs */ + uint8_t random = 1; /* can't init to 0, any other is ok */ + + // ------ Event loop ------ // + while (1) { + + /* Display and output */ + random = LFSR8_step(random); + PORTB = random; + _delay_ms(DELAY); + + } /* End event loop */ + return 0; +} + + +// ----------------- LFSR Routines ---------------- // + +inline uint8_t LFSR8_step(uint8_t random) { + /* + Takes an 8-bit number, takes one step in a () LFSR. + [3, 4, 5, 7] is the set of taps for 8-bits that goes through + the whole cycle before repeating. + + If you're really serious about randomness, you'll want a different + algorithm. In fact, this is a great demo of how "predictable" + the "pseudo-random" sequence is. + + Note that this is not a very efficient way to code this up, + but it's meant mostly for teaching and is plenty fast + because the compiler does an OK job with it. + */ + uint8_t tap1, tap2, tap3, tap4; + uint8_t newBit; + tap1 = 1 & (random >> 3); + tap2 = 1 & (random >> 4); + tap3 = 1 & (random >> 5); + tap4 = 1 & (random >> 7); + + newBit = tap1 ^ tap2 ^ tap3 ^ tap4; + random = ((random << 1) | newBit); + return (random); +} + + +inline uint16_t LFSR16(uint16_t random) { + // 3, 12, 14, 15 are the maximal taps for 16 bits. + uint16_t tap1, tap2, tap3, tap4; + uint16_t newBit; + tap1 = 1 & (random >> 3); + tap2 = 1 & (random >> 12); + tap3 = 1 & (random >> 14); + tap4 = 1 & (random >> 15); + + newBit = tap1 ^ tap2 ^ tap3 ^ tap4; + random = ((random << 1) | newBit); + return (random); +} diff --git a/Make AVR Examples/Chapter04_Bit-Twiddling/cylonEyes_8LFSR/Makefile b/Make AVR Examples/Chapter04_Bit-Twiddling/cylonEyes_8LFSR/Makefile new file mode 100644 index 0000000..3d9e544 --- /dev/null +++ b/Make AVR Examples/Chapter04_Bit-Twiddling/cylonEyes_8LFSR/Makefile @@ -0,0 +1,196 @@ + +##########------------------------------------------------------########## +########## Project-specific Details ########## +########## Check these every time you start a new project ########## +##########------------------------------------------------------########## + +MCU = atmega168p +F_CPU = 1000000UL +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 diff --git a/Make AVR Examples/Chapter04_Bit-Twiddling/cylonEyes_counting/Makefile b/Make AVR Examples/Chapter04_Bit-Twiddling/cylonEyes_counting/Makefile new file mode 100644 index 0000000..3d9e544 --- /dev/null +++ b/Make AVR Examples/Chapter04_Bit-Twiddling/cylonEyes_counting/Makefile @@ -0,0 +1,196 @@ + +##########------------------------------------------------------########## +########## Project-specific Details ########## +########## Check these every time you start a new project ########## +##########------------------------------------------------------########## + +MCU = atmega168p +F_CPU = 1000000UL +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 diff --git a/Make AVR Examples/Chapter04_Bit-Twiddling/cylonEyes_counting/counting.c b/Make AVR Examples/Chapter04_Bit-Twiddling/cylonEyes_counting/counting.c new file mode 100644 index 0000000..018fe6c --- /dev/null +++ b/Make AVR Examples/Chapter04_Bit-Twiddling/cylonEyes_counting/counting.c @@ -0,0 +1,15 @@ +#include /* Defines pins, ports, etc */ +#define F_CPU 1000000UL /* Sets up the chip speed for delay.h */ +#include /* Functions to waste time */ + +#define DELAYTIME 100 + +int main(void) { + DDRB = 0b11111111; + PORTB = 0; + + while (1) { + PORTB = PORTB + 1; + _delay_ms(DELAYTIME); + } +} diff --git a/Make AVR Examples/Chapter04_Bit-Twiddling/cylonEyes_halfStepping/Makefile b/Make AVR Examples/Chapter04_Bit-Twiddling/cylonEyes_halfStepping/Makefile new file mode 100644 index 0000000..3d9e544 --- /dev/null +++ b/Make AVR Examples/Chapter04_Bit-Twiddling/cylonEyes_halfStepping/Makefile @@ -0,0 +1,196 @@ + +##########------------------------------------------------------########## +########## Project-specific Details ########## +########## Check these every time you start a new project ########## +##########------------------------------------------------------########## + +MCU = atmega168p +F_CPU = 1000000UL +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 diff --git a/Make AVR Examples/Chapter04_Bit-Twiddling/cylonEyes_halfStepping/cylonEyes_halfStepping.c b/Make AVR Examples/Chapter04_Bit-Twiddling/cylonEyes_halfStepping/cylonEyes_halfStepping.c new file mode 100644 index 0000000..c1418ee --- /dev/null +++ b/Make AVR Examples/Chapter04_Bit-Twiddling/cylonEyes_halfStepping/cylonEyes_halfStepping.c @@ -0,0 +1,45 @@ +/* Cylon Eyes + Half-stepping turns the next light on for a little bit, + makes the pattern visually smoother, improves overall + cylonity. +*/ + +// ------- Preamble -------- // +#include /* Defines pins, ports, etc */ +#include /* Functions to waste time */ + +#define DELAYTIME 85 /* milliseconds, 50-75 is good for smooth eyes */ + +int main(void) { + + // -------- Inits --------- // + uint8_t i; + DDRB = 0xff; /* Data Direction Register B: + all set up for output */ + + // ------ Event loop ------ // + while (1) { /* mainloop */ + + for (i = 0; i < 7; i++) { /* count i from 0 to 6 */ + PORTB |= (1 << i); /* turn on led i */ + _delay_ms(DELAYTIME); /* wait */ + + PORTB |= (1 << (i + 1)); /* turn the next led on */ + _delay_ms(DELAYTIME / 2); + + PORTB &= ~(1 << i); /* turn off led i */ + } + + for (i = 7; i > 0; i--) { /* count i from 7 to 1 */ + PORTB |= (1 << i); + _delay_ms(DELAYTIME); + + PORTB |= (1 << (i - 1)); /* turn the next led on */ + _delay_ms(DELAYTIME / 2); + + PORTB &= ~(1 << i); /* turn off led i */ + } + + } /* End event loop */ + return 0; +} /* end main */ diff --git a/Make AVR Examples/Chapter04_Bit-Twiddling/cylonEyes_naive/Makefile b/Make AVR Examples/Chapter04_Bit-Twiddling/cylonEyes_naive/Makefile new file mode 100644 index 0000000..3d9e544 --- /dev/null +++ b/Make AVR Examples/Chapter04_Bit-Twiddling/cylonEyes_naive/Makefile @@ -0,0 +1,196 @@ + +##########------------------------------------------------------########## +########## Project-specific Details ########## +########## Check these every time you start a new project ########## +##########------------------------------------------------------########## + +MCU = atmega168p +F_CPU = 1000000UL +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 diff --git a/Make AVR Examples/Chapter04_Bit-Twiddling/cylonEyes_naive/cylonEyes_naive.c b/Make AVR Examples/Chapter04_Bit-Twiddling/cylonEyes_naive/cylonEyes_naive.c new file mode 100644 index 0000000..434fa93 --- /dev/null +++ b/Make AVR Examples/Chapter04_Bit-Twiddling/cylonEyes_naive/cylonEyes_naive.c @@ -0,0 +1,42 @@ +#include /* Defines pins, ports, etc */ +#define F_CPU 1000000UL /* Sets up the chip speed for delay.h */ +#include /* Functions to waste time */ + +#define DELAYTIME 75 /* milliseconds */ + +int main(void) { + + DDRB = 0b11111111; /* Data Direction Register B: + all set up for output */ + + while (1) { + PORTB = 0b00000001; + _delay_ms(DELAYTIME); + PORTB = 0b00000010; + _delay_ms(DELAYTIME); + PORTB = 0b00000100; + _delay_ms(DELAYTIME); + PORTB = 0b00001000; + _delay_ms(DELAYTIME); + PORTB = 0b00010000; + _delay_ms(DELAYTIME); + PORTB = 0b00100000; + _delay_ms(DELAYTIME); + PORTB = 0b01000000; + _delay_ms(DELAYTIME); + PORTB = 0b10000000; + _delay_ms(DELAYTIME); + PORTB = 0b01000000; + _delay_ms(DELAYTIME); + PORTB = 0b00100000; + _delay_ms(DELAYTIME); + PORTB = 0b00010000; + _delay_ms(DELAYTIME); + PORTB = 0b00001000; + _delay_ms(DELAYTIME); + PORTB = 0b00000100; + _delay_ms(DELAYTIME); + PORTB = 0b00000010; + _delay_ms(DELAYTIME); + } +} diff --git a/Make AVR Examples/Chapter04_Bit-Twiddling/cylonEyes_quasiRandomToggle/Makefile b/Make AVR Examples/Chapter04_Bit-Twiddling/cylonEyes_quasiRandomToggle/Makefile new file mode 100644 index 0000000..3d9e544 --- /dev/null +++ b/Make AVR Examples/Chapter04_Bit-Twiddling/cylonEyes_quasiRandomToggle/Makefile @@ -0,0 +1,196 @@ + +##########------------------------------------------------------########## +########## Project-specific Details ########## +########## Check these every time you start a new project ########## +##########------------------------------------------------------########## + +MCU = atmega168p +F_CPU = 1000000UL +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 diff --git a/Make AVR Examples/Chapter04_Bit-Twiddling/cylonEyes_quasiRandomToggle/quasiRandomToggle.c b/Make AVR Examples/Chapter04_Bit-Twiddling/cylonEyes_quasiRandomToggle/quasiRandomToggle.c new file mode 100644 index 0000000..2b0ff82 --- /dev/null +++ b/Make AVR Examples/Chapter04_Bit-Twiddling/cylonEyes_quasiRandomToggle/quasiRandomToggle.c @@ -0,0 +1,30 @@ + /* Cylon Eyes */ + +// ------- Preamble -------- // +#include /* Defines pins, ports, etc */ +#include /* Functions to waste time */ + +#define DELAYTIME 45 /* milliseconds */ +#define LED_PORT PORTB +#define LED_PIN PINB +#define LED_DDR DDRB + +int main(void) { + + // -------- Inits --------- // + uint16_t x = 0x1234; + uint8_t y; + LED_DDR = 0xff; /* Data Direction Register B: + all set up for output */ + + // ------ Event loop ------ // + while (1) { + + x = 2053 * x + 13849; /* "random" number generator */ + y = (x >> 8) & 0b00000111; /* pick three bits from high byte */ + LED_PORT ^= (1 << y); /* toggle one bit */ + _delay_ms(100); + + } /* End event loop */ + return 0; +} diff --git a/Make AVR Examples/Chapter04_Bit-Twiddling/showingOffBits/Makefile b/Make AVR Examples/Chapter04_Bit-Twiddling/showingOffBits/Makefile new file mode 100644 index 0000000..3d9e544 --- /dev/null +++ b/Make AVR Examples/Chapter04_Bit-Twiddling/showingOffBits/Makefile @@ -0,0 +1,196 @@ + +##########------------------------------------------------------########## +########## Project-specific Details ########## +########## Check these every time you start a new project ########## +##########------------------------------------------------------########## + +MCU = atmega168p +F_CPU = 1000000UL +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 diff --git a/Make AVR Examples/Chapter04_Bit-Twiddling/showingOffBits/showingOffBits.c b/Make AVR Examples/Chapter04_Bit-Twiddling/showingOffBits/showingOffBits.c new file mode 100644 index 0000000..2c1a7ca --- /dev/null +++ b/Make AVR Examples/Chapter04_Bit-Twiddling/showingOffBits/showingOffBits.c @@ -0,0 +1,59 @@ + /* Showing off some patterns to practice our bit-twiddling */ + +// ------- Preamble -------- // +#include +#include +#include /* Functions to waste time */ + +#define DELAYTIME 85 /* milliseconds */ +#define LED_PORT PORTB +#define LED_DDR DDRB + +int main(void) { + + uint8_t i; + uint8_t repetitions; + uint8_t whichLED; + uint16_t randomNumber = 0x1234; + + // -------- Inits --------- // + LED_DDR = 0xff; /* all LEDs configured for output */ + // ------ Event loop ------ // + while (1) { + /* Go Left */ + for (i = 0; i < 8; i++) { + LED_PORT |= (1 << i); /* turn on the i'th pin */ + _delay_ms(DELAYTIME); /* wait */ + } + for (i = 0; i < 8; i++) { + LED_PORT &= ~(1 << i); /* turn off the i'th pin */ + _delay_ms(DELAYTIME); /* wait */ + } + _delay_ms(5 * DELAYTIME); /* pause */ + + /* Go Right */ + for (i = 7; i < 255; i--) { + LED_PORT |= (1 << i); /* turn on the i'th pin */ + _delay_ms(DELAYTIME); /* wait */ + } + for (i = 7; i < 255; i--) { + LED_PORT &= ~(1 << i); /* turn off the i'th pin */ + _delay_ms(DELAYTIME); /* wait */ + } + _delay_ms(5 * DELAYTIME); /* pause */ + + /* Toggle "random" bits for a while */ + for (repetitions = 0; repetitions < 75; repetitions++) { + /* "random" number generator */ + randomNumber = 2053 * randomNumber + 13849; + /* low three bits from high byte */ + whichLED = (randomNumber >> 8) & 0b00000111; + LED_PORT ^= (1 << whichLED); /* toggle our LED */ + _delay_ms(DELAYTIME); + } + LED_PORT = 0; /* all LEDs off */ + _delay_ms(5 * DELAYTIME); /* pause */ + + } /* End event loop */ + return 0; /* This line is never reached */ +} diff --git a/Make AVR Examples/Chapter05_Serial-IO/serialLoopback/Makefile b/Make AVR Examples/Chapter05_Serial-IO/serialLoopback/Makefile new file mode 100644 index 0000000..3d9e544 --- /dev/null +++ b/Make AVR Examples/Chapter05_Serial-IO/serialLoopback/Makefile @@ -0,0 +1,196 @@ + +##########------------------------------------------------------########## +########## Project-specific Details ########## +########## Check these every time you start a new project ########## +##########------------------------------------------------------########## + +MCU = atmega168p +F_CPU = 1000000UL +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 diff --git a/Make AVR Examples/Chapter05_Serial-IO/serialLoopback/serialLoopback.c b/Make AVR Examples/Chapter05_Serial-IO/serialLoopback/serialLoopback.c new file mode 100644 index 0000000..014cd96 --- /dev/null +++ b/Make AVR Examples/Chapter05_Serial-IO/serialLoopback/serialLoopback.c @@ -0,0 +1,31 @@ +/* +A simple test of serial-port functionality. +Takes in a character at a time and sends it right back out, + displaying the ASCII value on the LEDs. +*/ + +// ------- Preamble -------- // +#include +#include +#include "pinDefines.h" +#include "USART.h" + +int main(void) { + char serialCharacter; + + // -------- Inits --------- // + LED_DDR = 0xff; /* set up LEDs for output */ + initUSART(); + printString("Hello World!\r\n"); /* to test */ + + // ------ Event loop ------ // + while (1) { + + serialCharacter = receiveByte(); + transmitByte(serialCharacter); + LED_PORT = serialCharacter; + /* display ascii/numeric value of character */ + + } /* End event loop */ + return 0; +} diff --git a/Make AVR Examples/Chapter05_Serial-IO/serialOrgan/Makefile b/Make AVR Examples/Chapter05_Serial-IO/serialOrgan/Makefile new file mode 100644 index 0000000..3d9e544 --- /dev/null +++ b/Make AVR Examples/Chapter05_Serial-IO/serialOrgan/Makefile @@ -0,0 +1,196 @@ + +##########------------------------------------------------------########## +########## Project-specific Details ########## +########## Check these every time you start a new project ########## +##########------------------------------------------------------########## + +MCU = atmega168p +F_CPU = 1000000UL +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 diff --git a/Make AVR Examples/Chapter05_Serial-IO/serialOrgan/autoPlay.py b/Make AVR Examples/Chapter05_Serial-IO/serialOrgan/autoPlay.py new file mode 100644 index 0000000..8b0cadd --- /dev/null +++ b/Make AVR Examples/Chapter05_Serial-IO/serialOrgan/autoPlay.py @@ -0,0 +1,50 @@ +## Scripting in python to drive the serial-port organ + +## So far, the "protocol" is simple. +## Python routine sends a note, waits for a return character, then sends next, etc. +## Organ listens for notes, when it gets one sends an 'N' to say it's ready + +import serial + +def playString(noteString, serialPort): + for letter in noteString: + print(letter) + serialPort.write(letter.encode()) + returnValue = serialPort.read(1) + +if __name__ == "__main__": + + import time + from urllib.request import urlopen + + ## Need to consider alternatives for Mac / Windows + ## list all serial ports being used: python -m serial.tools.list_ports + PORT = "/dev/ttyUSB0" # Change this to the current serial port being used + BAUD = 9600 + + s = serial.Serial(PORT, BAUD) + s.flush() + ## flush clears the buffer so that we're starting fresh + ## More on serial buffers later. + + ## An intentional example. You can use this for playing music on purpose. + playString("f g h j k l ; ]'[", s) + input("Press enter for next demo\n") + + ## A fun / stupid example. You can just type stuff and see what comes out. + playString("hello there, this is a random string turned into 'music'", s) + input("Press enter for next demo\n") + + ## Website no longer alive... skipping: + ## A really frivolous example. Play websites! + ## Bonus points for first person to tweet themselves a song. + #print ("Downloading song data from http://serialorgansongs.jottit.com/...") + #import re + #contentFilter = re.compile(r'

(.*?)

') + #songSite = urlopen("http://serialorgansongs.jottit.com/").read() + #songText = contentFilter.findall(songSite)[0] + #playString(songText, s) + + ## Or interactive + mySong = input("\nType in your own song: ") + playString(mySong, s) diff --git a/Make AVR Examples/Chapter05_Serial-IO/serialOrgan/organ.c b/Make AVR Examples/Chapter05_Serial-IO/serialOrgan/organ.c new file mode 100644 index 0000000..6041a5a --- /dev/null +++ b/Make AVR Examples/Chapter05_Serial-IO/serialOrgan/organ.c @@ -0,0 +1,26 @@ +/* +Simple routines to play notes out to a speaker +*/ + +#include +#include +#include "organ.h" +#include "pinDefines.h" + +void playNote(uint16_t period, uint16_t duration) { + uint16_t elapsed; + uint16_t i; + for (elapsed = 0; elapsed < duration; elapsed += period) { + /* For loop with variable delay selects the pitch */ + for (i = 0; i < period; i++) { + _delay_us(1); + } + SPEAKER_PORT ^= (1 << SPEAKER); + } +} + +void rest(uint16_t duration) { + do { + _delay_us(1); + } while (--duration); +} diff --git a/Make AVR Examples/Chapter05_Serial-IO/serialOrgan/organ.h b/Make AVR Examples/Chapter05_Serial-IO/serialOrgan/organ.h new file mode 100644 index 0000000..a9c853d --- /dev/null +++ b/Make AVR Examples/Chapter05_Serial-IO/serialOrgan/organ.h @@ -0,0 +1,9 @@ + +// ------------- Function prototypes -------------- // + +// Plays a note for the given duration. None of these times are +// calibrated to actual notes or tempi. It's all relative to TIMEBASE. +void playNote(uint16_t period, uint16_t duration); + +// Does nothing for a time equal to the passed duration. +void rest(uint16_t duration); diff --git a/Make AVR Examples/Chapter05_Serial-IO/serialOrgan/scale16.h b/Make AVR Examples/Chapter05_Serial-IO/serialOrgan/scale16.h new file mode 100644 index 0000000..e317746 --- /dev/null +++ b/Make AVR Examples/Chapter05_Serial-IO/serialOrgan/scale16.h @@ -0,0 +1,99 @@ +// Scale in the key of 25000 +// Automatically generated by scaleGenerator.py + +#define C0 25000 +#define Cx0 23597 +#define D0 22272 +#define Dx0 21022 +#define E0 19843 +#define F0 18729 +#define Fx0 17678 +#define G0 16685 +#define Gx0 15749 +#define A0 14865 +#define Ax0 14031 +#define B0 13243 +#define C1 12500 +#define Cx1 11798 +#define D1 11136 +#define Dx1 10511 +#define E1 9921 +#define F1 9364 +#define Fx1 8839 +#define G1 8343 +#define Gx1 7875 +#define A1 7433 +#define Ax1 7015 +#define B1 6622 +#define C2 6250 +#define Cx2 5899 +#define D2 5568 +#define Dx2 5256 +#define E2 4961 +#define F2 4682 +#define Fx2 4419 +#define G2 4171 +#define Gx2 3937 +#define A2 3716 +#define Ax2 3508 +#define B2 3311 +#define C3 3125 +#define Cx3 2950 +#define D3 2784 +#define Dx3 2628 +#define E3 2480 +#define F3 2341 +#define Fx3 2210 +#define G3 2086 +#define Gx3 1969 +#define A3 1858 +#define Ax3 1754 +#define B3 1655 +#define C4 1562 +#define Cx4 1474 +#define D4 1392 +#define Dx4 1313 +#define E4 1240 +#define F4 1170 +#define Fx4 1105 +#define G4 1043 +#define Gx4 984 +#define A4 929 +#define Ax4 877 +#define B4 827 +#define C5 781 +#define Cx5 737 +#define D5 696 +#define Dx5 657 +#define E5 620 +#define F5 585 +#define Fx5 552 +#define G5 521 +#define Gx5 492 +#define A5 464 +#define Ax5 438 +#define B5 414 +#define C6 390 +#define Cx6 368 +#define D6 347 +#define Dx6 328 +#define E6 310 +#define F6 292 +#define Fx6 276 +#define G6 260 +#define Gx6 246 +#define A6 232 +#define Ax6 219 +#define B6 207 +#define C7 195 +#define Cx7 184 +#define D7 174 +#define Dx7 164 +#define E7 155 +#define F7 146 +#define Fx7 138 +#define G7 130 +#define Gx7 123 +#define A7 116 +#define Ax7 109 +#define B7 103 diff --git a/Make AVR Examples/Chapter05_Serial-IO/serialOrgan/scaleGenerator.py b/Make AVR Examples/Chapter05_Serial-IO/serialOrgan/scaleGenerator.py new file mode 100644 index 0000000..f72a4da --- /dev/null +++ b/Make AVR Examples/Chapter05_Serial-IO/serialOrgan/scaleGenerator.py @@ -0,0 +1,46 @@ +# scaleGenerator.py +# Scales are in terms of times per cycle (period) rather +# than pitch. +# + +import math + +SCALE = ['C', 'Cx', 'D', 'Dx', 'E', 'F', 'Fx', 'G', 'Gx', 'A', 'Ax', 'B'] + +def calculateOctave(baseLength): + periods = [baseLength / math.exp(x*math.log(2)/12) for x in range(0, 12)] + periods = [int(round(x)) for x in periods] + return( zip(SCALE, periods) ) + +def makePitches(basePitch, numOctaves): + pitchList = [] + for octave in range(0, numOctaves): + for note, period in calculateOctave(basePitch / 2**octave): + if period < 65500: + noteString = note + str(octave) + pitchList.append((noteString,period)) + return(pitchList) + +def makeDefines(basePitch, numOctaves): + pitchList = makePitches(basePitch, numOctaves) + defineString = "// Scale in the key of {} \n".format(basePitch) + defineString += "// Automatically generated by scaleGenerator.py \n\n" + for (note, length) in pitchList: + defineString += "#define {:<5}{:>6}\n".format(note, length) + return(defineString) + +if __name__ == "__main__": + + ## Change these if you like + BASEPITCH = 25000 + OCTAVES = 8 + OUTFILE = "scale16.h" + + ## Write it out to a file + out = open(OUTFILE, "w") + out.write(makeDefines(BASEPITCH, OCTAVES)) + out.close() + + + + diff --git a/Make AVR Examples/Chapter05_Serial-IO/serialOrgan/serialOrgan.c b/Make AVR Examples/Chapter05_Serial-IO/serialOrgan/serialOrgan.c new file mode 100644 index 0000000..5dcb3b5 --- /dev/null +++ b/Make AVR Examples/Chapter05_Serial-IO/serialOrgan/serialOrgan.c @@ -0,0 +1,74 @@ +/* + +serialOrgan.c + +Reads a character in serial from the keyboard, plays a note. + +See organ.h for pin defines and other macros +See organ.c (and include it in the Makefile) for playNote() and rest() + +*/ + +// ------- Preamble -------- // +#include +#include +#include "organ.h" +#include "scale16.h" +#include "pinDefines.h" +#include "USART.h" + +#define NOTE_DURATION 0xF000 /* determines long note length */ + +int main(void) { + + // -------- Inits --------- // + SPEAKER_DDR |= (1 << SPEAKER); /* speaker for output */ + initUSART(); + printString("----- Serial Organ ------\r\n"); + + char fromCompy; /* used to store serial input */ + uint16_t currentNoteLength = NOTE_DURATION / 2; + const uint8_t keys[] = { 'a', 'w', 's', 'e', 'd', 'f', 't', + 'g', 'y', 'h', 'j', 'i', 'k', 'o', + 'l', 'p', ';', '\'' + }; + const uint16_t notes[] = { G4, Gx4, A4, Ax4, B4, C5, Cx5, + D5, Dx5, E5, F5, Fx5, G5, Gx5, + A5, Ax5, B5, C6 + }; + uint8_t isNote; + uint8_t i; + + // ------ Event loop ------ // + while (1) { + + /* Get Key */ + fromCompy = receiveByte(); /* waits here until there is input */ + transmitByte('N'); /* alert computer we're ready for next note */ + + /* Play Notes */ + isNote = 0; + for (i = 0; i < sizeof(keys); i++) { + if (fromCompy == keys[i]) { /* found match in lookup table */ + playNote(notes[i], currentNoteLength); + isNote = 1; /* record that we've found a note */ + break; /* drop out of for() loop */ + } + } + + /* Handle non-note keys: tempo changes and rests */ + if (!isNote) { + if (fromCompy == '[') { /* code for short note */ + currentNoteLength = NOTE_DURATION / 2; + } + else if (fromCompy == ']') { /* code for long note */ + currentNoteLength = NOTE_DURATION; + } + else { /* unrecognized, just rest */ + rest(currentNoteLength); + } + } + + } /* End event loop */ + return 0; +} diff --git a/Make AVR Examples/Chapter06_Digital-Input/avrMusicBox/Makefile b/Make AVR Examples/Chapter06_Digital-Input/avrMusicBox/Makefile new file mode 100644 index 0000000..3d9e544 --- /dev/null +++ b/Make AVR Examples/Chapter06_Digital-Input/avrMusicBox/Makefile @@ -0,0 +1,196 @@ + +##########------------------------------------------------------########## +########## Project-specific Details ########## +########## Check these every time you start a new project ########## +##########------------------------------------------------------########## + +MCU = atmega168p +F_CPU = 1000000UL +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 diff --git a/Make AVR Examples/Chapter06_Digital-Input/avrMusicBox/avrMusicBox.c b/Make AVR Examples/Chapter06_Digital-Input/avrMusicBox/avrMusicBox.c new file mode 100644 index 0000000..2e1886d --- /dev/null +++ b/Make AVR Examples/Chapter06_Digital-Input/avrMusicBox/avrMusicBox.c @@ -0,0 +1,48 @@ +// Music Box Input Demo + +// ------- Preamble -------- // +#include +#include +#include "organ.h" +#include "scale16.h" +#include "pinDefines.h" + +#define SONG_LENGTH (sizeof(song) / sizeof(uint16_t)) + +int main(void) { + const uint16_t song[] = { + E6, E6, E6, C6, E6, G6, G5, + C6, G5, E5, A5, B5, Ax5, A5, + G5, E6, G6, A6, F6, G6, E6, C6, D6, B5, + C6, G5, E5, A5, B5, Ax5, A5, + G5, E6, G6, A6, F6, G6, E6, C6, D6, B5, + /* etc */ + }; + /* starting at end b/c routine starts by incrementing and then playing + this makes the song start at the beginning after reboot */ + uint8_t whichNote = SONG_LENGTH - 1; + uint8_t wasButtonPressed = 0; + + // -------- Inits --------- // + SPEAKER_DDR |= (1 << SPEAKER); /* speaker for output */ + BUTTON_PORT |= (1 << BUTTON); /* pullup on button */ + + // ------ Event loop ------ // + while (1) { + if (bit_is_clear(BUTTON_PIN, BUTTON)) { + if (!wasButtonPressed) { /* if it's a new press ... */ + whichNote++; /* advance to next note */ + /* but don't run over the end */ + if (whichNote == SONG_LENGTH) { + whichNote = 0; + } + wasButtonPressed = 1; + } + playNote(song[whichNote], 1600); + } + else { + wasButtonPressed = 0; + } + } /* End event loop */ + return 0; +} diff --git a/Make AVR Examples/Chapter06_Digital-Input/avrMusicBox/organ.c b/Make AVR Examples/Chapter06_Digital-Input/avrMusicBox/organ.c new file mode 100644 index 0000000..6041a5a --- /dev/null +++ b/Make AVR Examples/Chapter06_Digital-Input/avrMusicBox/organ.c @@ -0,0 +1,26 @@ +/* +Simple routines to play notes out to a speaker +*/ + +#include +#include +#include "organ.h" +#include "pinDefines.h" + +void playNote(uint16_t period, uint16_t duration) { + uint16_t elapsed; + uint16_t i; + for (elapsed = 0; elapsed < duration; elapsed += period) { + /* For loop with variable delay selects the pitch */ + for (i = 0; i < period; i++) { + _delay_us(1); + } + SPEAKER_PORT ^= (1 << SPEAKER); + } +} + +void rest(uint16_t duration) { + do { + _delay_us(1); + } while (--duration); +} diff --git a/Make AVR Examples/Chapter06_Digital-Input/avrMusicBox/organ.h b/Make AVR Examples/Chapter06_Digital-Input/avrMusicBox/organ.h new file mode 100644 index 0000000..a9c853d --- /dev/null +++ b/Make AVR Examples/Chapter06_Digital-Input/avrMusicBox/organ.h @@ -0,0 +1,9 @@ + +// ------------- Function prototypes -------------- // + +// Plays a note for the given duration. None of these times are +// calibrated to actual notes or tempi. It's all relative to TIMEBASE. +void playNote(uint16_t period, uint16_t duration); + +// Does nothing for a time equal to the passed duration. +void rest(uint16_t duration); diff --git a/Make AVR Examples/Chapter06_Digital-Input/avrMusicBox/scale16.h b/Make AVR Examples/Chapter06_Digital-Input/avrMusicBox/scale16.h new file mode 100644 index 0000000..c71928b --- /dev/null +++ b/Make AVR Examples/Chapter06_Digital-Input/avrMusicBox/scale16.h @@ -0,0 +1,99 @@ +// Scale in the key of 12000 +// Automatically generated by scaleGenerator.py + +#define C0 12000 +#define Cx0 11326 +#define D0 10691 +#define Dx0 10091 +#define E0 9524 +#define F0 8990 +#define Fx0 8485 +#define G0 8009 +#define Gx0 7560 +#define A0 7135 +#define Ax0 6735 +#define B0 6357 +#define C1 6000 +#define Cx1 5663 +#define D1 5345 +#define Dx1 5045 +#define E1 4762 +#define F1 4495 +#define Fx1 4243 +#define G1 4005 +#define Gx1 3780 +#define A1 3568 +#define Ax1 3367 +#define B1 3178 +#define C2 3000 +#define Cx2 2832 +#define D2 2673 +#define Dx2 2523 +#define E2 2381 +#define F2 2247 +#define Fx2 2121 +#define G2 2002 +#define Gx2 1890 +#define A2 1784 +#define Ax2 1684 +#define B2 1589 +#define C3 1500 +#define Cx3 1416 +#define D3 1336 +#define Dx3 1261 +#define E3 1191 +#define F3 1124 +#define Fx3 1061 +#define G3 1001 +#define Gx3 945 +#define A3 892 +#define Ax3 842 +#define B3 795 +#define C4 750 +#define Cx4 708 +#define D4 668 +#define Dx4 631 +#define E4 595 +#define F4 562 +#define Fx4 530 +#define G4 501 +#define Gx4 472 +#define A4 446 +#define Ax4 421 +#define B4 397 +#define C5 375 +#define Cx5 354 +#define D5 334 +#define Dx5 315 +#define E5 298 +#define F5 281 +#define Fx5 265 +#define G5 250 +#define Gx5 236 +#define A5 223 +#define Ax5 210 +#define B5 199 +#define C6 187 +#define Cx6 177 +#define D6 167 +#define Dx6 157 +#define E6 148 +#define F6 140 +#define Fx6 132 +#define G6 125 +#define Gx6 118 +#define A6 111 +#define Ax6 105 +#define B6 99 +#define C7 93 +#define Cx7 88 +#define D7 83 +#define Dx7 78 +#define E7 74 +#define F7 70 +#define Fx7 66 +#define G7 62 +#define Gx7 59 +#define A7 55 +#define Ax7 52 +#define B7 49 diff --git a/Make AVR Examples/Chapter06_Digital-Input/avrMusicBox/scaleGenerator.py b/Make AVR Examples/Chapter06_Digital-Input/avrMusicBox/scaleGenerator.py new file mode 100644 index 0000000..d4bd0cc --- /dev/null +++ b/Make AVR Examples/Chapter06_Digital-Input/avrMusicBox/scaleGenerator.py @@ -0,0 +1,46 @@ +# scaleGenerator.py +# Scales are in terms of times per cycle (period) rather +# than pitch. +# + +import math + +SCALE = ['C', 'Cx', 'D', 'Dx', 'E', 'F', 'Fx', 'G', 'Gx', 'A', 'Ax', 'B'] + +def calculateOctave(baseLength): + periods = [baseLength / math.exp(x*math.log(2)/12) for x in range(0, 12)] + periods = [int(round(x)) for x in periods] + return( zip(SCALE, periods) ) + +def makePitches(basePitch, numOctaves): + pitchList = [] + for octave in range(0, numOctaves): + for note, period in calculateOctave(basePitch / 2**octave): + if period < 65500: + noteString = note + str(octave) + pitchList.append((noteString,period)) + return(pitchList) + +def makeDefines(basePitch, numOctaves): + pitchList = makePitches(basePitch, numOctaves) + defineString = "// Scale in the key of {} \n".format(basePitch) + defineString += "// Automatically generated by scaleGenerator.py \n\n" + for (note, length) in pitchList: + defineString += "#define {:<5}{:>6}\n".format(note, length) + return(defineString) + +if __name__ == "__main__": + + ## Change these if you like + BASEPITCH = 10000 + OCTAVES = 8 + OUTFILE = "scale16.h" + + ## Write it out to a file + out = open(OUTFILE, "w") + out.write(makeDefines(BASEPITCH, OCTAVES)) + out.close() + + + + diff --git a/Make AVR Examples/Chapter06_Digital-Input/bossButton/Makefile b/Make AVR Examples/Chapter06_Digital-Input/bossButton/Makefile new file mode 100644 index 0000000..3d9e544 --- /dev/null +++ b/Make AVR Examples/Chapter06_Digital-Input/bossButton/Makefile @@ -0,0 +1,196 @@ + +##########------------------------------------------------------########## +########## Project-specific Details ########## +########## Check these every time you start a new project ########## +##########------------------------------------------------------########## + +MCU = atmega168p +F_CPU = 1000000UL +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 diff --git a/Make AVR Examples/Chapter06_Digital-Input/bossButton/bossButton.c b/Make AVR Examples/Chapter06_Digital-Input/bossButton/bossButton.c new file mode 100644 index 0000000..654e9fb --- /dev/null +++ b/Make AVR Examples/Chapter06_Digital-Input/bossButton/bossButton.c @@ -0,0 +1,46 @@ +/* +bossButton.c + +As long as we've got a button wired up to the AVR, +might as well have some fun. + +Upload this code to your AVR, run bossButton.py. +Press button. + +*/ + +// ------- Preamble -------- // +#include +#include +#include "pinDefines.h" +#include "USART.h" + +static inline void blinkLED(void) { + LED_PORT = (1 << LED0); + _delay_ms(1000); + LED_PORT &= ~(1 << LED0); +} + + +int main(void) { + + // -------- Inits --------- // + BUTTON_PORT |= (1 << BUTTON); /* input mode, turn on pullup */ + + LED_DDR = (1 << LED0); + blinkLED(); + + initUSART(); + transmitByte('O'); + + // ------ Event loop ------ // + while (1) { + + if (bit_is_clear(BUTTON_PIN, BUTTON)) { + transmitByte('X'); + blinkLED(); + } + + } /* End event loop */ + return 0; +} diff --git a/Make AVR Examples/Chapter06_Digital-Input/bossButton/bossButton.py b/Make AVR Examples/Chapter06_Digital-Input/bossButton/bossButton.py new file mode 100644 index 0000000..c33409a --- /dev/null +++ b/Make AVR Examples/Chapter06_Digital-Input/bossButton/bossButton.py @@ -0,0 +1,31 @@ +## Simple demo +## Sits forever listening to serial port +## When you press button, opens website of your choosing. +## Extend this to many buttons and you'll have a physical +## web-launcher. + +BOSS_SITE = "http://www.cartalk.com/content/boss-redirect" +## or perhaps more topical... +XKCD = "http://xkcd.com/353/" + +SERIAL_PORT = "/dev/ttyUSB0" +BAUD_RATE = 9600 + +import serial +import webbrowser + +sp = serial.Serial(SERIAL_PORT, BAUD_RATE, timeout = 5) +sp.flush() +print ("Boss Button") + +while(1): # Sit and wait forever + response = sp.read(1) # get one byte + if response == "O": + print ("Got OK Byte. Waiting for button press.") + elif response == b"X": + print ("Got Boss Byte! Alarm!") + webbrowser.open(BOSS_SITE) + else: + print ("Got nothing. Still waiting.") + + diff --git a/Make AVR Examples/Chapter06_Digital-Input/debouncer/Makefile b/Make AVR Examples/Chapter06_Digital-Input/debouncer/Makefile new file mode 100644 index 0000000..3d9e544 --- /dev/null +++ b/Make AVR Examples/Chapter06_Digital-Input/debouncer/Makefile @@ -0,0 +1,196 @@ + +##########------------------------------------------------------########## +########## Project-specific Details ########## +########## Check these every time you start a new project ########## +##########------------------------------------------------------########## + +MCU = atmega168p +F_CPU = 1000000UL +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 diff --git a/Make AVR Examples/Chapter06_Digital-Input/debouncer/debouncer.c b/Make AVR Examples/Chapter06_Digital-Input/debouncer/debouncer.c new file mode 100644 index 0000000..a03a9fa --- /dev/null +++ b/Make AVR Examples/Chapter06_Digital-Input/debouncer/debouncer.c @@ -0,0 +1,40 @@ + + +// ------- Preamble -------- // +#include +#include "pinDefines.h" + +#include +#define DEBOUNCE_TIME 1000 /* microseconds */ + +uint8_t debounce(void) { + if (bit_is_clear(BUTTON_PIN, BUTTON)) { /* button is pressed now */ + _delay_us(DEBOUNCE_TIME); + if (bit_is_clear(BUTTON_PIN, BUTTON)) { /* still pressed */ + return (1); + } + } + return 0; +} + +int main(void) { + // -------- Inits --------- // + uint8_t buttonWasPressed=0; /* state */ + BUTTON_PORT |= (1 << BUTTON); /* enable the pullup on the button */ + LED_DDR = (1 << LED0); /* set up LED for output */ + + // ------ Event loop ------ // + while (1) { + if (debounce()) { /* debounced button press */ + if (buttonWasPressed == 0) { /* but wasn't last time through */ + LED_PORT ^= (1 << LED0); /* do whatever */ + buttonWasPressed = 1; /* update the state */ + } + } + else { /* button is not pressed now */ + buttonWasPressed = 0; /* update the state */ + } + + } /* End event loop */ + return 0; /* This line is never reached */ +} diff --git a/Make AVR Examples/Chapter06_Digital-Input/simpleButton/Makefile b/Make AVR Examples/Chapter06_Digital-Input/simpleButton/Makefile new file mode 100644 index 0000000..3d9e544 --- /dev/null +++ b/Make AVR Examples/Chapter06_Digital-Input/simpleButton/Makefile @@ -0,0 +1,196 @@ + +##########------------------------------------------------------########## +########## Project-specific Details ########## +########## Check these every time you start a new project ########## +##########------------------------------------------------------########## + +MCU = atmega168p +F_CPU = 1000000UL +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 diff --git a/Make AVR Examples/Chapter06_Digital-Input/simpleButton/simpleButton.c b/Make AVR Examples/Chapter06_Digital-Input/simpleButton/simpleButton.c new file mode 100644 index 0000000..aecbffe --- /dev/null +++ b/Make AVR Examples/Chapter06_Digital-Input/simpleButton/simpleButton.c @@ -0,0 +1,27 @@ +/* + Demo of the simplest on/off button code + Button connected to PD2 + LEDs connected to PB0..PB7 +*/ + +// ------- Preamble -------- // +#include +#include + +int main(void) { + // -------- Inits --------- // + PORTD |= (1 << PD2); /* initialize pullup resistor on our input pin */ + DDRB = 0xff; /* set up all LEDs for output */ + + // ------ Event loop ------ // + while (1) { + if (bit_is_clear(PIND, PD2)) { /* look for button press */ + /* equivalent to if ((PIND & (1 << PD2)) == 0 ){ */ + PORTB = 0b00111100; /* pressed */ + } + else { /* not pressed */ + PORTB = 0b11000011; + } + } /* End event loop */ + return 0; +} diff --git a/Make AVR Examples/Chapter06_Digital-Input/toggleButton/Makefile b/Make AVR Examples/Chapter06_Digital-Input/toggleButton/Makefile new file mode 100644 index 0000000..3d9e544 --- /dev/null +++ b/Make AVR Examples/Chapter06_Digital-Input/toggleButton/Makefile @@ -0,0 +1,196 @@ + +##########------------------------------------------------------########## +########## Project-specific Details ########## +########## Check these every time you start a new project ########## +##########------------------------------------------------------########## + +MCU = atmega168p +F_CPU = 1000000UL +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 diff --git a/Make AVR Examples/Chapter06_Digital-Input/toggleButton/toggleButton.c b/Make AVR Examples/Chapter06_Digital-Input/toggleButton/toggleButton.c new file mode 100644 index 0000000..b9dbcd5 --- /dev/null +++ b/Make AVR Examples/Chapter06_Digital-Input/toggleButton/toggleButton.c @@ -0,0 +1,28 @@ +/* + Demonstrates using state to detect button presses +*/ + +// ------- Preamble -------- // +#include +#include "pinDefines.h" + +int main(void) { + // -------- Inits --------- // + uint8_t buttonWasPressed; /* state */ + BUTTON_PORT |= (1 << BUTTON); /* enable the pullup on the button */ + LED_DDR = (1 << LED0); /* set up LED for output */ + + // ------ Event loop ------ // + while (1) { + if (bit_is_clear(BUTTON_PIN, BUTTON)) { /* button is pressed now */ + if (buttonWasPressed == 0) { /* but wasn't last time through */ + LED_PORT ^= (1 << LED0); /* do whatever */ + buttonWasPressed = 1; /* update the state */ + } + } + else { /* button is not pressed now */ + buttonWasPressed = 0; /* update the state */ + } + } /* End event loop */ + return 0; /* This line is never reached */ +} diff --git a/Make AVR Examples/Chapter06_Digital-Input/toggleButton_debounced/Makefile b/Make AVR Examples/Chapter06_Digital-Input/toggleButton_debounced/Makefile new file mode 100644 index 0000000..3d9e544 --- /dev/null +++ b/Make AVR Examples/Chapter06_Digital-Input/toggleButton_debounced/Makefile @@ -0,0 +1,196 @@ + +##########------------------------------------------------------########## +########## Project-specific Details ########## +########## Check these every time you start a new project ########## +##########------------------------------------------------------########## + +MCU = atmega168p +F_CPU = 1000000UL +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 diff --git a/Make AVR Examples/Chapter06_Digital-Input/toggleButton_debounced/toggleButton_debounced.c b/Make AVR Examples/Chapter06_Digital-Input/toggleButton_debounced/toggleButton_debounced.c new file mode 100644 index 0000000..d1b3d99 --- /dev/null +++ b/Make AVR Examples/Chapter06_Digital-Input/toggleButton_debounced/toggleButton_debounced.c @@ -0,0 +1,42 @@ +/* + Demonstrates using state to detect button presses +*/ + +// ------- Preamble -------- // +#include +#include "pinDefines.h" + +#include +#define DEBOUNCE_TIME 5 /* microseconds */ + +uint8_t debouncePress(void) { + if (bit_is_clear(BUTTON_PIN, BUTTON)) { /* button is pressed now */ + _delay_ms(DEBOUNCE_TIME); + if (bit_is_clear(BUTTON_PIN, BUTTON)) { /* still pressed */ + return (1); + } + } + return 0; +} + +int main(void) { + // -------- Inits --------- // + uint8_t buttonWasPressed=0; /* state */ + BUTTON_PORT |= (1 << BUTTON); /* enable the pullup on the button */ + LED_DDR = (1 << LED0); /* set up LED for output */ + + // ------ Event loop ------ // + while (1) { + if (debouncePress()) { + if (buttonWasPressed == 0) { /* but wasn't last time through */ + LED_PORT ^= (1 << LED0); /* do whatever */ + buttonWasPressed = 1; /* update the state */ + } + } + else { /* button is not pressed now */ + buttonWasPressed = 0; /* update the state */ + } + + } /* End event loop */ + return 0; /* This line is never reached */ +} diff --git a/Make AVR Examples/Chapter07_Analog-to-Digital-Conversion-I/lightSensor/.gitignore b/Make AVR Examples/Chapter07_Analog-to-Digital-Conversion-I/lightSensor/.gitignore new file mode 100644 index 0000000..33cb8f6 --- /dev/null +++ b/Make AVR Examples/Chapter07_Analog-to-Digital-Conversion-I/lightSensor/.gitignore @@ -0,0 +1 @@ +lightSensor_50Hz.c diff --git a/Make AVR Examples/Chapter07_Analog-to-Digital-Conversion-I/lightSensor/Makefile b/Make AVR Examples/Chapter07_Analog-to-Digital-Conversion-I/lightSensor/Makefile new file mode 100644 index 0000000..3d9e544 --- /dev/null +++ b/Make AVR Examples/Chapter07_Analog-to-Digital-Conversion-I/lightSensor/Makefile @@ -0,0 +1,196 @@ + +##########------------------------------------------------------########## +########## Project-specific Details ########## +########## Check these every time you start a new project ########## +##########------------------------------------------------------########## + +MCU = atmega168p +F_CPU = 1000000UL +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 diff --git a/Make AVR Examples/Chapter07_Analog-to-Digital-Conversion-I/lightSensor/lightSensor.c b/Make AVR Examples/Chapter07_Analog-to-Digital-Conversion-I/lightSensor/lightSensor.c new file mode 100644 index 0000000..6136b1e --- /dev/null +++ b/Make AVR Examples/Chapter07_Analog-to-Digital-Conversion-I/lightSensor/lightSensor.c @@ -0,0 +1,41 @@ +// Quick Demo of light sensor + +// ------- Preamble -------- // +#include +#include +#include "pinDefines.h" + +// -------- Functions --------- // +static inline void initADC0(void) { + ADMUX |= (1 << REFS0); /* reference voltage on AVCC */ + ADCSRA |= (1 << ADPS2); /* ADC clock prescaler /16 */ + ADCSRA |= (1 << ADEN); /* enable ADC */ +} + +int main(void) { + + // -------- Inits --------- // + uint8_t ledValue; + uint16_t adcValue; + uint8_t i; + + initADC0(); + LED_DDR = 0xff; + + // ------ Event loop ------ // + while (1) { + + ADCSRA |= (1 << ADSC); /* start ADC conversion */ + loop_until_bit_is_clear(ADCSRA, ADSC); /* wait until done */ + adcValue = ADC; /* read ADC in */ + /* Have 10 bits, want 3 (eight LEDs after all) */ + ledValue = (adcValue >> 7); + /* Light up all LEDs up to ledValue */ + LED_PORT = 0; + for (i = 0; i <= ledValue; i++) { + LED_PORT |= (1 << i); + } + _delay_ms(50); + } /* End event loop */ + return 0; /* This line is never reached */ +} diff --git a/Make AVR Examples/Chapter07_Analog-to-Digital-Conversion-I/nightLight/Makefile b/Make AVR Examples/Chapter07_Analog-to-Digital-Conversion-I/nightLight/Makefile new file mode 100644 index 0000000..3d9e544 --- /dev/null +++ b/Make AVR Examples/Chapter07_Analog-to-Digital-Conversion-I/nightLight/Makefile @@ -0,0 +1,196 @@ + +##########------------------------------------------------------########## +########## Project-specific Details ########## +########## Check these every time you start a new project ########## +##########------------------------------------------------------########## + +MCU = atmega168p +F_CPU = 1000000UL +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 diff --git a/Make AVR Examples/Chapter07_Analog-to-Digital-Conversion-I/nightLight/nightLight.c b/Make AVR Examples/Chapter07_Analog-to-Digital-Conversion-I/nightLight/nightLight.c new file mode 100644 index 0000000..d20f3b6 --- /dev/null +++ b/Make AVR Examples/Chapter07_Analog-to-Digital-Conversion-I/nightLight/nightLight.c @@ -0,0 +1,39 @@ +// Quick and dirty adjustable-threshold night-light. + +// ------- Preamble -------- // +#include +#include +#include "pinDefines.h" + +uint16_t readADC(uint8_t channel) { + ADMUX = (0xf0 & ADMUX) | channel; + ADCSRA |= (1 << ADSC); + loop_until_bit_is_clear(ADCSRA, ADSC); + return (ADC); +} + +int main(void) { + // -------- Inits --------- // + uint16_t lightThreshold; + uint16_t sensorValue; + // Set up ADC + ADMUX |= (1 << REFS0); /* reference voltage on AVCC */ + ADCSRA |= (1 << ADPS1) | (1 << ADPS0); /* ADC clock prescaler /8 */ + ADCSRA |= (1 << ADEN); /* enable ADC */ + + LED_DDR = 0xff; + // ------ Event loop ------ // + while (1) { + + lightThreshold = readADC(POT); + sensorValue = readADC(LIGHT_SENSOR); + + if (sensorValue < lightThreshold) { + LED_PORT = 0xff; + } + else { + LED_PORT = 0x00; + } + } /* End event loop */ + return 0; /* This line is never reached */ +} diff --git a/Make AVR Examples/Chapter07_Analog-to-Digital-Conversion-I/slowScope/Makefile b/Make AVR Examples/Chapter07_Analog-to-Digital-Conversion-I/slowScope/Makefile new file mode 100644 index 0000000..3d9e544 --- /dev/null +++ b/Make AVR Examples/Chapter07_Analog-to-Digital-Conversion-I/slowScope/Makefile @@ -0,0 +1,196 @@ + +##########------------------------------------------------------########## +########## Project-specific Details ########## +########## Check these every time you start a new project ########## +##########------------------------------------------------------########## + +MCU = atmega168p +F_CPU = 1000000UL +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 diff --git a/Make AVR Examples/Chapter07_Analog-to-Digital-Conversion-I/slowScope/serialScope.py b/Make AVR Examples/Chapter07_Analog-to-Digital-Conversion-I/slowScope/serialScope.py new file mode 100644 index 0000000..3f8828b --- /dev/null +++ b/Make AVR Examples/Chapter07_Analog-to-Digital-Conversion-I/slowScope/serialScope.py @@ -0,0 +1,35 @@ +import serial + +def readValue(serialPort): + return(ord(serialPort.read(1))) + +def plotValue(value): + """ Displays the value on a scaled scrolling bargraph""" + leadingSpaces = "-" * int(value*(SCREEN_WIDTH-3) / 255) + print(f"{leadingSpaces} {value:03}") + +def cheapoScope(serialPort): + while(1): + newValue = readValue(serialPort) + plotValue(newValue) + + +if __name__ == "__main__": + ## list all serial ports being used: python -m serial.tools.list_ports + PORT = '/dev/ttyUSB0' # update to whatever port is listed in serial.tools.list_ports + BAUDRATE = 9600 + TIMEOUT = None + SCREEN_WIDTH = 80 + + ## Take command-line arguments to override defaults above + import sys + if len(sys.argv) == 3: + port = sys.argv[1] + baudrate = int(sys.argv[2]) + else: # nothing passed, use defaults + print ("Optional arguments port, baudrate set to defaults.") + port, baudrate = (PORT, BAUDRATE) + + serialPort = serial.Serial(port, baudrate, timeout=TIMEOUT) + serialPort.flush() + cheapoScope(serialPort) diff --git a/Make AVR Examples/Chapter07_Analog-to-Digital-Conversion-I/slowScope/slowScope.c b/Make AVR Examples/Chapter07_Analog-to-Digital-Conversion-I/slowScope/slowScope.c new file mode 100644 index 0000000..991892a --- /dev/null +++ b/Make AVR Examples/Chapter07_Analog-to-Digital-Conversion-I/slowScope/slowScope.c @@ -0,0 +1,33 @@ +// Slow-scope. A free-running AVR / ADC "oscilloscope" + +// ------- Preamble -------- // +#include +#include +#include "pinDefines.h" +#include "USART.h" + +#define SAMPLE_DELAY 20 /* ms, controls the scroll-speed of the scope */ + +// -------- Functions --------- // +static inline void initFreerunningADC(void) { + ADMUX |= (1 << REFS0); /* reference voltage on AVCC */ + ADCSRA |= (1 << ADPS1) | (1 << ADPS0); /* ADC clock prescaler /8 */ + + ADMUX |= (1 << ADLAR); /* left-adjust result, return only 8 bits */ + + ADCSRA |= (1 << ADEN); /* enable ADC */ + ADCSRA |= (1 << ADATE); /* auto-trigger enable */ + ADCSRA |= (1 << ADSC); /* start first conversion */ +} + +int main(void) { + // -------- Inits --------- // + initUSART(); + initFreerunningADC(); + // ------ Event loop ------ // + while (1) { + transmitByte(ADCH); /* transmit the high byte, left-adjusted */ + _delay_ms(SAMPLE_DELAY); + } /* End event loop */ + return 0; /* This line is never reached */ +} diff --git a/Make AVR Examples/Chapter08_Hardware-Interrupts/capSense/Makefile b/Make AVR Examples/Chapter08_Hardware-Interrupts/capSense/Makefile new file mode 100644 index 0000000..5c588dc --- /dev/null +++ b/Make AVR Examples/Chapter08_Hardware-Interrupts/capSense/Makefile @@ -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 diff --git a/Make AVR Examples/Chapter08_Hardware-Interrupts/capSense/capSense.c b/Make AVR Examples/Chapter08_Hardware-Interrupts/capSense/capSense.c new file mode 100644 index 0000000..c90f657 --- /dev/null +++ b/Make AVR Examples/Chapter08_Hardware-Interrupts/capSense/capSense.c @@ -0,0 +1,67 @@ +/* + Capacitive touch sensor demo +*/ + +#include +#include +#include +#include +#include "pinDefines.h" +#include "USART.h" + +#define SENSE_TIME 50 +#define THRESHOLD 12000 + +// ------- Global Variables ---------- // +volatile uint16_t chargeCycleCount; + +// ------- Functions -------- // + +void initPinChangeInterrupt(void) { + PCICR |= (1 << PCIE1); /* enable Pin-change interrupts 1 (bank C) */ + PCMSK1 |= (1 << PC1); /* enable specific interrupt for our pin PC1 */ +} + +ISR(PCINT1_vect) { + chargeCycleCount++; /* count this change */ + + CAP_SENSOR_DDR |= (1 << CAP_SENSOR); /* output mode */ + _delay_us(1); /* charging delay */ + + CAP_SENSOR_DDR &= ~(1 << CAP_SENSOR); /* set as input */ + PCIFR |= (1 << PCIF1); /* clear the pin-change interrupt */ +} + + +int main(void) { + // -------- Inits --------- // + clock_prescale_set(clock_div_1); /* full speed */ + initUSART(); + printString("==[ Cap Sensor ]==\r\n\r\n"); + + LED_DDR = 0xff; + MCUCR |= (1 << PUD); /* disable all pullups */ + CAP_SENSOR_PORT |= (1 << CAP_SENSOR); /* we can leave output high */ + + initPinChangeInterrupt(); + + // ------ Event loop ------ // + while (1) { + + chargeCycleCount = 0; /* reset counter */ + CAP_SENSOR_DDR |= (1 << CAP_SENSOR); /* start with cap charged */ + sei(); /* start up interrupts, counting */ + _delay_ms(SENSE_TIME); + cli(); /* done */ + if (chargeCycleCount < THRESHOLD) { + LED_PORT = 0xff; + } + else { + LED_PORT = 0; + } + printWord(chargeCycleCount); /* for fine tuning */ + printString("\r\n"); + + } /* End event loop */ + return 0; /* This line is never reached */ +} diff --git a/Make AVR Examples/Chapter08_Hardware-Interrupts/capSense/serialScope.py b/Make AVR Examples/Chapter08_Hardware-Interrupts/capSense/serialScope.py new file mode 100644 index 0000000..3f8828b --- /dev/null +++ b/Make AVR Examples/Chapter08_Hardware-Interrupts/capSense/serialScope.py @@ -0,0 +1,35 @@ +import serial + +def readValue(serialPort): + return(ord(serialPort.read(1))) + +def plotValue(value): + """ Displays the value on a scaled scrolling bargraph""" + leadingSpaces = "-" * int(value*(SCREEN_WIDTH-3) / 255) + print(f"{leadingSpaces} {value:03}") + +def cheapoScope(serialPort): + while(1): + newValue = readValue(serialPort) + plotValue(newValue) + + +if __name__ == "__main__": + ## list all serial ports being used: python -m serial.tools.list_ports + PORT = '/dev/ttyUSB0' # update to whatever port is listed in serial.tools.list_ports + BAUDRATE = 9600 + TIMEOUT = None + SCREEN_WIDTH = 80 + + ## Take command-line arguments to override defaults above + import sys + if len(sys.argv) == 3: + port = sys.argv[1] + baudrate = int(sys.argv[2]) + else: # nothing passed, use defaults + print ("Optional arguments port, baudrate set to defaults.") + port, baudrate = (PORT, BAUDRATE) + + serialPort = serial.Serial(port, baudrate, timeout=TIMEOUT) + serialPort.flush() + cheapoScope(serialPort) diff --git a/Make AVR Examples/Chapter08_Hardware-Interrupts/helloInterrupt/Makefile b/Make AVR Examples/Chapter08_Hardware-Interrupts/helloInterrupt/Makefile new file mode 100644 index 0000000..3d9e544 --- /dev/null +++ b/Make AVR Examples/Chapter08_Hardware-Interrupts/helloInterrupt/Makefile @@ -0,0 +1,196 @@ + +##########------------------------------------------------------########## +########## Project-specific Details ########## +########## Check these every time you start a new project ########## +##########------------------------------------------------------########## + +MCU = atmega168p +F_CPU = 1000000UL +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 diff --git a/Make AVR Examples/Chapter08_Hardware-Interrupts/helloInterrupt/helloInterrupt.c b/Make AVR Examples/Chapter08_Hardware-Interrupts/helloInterrupt/helloInterrupt.c new file mode 100644 index 0000000..ab420c8 --- /dev/null +++ b/Make AVR Examples/Chapter08_Hardware-Interrupts/helloInterrupt/helloInterrupt.c @@ -0,0 +1,45 @@ +/* + +Demo of using interrupts for doing what they do best -- +two things at once. + +Flashes LED0 at a fixed rate, interrupting whenever button is pressed. + + */ + +// ------- Preamble -------- // +#include +#include +#include +#include "pinDefines.h" + +ISR(INT0_vect) { /* Run every time there is a change on button */ + if (bit_is_clear(BUTTON_PIN, BUTTON)) { + LED_PORT |= (1 << LED1); + } + else { + LED_PORT &= ~(1 << LED1); + } +} + +void initInterrupt0(void) { + EIMSK |= (1 << INT0); /* enable INT0 */ + EICRA |= (1 << ISC00); /* trigger when button changes */ + sei(); /* set (global) interrupt enable bit */ +} + +int main(void) { + // -------- Inits --------- // + LED_DDR = 0xff; /* all LEDs active */ + BUTTON_PORT |= (1 << BUTTON); /* pullup */ + initInterrupt0(); + + // ------ Event loop ------ // + while (1) { + + _delay_ms(200); + LED_PORT ^= (1 << LED0); + + } /* End event loop */ + return 0; /* This line is never reached */ +} diff --git a/Make AVR Examples/Chapter08_Hardware-Interrupts/helloPinChangeInterrupt/Makefile b/Make AVR Examples/Chapter08_Hardware-Interrupts/helloPinChangeInterrupt/Makefile new file mode 100644 index 0000000..3d9e544 --- /dev/null +++ b/Make AVR Examples/Chapter08_Hardware-Interrupts/helloPinChangeInterrupt/Makefile @@ -0,0 +1,196 @@ + +##########------------------------------------------------------########## +########## Project-specific Details ########## +########## Check these every time you start a new project ########## +##########------------------------------------------------------########## + +MCU = atmega168p +F_CPU = 1000000UL +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 diff --git a/Make AVR Examples/Chapter08_Hardware-Interrupts/helloPinChangeInterrupt/helloPinChangeInterrupt.c b/Make AVR Examples/Chapter08_Hardware-Interrupts/helloPinChangeInterrupt/helloPinChangeInterrupt.c new file mode 100644 index 0000000..16951a5 --- /dev/null +++ b/Make AVR Examples/Chapter08_Hardware-Interrupts/helloPinChangeInterrupt/helloPinChangeInterrupt.c @@ -0,0 +1,39 @@ + /* Demo using pin-change interrupts and in-ISR debouncing routine */ + +// ------- Preamble -------- // +#include +#include +#include +#include "pinDefines.h" + + +ISR(PCINT2_vect) { /* Run every time button state changes */ + if (bit_is_clear(BUTTON_PIN, BUTTON)) { + LED_PORT |= (1 << LED1); + } + else { + LED_PORT &= ~(1 << LED1); + } +} + +void initPinChangeInterrupt18(void) { + PCICR |= (1 << PCIE2); /* set pin-change interrupt for D pins */ + PCMSK2 |= (1 << PCINT18); /* set mask to look for PCINT18 / PD2 */ + sei(); /* set (global) interrupt enable bit */ +} + +int main(void) { + // -------- Inits --------- // + LED_DDR = 0xff; /* all LEDs active */ + BUTTON_PORT |= (1 << BUTTON); /* pullup */ + initPinChangeInterrupt18(); + + // ------ Event loop ------ // + while (1) { + + _delay_ms(200); + LED_PORT ^= (1 << LED0); + + } /* End event loop */ + return 0; /* This line is never reached */ +} diff --git a/Make AVR Examples/Chapter09_Introduction-to-Timer-Counter-Hardware/amRadio/Makefile b/Make AVR Examples/Chapter09_Introduction-to-Timer-Counter-Hardware/amRadio/Makefile new file mode 100644 index 0000000..5c588dc --- /dev/null +++ b/Make AVR Examples/Chapter09_Introduction-to-Timer-Counter-Hardware/amRadio/Makefile @@ -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 diff --git a/Make AVR Examples/Chapter09_Introduction-to-Timer-Counter-Hardware/amRadio/amRadio.c b/Make AVR Examples/Chapter09_Introduction-to-Timer-Counter-Hardware/amRadio/amRadio.c new file mode 100644 index 0000000..f3f341d --- /dev/null +++ b/Make AVR Examples/Chapter09_Introduction-to-Timer-Counter-Hardware/amRadio/amRadio.c @@ -0,0 +1,81 @@ +/* +Plays a simple tune, broadcasts it in the AM radio band. +*/ + +// ------- Preamble -------- // +#include /* Defines pins, ports, etc */ +#include /* Functions to waste time */ +#include +#include +#include "pinDefines.h" +#include "scale16.h" + +#define COUNTER_VALUE 3 /* determines carrier frequency */ + +// From f = f_cpu / ( 2* N* (1 + OCRnx) ) +// Good values for the AM band from 2 to 6: pick one that's clear +// Divide by two b/c we're toggling on or off each loop; +// a full cycle of the carrier takes two loops. +// 8Mhz / (2 * 1 * (1+2)) = 1333 kHz +// 8Mhz / (2 * 1 * (1+3)) = 1000 kHz +// 8Mhz / (2 * 1 * (1+4)) = 800 kHz +// 8Mhz / (2 * 1 * (1+5)) = 670 kHz +// 8Mhz / (2 * 1 * (1+6)) = 570 kHz +// 8Mhz / (2 * 1 * (1+7)) = 500 kHz + +static inline void initTimer0(void) { + TCCR0A |= (1 << WGM01); /* CTC mode */ + TCCR0A |= (1 << COM0B0); /* Toggles pin each time through */ + TCCR0B |= (1 << CS00); /* Clock at CPU frequency, ~8MHz */ + OCR0A = COUNTER_VALUE; /* carrier frequency */ +} + +static inline void initTimer1(void) { + TCCR1B |= (1 << WGM12); /* CTC mode */ + TCCR1B |= (1 << CS11); /* Clock at CPU/8 frequency, ~1MHz */ + TIMSK1 |= (1 << OCIE1A); /* enable output compare interrupt */ +} + +ISR(TIMER1_COMPA_vect) { /* ISR for audio-rate Timer 1 */ + ANTENNA_DDR ^= (1 << ANTENNA); /* toggle carrier on and off */ +} + +static inline void transmitBeep(uint16_t pitch, uint16_t duration) { + OCR1A = pitch; /* set pitch for timer1 */ + sei(); /* turn on interrupts */ + do { + _delay_ms(1); /* delay for pitch cycles */ + duration--; + } while (duration > 0); + cli(); /* and disable ISR so that it stops toggling */ + ANTENNA_DDR |= (1 << ANTENNA); /* back on full carrier */ +} + +int main(void) { + // -------- Inits --------- // + + clock_prescale_set(clock_div_1); /* CPU clock 8 MHz */ + initTimer0(); + initTimer1(); + + // ------ Event loop ------ // + while (1) { + + transmitBeep(E3, 200); + _delay_ms(100); + transmitBeep(E3, 200); + _delay_ms(200); + transmitBeep(E3, 200); + _delay_ms(200); + transmitBeep(C3, 200); + transmitBeep(E3, 200); + _delay_ms(200); + transmitBeep(G3, 400); + _delay_ms(500); + transmitBeep(G2, 400); + + _delay_ms(2500); + + } /* End event loop */ + return 0; /* This line is never reached */ +} diff --git a/Make AVR Examples/Chapter09_Introduction-to-Timer-Counter-Hardware/amRadio/scale16.h b/Make AVR Examples/Chapter09_Introduction-to-Timer-Counter-Hardware/amRadio/scale16.h new file mode 100644 index 0000000..73d968c --- /dev/null +++ b/Make AVR Examples/Chapter09_Introduction-to-Timer-Counter-Hardware/amRadio/scale16.h @@ -0,0 +1,78 @@ +/* Scale in the key of 1/10000 */ + +/* + These are periods -- if you delay this long, + then toggle the speaker pin, you'll get approximate + pitches. + + This is the 16-bit version. The pitches get less accurate + as they get higher, but not as bad as the 8-bit one. + + "x" denotes sharp. + + Can be generated by Python: + + import math + scale = ['C', 'Cx', 'D', 'Dx', 'E', 'F', 'Fx', 'G', 'Gx', 'A', 'Ax', 'B'] + + def octave(baseLength): + periods = [baseLength / math.exp(x*math.log(2)/12) for x in range(0, 12)] + periods = [int(round(x)) for x in periods] + return( zip(scale, periods) ) + + for i in range(0,4): + for note, period in octave(10000 / 2**i): + if period < 65500: + noteString = note + str(i) + print "#define {:<5}{:>6}".format(noteString, period) +*/ + + +#define C0 10000 +#define Cx0 9439 +#define D0 8909 +#define Dx0 8409 +#define E0 7937 +#define F0 7492 +#define Fx0 7071 +#define G0 6674 +#define Gx0 6300 +#define A0 5946 +#define Ax0 5612 +#define B0 5297 +#define C1 5000 +#define Cx1 4719 +#define D1 4454 +#define Dx1 4204 +#define E1 3969 +#define F1 3746 +#define Fx1 3536 +#define G1 3337 +#define Gx1 3150 +#define A1 2973 +#define Ax1 2806 +#define B1 2649 +#define C2 2500 +#define Cx2 2360 +#define D2 2227 +#define Dx2 2102 +#define E2 1984 +#define F2 1873 +#define Fx2 1768 +#define G2 1669 +#define Gx2 1575 +#define A2 1487 +#define Ax2 1403 +#define B2 1324 +#define C3 1250 +#define Cx3 1180 +#define D3 1114 +#define Dx3 1051 +#define E3 992 +#define F3 936 +#define Fx3 884 +#define G3 834 +#define Gx3 787 +#define A3 743 +#define Ax3 702 +#define B3 662 diff --git a/Make AVR Examples/Chapter09_Introduction-to-Timer-Counter-Hardware/reactionTimer/Makefile b/Make AVR Examples/Chapter09_Introduction-to-Timer-Counter-Hardware/reactionTimer/Makefile new file mode 100644 index 0000000..3d9e544 --- /dev/null +++ b/Make AVR Examples/Chapter09_Introduction-to-Timer-Counter-Hardware/reactionTimer/Makefile @@ -0,0 +1,196 @@ + +##########------------------------------------------------------########## +########## Project-specific Details ########## +########## Check these every time you start a new project ########## +##########------------------------------------------------------########## + +MCU = atmega168p +F_CPU = 1000000UL +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 diff --git a/Make AVR Examples/Chapter09_Introduction-to-Timer-Counter-Hardware/reactionTimer/reactionTimer.c b/Make AVR Examples/Chapter09_Introduction-to-Timer-Counter-Hardware/reactionTimer/reactionTimer.c new file mode 100644 index 0000000..c611b28 --- /dev/null +++ b/Make AVR Examples/Chapter09_Introduction-to-Timer-Counter-Hardware/reactionTimer/reactionTimer.c @@ -0,0 +1,68 @@ +/* + Press the button as quickly as you can after the LEDs light up. + Your time is printed out over the serial port. +*/ + +// ------- Preamble -------- // +#include +#include +#include +#include "pinDefines.h" +#include "USART.h" + +#include "support.h" + +static inline void initTimer1(void) { + /* Normal mode (default), just counting */ + TCCR1B |= (1 << CS11) | (1 << CS10); + /* Clock speed: 1 MHz / 64, + each tick is 64 microseconds ~= 15.6 per ms */ + /* No special output modes */ +} + +int main(void) { + uint16_t timerValue; + + // -------- Inits --------- // + + initUSART(); + initTimer1(); + LED_DDR = 0xff; /* all LEDs for output */ + BUTTON_PORT |= (1 << BUTTON); /* enable internal pull-up */ + + printString("\r\nReaction Timer:\r\n"); + printString("---------------\r\n"); + printString("Press any key to start.\r\n"); + + // ------ Event loop ------ // + while (1) { + + receiveByte(); /* press any key */ + printString("\r\nGet ready..."); + randomDelay(); + + printString("\r\nGo!\r\n"); + LED_PORT = 0xff; /* light LEDs */ + TCNT1 = 0; /* reset counter */ + + if (bit_is_clear(BUTTON_PIN, BUTTON)) { + /* Button pressed _exactly_ as LEDs light up. Suspicious. */ + printString("You're only cheating yourself.\r\n"); + } + else { + // Wait until button pressed, save timer value. + loop_until_bit_is_clear(BUTTON_PIN, BUTTON); + timerValue = TCNT1 >> 4; + /* each tick is approx 1/16 milliseconds, so we bit-shift divide */ + + printMilliseconds(timerValue); + printComments(timerValue); + } + + // Clear LEDs and start again. + LED_PORT = 0x00; + printString("Press any key to try again.\r\n"); + + } /* End event loop */ + return 0; /* This line is never reached */ +} diff --git a/Make AVR Examples/Chapter09_Introduction-to-Timer-Counter-Hardware/reactionTimer/support.c b/Make AVR Examples/Chapter09_Introduction-to-Timer-Counter-Hardware/reactionTimer/support.c new file mode 100644 index 0000000..822601f --- /dev/null +++ b/Make AVR Examples/Chapter09_Introduction-to-Timer-Counter-Hardware/reactionTimer/support.c @@ -0,0 +1,92 @@ + +#include "support.h" + +void printMilliseconds(uint16_t value) { + /* Given a value in milliseconds, prints out how many seconds + you took over the serial port. Does ascii conversion, prints + decimal point, and drops extra leading zeros. + */ + uint8_t digit; + + printString("\r\nYou took "); + /* add up ten-thousands */ + digit = 0; + while (value >= 10000) { + digit++; + value -= 10000; + } + if (digit) { + transmitByte('0' + digit); + } + /* add up thousands */ + digit = 0; + while (value >= 1000) { + digit++; + value -= 1000; + } + transmitByte('0' + digit); + + /* decimal point here b/c dividing by 1000 */ + transmitByte('.'); + + /* add up hundreds */ + digit = 0; + while (value >= 100) { + digit++; + value -= 100; + } + transmitByte('0' + digit); + /* add up tens */ + digit = 0; + while (value >= 10) { + digit++; + value -= 10; + } + transmitByte('0' + digit); + + // Ones digit is easy. + transmitByte('0' + value); + + printString(" seconds.\r\n"); +} + +void printComments(uint16_t value) { + /* Given a value in milliseconds, rates your reaction time */ + if (value > 2000) { + printString("----> Ummm...this is a reaction timer...\r\n"); + } + else if (value > 1000) { + printString("----> Hello?\r\n"); + } + else if (value > 500) { + printString("----> Slow.\r\n"); + } + else if (value > 250) { + printString("----> Have another cup of coffee.\r\n"); + } + else if (value > 200) { + printString("----> Respectable.\r\n"); + } + else if (value >= 150) { + printString("----> Fast.\r\n"); + } + else if (value < 150) { + printString("----> Amazing!\r\n"); + } +} + +void randomDelay(void) { + /* Waits for a "random" delay from 1 - 3.5 sec */ + /* Requires timer 1 initialized and running */ + /* It's not really random, but very hard to control -- + like coin-flipping. */ + uint8_t randomTime; + + _delay_ms(1000); /* wait at least 1 sec */ + randomTime = (uint8_t) TCNT1; + /* type-casting the 16-bit TCNT1 as an 8-bit number keeps + only the 8 least-significant (fastest-changing) bits */ + while (--randomTime) { + _delay_ms(10); /* max value is 255 ~= 2.5 sec */ + } +} diff --git a/Make AVR Examples/Chapter09_Introduction-to-Timer-Counter-Hardware/reactionTimer/support.h b/Make AVR Examples/Chapter09_Introduction-to-Timer-Counter-Hardware/reactionTimer/support.h new file mode 100644 index 0000000..5ec669b --- /dev/null +++ b/Make AVR Examples/Chapter09_Introduction-to-Timer-Counter-Hardware/reactionTimer/support.h @@ -0,0 +1,24 @@ + + /* Support functions that otherwise make the main code more readable */ + + /* Includes */ +#include +#include +#include "USART.h" + + /* Function Prototypes in support.c */ + +void printMilliseconds(uint16_t value); + /* Given a value in milliseconds, prints out how many seconds + you took over the serial port. Does ascii conversion, prints + decimal point, and drops extra leading zeros. + */ + +void printComments(uint16_t value); + /* Given a value in milliseconds, rates your reaction time */ + +void randomDelay(void); + /* Waits for a "random" delay from 1 - 3.5 sec */ + /* Requires timer 1 initialized and running */ + /* It's not really random, but very hard to control + like coin-flipping. */ diff --git a/Make AVR Examples/Chapter09_Introduction-to-Timer-Counter-Hardware/timerAudio/Makefile b/Make AVR Examples/Chapter09_Introduction-to-Timer-Counter-Hardware/timerAudio/Makefile new file mode 100644 index 0000000..3d9e544 --- /dev/null +++ b/Make AVR Examples/Chapter09_Introduction-to-Timer-Counter-Hardware/timerAudio/Makefile @@ -0,0 +1,196 @@ + +##########------------------------------------------------------########## +########## Project-specific Details ########## +########## Check these every time you start a new project ########## +##########------------------------------------------------------########## + +MCU = atmega168p +F_CPU = 1000000UL +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 diff --git a/Make AVR Examples/Chapter09_Introduction-to-Timer-Counter-Hardware/timerAudio/scale8.h b/Make AVR Examples/Chapter09_Introduction-to-Timer-Counter-Hardware/timerAudio/scale8.h new file mode 100644 index 0000000..f6c1b3b --- /dev/null +++ b/Make AVR Examples/Chapter09_Introduction-to-Timer-Counter-Hardware/timerAudio/scale8.h @@ -0,0 +1,48 @@ + /* Scale in the key of 1/200 */ + +/* + These are periods -- if you delay this long, + then toggle the speaker pin, you'll get approximate + pitches. + + This is the 8-bit version. The pitches get less accurate + as they get higher. + +*/ + +#define Gx0 252 +#define A0 238 +#define Ax0 224 +#define B0 212 +#define C1 200 +#define Cx0 189 +#define D1 178 +#define Dx0 168 +#define E1 159 +#define F1 150 +#define Fx1 141 +#define G1 133 +#define Gx1 126 +#define A1 119 +#define Ax1 112 +#define B1 106 +#define C2 100 +#define Cx2 94 +#define D2 89 +#define Dx2 84 +#define E2 79 +#define F2 75 +#define Fx2 71 +#define G2 67 +#define Gx2 63 +#define A2 59 +#define Ax2 56 +#define B2 53 +#define C3 50 +#define Cx3 47 +#define D3 44 +#define Dx3 42 +#define E3 40 +#define F3 37 +#define Fx3 35 +#define G3 33 diff --git a/Make AVR Examples/Chapter09_Introduction-to-Timer-Counter-Hardware/timerAudio/timerAudio.c b/Make AVR Examples/Chapter09_Introduction-to-Timer-Counter-Hardware/timerAudio/timerAudio.c new file mode 100644 index 0000000..f348d59 --- /dev/null +++ b/Make AVR Examples/Chapter09_Introduction-to-Timer-Counter-Hardware/timerAudio/timerAudio.c @@ -0,0 +1,49 @@ +/* + +Quick audio demo using Timer 0 to generate audio frequencies directly. + +*/ + +// ------- Preamble -------- // +#include /* Defines pins, ports, etc */ +#include /* Functions to waste time */ +#include "pinDefines.h" +#include "scale8.h" /* 8-bit scale */ + +static inline void initTimer(void) { + TCCR0A |= (1 << WGM01); /* CTC mode */ + TCCR0A |= (1 << COM0A0); /* Toggles pin each cycle through */ + TCCR0B |= (1 << CS00) | (1 << CS01); /* CPU clock / 64 */ +} + +static inline void playNote(uint8_t period, uint16_t duration) { + + TCNT0 = 0; /* reset the counter */ + OCR0A = period; /* set pitch */ + SPEAKER_DDR |= (1 << SPEAKER); /* enable output on speaker */ + + while (duration) { /* Variable delay */ + _delay_ms(1); + duration--; + } + SPEAKER_DDR &= ~(1 << SPEAKER); /* turn speaker off */ +} + +int main(void) { + // -------- Inits --------- // + initTimer(); + // ------ Event loop ------ // + while (1) { + /* Play some notes */ + playNote(C2, 200); + playNote(E2, 200); + playNote(G2, 200); + playNote(C3, 400); + + _delay_ms(1000); + _delay_ms(1000); + _delay_ms(1000); + + } /* End event loop */ + return 0; /* This line is never reached */ +} diff --git a/Make AVR Examples/Chapter10_Pulse-Width-Modulation/bruteForcePWM/Makefile b/Make AVR Examples/Chapter10_Pulse-Width-Modulation/bruteForcePWM/Makefile new file mode 100644 index 0000000..3d9e544 --- /dev/null +++ b/Make AVR Examples/Chapter10_Pulse-Width-Modulation/bruteForcePWM/Makefile @@ -0,0 +1,196 @@ + +##########------------------------------------------------------########## +########## Project-specific Details ########## +########## Check these every time you start a new project ########## +##########------------------------------------------------------########## + +MCU = atmega168p +F_CPU = 1000000UL +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 diff --git a/Make AVR Examples/Chapter10_Pulse-Width-Modulation/bruteForcePWM/pwm.c b/Make AVR Examples/Chapter10_Pulse-Width-Modulation/bruteForcePWM/pwm.c new file mode 100644 index 0000000..1031dcd --- /dev/null +++ b/Make AVR Examples/Chapter10_Pulse-Width-Modulation/bruteForcePWM/pwm.c @@ -0,0 +1,47 @@ +/* + * Quick and dirty PWM Demo + */ + +// ------- Preamble -------- // +#include /* Defines pins, ports, etc */ +#include /* Functions to waste time */ +#include "pinDefines.h" + +#define LED_DELAY 2 + +int main(void) { + + uint8_t brightness = 0; + int8_t direction = 1; + uint8_t i; + + // -------- Inits --------- // + + LED_DDR = 0xff; /* Init all LEDs */ + + // ------ Event loop ------ // + while (1) { + + // PWM + for (i = 0; i < 255; i++) { + if (i < brightness) { + LED_PORT = 0xff; /* turn on */ + } + else { + LED_PORT = 0; /* turn off */ + } + _delay_us(LED_DELAY); + } + + // Brighten and dim + if (brightness == 0) { + direction = 1; + } + if (brightness == 255) { + direction = -1; + } + brightness += direction; + + } /* End event loop */ + return 0; /* This line is never reached */ +} diff --git a/Make AVR Examples/Chapter10_Pulse-Width-Modulation/pwm/Makefile b/Make AVR Examples/Chapter10_Pulse-Width-Modulation/pwm/Makefile new file mode 100644 index 0000000..3d9e544 --- /dev/null +++ b/Make AVR Examples/Chapter10_Pulse-Width-Modulation/pwm/Makefile @@ -0,0 +1,196 @@ + +##########------------------------------------------------------########## +########## Project-specific Details ########## +########## Check these every time you start a new project ########## +##########------------------------------------------------------########## + +MCU = atmega168p +F_CPU = 1000000UL +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 diff --git a/Make AVR Examples/Chapter10_Pulse-Width-Modulation/pwm/pinDefines.h b/Make AVR Examples/Chapter10_Pulse-Width-Modulation/pwm/pinDefines.h new file mode 100644 index 0000000..19eb378 --- /dev/null +++ b/Make AVR Examples/Chapter10_Pulse-Width-Modulation/pwm/pinDefines.h @@ -0,0 +1,93 @@ +// --------------- +// Pin Defines +// --------------- + +#define LED_PORT PORTB +#define LED_PIN PINB +#define LED_DDR DDRB + +#define LED0 PB0 +#define LED1 PB1 +#define LED2 PB2 +#define LED3 PB3 +#define LED4 PB4 +#define LED5 PB5 +#define LED6 PB6 +#define LED7 PB7 + +#define BUTTON PD2 +#define BUTTON_PORT PORTD +#define BUTTON_PIN PIND +#define BUTTON_DDR DDRD + +#define BUTTON2 PD3 +#define BUTTON_PORT PORTD +#define BUTTON_PIN PIND +#define BUTTON_DDR DDRD + +#define BUTTON3 PD4 +#define BUTTON_PORT PORTD +#define BUTTON_PIN PIND +#define BUTTON_DDR DDRD + +#define SPEAKER PD6 /* OC0A */ +#define SPEAKER_PORT PORTD +#define SPEAKER_PIN PIND +#define SPEAKER_DDR DDRD + +#define ANTENNA PD5 /* OC0B */ +#define ANTENNA_PORT PORTD +#define ANTENNA_PIN PIND +#define ANTENNA_DDR DDRD + +#define MODULATION PD3 /* OC2B */ +#define MODULATION_PORT PORTD +#define MODULATION_PIN PIND +#define MODULATION_DDR DDRD + +#define LIGHT_SENSOR PC0 /* ADC0 */ +#define LIGHT_SENSOR_PORT PORTC +#define LIGHT_SENSOR_PIN PINC +#define LIGHT_SENSOR_DDR DDRC + +#define CAP_SENSOR PC1 /* ADC1 */ +#define CAP_SENSOR_PORT PORTC +#define CAP_SENSOR_PIN PINC +#define CAP_SENSOR_DDR DDRC + +#define PIEZO PC2 /* ADC2 */ +#define PIEZO_PORT PORTC +#define PIEZO_PIN PINC +#define PIEZO_DDR DDRC + +// SPI and I2C serial mode defines + +#define SPI_SS PB2 +#define SPI_SS_PORT PORTB +#define SPI_SS_PIN PINB +#define SPI_SS_DDR DDRB + +#define SPI_MOSI PB3 +#define SPI_MOSI_PORT PORTB +#define SPI_MOSI_PIN PINB +#define SPI_MOSI_DDR DDRB + +#define SPI_MISO PB4 +#define SPI_MISO_PORT PORTB +#define SPI_MISO_PIN PINB +#define SPI_MISO_DDR DDRB + +#define SPI_SCK PB5 +#define SPI_SCK_PORT PORTB +#define SPI_SCK_PIN PINB +#define SPI_SCK_DDR DDRB + +#define I2C_SDA PC4 +#define I2C_SDA_PORT PORTC +#define I2C_SDA_PIN PINC +#define I2C_SDA_DDR DDRC + +#define I2C_SCL PC5 +#define I2C_SCL_PORT PORTC +#define I2C_SCL_PIN PINC +#define I2C_SCL_DDR DDRC diff --git a/Make AVR Examples/Chapter10_Pulse-Width-Modulation/pwm/pwm.c b/Make AVR Examples/Chapter10_Pulse-Width-Modulation/pwm/pwm.c new file mode 100644 index 0000000..04edd15 --- /dev/null +++ b/Make AVR Examples/Chapter10_Pulse-Width-Modulation/pwm/pwm.c @@ -0,0 +1,45 @@ + /* Quick and dirty PWM Demo */ + +// ------- Preamble -------- // +#include /* Defines pins, ports, etc */ +#include /* Functions to waste time */ +#include "pinDefines.h" + +#define LED_DELAY 20 /* microseconds */ + +void pwmAllPins(uint8_t brightness) { + uint8_t i; + LED_PORT = 0xff; /* turn on */ + for (i = 0; i < 255; i++) { + if (i >= brightness) { /* once it's been on long enough */ + LED_PORT = 0; /* turn off */ + } + _delay_us(LED_DELAY); + } +} + +int main(void) { + + uint8_t brightness = 0; + int8_t direction = 1; + + // -------- Inits --------- // + + // Init all LEDs + LED_DDR = 0xff; + // ------ Event loop ------ // + while (1) { + // Brighten and dim + if (brightness == 0) { + direction = 1; + } + if (brightness == 255) { + direction = -1; + } + brightness += direction; + pwmAllPins(brightness); + + + } /* End event loop */ + return 0; /* This line is never reached */ +} diff --git a/Make AVR Examples/Chapter10_Pulse-Width-Modulation/pwmOnAnyPin/Makefile b/Make AVR Examples/Chapter10_Pulse-Width-Modulation/pwmOnAnyPin/Makefile new file mode 100644 index 0000000..3d9e544 --- /dev/null +++ b/Make AVR Examples/Chapter10_Pulse-Width-Modulation/pwmOnAnyPin/Makefile @@ -0,0 +1,196 @@ + +##########------------------------------------------------------########## +########## Project-specific Details ########## +########## Check these every time you start a new project ########## +##########------------------------------------------------------########## + +MCU = atmega168p +F_CPU = 1000000UL +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 diff --git a/Make AVR Examples/Chapter10_Pulse-Width-Modulation/pwmOnAnyPin/pwmOnAnyPin.c b/Make AVR Examples/Chapter10_Pulse-Width-Modulation/pwmOnAnyPin/pwmOnAnyPin.c new file mode 100644 index 0000000..729f506 --- /dev/null +++ b/Make AVR Examples/Chapter10_Pulse-Width-Modulation/pwmOnAnyPin/pwmOnAnyPin.c @@ -0,0 +1,59 @@ +// Quick and dirty demo of how to get PWM on any pin with interrupts +// ------- Preamble -------- // +#include +#include +#include +#include "pinDefines.h" + +#define DELAY 3 + +volatile uint8_t brightnessA; +volatile uint8_t brightnessB; + +// -------- Functions --------- // +static inline void initTimer0(void) { + /* must be /64 or more for ISR timing */ + TCCR0B |= (1 << CS01) | (1 << CS00); + /* both output compare interrupts */ + TIMSK0 |= ((1 << OCIE0A) | (1 << OCIE1B)); + TIMSK0 |= (1 << TOIE0); /* overflow interrupt enable */ + sei(); +} + +ISR(TIMER0_OVF_vect) { + LED_PORT = 0xff; + OCR0A = brightnessA; + OCR0B = brightnessB; +} +ISR(TIMER0_COMPA_vect) { + LED_PORT &= 0b11110000; /* turn off low four LEDs */ +} +ISR(TIMER0_COMPB_vect) { + LED_PORT &= 0b00001111; /* turn off high four LEDs */ +} + +int main(void) { + // -------- Inits --------- // + + uint8_t i; + LED_DDR = 0xff; + initTimer0(); + + // ------ Event loop ------ // + while (1) { + + for (i = 0; i < 255; i++) { + _delay_ms(DELAY); + brightnessA = i; + brightnessB = 255 - i; + } + + for (i = 254; i > 0; i--) { + _delay_ms(DELAY); + brightnessA = i; + brightnessB = 255 - i; + } + + } /* End event loop */ + return 0; /* This line is never reached */ +} diff --git a/Make AVR Examples/Chapter10_Pulse-Width-Modulation/pwmTimers/Makefile b/Make AVR Examples/Chapter10_Pulse-Width-Modulation/pwmTimers/Makefile new file mode 100644 index 0000000..3d9e544 --- /dev/null +++ b/Make AVR Examples/Chapter10_Pulse-Width-Modulation/pwmTimers/Makefile @@ -0,0 +1,196 @@ + +##########------------------------------------------------------########## +########## Project-specific Details ########## +########## Check these every time you start a new project ########## +##########------------------------------------------------------########## + +MCU = atmega168p +F_CPU = 1000000UL +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 diff --git a/Make AVR Examples/Chapter10_Pulse-Width-Modulation/pwmTimers/pwmTimers.c b/Make AVR Examples/Chapter10_Pulse-Width-Modulation/pwmTimers/pwmTimers.c new file mode 100644 index 0000000..4c18946 --- /dev/null +++ b/Make AVR Examples/Chapter10_Pulse-Width-Modulation/pwmTimers/pwmTimers.c @@ -0,0 +1,50 @@ + /* PWM Demo with serial control over three LEDs */ + +// ------- Preamble -------- // +#include /* Defines pins, ports, etc */ +#include /* Functions to waste time */ +#include "pinDefines.h" +#include "USART.h" + +static inline void initTimers(void) { + // Timer 1 A,B + TCCR1A |= (1 << WGM10); /* Fast PWM mode, 8-bit */ + TCCR1B |= (1 << WGM12); /* Fast PWM mode, pt.2 */ + TCCR1B |= (1 << CS11); /* PWM Freq = F_CPU/8/256 */ + TCCR1A |= (1 << COM1A1); /* PWM output on OCR1A */ + TCCR1A |= (1 << COM1B1); /* PWM output on OCR1B */ + + // Timer 2 + TCCR2A |= (1 << WGM20); /* Fast PWM mode */ + TCCR2A |= (1 << WGM21); /* Fast PWM mode, pt.2 */ + TCCR2B |= (1 << CS21); /* PWM Freq = F_CPU/8/256 */ + TCCR2A |= (1 << COM2A1); /* PWM output on OCR2A */ +} + +int main(void) { + + uint8_t brightness; + + // -------- Inits --------- // + + initTimers(); + initUSART(); + printString("-- LED PWM Demo --\r\n"); + + /* enable output on LED pins, triggered by PWM hardware */ + LED_DDR |= (1 << LED1); + LED_DDR |= (1 << LED2); + LED_DDR |= (1 << LED3); + + // ------ Event loop ------ // + while (1) { + + printString("\r\nEnter (0-255) for PWM duty cycle: "); + brightness = getNumber(); + OCR2A = OCR1B; + OCR1B = OCR1A; + OCR1A = brightness; + + } /* End event loop */ + return 0; /* This line is never reached */ +} diff --git a/Make AVR Examples/Chapter10_Pulse-Width-Modulation/pwm_cross-fading_cylons/Makefile b/Make AVR Examples/Chapter10_Pulse-Width-Modulation/pwm_cross-fading_cylons/Makefile new file mode 100644 index 0000000..3d9e544 --- /dev/null +++ b/Make AVR Examples/Chapter10_Pulse-Width-Modulation/pwm_cross-fading_cylons/Makefile @@ -0,0 +1,196 @@ + +##########------------------------------------------------------########## +########## Project-specific Details ########## +########## Check these every time you start a new project ########## +##########------------------------------------------------------########## + +MCU = atmega168p +F_CPU = 1000000UL +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 diff --git a/Make AVR Examples/Chapter10_Pulse-Width-Modulation/pwm_cross-fading_cylons/cross-fading_cylons.c b/Make AVR Examples/Chapter10_Pulse-Width-Modulation/pwm_cross-fading_cylons/cross-fading_cylons.c new file mode 100644 index 0000000..439873e --- /dev/null +++ b/Make AVR Examples/Chapter10_Pulse-Width-Modulation/pwm_cross-fading_cylons/cross-fading_cylons.c @@ -0,0 +1,60 @@ +/* + +Cross-fading Cylon Eyes +A PWM demo + +*/ + +// ------- Preamble -------- // +#include /* Defines pins, ports, etc */ +#include /* Functions to waste time */ +#include "pinDefines.h" + + +int main(void) { + + uint8_t thisEye = 0; + uint8_t nextEye = 1; + uint8_t brightness = 0; + int8_t direction = 1; + uint8_t i; + + // -------- Inits --------- // + + // Init all LEDs + LED_DDR = 0xff; + SPEAKER_DDR |= (1 << SPEAKER); + // ------ Event loop ------ // + while (1) { + + // PWM + for (brightness = 0; brightness < 255; brightness++) { + for (i = 0; i < 255; i++) { + if (i < brightness) { + LED_PORT &= ~(1 << thisEye); /* dimming */ + LED_PORT |= (1 << nextEye); /* getting brighter */ + } + else { + LED_PORT |= (1 << thisEye); + LED_PORT &= ~(1 << nextEye); + } + LED_PORT &= ~(1 << thisEye); /* make sure it's off */ + } + } + + // Cylon Scan + thisEye += direction; /* move on to next eye */ + + if (thisEye == 0) { + direction = 1; + } + if (thisEye == 7) { + direction = -1; + } + + nextEye = thisEye + direction; + + + } /* End event loop */ + return 0; /* This line is never reached */ +} diff --git a/Make AVR Examples/Chapter11_Driving-Servo-Motors/servoSundial/Makefile b/Make AVR Examples/Chapter11_Driving-Servo-Motors/servoSundial/Makefile new file mode 100644 index 0000000..5c588dc --- /dev/null +++ b/Make AVR Examples/Chapter11_Driving-Servo-Motors/servoSundial/Makefile @@ -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 diff --git a/Make AVR Examples/Chapter11_Driving-Servo-Motors/servoSundial/_servoClockFunctions.c b/Make AVR Examples/Chapter11_Driving-Servo-Motors/servoSundial/_servoClockFunctions.c new file mode 100644 index 0000000..632bd46 --- /dev/null +++ b/Make AVR Examples/Chapter11_Driving-Servo-Motors/servoSundial/_servoClockFunctions.c @@ -0,0 +1,45 @@ +#include "_servoClockFunctions.h" + +// Realtime-clock handling functions +void initTimer0_Clock(void) { + /* Normal mode, just used for the overflow interrupt */ + TCCR0B |= (1 << CS00); /* 8 MHz clock = ~31250 overflows per second */ + TIMSK0 |= (1 << TOIE0); /* timer overflow interrupt enable */ +} + +void everySecond(void) { + seconds++; + if (seconds > 59) { + seconds = 0; + everyMinute(); + } + LED_PORT ^= (1 << LED0); /* blink */ + printTime(hours, minutes, seconds); /* serial output */ + /* Turn off servo motor after three seconds into new minute */ + if (seconds == 3) { + disableServo(); + } +} +void everyMinute(void) { + minutes++; + if (minutes > 59) { + minutes = 0; + everyHour(); + } + // If during business hours, set servo to new minute + // Otherwise, don't need to move motor when laser is off + if ((hours >= START_TIME) && (hours < STOP_TIME)) { + setServoPosition(); + enableServo(); + LASER_PORT |= (1 << LASER); /* make sure laser is on */ + } + else { /* make sure laser is off */ + LASER_PORT &= ~(1 << LASER); + } +} +void everyHour(void) { + hours++; + if (hours > 23) { /* loop around at end of day */ + hours = 0; + } +} diff --git a/Make AVR Examples/Chapter11_Driving-Servo-Motors/servoSundial/_servoClockFunctions.h b/Make AVR Examples/Chapter11_Driving-Servo-Motors/servoSundial/_servoClockFunctions.h new file mode 100644 index 0000000..3581829 --- /dev/null +++ b/Make AVR Examples/Chapter11_Driving-Servo-Motors/servoSundial/_servoClockFunctions.h @@ -0,0 +1,18 @@ + + +#include "servoSundial.h" +// Note: Global variables (ticks, seconds, minutes, hours) +// are declared in servoSundial.h +// and defined in servoSundial.c + +// Realtime-clock handling functions + +// This sets up the interrupt clock +void initTimer0_Clock(void); + +// These functions are called periodically +// to update the global time variables +// They cascade when needed (second -> minute) +void everySecond(void); +void everyMinute(void); +void everyHour(void); diff --git a/Make AVR Examples/Chapter11_Driving-Servo-Motors/servoSundial/_servoSerialHelpers.c b/Make AVR Examples/Chapter11_Driving-Servo-Motors/servoSundial/_servoSerialHelpers.c new file mode 100644 index 0000000..8760cf4 --- /dev/null +++ b/Make AVR Examples/Chapter11_Driving-Servo-Motors/servoSundial/_servoSerialHelpers.c @@ -0,0 +1,34 @@ + /* Functions for serial port output formatting and input */ +#include "_servoSerialHelpers.h" + +void printTime(uint8_t hours, uint8_t minutes, uint8_t seconds) { + printByte(hours); + transmitByte(':'); + printByte(minutes); + transmitByte(':'); + printByte(seconds); + transmitByte('\r'); + transmitByte('\n'); +} + +void pollSerial(void) { + /* Poll for serial input -- to set the time. */ + char input; + if (bit_is_set(UCSR0A, RXC0)) { + input = UDR0; + if (input == 'S') { /* enter set-time mode */ + printString("Setting time...\r\n"); + printString("Hour: "); + hours = getNumber(); + printString("\r\nMinutes: "); + minutes = getNumber(); + printString("\r\nSeconds: "); + seconds = getNumber(); + printString("\r\n"); + ticks = 0; + if ((hours >= START_TIME) && (hours < STOP_TIME)) { + setServoPosition(); + } + } + } +} diff --git a/Make AVR Examples/Chapter11_Driving-Servo-Motors/servoSundial/_servoSerialHelpers.h b/Make AVR Examples/Chapter11_Driving-Servo-Motors/servoSundial/_servoSerialHelpers.h new file mode 100644 index 0000000..5b86999 --- /dev/null +++ b/Make AVR Examples/Chapter11_Driving-Servo-Motors/servoSundial/_servoSerialHelpers.h @@ -0,0 +1,8 @@ + /* Functions for serial port output formatting and input */ + +#include "servoSundial.h" +// Prints out the time, nicely formatted +void printTime(uint8_t hours, uint8_t minutes, uint8_t seconds); +// Polls for serial input +// sets the time if receives an "S" +void pollSerial(void); diff --git a/Make AVR Examples/Chapter11_Driving-Servo-Motors/servoSundial/calibrateTime.py b/Make AVR Examples/Chapter11_Driving-Servo-Motors/servoSundial/calibrateTime.py new file mode 100644 index 0000000..216207a --- /dev/null +++ b/Make AVR Examples/Chapter11_Driving-Servo-Motors/servoSundial/calibrateTime.py @@ -0,0 +1,86 @@ + + +import time +import serial + +def readTime(serialPort): + '''Reads the time from the AVR over the serial port''' + serialPort.flushInput() + character = "" + while(not character == "\n"): # loop until see end of line + character = serialPort.read(1) + ## The time string looks something like '011:045:023\r\n' + timeString = serialPort.read(13) + hms = timeString.split(":") + hms = [int(x) for x in hms] # make hour, minute, second numeric + return(hms) + +def setTime(serialPort, hours, minutes, seconds): + '''Sends the time over the serial port''' + serialPort.flushOutput() + serialPort.write("S") + time.sleep(0.1) # delay while AVR sends + serialPort.write(str(hours) + "\r") + time.sleep(0.2) # delay while AVR sends + serialPort.write(str(minutes) + "\r") + time.sleep(0.2) # delay while AVR sends + serialPort.write(str(seconds) + "\r") + +def setTimeNow(serialPort): + '''Sets the AVR clock to the current time''' + hours, minutes, seconds = time.localtime()[3:6] + setTime(serialPort, hours, minutes, seconds) + return(time.time()) + +def calculateTimeDelay(serialPort): + '''Gets AVR time and subtracts off actual (computer) time''' + avrHMS = readTime(serialPort) + hms = time.localtime()[3:6] + hmsDifference = [x - y for x,y in zip(avrHMS, hms)] + out = "AVR is fast by: {x[0]} hours, {x[1]} minutes, and {x[2]} seconds" + print out.format(x=hmsDifference) + return(hmsDifference) + +def calculateTimeDrift(serialPort, startTime): + '''Calculates the ratio to multiply OVERFLOWS_PER_SECOND + given a start time and current error''' + h, m, s = calculateTimeDelay(serialPort) + driftSeconds = 60*60*h + 60*m + s + elapsed = time.time() - startTime + print "After {:.0f} seconds, ".format(elapsed) + return (driftSeconds / elapsed + 1) + + +if __name__ == "__main__": + + ## Set time automatically, recording start time, + ## then periodically calculate multiplication factor + OVERFLOWS_PER_SECOND = 31250 # set this to equal the value in your code + + SLEEP_TIME = 10 + ratioLog = [] + + s = serial.Serial("/dev/ttyUSB0", 9600, timeout=5) + print "Setting time to current time...." + ratio = 0 + while not ratio == 1: # make sure starting time is right on + startTime = setTimeNow(s) + ratio = calculateTimeDrift(s, startTime) + + ## Note: you can either leave this running or + ## you can re-run calculateTimeDrift() at any time in the future, + ## as long as you don't overwrite the original startTime + while(True): + ratio = calculateTimeDrift(s, startTime) + ratioLog.append([time.time()-startTime, ratio]) + newOverflow = int(OVERFLOWS_PER_SECOND * ratio) + print "OVERFLOWS_PER_SECOND should be {}\n\n".format(newOverflow) + time.sleep(SLEEP_TIME) + + ## As you leave this routine running, you should see it bounce + ## around a lot in the beginning and then settle down after + ## running a few hours. Ironically, it'll converge to a good + ## number faster if it's initially very out of sync. (If it + ## drifts faster, you can figure out the drift rate sooner.) + ## Leave it running for 24 hours and you'll get one-second-per-day + ## accuracy. diff --git a/Make AVR Examples/Chapter11_Driving-Servo-Motors/servoSundial/justSetTime.py b/Make AVR Examples/Chapter11_Driving-Servo-Motors/servoSundial/justSetTime.py new file mode 100644 index 0000000..f57be49 --- /dev/null +++ b/Make AVR Examples/Chapter11_Driving-Servo-Motors/servoSundial/justSetTime.py @@ -0,0 +1,6 @@ +import serial +import calibrateTime + +s = serial.Serial("/dev/ttyUSB0", 9600, timeout=5) +calibrateTime.setTimeNow(s) +s.close() diff --git a/Make AVR Examples/Chapter11_Driving-Servo-Motors/servoSundial/servoSundial.c b/Make AVR Examples/Chapter11_Driving-Servo-Motors/servoSundial/servoSundial.c new file mode 100644 index 0000000..8c5f26f --- /dev/null +++ b/Make AVR Examples/Chapter11_Driving-Servo-Motors/servoSundial/servoSundial.c @@ -0,0 +1,81 @@ + /* Quasi-realtime-clock with servo sundial. */ + +// ------- Includes -------- // +#include "servoSundial.h" +#include "_servoSerialHelpers.h" +#include "_servoClockFunctions.h" + +// -------- Global Variables --------- // +volatile uint16_t ticks; +volatile uint8_t hours = 15; /* arbitrary default time */ +volatile uint8_t minutes = 42; +volatile uint8_t seconds = 57; + +ISR(TIMER0_OVF_vect) { + /* This is going off very frequently, so we should make it speedy */ + ticks++; +} + +// -------- Functions --------- // +// Servo setup and utility functions +void initTimer1_Servo(void) { + /* Set up Timer1 (16bit) to give a pulse every 20ms */ + TCCR1A |= (1 << WGM11); /* Using Fast PWM mode */ + TCCR1B |= (1 << WGM12); /* counter max in ICR1 */ + TCCR1B |= (1 << WGM13); + TCCR1B |= (1 << CS11); /* /8 prescaling -- microsecond steps */ + TCCR1A |= (1 << COM1A1); /* set output on PB1 / OC1A for servo */ + ICR1 = 20000; /* TOP value = 20ms */ +} + +void enableServo(void) { + while (TCNT1 < PULSE_OVER) {; + } /* delay until pulse part of cycle done */ + SERVO_DDR |= (1 << SERVO); /* enable servo pulses */ +} + +void disableServo(void) { + while (TCNT1 < PULSE_OVER) {; + } /* delay until pulse part of cycle done */ + SERVO_DDR &= ~(1 << SERVO); /* disable servo pulses */ +} + +void setServoPosition(void) { + uint32_t elapsedMinutes; +/* using 32 bits b/c elapsedMinutes * PULSE_RANGE will overflow 16 bits */ + + + + elapsedMinutes = (hours - START_TIME) * 60 + minutes; + OCR1A = PULSE_MIN + elapsedMinutes * PULSE_RANGE / (HOURS_RANGE * 60); + enableServo(); +} + +int main(void) { + + // -------- Inits --------- // + clock_prescale_set(clock_div_1); /* CPU clock 8 MHz */ + initUSART(); + printString("\r\nWelcome to the Servo Sundial.\r\n"); + printString("Type S to set time.\r\n"); + + initTimer0_Clock(); + initTimer1_Servo(); + sei(); /* set enable interrupt bit */ + LED_DDR |= (1 << LED0); /* blinky output */ + LASER_DDR |= (1 << LASER); /* enable laser output */ + + // ------ Event loop ------ // + while (1) { + + /* Poll clock routine */ + if (ticks == OVERFLOWS_PER_SECOND) { + ticks = 0; + everySecond(); + } + + pollSerial(); + + } /* End event loop */ + return 0; /* This line is never reached */ +} diff --git a/Make AVR Examples/Chapter11_Driving-Servo-Motors/servoSundial/servoSundial.h b/Make AVR Examples/Chapter11_Driving-Servo-Motors/servoSundial/servoSundial.h new file mode 100644 index 0000000..72d568c --- /dev/null +++ b/Make AVR Examples/Chapter11_Driving-Servo-Motors/servoSundial/servoSundial.h @@ -0,0 +1,53 @@ + /* Quasi-realtime-clock with servo sundial. */ + +// ------- Includes -------- // +#include +#include +#include +#include +#include "pinDefines.h" +#include "USART.h" + +// Global variables that take care of clock +extern volatile uint16_t ticks; +extern volatile uint8_t hours; /* arbitrary default time */ +extern volatile uint8_t minutes; +extern volatile uint8_t seconds; + +// ------- Defines -------- // +#define PULSE_MIN 1000 /* experiment with these values */ +#define PULSE_MAX 2000 /* to match your own servo */ +#define PULSE_RANGE (PULSE_MAX - PULSE_MIN) +#define PULSE_OVER 3000 /* Must be larger than PULSE_MAX */ + +#define START_TIME 10 /* 10 am */ +#define STOP_TIME 22 /* 10 pm */ +#define HOURS_RANGE (STOP_TIME - START_TIME - 1) + +#define LASER PB2 +#define LASER_PORT PORTB +#define LASER_DDR DDRB + +#define SERVO PB1 +#define SERVO_PORT PORTB +#define SERVO_DDR DDRB + +#define OVERFLOWS_PER_SECOND 31250 /* nominal, should calibrate */ + + +// Serial input and output functions +void pollSerial(void); +void printTime(uint8_t hours, uint8_t minutes, uint8_t seconds); + +// Servo setup and utility functions +void initTimer1_Servo(void); +void enableServo(void); +void disableServo(void); +void setServoPosition(void); + +// Realtime-clock handling functions +// Use the globals ticks, hours, minutes, seconds +void initTimer0_Clock(void); +void everyHour(void); +void everyMinute(void); +void everySecond(void); diff --git a/Make AVR Examples/Chapter11_Driving-Servo-Motors/servoSundial/stepHours.py b/Make AVR Examples/Chapter11_Driving-Servo-Motors/servoSundial/stepHours.py new file mode 100644 index 0000000..5c72e9e --- /dev/null +++ b/Make AVR Examples/Chapter11_Driving-Servo-Motors/servoSundial/stepHours.py @@ -0,0 +1,17 @@ +import serial +import calibrateTime +import time + +s = serial.Serial("/dev/ttyUSB0", 9600, timeout=5) + +for hour in range(11,25): + print "Moving to {}.".format(hour) + calibrateTime.setTime(s, hour-2, 59, 59) + time.sleep(2) + for i in range(0, 60, 5): + calibrateTime.setTime(s, hour-1, i, 00) + time.sleep(0.5) + calibrateTime.setTime(s, hour, 0, 0) + discardThisInput = raw_input("\tpress return to continue\n") + +s.close() diff --git a/Make AVR Examples/Chapter11_Driving-Servo-Motors/servoWorkout/Makefile b/Make AVR Examples/Chapter11_Driving-Servo-Motors/servoWorkout/Makefile new file mode 100644 index 0000000..3d9e544 --- /dev/null +++ b/Make AVR Examples/Chapter11_Driving-Servo-Motors/servoWorkout/Makefile @@ -0,0 +1,196 @@ + +##########------------------------------------------------------########## +########## Project-specific Details ########## +########## Check these every time you start a new project ########## +##########------------------------------------------------------########## + +MCU = atmega168p +F_CPU = 1000000UL +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 diff --git a/Make AVR Examples/Chapter11_Driving-Servo-Motors/servoWorkout/servoWorkout.c b/Make AVR Examples/Chapter11_Driving-Servo-Motors/servoWorkout/servoWorkout.c new file mode 100644 index 0000000..c6d7161 --- /dev/null +++ b/Make AVR Examples/Chapter11_Driving-Servo-Motors/servoWorkout/servoWorkout.c @@ -0,0 +1,94 @@ + /* Quick interactive demo running servo with Timer 1 */ + +// ------- Preamble -------- // +#include +#include +#include + +#include "pinDefines.h" +#include "USART.h" + +#define PULSE_MIN 1000 /* experiment with these values */ +#define PULSE_MAX 2000 /* to match your own servo */ +#define PULSE_MID 1500 + +static inline uint16_t getNumber16(void); + +static inline void initTimer1Servo(void) { + /* Set up Timer1 (16bit) to give a pulse every 20ms */ + /* Use Fast PWM mode, counter max in ICR1 */ + TCCR1A |= (1 << WGM11); + TCCR1B |= (1 << WGM12) | (1 << WGM13); + TCCR1B |= (1 << CS10); /* /1 prescaling -- counting in microseconds */ + ICR1 = 20000; /* TOP value = 20ms */ + TCCR1A |= (1 << COM1A1); /* Direct output on PB1 / OC1A */ + DDRB |= (1 << PB1); /* set pin for output */ +} + +static inline void showOff(void) { + printString("Center\r\n"); + OCR1A = PULSE_MID; + _delay_ms(1500); + printString("Clockwise Max\r\n"); + OCR1A = PULSE_MIN; + _delay_ms(1500); + printString("Counterclockwise Max\r\n"); + OCR1A = PULSE_MAX; + _delay_ms(1500); + printString("Center\r\n"); + OCR1A = PULSE_MID; + _delay_ms(1500); +} + +int main(void) { + + // -------- Inits --------- // + uint16_t servoPulseLength; + OCR1A = PULSE_MID; /* set it to middle position initially */ + initTimer1Servo(); + initUSART(); + printString("\r\nWelcome to the Servo Demo\r\n"); + showOff(); + + // ------ Event loop ------ // + while (1) { + + printString("\r\nEnter a four-digit pulse length:\r\n"); + servoPulseLength = getNumber16(); + + printString("On my way....\r\n"); + OCR1A = servoPulseLength; + DDRB |= (1 << PB1); /* re-enable output pin */ + + _delay_ms(1000); + printString("Releasing...\r\n"); + while (TCNT1 < 3000) {; + } /* delay until pulse part of cycle done */ + DDRB &= ~(1 << PB1); /* disable output pin */ + + } /* End event loop */ + return 0; /* This line is never reached */ +} + +static inline uint16_t getNumber16(void) { + // Gets a PWM value from the serial port. + // Reads in characters, turns them into a number + char thousands = '0'; + char hundreds = '0'; + char tens = '0'; + char ones = '0'; + char thisChar = '0'; + + do { + thousands = hundreds; /* shift numbers over */ + hundreds = tens; + tens = ones; + ones = thisChar; + thisChar = receiveByte(); /* get a new character */ + transmitByte(thisChar); /* echo */ + } while (thisChar != '\r'); + + transmitByte('\n'); /* newline */ + return (1000 * (thousands - '0') + 100 * (hundreds - '0') + + 10 * (tens - '0') + ones - '0'); +} diff --git a/Make AVR Examples/Chapter12_Analog-to-Digital-Conversion-II/footstepDetector/Makefile b/Make AVR Examples/Chapter12_Analog-to-Digital-Conversion-II/footstepDetector/Makefile new file mode 100644 index 0000000..3d9e544 --- /dev/null +++ b/Make AVR Examples/Chapter12_Analog-to-Digital-Conversion-II/footstepDetector/Makefile @@ -0,0 +1,196 @@ + +##########------------------------------------------------------########## +########## Project-specific Details ########## +########## Check these every time you start a new project ########## +##########------------------------------------------------------########## + +MCU = atmega168p +F_CPU = 1000000UL +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 diff --git a/Make AVR Examples/Chapter12_Analog-to-Digital-Conversion-II/footstepDetector/footstepDetector.c b/Make AVR Examples/Chapter12_Analog-to-Digital-Conversion-II/footstepDetector/footstepDetector.c new file mode 100644 index 0000000..270bb36 --- /dev/null +++ b/Make AVR Examples/Chapter12_Analog-to-Digital-Conversion-II/footstepDetector/footstepDetector.c @@ -0,0 +1,98 @@ +/* + * Sensitive footstep-detector and EWMA demo + */ + +// ------- Preamble -------- // +#include +#include +#include +#include "pinDefines.h" +#include "USART.h" + +#define ON_TIME 2000 /* milliseconds */ +#define CYCLE_DELAY 10 /* milliseconds */ +#define INITIAL_PADDING 16 /* higher is less sensitive */ + +#define SWITCH PB7 /* Attach LED or switch relay here */ + +#define USE_POT 0 /* define to 1 if using potentiometer */ +#if USE_POT +#define POT PC5 /* optional padding pot */ +#endif + +// -------- Functions --------- // +void initADC(void) { + ADMUX |= (1 << REFS0); /* reference voltage to AVCC */ + ADCSRA |= (1 << ADPS1) | (1 << ADPS2); /* ADC clock prescaler /64 */ + ADCSRA |= (1 << ADEN); /* enable ADC */ +} + +uint16_t readADC(uint8_t channel) { + ADMUX = (0b11110000 & ADMUX) | channel; + ADCSRA |= (1 << ADSC); + loop_until_bit_is_clear(ADCSRA, ADSC); + return (ADC); +} + +int main(void) { + // -------- Inits --------- // + uint16_t lightsOutTimer = 0; /* timer for the switch */ + uint16_t adcValue; + uint16_t middleValue = 511; + uint16_t highValue = 520; + uint16_t lowValue = 500; + uint16_t noiseVolume = 0; + uint8_t padding = INITIAL_PADDING; + + LED_DDR = ((1 << LED0) | (1 << LED1) | (1 << SWITCH)); + initADC(); + initUSART(); + + // ------ Event loop ------ // + while (1) { + adcValue = readADC(PIEZO); + + /* moving average -- tracks sensor's bias voltage */ + middleValue = adcValue + middleValue - ((middleValue - 8) >> 4); + /* moving averages for positive and negative parts of signal */ + if (adcValue > (middleValue >> 4)) { + highValue = adcValue + highValue - ((highValue - 8) >> 4); + } + if (adcValue < (middleValue >> 4)) { + lowValue = adcValue + lowValue - ((lowValue - 8) >> 4); + } + /* "padding" provides a minimum value for the noise volume */ + noiseVolume = highValue - lowValue + padding; + + /* Now check to see if ADC value above or below thresholds */ + /* Comparison with >> 4 b/c EWMA is on different scale */ + if (adcValue < ((middleValue - noiseVolume) >> 4)) { + LED_PORT = (1 << LED0) | (1 << SWITCH); /* one LED, switch */ + lightsOutTimer = ON_TIME / CYCLE_DELAY; /* reset timer */ + } + else if (adcValue > ((middleValue + noiseVolume) >> 4)) { + LED_PORT = (1 << LED1) | (1 << SWITCH); /* other LED, switch */ + lightsOutTimer = ON_TIME / CYCLE_DELAY; /* reset timer */ + } + else { /* Nothing seen, turn off lights */ + LED_PORT &= ~(1 << LED0); + LED_PORT &= ~(1 << LED1); /* Both off */ + if (lightsOutTimer > 0) { /* time left on timer */ + lightsOutTimer--; + } + else { /* time's up */ + LED_PORT &= ~(1 << SWITCH); /* turn switch off */ + } + } +#if USE_POT /* optional padding potentiometer */ + padding = readADC(POT) >> 4; /* scale down to useful range */ +#endif + /* Serial output and delay */ + /* ADC is 10-bits, recenter around 127 for display purposes */ + transmitByte(adcValue - 512 + 127); + transmitByte((lowValue >> 4) - 512 + 127); + transmitByte((highValue >> 4) - 512 + 127); + _delay_ms(CYCLE_DELAY); + } /* End event loop */ + return 0; /* This line is never reached */ +} diff --git a/Make AVR Examples/Chapter12_Analog-to-Digital-Conversion-II/voltmeter/Makefile b/Make AVR Examples/Chapter12_Analog-to-Digital-Conversion-II/voltmeter/Makefile new file mode 100644 index 0000000..3d9e544 --- /dev/null +++ b/Make AVR Examples/Chapter12_Analog-to-Digital-Conversion-II/voltmeter/Makefile @@ -0,0 +1,196 @@ + +##########------------------------------------------------------########## +########## Project-specific Details ########## +########## Check these every time you start a new project ########## +##########------------------------------------------------------########## + +MCU = atmega168p +F_CPU = 1000000UL +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 diff --git a/Make AVR Examples/Chapter12_Analog-to-Digital-Conversion-II/voltmeter/voltmeter.c b/Make AVR Examples/Chapter12_Analog-to-Digital-Conversion-II/voltmeter/voltmeter.c new file mode 100644 index 0000000..810ad9a --- /dev/null +++ b/Make AVR Examples/Chapter12_Analog-to-Digital-Conversion-II/voltmeter/voltmeter.c @@ -0,0 +1,85 @@ +/* ADC Voltmeter + * Continuously outputs voltage over the serial line. + */ + +// ------- Preamble -------- // +#include +#include +#include +#include /* for ADC sleep mode */ +#include /* for round() and floor() */ + +#include "pinDefines.h" +#include "USART.h" + +/* Note: This voltmeter is only as accurate as your reference voltage. + * If you want four digits of accuracy, need to measure your AVCC well. + * Measure either AVCC of the voltage on AREF and enter it here. +*/ +#define REF_VCC 5.053 + /* measured division by voltage divider */ +#define VOLTAGE_DIV_FACTOR 3.114 + + +// -------- Functions --------- // +void initADC(void) { + ADMUX |= (0b00001111 & PC5); /* set mux to ADC5 */ + ADMUX |= (1 << REFS0); /* reference voltage on AVCC */ + ADCSRA |= (1 << ADPS1) | (1 << ADPS2); /* ADC clock prescaler /64 */ + ADCSRA |= (1 << ADEN); /* enable ADC */ +} + +void setupADCSleepmode(void) { + set_sleep_mode(SLEEP_MODE_ADC); /* defined in avr/sleep.h */ + ADCSRA |= (1 << ADIE); /* enable ADC interrupt */ + sei(); /* enable global interrupts */ +} + +EMPTY_INTERRUPT(ADC_vect); + +uint16_t oversample16x(void) { + uint16_t oversampledValue = 0; + uint8_t i; + for (i = 0; i < 16; i++) { + sleep_mode(); /* chip to sleep, takes ADC sample */ + oversampledValue += ADC; /* add them up 16x */ + } + return (oversampledValue >> 2); /* divide back down by four */ +} + +void printFloat(float number) { + number = round(number * 100) / 100; /* round off to 2 decimal places */ + transmitByte('0' + number / 10); /* tens place */ + transmitByte('0' + number - 10 * floor(number / 10)); /* ones */ + transmitByte('.'); + transmitByte('0' + (number * 10) - floor(number) * 10); /* tenths */ + /* hundredths place */ + transmitByte('0' + (number * 100) - floor(number * 10) * 10); + printString("\r\n"); +} + +int main(void) { + + float voltage; + + // -------- Inits --------- // + initUSART(); + printString("\r\nDigital Voltmeter\r\n\r\n"); + initADC(); + setupADCSleepmode(); + + // ------ Event loop ------ // + + while (1) { + + voltage = oversample16x() * VOLTAGE_DIV_FACTOR * REF_VCC / 4096; + printFloat(voltage); + /* alternatively, just print it out: + * printWord(voltage*100); + * but then you have to remember the decimal place + */ + _delay_ms(500); + + } /* End event loop */ + return 0; /* This line is never reached */ +} diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/adsr/Makefile b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/adsr/Makefile new file mode 100644 index 0000000..5c588dc --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/adsr/Makefile @@ -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 diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/adsr/adsr.c b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/adsr/adsr.c new file mode 100644 index 0000000..946adc2 --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/adsr/adsr.c @@ -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 */ +} diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/adsr/adsr.h b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/adsr/adsr.h new file mode 100644 index 0000000..24701ca --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/adsr/adsr.h @@ -0,0 +1,81 @@ + +/*Include file for ADSR synth project. */ + +#include /* Defines pins, ports, etc */ +#include /* Functions to waste time */ +#include +#include +#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); +} + diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/adsr/fullSaw15.h b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/adsr/fullSaw15.h new file mode 100644 index 0000000..69e64a3 --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/adsr/fullSaw15.h @@ -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 +}; diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/adsr/fullSaw3.h b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/adsr/fullSaw3.h new file mode 100644 index 0000000..c0c1877 --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/adsr/fullSaw3.h @@ -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 +}; diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/adsr/fullSaw7.h b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/adsr/fullSaw7.h new file mode 100644 index 0000000..ef38b9e --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/adsr/fullSaw7.h @@ -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 +}; diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/adsr/fullTri15.h b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/adsr/fullTri15.h new file mode 100644 index 0000000..e227e64 --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/adsr/fullTri15.h @@ -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 +}; diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/adsr/fullTri3.h b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/adsr/fullTri3.h new file mode 100644 index 0000000..236e937 --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/adsr/fullTri3.h @@ -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 +}; diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/adsr/fullTri7.h b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/adsr/fullTri7.h new file mode 100644 index 0000000..ed0af55 --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/adsr/fullTri7.h @@ -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 +}; diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/adsr/fullTriangle.h b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/adsr/fullTriangle.h new file mode 100644 index 0000000..04cb022 --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/adsr/fullTriangle.h @@ -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 +}; diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/adsr/generateScale.py b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/adsr/generateScale.py new file mode 100644 index 0000000..8e7dc3a --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/adsr/generateScale.py @@ -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) + + diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/adsr/generateWavetables.py b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/adsr/generateWavetables.py new file mode 100644 index 0000000..905593f --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/adsr/generateWavetables.py @@ -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. + diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/adsr/scale.h b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/adsr/scale.h new file mode 100644 index 0000000..6b12c73 --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/adsr/scale.h @@ -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 diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/arpeggiator/Makefile b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/arpeggiator/Makefile new file mode 100644 index 0000000..5c588dc --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/arpeggiator/Makefile @@ -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 diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/arpeggiator/arpeggiator.c b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/arpeggiator/arpeggiator.c new file mode 100644 index 0000000..d714194 --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/arpeggiator/arpeggiator.c @@ -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 /* Defines pins, ports, etc */ +#include /* Functions to waste time */ +#include +#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 */ +} + + + + diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/arpeggiator/music_formula_collection.txt b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/arpeggiator/music_formula_collection.txt new file mode 100644 index 0000000..a5aa852 --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/arpeggiator/music_formula_collection.txt @@ -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 diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds/Makefile b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds/Makefile new file mode 100644 index 0000000..5c588dc --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds/Makefile @@ -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 diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds/dds.c b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds/dds.c new file mode 100644 index 0000000..1a8ea58 --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds/dds.c @@ -0,0 +1,56 @@ + /* Direct-digital synthesis */ + +// ------- Preamble -------- // +#include /* Defines pins, ports, etc */ +#include /* Functions to waste time */ +#include + +#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 */ +} diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds/fullSaw15.h b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds/fullSaw15.h new file mode 100644 index 0000000..69e64a3 --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds/fullSaw15.h @@ -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 +}; diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds/fullSaw3.h b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds/fullSaw3.h new file mode 100644 index 0000000..c0c1877 --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds/fullSaw3.h @@ -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 +}; diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds/fullSaw7.h b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds/fullSaw7.h new file mode 100644 index 0000000..ef38b9e --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds/fullSaw7.h @@ -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 +}; diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds/fullSine.h b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds/fullSine.h new file mode 100644 index 0000000..430d305 --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds/fullSine.h @@ -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 +}; diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds/fullSquare15.h b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds/fullSquare15.h new file mode 100644 index 0000000..94a3515 --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds/fullSquare15.h @@ -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 +}; diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds/fullSquare3.h b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds/fullSquare3.h new file mode 100644 index 0000000..c20fa0a --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds/fullSquare3.h @@ -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 +}; diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds/fullSquare7.h b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds/fullSquare7.h new file mode 100644 index 0000000..43a679e --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds/fullSquare7.h @@ -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 +}; diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds/fullTri15.h b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds/fullTri15.h new file mode 100644 index 0000000..e227e64 --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds/fullTri15.h @@ -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 +}; diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds/fullTri3.h b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds/fullTri3.h new file mode 100644 index 0000000..236e937 --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds/fullTri3.h @@ -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 +}; diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds/fullTri7.h b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds/fullTri7.h new file mode 100644 index 0000000..ed0af55 --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds/fullTri7.h @@ -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 +}; diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds/fullTriangle.h b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds/fullTriangle.h new file mode 100644 index 0000000..04cb022 --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds/fullTriangle.h @@ -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 +}; diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds/generateWavetables.py b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds/generateWavetables.py new file mode 100644 index 0000000..905593f --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds/generateWavetables.py @@ -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. + diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_interrupts/Makefile b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_interrupts/Makefile new file mode 100644 index 0000000..5c588dc --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_interrupts/Makefile @@ -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 diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_interrupts/dds_interrupts.c b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_interrupts/dds_interrupts.c new file mode 100644 index 0000000..cdffa3e --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_interrupts/dds_interrupts.c @@ -0,0 +1,74 @@ + /* Direct-digital synthesis */ + +// ------- Preamble -------- // +#include /* Defines pins, ports, etc */ +#include /* Functions to waste time */ +#include +#include +#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 */ +} diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_interrupts/fullSaw15.h b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_interrupts/fullSaw15.h new file mode 100644 index 0000000..69e64a3 --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_interrupts/fullSaw15.h @@ -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 +}; diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_interrupts/fullSaw3.h b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_interrupts/fullSaw3.h new file mode 100644 index 0000000..c0c1877 --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_interrupts/fullSaw3.h @@ -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 +}; diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_interrupts/fullSaw7.h b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_interrupts/fullSaw7.h new file mode 100644 index 0000000..ef38b9e --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_interrupts/fullSaw7.h @@ -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 +}; diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_interrupts/fullSine.h b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_interrupts/fullSine.h new file mode 100644 index 0000000..430d305 --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_interrupts/fullSine.h @@ -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 +}; diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_interrupts/fullSquare15.h b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_interrupts/fullSquare15.h new file mode 100644 index 0000000..94a3515 --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_interrupts/fullSquare15.h @@ -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 +}; diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_interrupts/fullSquare3.h b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_interrupts/fullSquare3.h new file mode 100644 index 0000000..c20fa0a --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_interrupts/fullSquare3.h @@ -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 +}; diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_interrupts/fullSquare7.h b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_interrupts/fullSquare7.h new file mode 100644 index 0000000..43a679e --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_interrupts/fullSquare7.h @@ -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 +}; diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_interrupts/fullTri15.h b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_interrupts/fullTri15.h new file mode 100644 index 0000000..e227e64 --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_interrupts/fullTri15.h @@ -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 +}; diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_interrupts/fullTri3.h b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_interrupts/fullTri3.h new file mode 100644 index 0000000..236e937 --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_interrupts/fullTri3.h @@ -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 +}; diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_interrupts/fullTri7.h b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_interrupts/fullTri7.h new file mode 100644 index 0000000..ed0af55 --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_interrupts/fullTri7.h @@ -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 +}; diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_interrupts/fullTriangle.h b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_interrupts/fullTriangle.h new file mode 100644 index 0000000..04cb022 --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_interrupts/fullTriangle.h @@ -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 +}; diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_interrupts/generateWavetables.py b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_interrupts/generateWavetables.py new file mode 100644 index 0000000..905593f --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_interrupts/generateWavetables.py @@ -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. + diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_saw15/Makefile b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_saw15/Makefile new file mode 100644 index 0000000..5c588dc --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_saw15/Makefile @@ -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 diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_saw15/dds_saw15.c b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_saw15/dds_saw15.c new file mode 100644 index 0000000..35216e0 --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_saw15/dds_saw15.c @@ -0,0 +1,56 @@ + /* Direct-digital synthesis */ + +// ------- Preamble -------- // +#include /* Defines pins, ports, etc */ +#include /* Functions to waste time */ +#include + +#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 */ +} diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_saw15/fullSaw15.h b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_saw15/fullSaw15.h new file mode 100644 index 0000000..69e64a3 --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_saw15/fullSaw15.h @@ -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 +}; diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_saw15/fullSaw3.h b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_saw15/fullSaw3.h new file mode 100644 index 0000000..c0c1877 --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_saw15/fullSaw3.h @@ -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 +}; diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_saw15/fullSaw7.h b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_saw15/fullSaw7.h new file mode 100644 index 0000000..ef38b9e --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_saw15/fullSaw7.h @@ -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 +}; diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_saw15/fullSine.h b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_saw15/fullSine.h new file mode 100644 index 0000000..430d305 --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_saw15/fullSine.h @@ -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 +}; diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_saw15/fullSquare15.h b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_saw15/fullSquare15.h new file mode 100644 index 0000000..94a3515 --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_saw15/fullSquare15.h @@ -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 +}; diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_saw15/fullSquare3.h b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_saw15/fullSquare3.h new file mode 100644 index 0000000..c20fa0a --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_saw15/fullSquare3.h @@ -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 +}; diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_saw15/fullSquare7.h b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_saw15/fullSquare7.h new file mode 100644 index 0000000..43a679e --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_saw15/fullSquare7.h @@ -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 +}; diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_saw15/fullTri15.h b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_saw15/fullTri15.h new file mode 100644 index 0000000..e227e64 --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_saw15/fullTri15.h @@ -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 +}; diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_saw15/fullTri3.h b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_saw15/fullTri3.h new file mode 100644 index 0000000..236e937 --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_saw15/fullTri3.h @@ -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 +}; diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_saw15/fullTri7.h b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_saw15/fullTri7.h new file mode 100644 index 0000000..ed0af55 --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_saw15/fullTri7.h @@ -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 +}; diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_saw15/fullTriangle.h b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_saw15/fullTriangle.h new file mode 100644 index 0000000..04cb022 --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_saw15/fullTriangle.h @@ -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 +}; diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_saw15/generateWavetables.py b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_saw15/generateWavetables.py new file mode 100644 index 0000000..905593f --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dds_saw15/generateWavetables.py @@ -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. + diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dialTone/Makefile b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dialTone/Makefile new file mode 100644 index 0000000..5c588dc --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dialTone/Makefile @@ -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 diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dialTone/dialTone.c b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dialTone/dialTone.c new file mode 100644 index 0000000..eb22e8a --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dialTone/dialTone.c @@ -0,0 +1,85 @@ +/* Direct-digital synthesis */ + +// ------- Preamble -------- // +#include /* Defines pins, ports, etc */ +#include /* Functions to waste time */ +#include +#include +#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 */ +} + + + diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dialTone/fullSine.h b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dialTone/fullSine.h new file mode 100644 index 0000000..099a701 --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dialTone/fullSine.h @@ -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 +}; diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dialTone/generateWavetables.py b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dialTone/generateWavetables.py new file mode 100644 index 0000000..905593f --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/dialTone/generateWavetables.py @@ -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. + diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/fatSaw/Makefile b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/fatSaw/Makefile new file mode 100644 index 0000000..5c588dc --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/fatSaw/Makefile @@ -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 diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/fatSaw/fatSaw.c b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/fatSaw/fatSaw.c new file mode 100644 index 0000000..4b978b2 --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/fatSaw/fatSaw.c @@ -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 */ +} diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/fatSaw/fatSaw.h b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/fatSaw/fatSaw.h new file mode 100644 index 0000000..6bb7665 --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/fatSaw/fatSaw.h @@ -0,0 +1,36 @@ + +// ------- Preamble -------- // +#include /* Defines pins, ports, etc */ +#include /* Functions to waste time */ +#include +#include +#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 */ +} diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/fatSaw/fullSaw15.h b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/fatSaw/fullSaw15.h new file mode 100644 index 0000000..69e64a3 --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/fatSaw/fullSaw15.h @@ -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 +}; diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/fatSaw/fullSaw3.h b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/fatSaw/fullSaw3.h new file mode 100644 index 0000000..c0c1877 --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/fatSaw/fullSaw3.h @@ -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 +}; diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/fatSaw/fullSaw7.h b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/fatSaw/fullSaw7.h new file mode 100644 index 0000000..ef38b9e --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/fatSaw/fullSaw7.h @@ -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 +}; diff --git a/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/fatSaw/generateWavetables.py b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/fatSaw/generateWavetables.py new file mode 100644 index 0000000..905593f --- /dev/null +++ b/Make AVR Examples/Chapter13_Advanced-PWM-Tricks/fatSaw/generateWavetables.py @@ -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. + diff --git a/Make AVR Examples/Chapter14_Switches/dcMotorWorkout/Makefile b/Make AVR Examples/Chapter14_Switches/dcMotorWorkout/Makefile new file mode 100644 index 0000000..3d9e544 --- /dev/null +++ b/Make AVR Examples/Chapter14_Switches/dcMotorWorkout/Makefile @@ -0,0 +1,196 @@ + +##########------------------------------------------------------########## +########## Project-specific Details ########## +########## Check these every time you start a new project ########## +##########------------------------------------------------------########## + +MCU = atmega168p +F_CPU = 1000000UL +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 diff --git a/Make AVR Examples/Chapter14_Switches/dcMotorWorkout/dcMotorWorkout.c b/Make AVR Examples/Chapter14_Switches/dcMotorWorkout/dcMotorWorkout.c new file mode 100644 index 0000000..934040e --- /dev/null +++ b/Make AVR Examples/Chapter14_Switches/dcMotorWorkout/dcMotorWorkout.c @@ -0,0 +1,61 @@ + /* Demos PWM control of a DC motor */ + +// ------- Preamble -------- // +#include +#include +#include +#include "pinDefines.h" +#include "USART.h" + +#define SPEED_STEP_DELAY 2 /* milliseconds */ + +// -------- Functions --------- // +static inline void initTimer0(void) { + TCCR0A |= (1 << WGM00); /* Fast PWM mode */ + TCCR0A |= (1 << WGM01); /* Fast PWM mode, pt.2 */ + TCCR0A |= (1 << COM0B1); /* output PWM to pin */ + TCCR0B |= (1 << CS02); /* Clock with /1024 prescaler */ + //TCCR0B |= (1 << CS00); /* Clock with /1024 prescaler, pt.2 */ +} + + +int main(void) { + + uint8_t updateSpeed; + + // -------- Inits --------- // + initTimer0(); + OCR0B = 0; + + ANTENNA_DDR |= (1 << ANTENNA); /* now hooked up to MOSFET, output */ + LED_DDR |= (1 << LED0); + LED_DDR |= (1 << LED1); + + initUSART(); + printString("DC Motor Workout\r\n"); + + // ------ Event loop ------ // + while (1) { + + updateSpeed = getNumber(); + + /* Ramp up/down to desired speed */ + if (OCR0B < updateSpeed) { + LED_PORT |= (1 << LED0); + while (OCR0B < updateSpeed) { + OCR0B++; + _delay_ms(SPEED_STEP_DELAY); + } + } + else { + LED_PORT |= (1 << LED1); + while (OCR0B > updateSpeed) { + OCR0B--; + _delay_ms(SPEED_STEP_DELAY); + } + } + LED_PORT = 0; /* all off */ + + } /* End event loop */ + return 0; /* This line is never reached */ +} diff --git a/Make AVR Examples/Chapter15_Advanced-Motors/hBridgeWorkout/Makefile b/Make AVR Examples/Chapter15_Advanced-Motors/hBridgeWorkout/Makefile new file mode 100644 index 0000000..3d9e544 --- /dev/null +++ b/Make AVR Examples/Chapter15_Advanced-Motors/hBridgeWorkout/Makefile @@ -0,0 +1,196 @@ + +##########------------------------------------------------------########## +########## Project-specific Details ########## +########## Check these every time you start a new project ########## +##########------------------------------------------------------########## + +MCU = atmega168p +F_CPU = 1000000UL +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 diff --git a/Make AVR Examples/Chapter15_Advanced-Motors/hBridgeWorkout/hBridgeWorkout.c b/Make AVR Examples/Chapter15_Advanced-Motors/hBridgeWorkout/hBridgeWorkout.c new file mode 100644 index 0000000..a1178d9 --- /dev/null +++ b/Make AVR Examples/Chapter15_Advanced-Motors/hBridgeWorkout/hBridgeWorkout.c @@ -0,0 +1,63 @@ +// Simple demo of an h-bridge + +// ------- Preamble -------- // +#include +#include +#include +#include "pinDefines.h" + +static inline void setBridgeState(uint8_t bridgeA, uint8_t bridgeB) { + /* Utility function that lights LEDs when it energizes a bridge side */ + if (bridgeA) { + PORTD |= (1 << PD6); + LED_PORT |= (1 << LED0); + } + else { + PORTD &= ~(1 << PD6); + LED_PORT &= ~(1 << LED0); + } + if (bridgeB) { + PORTD |= (1 << PD5); + LED_PORT |= (1 << LED1); + } + else { + PORTD &= ~(1 << PD5); + LED_PORT &= ~(1 << LED1); + } +} + + +int main(void) { + // -------- Inits --------- // + + DDRD |= (1 << PD6); /* now hooked up to bridge, input1 */ + DDRD |= (1 << PD5); /* now hooked up to bridge, input2 */ + LED_DDR |= (1 << LED0); + LED_DDR |= (1 << LED1); + + // ------ Event loop ------ // + while (1) { + + setBridgeState(1, 0); /* "forward" */ + _delay_ms(2000); + + setBridgeState(0, 0); /* both low stops motor */ + _delay_ms(2000); + + setBridgeState(0, 1); /* "reverse" */ + _delay_ms(2000); + + setBridgeState(1, 1); /* both high also stops motor */ + _delay_ms(2000); + + // For extra-quick braking, energize the motor backwards + setBridgeState(1, 0); + _delay_ms(2000); + setBridgeState(0, 1); + _delay_ms(75); /* tune this time to match your system */ + setBridgeState(0, 0); + _delay_ms(2000); + + } /* End event loop */ + return 0; +} diff --git a/Make AVR Examples/Chapter15_Advanced-Motors/stepperWorkout/Makefile b/Make AVR Examples/Chapter15_Advanced-Motors/stepperWorkout/Makefile new file mode 100644 index 0000000..5c588dc --- /dev/null +++ b/Make AVR Examples/Chapter15_Advanced-Motors/stepperWorkout/Makefile @@ -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 diff --git a/Make AVR Examples/Chapter15_Advanced-Motors/stepperWorkout/half_stepping.h b/Make AVR Examples/Chapter15_Advanced-Motors/stepperWorkout/half_stepping.h new file mode 100644 index 0000000..a030b16 --- /dev/null +++ b/Make AVR Examples/Chapter15_Advanced-Motors/stepperWorkout/half_stepping.h @@ -0,0 +1,17 @@ + +// Include file for half-stepping order +// You may need to change the software pinouts around to match your motor +// or change your motor connections around to match the software. + +const uint8_t stepOrder[] = { + (1 << PB0) | (1 << PB2), + (1 << PB0), + (1 << PB0) | (1 << PB3), + (1 << PB3), + (1 << PB1) | (1 << PB3), + (1 << PB1), + (1 << PB1) | (1 << PB2), + (1 << PB2) +}; + +#define LAST_PHASE_IN_CYCLE 7 diff --git a/Make AVR Examples/Chapter15_Advanced-Motors/stepperWorkout/stepperWorkout.c b/Make AVR Examples/Chapter15_Advanced-Motors/stepperWorkout/stepperWorkout.c new file mode 100644 index 0000000..c79d057 --- /dev/null +++ b/Make AVR Examples/Chapter15_Advanced-Motors/stepperWorkout/stepperWorkout.c @@ -0,0 +1,137 @@ + /* Stepper Motor Demo with Accelerated Moves */ + +// ------- Preamble -------- // +#include +#include +#include +#include +#include "USART.h" + + /* Set these to +/- 1 for half-stepping, +/- 2 for full-stepping */ +#define FORWARD 1 +#define BACKWARD -1 +#define TURN 400 /* steps per rotation, + depends on stepping & motor */ + + /* These parameters will depend on your motor, what it's driving */ +#define MAX_DELAY 255 /* determines min startup speed */ +#define MIN_DELAY 10 /* determines max cruise speed */ +#define ACCELERATION 16 /* lower = smoother but slower accel */ + +#define RAMP_STEPS (MAX_DELAY - MIN_DELAY) / ACCELERATION + +// -------- Global Variables --------- // +const uint8_t motorPhases[] = { + (1 << PB0) | (1 << PB2), /* full */ + (1 << PB0), /* half */ + (1 << PB0) | (1 << PB3), /* full */ + (1 << PB3), /* half */ + (1 << PB1) | (1 << PB3), /* etc. */ + (1 << PB1), + (1 << PB1) | (1 << PB2), + (1 << PB2) +}; + +volatile uint8_t stepPhase = 0; +volatile int8_t direction = FORWARD; +volatile uint16_t stepCounter = 0; + +// -------- Functions --------- // +void initTimer(void) { + TCCR0A |= (1 << WGM01); /* CTC mode */ + TCCR0B |= (1 << CS00) | (1 << CS02); + OCR0A = MAX_DELAY; /* set default speed as slowest */ + sei(); /* enable global interrupts */ + /* Notice we haven't set the timer0 interrupt flag yet. */ +} + +ISR(TIMER0_COMPA_vect) { + stepPhase += direction; /* take step in right direction */ + stepPhase &= 0b00000111; /* keep phase in range 0-7 */ + PORTB = motorPhases[stepPhase]; /* write phase out to motor */ + stepCounter++; /* count step taken */ +} + +void takeSteps(uint16_t howManySteps, uint8_t delay) { + UDR0 = delay; /* send speed/delay over serial, non-blocking */ + OCR0A = delay; /* delay in counter compare register */ + stepCounter = 0; /* initialize to zero steps taken so far */ + TIMSK0 |= (1 << OCIE0A); /* turn on interrupts, stepping */ + while (!(stepCounter == howManySteps)) {; + } /* wait */ + TIMSK0 &= ~(1 << OCIE0A); /* turn back off */ +} + +void trapezoidMove(int16_t howManySteps) { + uint8_t delay = MAX_DELAY; + uint16_t stepsTaken = 0; + + /* set direction, make howManySteps > 0 */ + if (howManySteps > 0) { + direction = FORWARD; + } + else { + direction = BACKWARD; + howManySteps = -howManySteps; + } + + if (howManySteps > (RAMP_STEPS * 2)) { + /* Have enough steps for a full trapezoid */ + /* Accelerate */ + while (stepsTaken < RAMP_STEPS) { + takeSteps(1, delay); + delay -= ACCELERATION; + stepsTaken++; + } + /* Cruise */ + delay = MIN_DELAY; + takeSteps((howManySteps - 2 * RAMP_STEPS), delay); + stepsTaken += (howManySteps - 2 * RAMP_STEPS); + /* Decelerate */ + while (stepsTaken < howManySteps) { + takeSteps(1, delay); + delay += ACCELERATION; + stepsTaken++; + } + } + else { + /* Partial ramp up/down */ + while (stepsTaken <= howManySteps / 2) { + takeSteps(1, delay); + delay -= ACCELERATION; + stepsTaken++; + } + delay += ACCELERATION; + while (stepsTaken < howManySteps) { + takeSteps(1, delay); + delay += ACCELERATION; + stepsTaken++; + } + } +} + +int main(void) { + // -------- Inits --------- // + clock_prescale_set(clock_div_1); /* CPU clock 8 MHz */ + initUSART(); + _delay_ms(1000); + initTimer(); + DDRB = (1 << PB0) | (1 << PB1) | (1 << PB2) | (1 << PB3); + + // ------ Event loop ------ // + while (1) { + + /* Smooth movements, trapezoidal acceleration */ + trapezoidMove(2 * TURN); /* two full turns */ + trapezoidMove(-TURN / 2); /* half turn */ + trapezoidMove(TURN / 4); /* quarter turn */ + trapezoidMove(-TURN / 8); /* eighth */ + _delay_ms(TURN); + trapezoidMove(-TURN / 4); /* the other way */ + trapezoidMove(TURN / 8); + trapezoidMove(TURN / 2); /* half turn back to start */ + _delay_ms(1000); + + } /* End event loop */ + return 0; /* This line is never reached */ +} diff --git a/Make AVR Examples/Chapter16_SPI/spiEEPROMDemo/25LC256.c b/Make AVR Examples/Chapter16_SPI/spiEEPROMDemo/25LC256.c new file mode 100644 index 0000000..f08ee38 --- /dev/null +++ b/Make AVR Examples/Chapter16_SPI/spiEEPROMDemo/25LC256.c @@ -0,0 +1,105 @@ +#include "25LC256.h" + +void initSPI(void) { + SPI_SS_DDR |= (1 << SPI_SS); /* set SS output */ + SPI_SS_PORT |= (1 << SPI_SS); /* start off not selected (high) */ + + SPI_MOSI_DDR |= (1 << SPI_MOSI); /* output on MOSI */ + SPI_MISO_PORT |= (1 << SPI_MISO); /* pullup on MISO */ + SPI_SCK_DDR |= (1 << SPI_SCK); /* output on SCK */ + + /* Don't have to set phase, polarity b/c + * default works with 25LCxxx chips */ + SPCR |= (1 << SPR1); /* div 16, safer for breadboards */ + SPCR |= (1 << MSTR); /* clockmaster */ + SPCR |= (1 << SPE); /* enable */ +} + +void SPI_tradeByte(uint8_t byte) { + SPDR = byte; /* SPI starts sending immediately */ + loop_until_bit_is_set(SPSR, SPIF); /* wait until done */ + /* SPDR now contains the received byte */ +} + +void EEPROM_send16BitAddress(uint16_t address) { + SPI_tradeByte((uint8_t) (address >> 8)); /* most significant byte */ + SPI_tradeByte((uint8_t) address); /* least significant byte */ +} + +uint8_t EEPROM_readStatus(void) { + SLAVE_SELECT; + SPI_tradeByte(EEPROM_RDSR); + SPI_tradeByte(0); /* clock out eight bits */ + SLAVE_DESELECT; + return (SPDR); /* return the result */ +} + +void EEPROM_writeEnable(void) { + SLAVE_SELECT; + SPI_tradeByte(EEPROM_WREN); + SLAVE_DESELECT; +} + +uint8_t EEPROM_readByte(uint16_t address) { + SLAVE_SELECT; + SPI_tradeByte(EEPROM_READ); + EEPROM_send16BitAddress(address); + SPI_tradeByte(0); + SLAVE_DESELECT; + return (SPDR); +} + +uint16_t EEPROM_readWord(uint16_t address) { + uint16_t eepromWord; + SLAVE_SELECT; + SPI_tradeByte(EEPROM_READ); + EEPROM_send16BitAddress(address); + SPI_tradeByte(0); + eepromWord = SPDR; + eepromWord = (eepromWord << 8); /* most-sig bit */ + SPI_tradeByte(0); + eepromWord += SPDR; /* least-sig bit */ + SLAVE_DESELECT; + return (eepromWord); +} + +void EEPROM_writeByte(uint16_t address, uint8_t byte) { + EEPROM_writeEnable(); + SLAVE_SELECT; + SPI_tradeByte(EEPROM_WRITE); + EEPROM_send16BitAddress(address); + SPI_tradeByte(byte); + SLAVE_DESELECT; + while (EEPROM_readStatus() & _BV(EEPROM_WRITE_IN_PROGRESS)) {; + } +} + +void EEPROM_writeWord(uint16_t address, uint16_t word) { + EEPROM_writeEnable(); + SLAVE_SELECT; + SPI_tradeByte(EEPROM_WRITE); + EEPROM_send16BitAddress(address); + SPI_tradeByte((uint8_t) (word >> 8)); + SPI_tradeByte((uint8_t) word); + SLAVE_DESELECT; + while (EEPROM_readStatus() & _BV(EEPROM_WRITE_IN_PROGRESS)) {; + } +} + +void EEPROM_clearAll(void) { + uint8_t i; + uint16_t pageAddress = 0; + while (pageAddress < EEPROM_BYTES_MAX) { + EEPROM_writeEnable(); + SLAVE_SELECT; + SPI_tradeByte(EEPROM_WRITE); + EEPROM_send16BitAddress(pageAddress); + for (i = 0; i < EEPROM_BYTES_PER_PAGE; i++) { + SPI_tradeByte(0); + } + SLAVE_DESELECT; + pageAddress += EEPROM_BYTES_PER_PAGE; + while (EEPROM_readStatus() & _BV(EEPROM_WRITE_IN_PROGRESS)) {; + } + } +} diff --git a/Make AVR Examples/Chapter16_SPI/spiEEPROMDemo/25LC256.h b/Make AVR Examples/Chapter16_SPI/spiEEPROMDemo/25LC256.h new file mode 100644 index 0000000..01b793e --- /dev/null +++ b/Make AVR Examples/Chapter16_SPI/spiEEPROMDemo/25LC256.h @@ -0,0 +1,60 @@ + /* SPI EEPROM 25LC256 Library */ +#include +#include "pinDefines.h" + + + /* Which pin selects EEPROM as slave? */ +#define SLAVE_SELECT SPI_SS_PORT &= ~(1 << SPI_SS) +#define SLAVE_DESELECT SPI_SS_PORT |= (1 << SPI_SS) + +// Instruction Set -- from data sheet +#define EEPROM_READ 0b00000011 /* read memory */ +#define EEPROM_WRITE 0b00000010 /* write to memory */ + +#define EEPROM_WRDI 0b00000100 /* write disable */ +#define EEPROM_WREN 0b00000110 /* write enable */ + +#define EEPROM_RDSR 0b00000101 /* read status register */ +#define EEPROM_WRSR 0b00000001 /* write status register */ + +// EEPROM Status Register Bits -- from data sheet +// Use these to parse status register +#define EEPROM_WRITE_IN_PROGRESS 0 +#define EEPROM_WRITE_ENABLE_LATCH 1 +#define EEPROM_BLOCK_PROTECT_0 2 +#define EEPROM_BLOCK_PROTECT_1 3 + +#define EEPROM_BYTES_PER_PAGE 64 +#define EEPROM_BYTES_MAX 0x7FFF + +// Functions + + /* Init SPI to run EEPROM with phase, polarity = 0,0 */ +void initSPI(void); + + /* Generic. Just loads up HW SPI register and waits */ +void SPI_tradeByte(uint8_t byte); + + /* splits 16-bit address into 2 bytes, sends both */ +void EEPROM_send16BitAddress(uint16_t address); + + /* reads the EEPROM status register */ +uint8_t EEPROM_readStatus(void); + + /* helper: sets EEPROM write enable */ +void EEPROM_writeEnable(void); + + /* gets a byte from a given memory location */ +uint8_t EEPROM_readByte(uint16_t address); + + /* gets two bytes from a given memory location */ +uint16_t EEPROM_readWord(uint16_t address); + + /* writes a byte to a given memory location */ +void EEPROM_writeByte(uint16_t address, uint8_t byte); + + /* gets two bytes to a given memory location */ +void EEPROM_writeWord(uint16_t address, uint16_t word); + + /* sets every byte in memory to zero */ +void EEPROM_clearAll(void); diff --git a/Make AVR Examples/Chapter16_SPI/spiEEPROMDemo/Makefile b/Make AVR Examples/Chapter16_SPI/spiEEPROMDemo/Makefile new file mode 100644 index 0000000..3d9e544 --- /dev/null +++ b/Make AVR Examples/Chapter16_SPI/spiEEPROMDemo/Makefile @@ -0,0 +1,196 @@ + +##########------------------------------------------------------########## +########## Project-specific Details ########## +########## Check these every time you start a new project ########## +##########------------------------------------------------------########## + +MCU = atmega168p +F_CPU = 1000000UL +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 diff --git a/Make AVR Examples/Chapter16_SPI/spiEEPROMDemo/spiEEPROMDemo.c b/Make AVR Examples/Chapter16_SPI/spiEEPROMDemo/spiEEPROMDemo.c new file mode 100644 index 0000000..ba56d63 --- /dev/null +++ b/Make AVR Examples/Chapter16_SPI/spiEEPROMDemo/spiEEPROMDemo.c @@ -0,0 +1,55 @@ + /* SPI EEPROM 25LC256 Demo */ + +// ------- Preamble -------- // +#include +#include + +#include "pinDefines.h" + +#include "USART.h" +#include "25LC256.h" + +int main(void) { + uint8_t i; + uint8_t address; + + // -------- Inits --------- // + initSPI(); + initUSART(); + + // ------ Event loop ------ // + while (1) { + printString("\r\n==== EEPROM Memory Playground ====\r\n"); + printString("Address Value\r\n"); + + for (i = 0; i < 10; i++) { /* print out first ten bytes of memory */ + printString(" "); + printByte(i); + printString(" "); + printByte(EEPROM_readByte(i)); + printString("\r\n"); + } + printString(" [e] to erase all memory\r\n"); + printString(" [w] to write byte to memory\r\n\r\n"); + + switch (receiveByte()) { /* take input */ + case 'e': + printString("Clearing EEPROM, this could take a few seconds.\r\n"); + EEPROM_clearAll(); + break; + case 'w': + printString("Which memory slot would you like to write to?\r\n"); + address = getNumber(); + printString("\r\nWhat number would you like to store there?\r\n"); + i = getNumber(); + EEPROM_writeByte(address, i); + printString("\r\n"); + break; + default: + printString("What??\r\n"); + } + + + } /* End event loop */ + return 0; /* This line is never reached */ +} diff --git a/Make AVR Examples/Chapter17_I2C/i2cThermometer/Makefile b/Make AVR Examples/Chapter17_I2C/i2cThermometer/Makefile new file mode 100644 index 0000000..5c588dc --- /dev/null +++ b/Make AVR Examples/Chapter17_I2C/i2cThermometer/Makefile @@ -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 diff --git a/Make AVR Examples/Chapter17_I2C/i2cThermometer/i2c.c b/Make AVR Examples/Chapter17_I2C/i2cThermometer/i2c.c new file mode 100644 index 0000000..17fa934 --- /dev/null +++ b/Make AVR Examples/Chapter17_I2C/i2cThermometer/i2c.c @@ -0,0 +1,38 @@ +#include "i2c.h" + +void initI2C(void) { + TWBR = 32; /* set bit rate, see p. 242 */ + /* 8MHz / (16+2*TWBR*1) ~= 100kHz */ + TWCR |= (1 << TWEN); /* enable */ +} + +void i2cWaitForComplete(void) { + loop_until_bit_is_set(TWCR, TWINT); +} + +void i2cStart(void) { + TWCR = (_BV(TWINT) | _BV(TWEN) | _BV(TWSTA)); + i2cWaitForComplete(); +} + +void i2cStop(void) { + TWCR = (_BV(TWINT) | _BV(TWEN) | _BV(TWSTO)); +} + +uint8_t i2cReadAck(void) { + TWCR = (_BV(TWINT) | _BV(TWEN) | _BV(TWEA)); + i2cWaitForComplete(); + return (TWDR); +} + +uint8_t i2cReadNoAck(void) { + TWCR = (_BV(TWINT) | _BV(TWEN)); + i2cWaitForComplete(); + return (TWDR); +} + +void i2cSend(uint8_t data) { + TWDR = data; + TWCR = (_BV(TWINT) | _BV(TWEN)); /* init and enable */ + i2cWaitForComplete(); +} diff --git a/Make AVR Examples/Chapter17_I2C/i2cThermometer/i2c.h b/Make AVR Examples/Chapter17_I2C/i2cThermometer/i2c.h new file mode 100644 index 0000000..21b4c4c --- /dev/null +++ b/Make AVR Examples/Chapter17_I2C/i2cThermometer/i2c.h @@ -0,0 +1,22 @@ +// Functions for i2c communication +#include +#include "pinDefines.h" + +void initI2C(void); + /* Sets pullups and initializes bus speed to 100kHz (at FCPU=8MHz) */ + +void i2cWaitForComplete(void); + /* Waits until the hardware sets the TWINT flag */ + +void i2cStart(void); + /* Sends a start condition (sets TWSTA) */ +void i2cStop(void); + /* Sends a stop condition (sets TWSTO) */ + +void i2cSend(uint8_t data); + /* Loads data, sends it out, waiting for completion */ + +uint8_t i2cReadAck(void); + /* Read in from slave, sending ACK when done (sets TWEA) */ +uint8_t i2cReadNoAck(void); + /* Read in from slave, sending NOACK when done (no TWEA) */ diff --git a/Make AVR Examples/Chapter17_I2C/i2cThermometer/i2cThermometer.c b/Make AVR Examples/Chapter17_I2C/i2cThermometer/i2cThermometer.c new file mode 100644 index 0000000..c02a514 --- /dev/null +++ b/Make AVR Examples/Chapter17_I2C/i2cThermometer/i2cThermometer.c @@ -0,0 +1,60 @@ + /* Reads LM75 Thermometer and Prints Value over Serial */ + +// ------- Preamble -------- // +#include +#include +#include + +#include "pinDefines.h" +#include "USART.h" +#include "i2c.h" + +// -------- Defines -------- // + +#define LM75_ADDRESS_W 0b10010000 +#define LM75_ADDRESS_R 0b10010001 +#define LM75_TEMP_REGISTER 0b00000000 +#define LM75_CONFIG_REGISTER 0b00000001 +#define LM75_THYST_REGISTER 0b00000010 +#define LM75_TOS_REGISTER 0b00000011 +// -------- Functions --------- // + +int main(void) { + + uint8_t tempHighByte, tempLowByte; + + // -------- Inits --------- // + clock_prescale_set(clock_div_1); /* 8MHz */ + initUSART(); + printString("\r\n==== i2c Thermometer ====\r\n"); + initI2C(); + + // ------ Event loop ------ // + while (1) { + /* To set register, address LM75 in write mode */ + i2cStart(); + i2cSend(LM75_ADDRESS_W); + i2cSend(LM75_TEMP_REGISTER); + i2cStart(); /* restart, just send start again */ + /* Setup and send address, with read bit */ + i2cSend(LM75_ADDRESS_R); + /* Now receive two bytes of temperature */ + tempHighByte = i2cReadAck(); + tempLowByte = i2cReadNoAck(); + i2cStop(); + + // Print it out nicely over serial for now... + printByte(tempHighByte); + if (tempLowByte & _BV(7)) { + printString(".5\r\n"); + } + else { + printString(".0\r\n"); + } + + /* Once per second */ + _delay_ms(1000); + + } /* End event loop */ + return 0; /* This line is never reached */ +} diff --git a/Make AVR Examples/Chapter17_I2C/loggingThermometer/25LC256.c b/Make AVR Examples/Chapter17_I2C/loggingThermometer/25LC256.c new file mode 100644 index 0000000..ade4746 --- /dev/null +++ b/Make AVR Examples/Chapter17_I2C/loggingThermometer/25LC256.c @@ -0,0 +1,105 @@ +#include "25LC256.h" + +void initSPI(void) { + SPI_SS_DDR |= (1 << SPI_SS); /* set SS output */ + SPI_SS_PORT |= (1 << SPI_SS); /* start off not selected (high) */ + + SPI_MOSI_DDR |= (1 << SPI_MOSI); /* output on MOSI */ + SPI_MISO_PORT |= (1 << SPI_MISO); /* pullup on MISO */ + SPI_SCK_DDR |= (1 << SPI_SCK); /* output on SCK */ + + /* Don't have to set phase, polarity b/c + default works with 25LCxxx chips */ + SPCR |= (1 << SPR1); /* div 16, safer for breadboards */ + SPCR |= (1 << MSTR); /* clockmaster */ + SPCR |= (1 << SPE); /* enable */ +} + +void SPI_tradeByte(uint8_t byte) { + SPDR = byte; /* SPI starts sending immediately */ + loop_until_bit_is_set(SPSR, SPIF); /* wait until done */ + /* SPDR now contains the received byte */ +} + +void EEPROM_send16BitAddress(uint16_t address) { + SPI_tradeByte((uint8_t) (address >> 8)); /* most significant byte */ + SPI_tradeByte((uint8_t) address); /* least significant byte */ +} + +uint8_t EEPROM_readStatus(void) { + SLAVE_SELECT; + SPI_tradeByte(EEPROM_RDSR); + SPI_tradeByte(0); /* clock out eight bits */ + SLAVE_DESELECT; + return (SPDR); /* return the result */ +} + +void EEPROM_writeEnable(void) { + SLAVE_SELECT; + SPI_tradeByte(EEPROM_WREN); + SLAVE_DESELECT; +} + +uint8_t EEPROM_readByte(uint16_t address) { + SLAVE_SELECT; + SPI_tradeByte(EEPROM_READ); + EEPROM_send16BitAddress(address); + SPI_tradeByte(0); + SLAVE_DESELECT; + return (SPDR); +} + +uint16_t EEPROM_readWord(uint16_t address) { + uint16_t eepromWord; + SLAVE_SELECT; + SPI_tradeByte(EEPROM_READ); + EEPROM_send16BitAddress(address); + SPI_tradeByte(0); + eepromWord = SPDR; + eepromWord = (eepromWord << 8); /* most-sig bit */ + SPI_tradeByte(0); + eepromWord += SPDR; /* least-sig bit */ + SLAVE_DESELECT; + return (eepromWord); +} + +void EEPROM_writeByte(uint16_t address, uint8_t byte) { + EEPROM_writeEnable(); + SLAVE_SELECT; + SPI_tradeByte(EEPROM_WRITE); + EEPROM_send16BitAddress(address); + SPI_tradeByte(byte); + SLAVE_DESELECT; + while (EEPROM_readStatus() & _BV(EEPROM_WRITE_IN_PROGRESS)) {; + } +} + +void EEPROM_writeWord(uint16_t address, uint16_t word) { + EEPROM_writeEnable(); + SLAVE_SELECT; + SPI_tradeByte(EEPROM_WRITE); + EEPROM_send16BitAddress(address); + SPI_tradeByte((uint8_t) (word >> 8)); + SPI_tradeByte((uint8_t) word); + SLAVE_DESELECT; + while (EEPROM_readStatus() & _BV(EEPROM_WRITE_IN_PROGRESS)) {; + } +} + +void EEPROM_clearAll(void) { + uint8_t i; + uint16_t pageAddress = 0; + while (pageAddress < EEPROM_BYTES_MAX) { + EEPROM_writeEnable(); + SLAVE_SELECT; + SPI_tradeByte(EEPROM_WRITE); + EEPROM_send16BitAddress(pageAddress); + for (i = 0; i < EEPROM_BYTES_PER_PAGE; i++) { + SPI_tradeByte(0); + } + SLAVE_DESELECT; + pageAddress += EEPROM_BYTES_PER_PAGE; + while (EEPROM_readStatus() & _BV(EEPROM_WRITE_IN_PROGRESS)) {; + } + } +} diff --git a/Make AVR Examples/Chapter17_I2C/loggingThermometer/25LC256.h b/Make AVR Examples/Chapter17_I2C/loggingThermometer/25LC256.h new file mode 100644 index 0000000..c8ef4fc --- /dev/null +++ b/Make AVR Examples/Chapter17_I2C/loggingThermometer/25LC256.h @@ -0,0 +1,60 @@ + /* SPI EEPROM 25LC256 Library */ +#include +#include "pinDefines.h" + + + /* Which pin selects EEPROM as slave? */ +#define SLAVE_SELECT SPI_SS_PORT &= ~(1 << SPI_SS) +#define SLAVE_DESELECT SPI_SS_PORT |= (1 << SPI_SS) + +// Instruction Set -- from data sheet +#define EEPROM_READ 0b00000011 /* read memory */ +#define EEPROM_WRITE 0b00000010 /* write to memory */ + +#define EEPROM_WRDI 0b00000100 /* write disable */ +#define EEPROM_WREN 0b00000110 /* write enable */ + +#define EEPROM_RDSR 0b00000101 /* read status register */ +#define EEPROM_WRSR 0b00000001 /* write status register */ + +// EEPROM Status Register Bits -- from data sheet +// Use these to parse status register +#define EEPROM_WRITE_IN_PROGRESS 0 +#define EEPROM_WRITE_ENABLE_LATCH 1 +#define EEPROM_BLOCK_PROTECT_0 2 +#define EEPROM_BLOCK_PROTECT_1 3 + +#define EEPROM_BYTES_PER_PAGE 64 +#define EEPROM_BYTES_MAX 0x7FFF + +// Functions + +void initSPI(void); + /* Init SPI to run EEPROM with phase, polarity = 0,0 */ + +void SPI_tradeByte(uint8_t byte); + /* Generic. Just loads up HW SPI register and waits */ + +void EEPROM_send16BitAddress(uint16_t address); + /* splits 16-bit address into 2 bytes, sends both */ + +uint8_t EEPROM_readStatus(void); + /* reads the EEPROM status register */ + +void EEPROM_writeEnable(void); + /* helper: sets EEPROM write enable */ + +uint8_t EEPROM_readByte(uint16_t address); + /* gets a byte from a given memory location */ + +uint16_t EEPROM_readWord(uint16_t address); + /* gets two bytes from a given memory location */ + +void EEPROM_writeByte(uint16_t address, uint8_t byte); + /* writes a byte to a given memory location */ + +void EEPROM_writeWord(uint16_t address, uint16_t word); + /* gets two bytes to a given memory location */ + +void EEPROM_clearAll(void); + /* sets every byte in memory to zero */ diff --git a/Make AVR Examples/Chapter17_I2C/loggingThermometer/Makefile b/Make AVR Examples/Chapter17_I2C/loggingThermometer/Makefile new file mode 100644 index 0000000..5c588dc --- /dev/null +++ b/Make AVR Examples/Chapter17_I2C/loggingThermometer/Makefile @@ -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 diff --git a/Make AVR Examples/Chapter17_I2C/loggingThermometer/i2c.c b/Make AVR Examples/Chapter17_I2C/loggingThermometer/i2c.c new file mode 100644 index 0000000..18d06e5 --- /dev/null +++ b/Make AVR Examples/Chapter17_I2C/loggingThermometer/i2c.c @@ -0,0 +1,39 @@ +#include "i2c.h" + +void initI2C(void) { + /* set pullups for SDA, SCL lines */ + I2C_SDA_PORT |= ((1 << I2C_SDA) | (1 << I2C_SCL)); + TWBR = 32; /* set bit rate (p.242): 8MHz / (16+2*TWBR*1) ~= 100kHz */ + TWCR |= (1 << TWEN); /* enable */ +} + +void i2cWaitForComplete(void) { + loop_until_bit_is_set(TWCR, TWINT); +} + +void i2cStart(void) { + TWCR = (_BV(TWINT) | _BV(TWEN) | _BV(TWSTA)); + i2cWaitForComplete(); +} + +void i2cStop(void) { + TWCR = (_BV(TWINT) | _BV(TWEN) | _BV(TWSTO)); +} + +uint8_t i2cReadAck(void) { + TWCR = (_BV(TWINT) | _BV(TWEN) | _BV(TWEA)); + i2cWaitForComplete(); + return (TWDR); +} + +uint8_t i2cReadNoAck(void) { + TWCR = (_BV(TWINT) | _BV(TWEN)); + i2cWaitForComplete(); + return (TWDR); +} + +void i2cSend(uint8_t data) { + TWDR = data; + TWCR = (_BV(TWINT) | _BV(TWEN)); /* init and enable */ + i2cWaitForComplete(); +} diff --git a/Make AVR Examples/Chapter17_I2C/loggingThermometer/i2c.h b/Make AVR Examples/Chapter17_I2C/loggingThermometer/i2c.h new file mode 100644 index 0000000..21b4c4c --- /dev/null +++ b/Make AVR Examples/Chapter17_I2C/loggingThermometer/i2c.h @@ -0,0 +1,22 @@ +// Functions for i2c communication +#include +#include "pinDefines.h" + +void initI2C(void); + /* Sets pullups and initializes bus speed to 100kHz (at FCPU=8MHz) */ + +void i2cWaitForComplete(void); + /* Waits until the hardware sets the TWINT flag */ + +void i2cStart(void); + /* Sends a start condition (sets TWSTA) */ +void i2cStop(void); + /* Sends a stop condition (sets TWSTO) */ + +void i2cSend(uint8_t data); + /* Loads data, sends it out, waiting for completion */ + +uint8_t i2cReadAck(void); + /* Read in from slave, sending ACK when done (sets TWEA) */ +uint8_t i2cReadNoAck(void); + /* Read in from slave, sending NOACK when done (no TWEA) */ diff --git a/Make AVR Examples/Chapter17_I2C/loggingThermometer/loggingThermometer.c b/Make AVR Examples/Chapter17_I2C/loggingThermometer/loggingThermometer.c new file mode 100644 index 0000000..0efbb35 --- /dev/null +++ b/Make AVR Examples/Chapter17_I2C/loggingThermometer/loggingThermometer.c @@ -0,0 +1,172 @@ + + +// ------- Preamble -------- // +#include +#include +#include +#include + +#include "pinDefines.h" +#include "USART.h" + +#include "i2c.h" /* for i2c functions */ +#include "25LC256.h" /* for EEPROM specific */ + +// -------- Defines --------- // + +#define LM75_ADDRESS_W 0b10010000 +#define LM75_ADDRESS_R 0b10010001 +#define LM75_TEMP_REGISTER 0b00000000 +#define LM75_CONFIG_REGISTER 0b00000001 +#define LM75_THYST_REGISTER 0b00000010 +#define LM75_TOS_REGISTER 0b00000011 + +#define CURRENT_LOCATION_POINTER 0 + /* where to store a pointer to the current reading in EEPROM */ +#define SECONDS_POINTER 2 + /* store seconds-delay value here */ +#define MEMORY_START 4 + /* where to start logging temperature values */ +#define MENU_DELAY 5 + /* seconds to wait before bypassing main menu */ + +// -------- Functions --------- // + +static inline void printTemperature(uint8_t tempReading) { + /* temperature stored as 2x Celcius */ + printByte((tempReading >> 1)); + if (tempReading & 1) { + printString(".5\r\n"); + } + else { + printString(".0\r\n"); + } +} + +int main(void) { + uint16_t secondsDelay; /* how long to wait between readings */ + uint16_t currentMemoryLocation; /* where are we in EEPROM? */ + uint16_t i; /* used to count memory locations */ + uint8_t tempHighByte, tempLowByte, temperatureByte; /* from LM75 */ + uint8_t enterMenu; /* logical flag */ + + // -------- Inits --------- // + clock_prescale_set(clock_div_1); /* 8 MHz */ + initSPI(); + initI2C(); + initUSART(); + LED_DDR |= (1 << LED0); + + /* Load up last values from EEPROM */ + secondsDelay = EEPROM_readWord(SECONDS_POINTER); + + /* Delay to allow input to enter main menu */ + printString("*** Press [m] within "); + printByte(MENU_DELAY); + printString(" seconds to enter menu. ***\r\n "); + + for (i = 0; i < MENU_DELAY; i++) { + _delay_ms(1000); + } + + if (bit_is_set(UCSR0A, RXC0) && (UDR0 == 'm')) { + enterMenu = 1; + } + else { + enterMenu = 0; + } + + while (enterMenu) { + printString("\r\n====[ Logging Thermometer ]====\r\n "); + currentMemoryLocation = EEPROM_readWord(CURRENT_LOCATION_POINTER); + printWord(currentMemoryLocation - MEMORY_START); + printString(" readings in log.\r\n "); + printWord(secondsDelay); + printString(" seconds between readings.\r\n"); + printString(" [<] to shorten sample delay time\r\n"); + printString(" [>] to increase sample delay time\r\n"); + printString(" [?] to reset delay time to 60 sec\r\n"); + printString(" [d] to print out log over serial\r\n"); + printString(" [e] to erase memory\r\n"); + printString(" [s] to start logging\r\n\r\n"); + + switch (receiveByte()) { + case 'd': + SLAVE_SELECT; + SPI_tradeByte(EEPROM_READ); + EEPROM_send16BitAddress(MEMORY_START); + for (i = MEMORY_START; i < currentMemoryLocation; i++) { + SPI_tradeByte(0); + printTemperature(SPDR); + } + SLAVE_DESELECT; + break; + case '<': + if (secondsDelay >= 10) { + secondsDelay -= 5; + EEPROM_writeWord(SECONDS_POINTER, secondsDelay); + } + break; + case '>': + if (secondsDelay < 65000) { + secondsDelay += 5; + EEPROM_writeWord(SECONDS_POINTER, secondsDelay); + } + break; + case '?': + secondsDelay = 60; + EEPROM_writeWord(SECONDS_POINTER, secondsDelay); + break; + case 'e': + printString("Clearing EEPROM, this could take a few seconds.\r\n"); + EEPROM_clearAll(); + EEPROM_writeWord(CURRENT_LOCATION_POINTER, MEMORY_START); + EEPROM_writeWord(SECONDS_POINTER, secondsDelay); + break; + case 's': + printString("OK, logging...\r\n"); + enterMenu = 0; + break; + default: + printString("Sorry, didn't understand that.\r\n"); + } + } + + // ------ Event loop ------ // + while (1) { + + currentMemoryLocation = EEPROM_readWord(CURRENT_LOCATION_POINTER); + + /* Make sure in temperature mode */ + i2cStart(); + i2cSend(LM75_ADDRESS_W); + i2cSend(LM75_TEMP_REGISTER); + /* Get Temp from thermometer */ + i2cStart(); /* Setup and send address, with read bit */ + i2cSend(LM75_ADDRESS_R); + tempHighByte = i2cReadAck(); /* two bytes of temperature */ + tempLowByte = i2cReadNoAck(); + i2cStop(); + temperatureByte = (tempHighByte << 1) | (tempLowByte >> 7); + /* temperatureByte now contains 2x the temperature in Celcius */ + printTemperature(temperatureByte); /* serial output */ + + /* Save the new temperature value */ + EEPROM_writeByte(currentMemoryLocation, temperatureByte); + + /* move on to next location and record new position + if not already at the end of memory */ + if (currentMemoryLocation < EEPROM_BYTES_MAX) { + currentMemoryLocation++; + EEPROM_writeWord(CURRENT_LOCATION_POINTER, currentMemoryLocation); + } + + /* delay */ + for (i = 0; i < secondsDelay; i++) { + _delay_ms(1000); + LED_PORT ^= (1 << LED0); /* blink to show working */ + } + + } /* End event loop */ + return 0; /* This line is never reached */ +} diff --git a/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/progmemDemo1/Makefile b/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/progmemDemo1/Makefile new file mode 100644 index 0000000..3d9e544 --- /dev/null +++ b/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/progmemDemo1/Makefile @@ -0,0 +1,196 @@ + +##########------------------------------------------------------########## +########## Project-specific Details ########## +########## Check these every time you start a new project ########## +##########------------------------------------------------------########## + +MCU = atmega168p +F_CPU = 1000000UL +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 diff --git a/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/progmemDemo1/progmemDemo1.c b/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/progmemDemo1/progmemDemo1.c new file mode 100644 index 0000000..effff1b --- /dev/null +++ b/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/progmemDemo1/progmemDemo1.c @@ -0,0 +1,32 @@ + /* First steps into using program memory */ + +#include +#include +#include +#include "USART.h" + +const char myVeryLongString[] PROGMEM = "\r\nHi there, \ +this is an example of a long string.\r\n\ +The kind that you wouldn't want to store in RAM.\r\n"; +const uint16_t sixteenBits PROGMEM = 12345; + +int main(void) { + initUSART(); + char oneLetter; + uint8_t i; + + while (1) { + for (i = 0; i < sizeof(myVeryLongString); i++) { + oneLetter = pgm_read_byte(&(myVeryLongString[i])); + transmitByte(oneLetter); + _delay_ms(100); /* slow it down to simulate typing effect :) */ + } + _delay_ms(1000); + + printWord(&sixteenBits); /* this throws a compiler warning... */ + transmitByte('\r'); + transmitByte('\n'); + printWord(pgm_read_word(&sixteenBits)); + } /* End event loop */ + return 0; /* This line is never reached */ +} diff --git a/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/progmemDemo2/Makefile b/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/progmemDemo2/Makefile new file mode 100644 index 0000000..3d9e544 --- /dev/null +++ b/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/progmemDemo2/Makefile @@ -0,0 +1,196 @@ + +##########------------------------------------------------------########## +########## Project-specific Details ########## +########## Check these every time you start a new project ########## +##########------------------------------------------------------########## + +MCU = atmega168p +F_CPU = 1000000UL +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 diff --git a/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/progmemDemo2/progmemDemo2.c b/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/progmemDemo2/progmemDemo2.c new file mode 100644 index 0000000..c2748d7 --- /dev/null +++ b/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/progmemDemo2/progmemDemo2.c @@ -0,0 +1,37 @@ + /* Second steps into using program memory */ + /* Storing the addresses in pointers */ + +#include +#include +#include +#include "USART.h" + +const char myVeryLongString[] PROGMEM = "\r\nHi there, \ +this is an example of a long string.\r\n\ +The kind that you wouldn't want to store in RAM.\r\n"; +const uint16_t sixteenBits PROGMEM = 12345; + +int main(void) { + initUSART(); + + const char *stringPointer; + const uint16_t *wordPointer; + uint8_t i; + char oneLetter; + + stringPointer = &myVeryLongString[0]; /* address of first char */ + // stringPointer = myVeryLongString; /* same as above */ + wordPointer = &sixteenBits; /* address of first byte */ + + while (1) { + for (i = 0; i < sizeof(myVeryLongString); i++) { + oneLetter = pgm_read_byte(stringPointer + i); + transmitByte(oneLetter); + _delay_ms(100); /* slow it down to simulate typing effect :) */ + } + _delay_ms(1000); + + printWord(pgm_read_word(wordPointer)); + } /* End event loop */ + return 0; /* This line is never reached */ +} diff --git a/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/progmemDemo3/Makefile b/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/progmemDemo3/Makefile new file mode 100644 index 0000000..3d9e544 --- /dev/null +++ b/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/progmemDemo3/Makefile @@ -0,0 +1,196 @@ + +##########------------------------------------------------------########## +########## Project-specific Details ########## +########## Check these every time you start a new project ########## +##########------------------------------------------------------########## + +MCU = atmega168p +F_CPU = 1000000UL +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 diff --git a/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/progmemDemo3/progmemDemo3.c b/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/progmemDemo3/progmemDemo3.c new file mode 100644 index 0000000..31df8cb --- /dev/null +++ b/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/progmemDemo3/progmemDemo3.c @@ -0,0 +1,33 @@ + /* Third step into using program memory */ + /* Passing pointers to functions */ + +#include +#include +#include +#include "USART.h" + +const char myVeryLongString1[] PROGMEM = "\r\nHi there, \ +this is an example of a long string.\r\n\ +The kind that you wouldn't want to store in RAM.\r\n"; +const char myVeryLongString2[] PROGMEM = "All work and no play \ +makes Jack something something.\r\n"; + +void printString_Progmem(const char *stringP) { + char oneLetter; + while ((oneLetter = pgm_read_byte(stringP))) { + transmitByte(oneLetter); + stringP++; + _delay_ms(100); /* only b/c it's cute */ + } +} + +int main(void) { + initUSART(); + while (1) { + printString_Progmem(&myVeryLongString1[0]); + printString_Progmem(&myVeryLongString1[50]); + printString_Progmem(myVeryLongString2); + _delay_ms(1000); + } /* End event loop */ + return 0; /* This line is never reached */ +} diff --git a/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/progmemDemo4/Makefile b/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/progmemDemo4/Makefile new file mode 100644 index 0000000..3d9e544 --- /dev/null +++ b/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/progmemDemo4/Makefile @@ -0,0 +1,196 @@ + +##########------------------------------------------------------########## +########## Project-specific Details ########## +########## Check these every time you start a new project ########## +##########------------------------------------------------------########## + +MCU = atmega168p +F_CPU = 1000000UL +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 diff --git a/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/progmemDemo4/progmemDemo4.c b/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/progmemDemo4/progmemDemo4.c new file mode 100644 index 0000000..46b62fd --- /dev/null +++ b/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/progmemDemo4/progmemDemo4.c @@ -0,0 +1,36 @@ + /* Fourth step into using program memory */ + /* Passing data array pointers to functions */ + +#include +#include +#include +#include "USART.h" + +const uint16_t myData[] PROGMEM = + { 1111, 2222, 3333, 4444, 5555, 6666, 7777, 8888, 9999, 10000 }; +const uint16_t myData2[] PROGMEM = { 123, 456, 789, 012, 345, 678, 999 }; + +void printData_Progmem(const uint16_t * dataPointer, uint8_t length) { + while (length) { + printWord((uint16_t) dataPointer); /* print out address */ + printString(": "); + printWord(pgm_read_word(dataPointer)); /* print out data */ + printString("\r\n"); + dataPointer++; /* move to next byte */ + length--; /* one less byte to go */ + _delay_ms(100); + } +} + +int main(void) { + initUSART(); + while (1) { + printData_Progmem(myData, sizeof(myData) / 2); + printString("\r\n"); + _delay_ms(1000); + printData_Progmem(myData2, sizeof(myData2) / sizeof(myData2[0])); + printString("\r\n"); + _delay_ms(1000); + } /* End event loop */ + return 0; /* This line is never reached */ +} diff --git a/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/progmemDemo5/Makefile b/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/progmemDemo5/Makefile new file mode 100644 index 0000000..3d9e544 --- /dev/null +++ b/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/progmemDemo5/Makefile @@ -0,0 +1,196 @@ + +##########------------------------------------------------------########## +########## Project-specific Details ########## +########## Check these every time you start a new project ########## +##########------------------------------------------------------########## + +MCU = atmega168p +F_CPU = 1000000UL +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 diff --git a/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/progmemDemo5/progmemDemo5.c b/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/progmemDemo5/progmemDemo5.c new file mode 100644 index 0000000..98c7d0e --- /dev/null +++ b/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/progmemDemo5/progmemDemo5.c @@ -0,0 +1,68 @@ + /* Fourth step into using program memory */ + /* Passing data array pointers to functions */ + +#include +#include +#include +#include "USART.h" + +const uint16_t myData[] PROGMEM = + { 1111, 2222, 3333, 4444, 5555, 6666, 7777, 8888, 9999, 10000 }; + +const char string1[] PROGMEM = "Hey, string one!\r\n"; +const char string2[] PROGMEM = "Oh my, string two!\r\n"; +const char *const stringIndex[] PROGMEM = { string1, string2 }; + + +void printData_Progmem(const uint16_t * dataPointer, uint8_t length) { + while (length) { + printWord((uint16_t) dataPointer); /* print out address */ + printString(": "); + printWord(pgm_read_word(dataPointer)); /* print out data */ + printString("\r\n"); + dataPointer++; /* move to next byte */ + length--; /* one less byte to go */ + _delay_ms(100); + } +} +void printString_Progmem(const char *stringP) { + char oneLetter; + while ((oneLetter = pgm_read_byte(stringP))) { + transmitByte(oneLetter); + stringP++; + } +} + +int main(void) { + initUSART(); + + uint8_t myArray[] = { 10, 11, 12 }; + uint8_t *p; + uint8_t i; + p = &myArray[0]; + for (i = 0; i < sizeof(myArray); i++) { + printByte(*(p + i)); + printString("\r\n"); + _delay_ms(1000); + } + + + /* To use them: */ + char *stringPointer; + /* Get the pointer to the string you want from PROGMEM */ + + stringPointer = (char *) pgm_read_word(&stringIndex[0]); + printString_Progmem(stringPointer); + /* or */ + stringPointer = (char *) pgm_read_word(&stringIndex[1]); + printString_Progmem(&stringPointer[0]); + /* or */ + printString_Progmem(PSTR("And this string got inlined.\r\n")); + + while (1) { + printData_Progmem(myData, sizeof(myData) / sizeof(myData[0])); + printString("\r\n"); + _delay_ms(1000); + } /* End event loop */ + return 0; /* This line is never reached */ +} diff --git a/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/talkingVoltmeter/Makefile b/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/talkingVoltmeter/Makefile new file mode 100644 index 0000000..5c588dc --- /dev/null +++ b/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/talkingVoltmeter/Makefile @@ -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 diff --git a/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/talkingVoltmeter/allDigits.h b/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/talkingVoltmeter/allDigits.h new file mode 100644 index 0000000..da176f5 --- /dev/null +++ b/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/talkingVoltmeter/allDigits.h @@ -0,0 +1,14923 @@ +const uint8_t DPCM_one_8000[] PROGMEM = { + 153, + 90, + 106, + 150, + 105, + 86, + 150, + 106, + 150, + 85, + 165, + 150, + 165, + 169, + 90, + 85, + 102, + 149, + 166, + 105, + 153, + 165, + 170, + 89, + 106, + 153, + 150, + 101, + 149, + 105, + 150, + 166, + 153, + 85, + 149, + 85, + 170, + 170, + 169, + 149, + 85, + 89, + 106, + 169, + 169, + 105, + 170, + 170, + 169, + 85, + 90, + 170, + 169, + 85, + 85, + 170, + 169, + 85, + 85, + 170, + 170, + 150, + 170, + 153, + 85, + 85, + 170, + 170, + 89, + 85, + 86, + 170, + 170, + 85, + 85, + 106, + 170, + 149, + 85, + 170, + 170, + 149, + 85, + 170, + 149, + 85, + 90, + 170, + 85, + 86, + 191, + 165, + 64, + 90, + 170, + 149, + 85, + 106, + 85, + 170, + 169, + 85, + 90, + 170, + 149, + 86, + 170, + 165, + 17, + 127, + 250, + 64, + 22, + 186, + 149, + 86, + 169, + 85, + 107, + 233, + 69, + 106, + 169, + 85, + 106, + 169, + 85, + 145, + 26, + 255, + 224, + 1, + 175, + 233, + 86, + 169, + 81, + 107, + 233, + 1, + 111, + 165, + 85, + 170, + 149, + 90, + 169, + 1, + 191, + 244, + 0, + 127, + 228, + 86, + 185, + 65, + 111, + 228, + 1, + 191, + 144, + 90, + 169, + 85, + 170, + 170, + 0, + 191, + 252, + 0, + 127, + 213, + 42, + 232, + 2, + 255, + 144, + 27, + 249, + 69, + 170, + 149, + 106, + 169, + 106, + 64, + 127, + 252, + 1, + 191, + 149, + 126, + 128, + 27, + 253, + 1, + 191, + 149, + 106, + 149, + 90, + 169, + 170, + 168, + 2, + 255, + 160, + 15, + 245, + 111, + 224, + 2, + 255, + 80, + 86, + 170, + 165, + 89, + 166, + 85, + 186, + 149, + 96, + 15, + 250, + 0, + 123, + 150, + 254, + 0, + 175, + 229, + 85, + 86, + 250, + 69, + 170, + 85, + 170, + 149, + 105, + 0, + 255, + 0, + 15, + 98, + 255, + 64, + 63, + 197, + 190, + 0, + 191, + 65, + 174, + 81, + 170, + 86, + 169, + 64, + 47, + 240, + 4, + 246, + 191, + 192, + 27, + 244, + 111, + 128, + 127, + 209, + 46, + 145, + 105, + 86, + 186, + 18, + 0, + 255, + 0, + 31, + 171, + 248, + 2, + 254, + 70, + 164, + 27, + 233, + 42, + 149, + 106, + 149, + 126, + 132, + 104, + 7, + 252, + 2, + 250, + 127, + 192, + 47, + 244, + 89, + 2, + 254, + 6, + 229, + 26, + 165, + 106, + 166, + 86, + 96, + 143, + 240, + 11, + 218, + 250, + 0, + 255, + 230, + 64, + 127, + 196, + 106, + 149, + 165, + 106, + 150, + 165, + 85, + 160, + 143, + 240, + 7, + 203, + 252, + 1, + 255, + 186, + 64, + 127, + 209, + 89, + 170, + 85, + 106, + 166, + 101, + 106, + 86, + 13, + 255, + 2, + 84, + 255, + 64, + 46, + 255, + 128, + 31, + 234, + 21, + 170, + 149, + 105, + 154, + 85, + 169, + 149, + 104, + 63, + 224, + 12, + 27, + 248, + 4, + 87, + 252, + 0, + 155, + 248, + 22, + 154, + 164, + 86, + 169, + 90, + 213, + 85, + 168, + 63, + 192, + 40, + 47, + 208, + 20, + 47, + 228, + 16, + 47, + 228, + 20, + 107, + 149, + 85, + 170, + 150, + 165, + 86, + 105, + 15, + 240, + 5, + 15, + 241, + 84, + 15, + 245, + 164, + 15, + 245, + 148, + 43, + 170, + 81, + 106, + 170, + 86, + 101, + 150, + 176, + 255, + 1, + 0, + 255, + 91, + 0, + 255, + 175, + 0, + 170, + 234, + 84, + 106, + 170, + 85, + 90, + 169, + 90, + 149, + 150, + 168, + 63, + 194, + 192, + 63, + 159, + 128, + 121, + 191, + 64, + 101, + 190, + 153, + 85, + 170, + 165, + 90, + 154, + 149, + 105, + 90, + 90, + 128, + 253, + 61, + 3, + 230, + 252, + 2, + 139, + 232, + 100, + 26, + 170, + 149, + 90, + 169, + 85, + 90, + 165, + 105, + 106, + 149, + 86, + 160, + 255, + 95, + 0, + 229, + 253, + 5, + 66, + 250, + 100, + 22, + 91, + 165, + 149, + 105, + 106, + 150, + 105, + 101, + 150, + 90, + 85, + 106, + 11, + 250, + 244, + 26, + 91, + 229, + 85, + 90, + 170, + 149, + 85, + 170, + 170, + 86, + 150, + 169, + 85, + 106, + 170, + 150, + 153, + 153, + 169, + 39, + 170, + 216, + 85, + 106, + 149, + 149, + 106, + 170, + 85, + 86, + 169, + 169, + 86, + 86, + 165, + 154, + 153, + 165, + 170, + 102, + 149, + 165, + 86, + 153, + 170, + 102, + 102, + 149, + 85, + 90, + 169, + 86, + 170, + 149, + 102, + 170, + 85, + 85, + 165, + 169, + 90, + 150, + 165, + 106, + 154, + 85, + 86, + 170, + 170, + 150, + 85, + 85, + 85, + 90, + 165, + 85, + 106, + 106, + 166, + 105, + 165, + 150, + 149, + 85, + 90, + 170, + 85, + 165, + 85, + 86, + 170, + 170, + 165, + 105, + 86, + 85, + 169, + 105, + 89, + 165, + 170, + 169, + 149, + 170, + 85, + 85, + 170, + 165, + 86, + 170, + 105, + 85, + 154, + 170, + 106, + 170, + 85, + 105, + 85, + 86, + 165, + 105, + 166, + 170, + 169, + 165, + 89, + 85, + 86, + 90, + 169, + 86, + 149, + 170, + 85, + 90, + 106, + 170, + 169, + 85, + 165, + 85, + 90, + 85, + 165, + 170, + 170, + 102, + 170, + 153, + 85, + 90, + 170, + 89, + 170, + 170, + 85, + 85, + 86, + 170, + 106, + 170, + 149, + 89, + 85, + 150, + 101, + 105, + 154, + 170, + 170, + 166, + 85, + 85, + 101, + 102, + 90, + 170, + 165, + 150, + 85, + 102, + 154, + 170, + 170, + 149, + 86, + 85, + 89, + 102, + 165, + 106, + 170, + 170, + 149, + 101, + 85, + 101, + 154, + 166, + 106, + 165, + 85, + 85, + 106, + 106, + 170, + 170, + 85, + 85, + 85, + 89, + 86, + 170, + 170, + 170, + 165, + 105, + 85, + 86, + 101, + 170, + 170, + 101, + 165, + 85, + 86, + 170, + 170, + 170, + 165, + 85, + 85, + 85, + 101, + 170, + 106, + 101, + 170, + 105, + 101, + 153, + 85, + 101, + 106, + 149, + 101, + 149, + 89, + 166, + 170, + 170, + 165, + 85, + 85, + 90, + 85, + 105, + 150, + 169, + 106, + 90, + 153, + 101, + 85, + 153, + 166, + 150, + 85, + 166, + 154, + 170, + 170, + 170, + 85, + 149, + 89, + 149, + 101, + 106, + 102, + 169, + 170, + 101, + 89, + 90, + 85, + 166, + 154, + 86, + 166, + 166, + 154, + 169, + 86, + 85, + 89, + 90, + 85, + 170, + 154, + 165, + 105, + 105, + 105, + 85, + 165, + 89, + 166, + 154, + 169, + 150, + 150, + 105, + 102, + 149, + 105, + 90, + 85, + 150, + 105, + 106, + 90, + 165, + 105, + 90, + 90, + 89, + 90, + 105, + 106, + 154, + 153, + 165, + 149, + 105, + 105, + 149, + 105, + 102, + 154, + 105, + 105, + 170, + 102, + 150, + 154, + 101, + 154, + 85, + 102, + 154, + 149, + 170, + 149, + 165, + 169, + 154, + 105, + 106, + 89, + 101, + 165, + 89, + 169, + 106, + 105, + 102, + 89, + 150, + 101, + 169, + 101, + 86, + 165, + 106, + 150, + 149, + 101, + 165, + 169, + 86, + 169, + 102, + 170, + 150, + 153, + 105, + 154, + 89, + 165, + 102, + 86, + 150, + 105, + 149, + 150, + 150, + 149, + 165, + 169, + 106, + 165, + 105, + 149, + 105, + 166, + 90, + 102, + 102, + 101, + 153, + 149, + 90, + 102, + 102, + 89, + 169, + 169, + 106, + 86, + 154, + 149, + 165, + 106, + 150, + 149, + 169, + 105, + 150, + 165, + 154, + 150, + 105, + 106, + 150, + 153, + 90, + 165, + 101, + 166, + 166, + 149, + 154, + 89, + 105, + 169, + 105, + 85, + 154, + 105, + 86, + 165, + 90, + 165, + 102, + 90, + 86, + 169, + 150, + 150, + 165, + 150, + 86, + 165, + 150, + 106, + 150, + 150, + 89, + 105, + 102, + 149, + 105, + 105, + 90, + 150, + 105, + 90, + 85, + 154, + 105, + 150, + 165, + 170, + 102, + 105, + 170, + 149, + 149, + 165, + 90, + 105, + 170, + 90, + 101, + 169, + 154, + 102, + 154, + 90, + 102, + 165, + 101, + 166, + 165, + 101, + 154, + 89, + 101, + 165, + 105, + 102, + 149, + 169, + 105, + 106, + 102, + 85, + 150, + 165, + 102, + 86, + 165, + 102, + 90, + 85, + 165, + 85, + 169, + 102, + 90, + 89, + 149, + 169, + 106, + 150, + 90, + 89, + 86, + 90, + 101, + 154, + 90, + 101, + 153, + 169, + 165, + 105, + 154, + 150, + 86, + 153, + 150, + 105, + 165, + 86, + 90, + 101, + 90, + 101, + 169, + 150, + 85, + 165, + 90, + 89, + 154, + 90, + 90, + 153, + 106, + 150, + 153, + 169, + 106, + 149, + 106, + 105, + 153, + 166, + 90, + 89, + 165, + 86, + 105, + 154, + 150, + 165, + 150, + 102, + 149, + 150, + 101, + 149, + 165, + 102, + 169, + 105, + 105, + 166, + 90, + 89, + 102, + 105, + 169, + 106, + 165, + 105, + 149, + 105, + 102, + 85, + 149, + 102, + 90, + 85, + 165, + 89, + 166, + 150, + 90, + 150, + 101, + 86, + 165, + 154, + 85, + 105, + 90, + 154, + 150, + 102, + 169, + 150, + 169, + 89, + 90, + 169, + 90, + 150, + 153, + 106, + 86, + 153, + 166, + 150, + 165, + 86, + 150, + 150, + 154, + 85, + 85, + 165, + 154, + 149, + 106, + 90, + 85, + 154, + 90, + 105, + 105, + 85, + 150, + 105, + 90, + 165, + 149, + 166, + 90, + 165, + 106, + 149, + 170, + 149, + 166, + 169, + 149, + 165, + 165, + 170, + 105, + 102, + 101, + 169, + 150, + 154, + 90, + 105, + 89, + 102, + 165, + 90, + 86, + 150, + 165, + 170, + 150, + 105, + 154, + 89, + 149, +}; +const uint8_t DPCM_two_8000[] PROGMEM = { + 153, + 105, + 154, + 102, + 101, + 170, + 86, + 86, + 105, + 150, + 149, + 153, + 102, + 170, + 90, + 102, + 150, + 149, + 169, + 165, + 105, + 150, + 170, + 101, + 165, + 153, + 105, + 102, + 90, + 169, + 102, + 106, + 101, + 154, + 85, + 101, + 150, + 101, + 105, + 166, + 85, + 166, + 101, + 90, + 165, + 153, + 105, + 153, + 153, + 131, + 101, + 101, + 153, + 153, + 169, + 165, + 166, + 101, + 153, + 169, + 166, + 153, + 154, + 101, + 170, + 86, + 153, + 102, + 89, + 94, + 232, + 85, + 86, + 102, + 102, + 153, + 205, + 89, + 115, + 102, + 108, + 217, + 167, + 38, + 150, + 88, + 166, + 105, + 153, + 230, + 85, + 153, + 166, + 117, + 161, + 153, + 157, + 53, + 115, + 77, + 93, + 89, + 104, + 163, + 151, + 89, + 156, + 227, + 103, + 41, + 153, + 201, + 99, + 97, + 213, + 206, + 30, + 52, + 211, + 207, + 25, + 49, + 225, + 206, + 90, + 49, + 150, + 143, + 29, + 50, + 168, + 201, + 106, + 114, + 104, + 169, + 150, + 38, + 88, + 216, + 195, + 99, + 44, + 221, + 102, + 55, + 52, + 196, + 199, + 54, + 56, + 229, + 135, + 30, + 40, + 150, + 211, + 89, + 168, + 105, + 162, + 155, + 41, + 156, + 211, + 35, + 60, + 232, + 211, + 39, + 56, + 220, + 195, + 99, + 108, + 168, + 229, + 91, + 45, + 88, + 147, + 151, + 92, + 164, + 179, + 90, + 93, + 105, + 162, + 151, + 153, + 153, + 165, + 163, + 90, + 92, + 165, + 98, + 154, + 157, + 105, + 118, + 153, + 154, + 102, + 89, + 154, + 102, + 105, + 166, + 149, + 89, + 101, + 169, + 150, + 86, + 89, + 152, + 214, + 164, + 48, + 125, + 207, + 11, + 4, + 144, + 242, + 255, + 188, + 44, + 21, + 71, + 159, + 61, + 180, + 146, + 192, + 195, + 243, + 60, + 28, + 34, + 129, + 195, + 250, + 252, + 112, + 36, + 15, + 31, + 174, + 104, + 132, + 198, + 135, + 39, + 204, + 240, + 240, + 200, + 15, + 31, + 95, + 227, + 192, + 192, + 188, + 125, + 125, + 75, + 11, + 42, + 131, + 205, + 205, + 52, + 60, + 49, + 19, + 207, + 199, + 180, + 224, + 240, + 121, + 111, + 46, + 21, + 194, + 198, + 220, + 242, + 48, + 207, + 15, + 12, + 133, + 112, + 248, + 246, + 53, + 12, + 15, + 27, + 215, + 198, + 84, + 108, + 169, + 131, + 3, + 204, + 244, + 60, + 184, + 51, + 143, + 199, + 145, + 196, + 176, + 120, + 239, + 47, + 21, + 82, + 166, + 107, + 12, + 15, + 51, + 208, + 241, + 240, + 206, + 63, + 31, + 13, + 1, + 145, + 227, + 253, + 109, + 36, + 53, + 182, + 182, + 60, + 12, + 55, + 208, + 193, + 240, + 222, + 127, + 31, + 13, + 2, + 145, + 242, + 245, + 108, + 41, + 102, + 230, + 202, + 15, + 7, + 30, + 240, + 48, + 108, + 123, + 175, + 203, + 147, + 64, + 233, + 61, + 173, + 87, + 6, + 135, + 167, + 117, + 112, + 192, + 243, + 190, + 15, + 3, + 70, + 250, + 124, + 124, + 5, + 7, + 199, + 241, + 228, + 36, + 29, + 110, + 217, + 213, + 195, + 3, + 206, + 253, + 12, + 15, + 22, + 252, + 244, + 56, + 2, + 67, + 219, + 230, + 40, + 9, + 41, + 174, + 218, + 133, + 131, + 3, + 205, + 190, + 12, + 11, + 37, + 252, + 125, + 20, + 64, + 177, + 254, + 175, + 1, + 65, + 181, + 191, + 102, + 85, + 160, + 240, + 251, + 143, + 195, + 208, + 223, + 31, + 203, + 192, + 44, + 15, + 171, + 245, + 160, + 25, + 75, + 249, + 184, + 105, + 70, + 15, + 195, + 252, + 47, + 10, + 64, + 124, + 63, + 21, + 80, + 56, + 43, + 247, + 240, + 21, + 11, + 106, + 185, + 105, + 85, + 150, + 15, + 195, + 252, + 15, + 1, + 148, + 62, + 31, + 224, + 116, + 26, + 81, + 254, + 170, + 1, + 145, + 189, + 111, + 85, + 165, + 102, + 192, + 252, + 255, + 193, + 208, + 11, + 67, + 246, + 255, + 2, + 64, + 111, + 15, + 229, + 184, + 10, + 82, + 249, + 106, + 6, + 149, + 107, + 15, + 211, + 191, + 11, + 0, + 62, + 31, + 248, + 124, + 1, + 144, + 189, + 63, + 228, + 96, + 26, + 170, + 234, + 85, + 85, + 106, + 170, + 15, + 231, + 126, + 6, + 16, + 31, + 27, + 252, + 42, + 64, + 121, + 47, + 186, + 248, + 17, + 85, + 186, + 150, + 145, + 90, + 90, + 170, + 7, + 251, + 47, + 64, + 40, + 11, + 184, + 191, + 80, + 160, + 22, + 166, + 175, + 213, + 84, + 6, + 165, + 170, + 149, + 85, + 90, + 170, + 130, + 255, + 143, + 224, + 10, + 0, + 191, + 175, + 248, + 1, + 80, + 111, + 171, + 249, + 5, + 85, + 111, + 149, + 165, + 86, + 165, + 106, + 144, + 127, + 225, + 254, + 1, + 144, + 11, + 254, + 111, + 128, + 26, + 69, + 191, + 165, + 80, + 86, + 166, + 170, + 165, + 90, + 90, + 170, + 160, + 47, + 240, + 255, + 64, + 22, + 66, + 255, + 150, + 164, + 1, + 169, + 95, + 249, + 5, + 80, + 107, + 230, + 90, + 85, + 106, + 166, + 180, + 11, + 252, + 107, + 208, + 2, + 145, + 175, + 248, + 26, + 64, + 111, + 170, + 191, + 64, + 90, + 85, + 190, + 85, + 101, + 86, + 170, + 89, + 1, + 255, + 89, + 248, + 0, + 170, + 87, + 254, + 65, + 85, + 5, + 191, + 170, + 165, + 5, + 170, + 86, + 169, + 85, + 165, + 170, + 170, + 0, + 191, + 229, + 174, + 64, + 26, + 165, + 191, + 212, + 21, + 85, + 111, + 233, + 85, + 85, + 106, + 165, + 106, + 81, + 106, + 170, + 170, + 128, + 31, + 249, + 86, + 148, + 6, + 234, + 90, + 169, + 5, + 170, + 86, + 169, + 85, + 170, + 86, + 169, + 85, + 165, + 90, + 170, + 86, + 164, + 6, + 255, + 149, + 101, + 65, + 170, + 149, + 170, + 85, + 106, + 149, + 170, + 149, + 90, + 149, + 170, + 85, + 90, + 165, + 90, + 169, + 105, + 85, + 171, + 165, + 89, + 85, + 106, + 165, + 90, + 149, + 86, + 169, + 90, + 169, + 86, + 165, + 90, + 165, + 86, + 170, + 90, + 169, + 85, + 170, + 149, + 170, + 149, + 90, + 85, + 170, + 149, + 90, + 165, + 90, + 165, + 170, + 149, + 86, + 170, + 169, + 85, + 170, + 106, + 169, + 86, + 150, + 90, + 170, + 85, + 85, + 90, + 170, + 86, + 149, + 90, + 170, + 165, + 165, + 85, + 105, + 106, + 169, + 85, + 102, + 170, + 170, + 149, + 90, + 165, + 90, + 149, + 101, + 106, + 90, + 165, + 165, + 89, + 106, + 165, + 165, + 149, + 85, + 165, + 170, + 153, + 86, + 170, + 90, + 169, + 85, + 169, + 106, + 86, + 86, + 169, + 86, + 166, + 85, + 170, + 154, + 166, + 85, + 169, + 85, + 170, + 86, + 85, + 85, + 170, + 86, + 170, + 85, + 106, + 149, + 85, + 101, + 165, + 169, + 169, + 101, + 150, + 150, + 166, + 149, + 150, + 170, + 86, + 89, + 149, + 106, + 89, + 106, + 106, + 86, + 106, + 170, + 86, + 149, + 90, + 165, + 150, + 165, + 154, + 165, + 169, + 85, + 105, + 166, + 105, + 85, + 169, + 90, + 101, + 154, + 90, + 90, + 149, + 170, + 149, + 166, + 89, + 170, + 85, + 102, + 89, + 166, + 165, + 105, + 165, + 166, + 106, + 165, + 101, + 101, + 90, + 89, + 106, + 149, + 170, + 154, + 106, + 86, + 89, + 170, + 166, + 85, + 149, + 106, + 89, + 86, + 101, + 170, + 90, + 105, + 106, + 90, + 101, + 165, + 89, + 105, + 154, + 150, + 85, + 165, + 105, + 153, + 169, + 86, + 154, + 106, + 153, + 85, + 153, + 106, + 150, + 89, + 105, + 154, + 165, + 165, + 150, + 105, + 106, + 150, + 153, + 90, + 85, + 106, + 149, + 86, + 89, + 102, + 154, + 85, + 169, + 105, + 170, + 85, + 105, + 102, + 153, + 153, + 86, + 153, + 106, + 90, + 149, + 165, +}; +const uint8_t DPCM_three_8000[] PROGMEM = { + 153, + 149, + 169, + 89, + 150, + 89, + 166, + 102, + 102, + 165, + 153, + 102, + 90, + 90, + 101, + 154, + 149, + 166, + 105, + 106, + 102, + 154, + 105, + 150, + 153, + 102, + 166, + 149, + 150, + 150, + 89, + 166, + 90, + 149, + 105, + 90, + 165, + 105, + 90, + 89, + 169, + 90, + 105, + 170, + 90, + 86, + 150, + 150, + 149, + 149, + 170, + 101, + 150, + 89, + 85, + 165, + 150, + 169, + 101, + 165, + 86, + 150, + 149, + 106, + 89, + 106, + 86, + 89, + 166, + 90, + 149, + 105, + 90, + 166, + 89, + 165, + 90, + 105, + 105, + 166, + 165, + 166, + 89, + 105, + 169, + 150, + 150, + 90, + 90, + 85, + 101, + 169, + 153, + 86, + 89, + 102, + 149, + 154, + 149, + 149, + 102, + 165, + 166, + 86, + 102, + 165, + 169, + 149, + 90, + 106, + 150, + 102, + 105, + 149, + 165, + 101, + 149, + 166, + 166, + 85, + 165, + 165, + 105, + 89, + 169, + 102, + 166, + 98, + 217, + 89, + 149, + 153, + 89, + 150, + 90, + 102, + 105, + 105, + 150, + 170, + 88, + 101, + 105, + 154, + 60, + 182, + 89, + 166, + 153, + 153, + 117, + 102, + 101, + 165, + 213, + 153, + 117, + 170, + 150, + 138, + 90, + 41, + 121, + 166, + 214, + 102, + 153, + 153, + 102, + 105, + 149, + 170, + 86, + 89, + 105, + 113, + 226, + 214, + 90, + 89, + 85, + 106, + 170, + 170, + 85, + 85, + 89, + 170, + 105, + 150, + 154, + 106, + 96, + 252, + 62, + 47, + 129, + 160, + 104, + 47, + 155, + 229, + 101, + 85, + 149, + 170, + 169, + 150, + 150, + 166, + 67, + 225, + 252, + 126, + 1, + 128, + 56, + 47, + 159, + 209, + 84, + 26, + 91, + 150, + 229, + 160, + 165, + 170, + 67, + 240, + 252, + 47, + 0, + 132, + 15, + 71, + 226, + 248, + 5, + 129, + 180, + 189, + 106, + 84, + 105, + 47, + 80, + 252, + 63, + 7, + 192, + 37, + 3, + 224, + 253, + 47, + 65, + 160, + 46, + 75, + 210, + 164, + 26, + 91, + 224, + 63, + 15, + 193, + 240, + 25, + 64, + 248, + 63, + 15, + 192, + 180, + 31, + 71, + 228, + 184, + 11, + 71, + 224, + 15, + 195, + 240, + 252, + 15, + 1, + 125, + 47, + 131, + 192, + 244, + 47, + 75, + 228, + 248, + 25, + 90, + 233, + 131, + 240, + 252, + 47, + 3, + 192, + 226, + 95, + 211, + 240, + 56, + 30, + 11, + 230, + 244, + 184, + 31, + 15, + 196, + 63, + 15, + 195, + 240, + 240, + 60, + 63, + 253, + 252, + 56, + 13, + 15, + 95, + 174, + 73, + 66, + 147, + 230, + 160, + 240, + 252, + 61, + 15, + 3, + 2, + 190, + 191, + 15, + 3, + 131, + 195, + 226, + 240, + 164, + 27, + 75, + 199, + 131, + 195, + 240, + 240, + 240, + 8, + 71, + 227, + 240, + 240, + 48, + 60, + 47, + 47, + 7, + 130, + 162, + 123, + 167, + 15, + 15, + 67, + 192, + 224, + 52, + 30, + 31, + 15, + 193, + 65, + 105, + 189, + 125, + 41, + 25, + 26, + 126, + 122, + 15, + 15, + 7, + 196, + 112, + 52, + 60, + 63, + 223, + 195, + 67, + 195, + 211, + 241, + 184, + 40, + 58, + 123, + 246, + 96, + 192, + 243, + 248, + 76, + 42, + 14, + 143, + 247, + 209, + 192, + 17, + 61, + 125, + 121, + 16, + 69, + 95, + 175, + 157, + 67, + 3, + 207, + 241, + 176, + 208, + 50, + 63, + 159, + 47, + 8, + 3, + 199, + 203, + 194, + 128, + 96, + 170, + 250, + 184, + 131, + 199, + 203, + 240, + 244, + 176, + 52, + 95, + 127, + 14, + 5, + 2, + 86, + 247, + 211, + 128, + 64, + 181, + 245, + 241, + 225, + 12, + 63, + 63, + 143, + 3, + 2, + 135, + 243, + 242, + 192, + 80, + 36, + 189, + 253, + 240, + 80, + 36, + 125, + 250, + 179, + 73, + 28, + 62, + 63, + 79, + 7, + 2, + 75, + 215, + 231, + 194, + 65, + 65, + 169, + 252, + 240, + 208, + 144, + 169, + 249, + 248, + 176, + 195, + 15, + 207, + 243, + 192, + 193, + 33, + 249, + 247, + 240, + 160, + 80, + 113, + 239, + 251, + 224, + 64, + 85, + 186, + 250, + 226, + 98, + 12, + 63, + 63, + 142, + 11, + 4, + 15, + 95, + 159, + 199, + 3, + 6, + 71, + 143, + 159, + 70, + 5, + 10, + 91, + 155, + 135, + 75, + 12, + 63, + 63, + 207, + 11, + 8, + 15, + 31, + 159, + 159, + 2, + 6, + 9, + 219, + 175, + 153, + 65, + 5, + 90, + 170, + 169, + 150, + 131, + 207, + 207, + 243, + 194, + 130, + 2, + 199, + 235, + 231, + 194, + 66, + 66, + 139, + 159, + 186, + 149, + 65, + 86, + 166, + 230, + 166, + 147, + 12, + 62, + 63, + 44, + 28, + 16, + 13, + 47, + 127, + 174, + 29, + 24, + 40, + 44, + 126, + 185, + 100, + 20, + 89, + 170, + 170, + 169, + 172, + 48, + 252, + 254, + 56, + 116, + 80, + 52, + 125, + 253, + 184, + 180, + 96, + 160, + 242, + 231, + 218, + 85, + 85, + 86, + 170, + 170, + 166, + 86, + 44, + 223, + 123, + 60, + 148, + 32, + 24, + 174, + 190, + 189, + 100, + 32, + 85, + 93, + 186, + 246, + 149, + 85, + 86, + 166, + 166, + 166, + 90, + 12, + 51, + 63, + 220, + 117, + 32, + 8, + 106, + 190, + 254, + 165, + 20, + 21, + 155, + 105, + 170, + 166, + 85, + 85, + 150, + 169, + 165, + 166, + 131, + 12, + 223, + 183, + 77, + 69, + 18, + 10, + 175, + 190, + 169, + 149, + 85, + 86, + 109, + 246, + 218, + 149, + 86, + 86, + 154, + 166, + 170, + 104, + 56, + 215, + 183, + 124, + 208, + 81, + 20, + 150, + 183, + 234, + 153, + 85, + 85, + 86, + 154, + 170, + 165, + 149, + 85, + 106, + 170, + 169, + 105, + 152, + 50, + 205, + 223, + 179, + 196, + 129, + 18, + 154, + 235, + 170, + 149, + 85, + 86, + 86, + 158, + 106, + 101, + 85, + 85, + 106, + 170, + 105, + 105, + 147, + 206, + 59, + 119, + 205, + 85, + 33, + 73, + 170, + 106, + 169, + 165, + 85, + 85, + 150, + 106, + 170, + 89, + 85, + 85, + 170, + 170, + 154, + 154, + 97, + 195, + 79, + 110, + 154, + 89, + 24, + 25, + 106, + 170, + 169, + 165, + 85, + 86, + 154, + 106, + 170, + 153, + 85, + 101, + 170, + 166, + 150, + 90, + 84, + 210, + 151, + 154, + 154, + 86, + 69, + 85, + 106, + 170, + 170, + 105, + 85, + 85, + 102, + 169, + 170, + 150, + 85, + 89, + 106, + 154, + 105, + 150, + 101, + 89, + 106, + 106, + 169, + 165, + 101, + 85, + 150, + 170, + 170, + 165, + 86, + 86, + 90, + 170, + 105, + 105, + 101, + 150, + 154, + 89, + 102, + 153, + 165, + 154, + 106, + 106, + 90, + 89, + 105, + 165, + 150, + 154, + 86, + 90, + 89, + 105, + 101, + 165, + 170, + 165, + 149, + 150, + 150, + 90, + 105, + 105, + 166, + 150, + 89, + 150, + 154, + 90, + 105, + 105, + 101, + 150, + 165, + 166, + 154, + 89, + 101, + 166, + 89, + 105, + 166, + 154, + 89, + 105, + 101, + 166, + 154, + 101, + 150, + 86, + 154, + 105, + 105, + 105, + 165, + 150, + 150, + 154, + 90, + 101, + 86, + 89, + 90, + 105, + 105, + 169, + 102, + 150, + 90, + 89, + 90, + 86, + 90, + 90, + 89, + 106, + 85, + 153, + 85, + 102, + 106, + 105, + 165, + 166, + 150, + 89, + 170, + 89, + 105, + 105, + 165, + 150, + 154, + 89, + 101, + 170, + 149, + 102, + 166, + 105, + 169, + 165, + 166, + 150, + 150, + 86, + 154, + 89, + 105, + 165, + 165, + 166, + 149, + 169, + 150, + 154, + 149, + 89, + 86, + 90, + 102, + 150, + 105, + 90, + 153, + 169, + 166, + 149, + 105, + 101, + 90, + 165, + 166, + 166, + 150, + 170, + 105, + 85, + 170, + 86, + 90, + 153, + 101, + 102, + 166, + 153, + 166, + 169, + 102, + 166, + 149, + 105, + 85, + 150, + 106, + 89, + 165, + 166, + 90, + 169, + 170, + 101, + 86, + 149, + 105, + 89, + 150, + 106, + 165, + 165, + 90, + 86, + 149, + 149, + 166, + 90, + 102, +}; +const uint8_t DPCM_four_8000[] PROGMEM = { + 153, + 89, + 166, + 105, + 89, + 166, + 105, + 90, + 90, + 89, + 102, + 90, + 89, + 166, + 154, + 89, + 165, + 150, + 166, + 150, + 149, + 105, + 102, + 105, + 89, + 106, + 89, + 101, + 90, + 90, + 149, + 165, + 165, + 105, + 150, + 105, + 101, + 90, + 154, + 150, + 90, + 105, + 166, + 102, + 90, + 85, + 90, + 165, + 102, + 90, + 149, + 105, + 106, + 85, + 102, + 90, + 86, + 102, + 149, + 165, + 153, + 102, + 106, + 154, + 101, + 165, + 150, + 153, + 169, + 101, + 170, + 153, + 153, + 105, + 105, + 165, + 86, + 169, + 105, + 105, + 165, + 106, + 165, + 169, + 169, + 106, + 89, + 149, + 101, + 153, + 106, + 89, + 101, + 102, + 149, + 90, + 154, + 90, + 165, + 90, + 90, + 105, + 101, + 170, + 85, + 106, + 165, + 106, + 154, + 166, + 101, + 169, + 169, + 85, + 170, + 149, + 105, + 165, + 90, + 166, + 85, + 85, + 86, + 191, + 228, + 1, + 106, + 170, + 169, + 85, + 105, + 86, + 170, + 170, + 144, + 11, + 255, + 208, + 1, + 230, + 5, + 191, + 244, + 0, + 107, + 170, + 169, + 85, + 90, + 169, + 106, + 169, + 80, + 3, + 255, + 240, + 0, + 186, + 129, + 127, + 252, + 0, + 11, + 255, + 169, + 80, + 70, + 90, + 170, + 169, + 149, + 64, + 15, + 255, + 192, + 2, + 238, + 21, + 255, + 224, + 0, + 111, + 250, + 166, + 85, + 21, + 170, + 154, + 89, + 85, + 128, + 15, + 255, + 192, + 2, + 239, + 20, + 255, + 244, + 0, + 47, + 250, + 170, + 149, + 5, + 170, + 170, + 150, + 89, + 164, + 3, + 255, + 248, + 0, + 47, + 229, + 31, + 254, + 64, + 3, + 255, + 250, + 165, + 65, + 90, + 186, + 165, + 85, + 106, + 64, + 47, + 255, + 128, + 2, + 254, + 80, + 255, + 244, + 0, + 47, + 254, + 149, + 85, + 21, + 171, + 170, + 101, + 85, + 170, + 64, + 63, + 255, + 64, + 3, + 254, + 81, + 255, + 228, + 0, + 127, + 250, + 86, + 85, + 21, + 170, + 170, + 165, + 85, + 106, + 144, + 7, + 255, + 224, + 0, + 191, + 165, + 127, + 249, + 0, + 31, + 254, + 81, + 106, + 85, + 90, + 170, + 149, + 85, + 106, + 170, + 0, + 63, + 255, + 0, + 7, + 254, + 65, + 255, + 208, + 0, + 191, + 229, + 86, + 169, + 85, + 170, + 165, + 89, + 85, + 170, + 170, + 0, + 47, + 255, + 64, + 7, + 253, + 81, + 255, + 224, + 0, + 191, + 229, + 90, + 169, + 85, + 170, + 154, + 105, + 85, + 86, + 170, + 128, + 11, + 255, + 192, + 2, + 254, + 64, + 191, + 244, + 0, + 111, + 249, + 86, + 149, + 85, + 170, + 150, + 170, + 85, + 90, + 170, + 168, + 0, + 255, + 248, + 0, + 47, + 229, + 111, + 253, + 0, + 47, + 250, + 154, + 169, + 85, + 170, + 170, + 170, + 149, + 86, + 170, + 170, + 0, + 47, + 254, + 0, + 11, + 233, + 27, + 253, + 0, + 27, + 250, + 90, + 169, + 1, + 170, + 149, + 106, + 165, + 85, + 170, + 170, + 160, + 3, + 255, + 192, + 6, + 249, + 7, + 255, + 64, + 27, + 249, + 107, + 229, + 1, + 174, + 165, + 105, + 85, + 169, + 85, + 170, + 164, + 0, + 255, + 240, + 1, + 254, + 6, + 255, + 128, + 11, + 229, + 91, + 232, + 1, + 190, + 166, + 149, + 86, + 170, + 85, + 170, + 149, + 128, + 15, + 255, + 0, + 46, + 80, + 255, + 208, + 7, + 233, + 27, + 248, + 1, + 170, + 90, + 165, + 22, + 185, + 85, + 170, + 149, + 168, + 1, + 255, + 192, + 7, + 208, + 127, + 240, + 6, + 249, + 27, + 249, + 1, + 170, + 90, + 169, + 22, + 170, + 90, + 149, + 106, + 150, + 96, + 15, + 253, + 0, + 105, + 7, + 255, + 0, + 121, + 70, + 254, + 1, + 169, + 90, + 233, + 86, + 165, + 106, + 149, + 106, + 165, + 107, + 0, + 191, + 208, + 6, + 1, + 191, + 192, + 26, + 70, + 255, + 64, + 105, + 90, + 249, + 90, + 149, + 106, + 165, + 165, + 85, + 170, + 148, + 3, + 255, + 0, + 0, + 31, + 252, + 5, + 85, + 127, + 149, + 89, + 90, + 170, + 90, + 149, + 90, + 169, + 169, + 86, + 169, + 106, + 144, + 63, + 240, + 0, + 1, + 255, + 144, + 80, + 47, + 249, + 105, + 5, + 190, + 106, + 148, + 90, + 86, + 170, + 85, + 90, + 170, + 170, + 64, + 255, + 192, + 0, + 31, + 253, + 84, + 0, + 191, + 169, + 84, + 42, + 169, + 170, + 85, + 105, + 106, + 149, + 101, + 90, + 170, + 170, + 1, + 255, + 64, + 64, + 127, + 249, + 144, + 26, + 254, + 164, + 5, + 170, + 111, + 149, + 105, + 91, + 229, + 101, + 86, + 150, + 165, + 106, + 64, + 255, + 208, + 0, + 63, + 254, + 144, + 10, + 255, + 169, + 5, + 91, + 170, + 149, + 86, + 170, + 229, + 105, + 86, + 165, + 190, + 90, + 144, + 63, + 246, + 64, + 15, + 255, + 228, + 7, + 171, + 250, + 1, + 150, + 233, + 101, + 85, + 186, + 105, + 86, + 150, + 170, + 90, + 149, + 169, + 3, + 255, + 164, + 0, + 254, + 254, + 0, + 41, + 191, + 149, + 21, + 91, + 150, + 149, + 170, + 149, + 85, + 106, + 165, + 106, + 90, + 149, + 184, + 15, + 255, + 160, + 2, + 251, + 248, + 0, + 102, + 254, + 85, + 85, + 170, + 90, + 86, + 170, + 149, + 90, + 170, + 165, + 90, + 170, + 86, + 228, + 15, + 251, + 224, + 2, + 235, + 244, + 4, + 90, + 249, + 85, + 85, + 170, + 106, + 86, + 149, + 165, + 106, + 165, + 165, + 106, + 165, + 149, + 185, + 1, + 249, + 189, + 1, + 169, + 190, + 6, + 86, + 170, + 90, + 85, + 165, + 170, + 90, + 86, + 165, + 169, + 90, + 149, + 169, + 90, + 90, + 149, + 169, + 11, + 234, + 228, + 6, + 150, + 229, + 85, + 90, + 165, + 101, + 85, + 106, + 90, + 85, + 170, + 170, + 149, + 85, + 170, + 169, + 85, + 106, + 85, + 126, + 170, + 85, + 90, + 170, + 85, + 86, + 170, + 149, + 90, + 150, + 165, + 85, + 169, + 106, + 86, + 85, + 170, + 85, + 90, + 105, + 90, + 170, + 169, + 86, + 106, + 154, + 86, + 149, + 150, + 170, + 86, + 85, + 169, + 169, + 90, + 150, + 165, + 86, + 86, + 169, + 90, + 86, + 165, + 165, + 86, + 149, + 165, + 165, + 101, + 89, + 165, + 105, + 105, + 85, + 170, + 169, + 85, + 105, + 106, + 149, + 106, + 90, + 150, + 165, + 106, + 86, + 149, + 101, + 165, + 90, + 150, + 150, + 150, + 149, + 86, + 90, + 150, + 165, + 106, + 85, + 106, + 165, + 165, + 106, + 86, + 149, + 169, + 105, + 90, + 85, + 170, + 169, + 90, + 150, + 149, + 86, + 169, + 85, + 106, + 169, + 85, + 105, + 169, + 86, + 86, + 166, + 105, + 105, + 85, + 90, + 106, + 105, + 86, + 150, + 165, + 149, + 106, + 90, + 86, + 169, + 105, + 86, + 150, + 169, + 86, + 170, + 85, + 150, + 165, + 90, + 90, + 149, + 165, + 105, + 90, + 86, + 85, + 165, + 105, + 101, + 149, + 169, + 169, + 86, + 89, + 170, + 89, + 90, + 106, + 90, + 101, + 169, + 85, + 106, + 165, + 150, + 105, + 166, + 102, + 149, + 90, + 106, + 149, + 150, + 106, + 89, + 106, + 90, + 149, + 106, + 85, + 150, + 170, + 101, + 149, + 150, +}; +const uint8_t DPCM_five_8000[] PROGMEM = { + 154, + 102, + 105, + 153, + 154, + 105, + 105, + 102, + 153, + 102, + 153, + 86, + 85, + 165, + 150, + 150, + 149, + 165, + 102, + 150, + 165, + 90, + 106, + 153, + 101, + 86, + 149, + 106, + 86, + 165, + 169, + 102, + 106, + 150, + 105, + 105, + 166, + 154, + 101, + 150, + 85, + 166, + 90, + 101, + 106, + 165, + 90, + 89, + 170, + 86, + 170, + 89, + 102, + 86, + 150, + 90, + 153, + 106, + 149, + 170, + 168, + 2, + 255, + 64, + 5, + 11, + 254, + 0, + 86, + 175, + 229, + 21, + 170, + 165, + 86, + 170, + 85, + 105, + 4, + 255, + 64, + 15, + 27, + 252, + 0, + 191, + 219, + 164, + 6, + 250, + 149, + 21, + 190, + 145, + 111, + 149, + 106, + 4, + 255, + 64, + 15, + 159, + 252, + 0, + 255, + 213, + 100, + 7, + 249, + 5, + 106, + 186, + 65, + 127, + 145, + 106, + 144, + 95, + 244, + 2, + 249, + 255, + 192, + 31, + 244, + 1, + 70, + 255, + 64, + 106, + 170, + 84, + 106, + 149, + 175, + 149, + 65, + 127, + 160, + 15, + 247, + 190, + 0, + 191, + 192, + 10, + 107, + 244, + 7, + 229, + 90, + 165, + 85, + 91, + 229, + 74, + 192, + 127, + 160, + 15, + 241, + 126, + 0, + 255, + 128, + 47, + 171, + 224, + 31, + 224, + 11, + 164, + 85, + 107, + 228, + 27, + 240, + 15, + 248, + 3, + 252, + 111, + 128, + 63, + 208, + 47, + 135, + 244, + 11, + 244, + 11, + 169, + 102, + 90, + 164, + 43, + 182, + 3, + 253, + 0, + 255, + 91, + 224, + 15, + 248, + 10, + 161, + 253, + 6, + 252, + 6, + 170, + 89, + 86, + 249, + 6, + 170, + 176, + 47, + 208, + 15, + 246, + 253, + 1, + 255, + 0, + 170, + 111, + 64, + 191, + 148, + 85, + 170, + 69, + 190, + 81, + 170, + 91, + 1, + 254, + 0, + 255, + 111, + 192, + 47, + 224, + 25, + 106, + 245, + 7, + 238, + 80, + 47, + 228, + 27, + 165, + 154, + 149, + 172, + 11, + 248, + 3, + 254, + 253, + 0, + 255, + 132, + 16, + 191, + 129, + 125, + 165, + 66, + 254, + 1, + 190, + 150, + 165, + 86, + 176, + 15, + 240, + 15, + 251, + 244, + 3, + 255, + 20, + 2, + 250, + 69, + 100, + 127, + 65, + 254, + 22, + 150, + 170, + 85, + 170, + 188, + 15, + 248, + 3, + 191, + 252, + 2, + 255, + 168, + 2, + 254, + 65, + 154, + 173, + 85, + 170, + 86, + 170, + 149, + 85, + 185, + 169, + 15, + 252, + 1, + 203, + 252, + 0, + 255, + 249, + 1, + 254, + 86, + 85, + 190, + 6, + 169, + 89, + 106, + 164, + 86, + 165, + 95, + 192, + 255, + 64, + 52, + 255, + 128, + 63, + 255, + 128, + 47, + 217, + 21, + 169, + 150, + 154, + 89, + 169, + 170, + 69, + 170, + 90, + 104, + 15, + 248, + 1, + 15, + 248, + 2, + 171, + 244, + 6, + 254, + 225, + 90, + 90, + 165, + 165, + 106, + 86, + 169, + 22, + 249, + 85, + 180, + 63, + 240, + 0, + 63, + 208, + 5, + 127, + 208, + 22, + 191, + 128, + 106, + 170, + 101, + 165, + 106, + 149, + 85, + 106, + 165, + 86, + 244, + 63, + 224, + 0, + 63, + 224, + 64, + 191, + 128, + 85, + 191, + 64, + 106, + 170, + 149, + 106, + 165, + 86, + 170, + 85, + 169, + 90, + 172, + 15, + 244, + 64, + 15, + 228, + 16, + 47, + 213, + 64, + 63, + 229, + 70, + 190, + 85, + 170, + 149, + 90, + 169, + 149, + 171, + 149, + 90, + 195, + 255, + 12, + 3, + 250, + 132, + 11, + 253, + 144, + 7, + 250, + 84, + 46, + 166, + 134, + 169, + 105, + 86, + 149, + 85, + 169, + 85, + 172, + 63, + 241, + 192, + 63, + 125, + 128, + 191, + 174, + 0, + 106, + 249, + 81, + 170, + 169, + 86, + 165, + 86, + 170, + 85, + 106, + 149, + 90, + 160, + 255, + 203, + 0, + 254, + 249, + 2, + 175, + 248, + 1, + 91, + 229, + 65, + 110, + 169, + 86, + 150, + 169, + 105, + 165, + 154, + 105, + 90, + 131, + 255, + 92, + 2, + 251, + 244, + 6, + 111, + 244, + 0, + 111, + 229, + 66, + 170, + 169, + 25, + 106, + 165, + 86, + 106, + 165, + 85, + 153, + 15, + 252, + 224, + 31, + 239, + 192, + 37, + 191, + 149, + 1, + 190, + 164, + 86, + 155, + 165, + 85, + 170, + 150, + 90, + 106, + 165, + 86, + 240, + 255, + 223, + 0, + 189, + 253, + 1, + 71, + 254, + 100, + 22, + 171, + 149, + 25, + 110, + 149, + 86, + 169, + 105, + 90, + 154, + 165, + 105, + 104, + 63, + 247, + 192, + 30, + 127, + 64, + 66, + 255, + 169, + 5, + 106, + 229, + 85, + 106, + 169, + 85, + 170, + 149, + 86, + 170, + 149, + 86, + 104, + 63, + 251, + 192, + 22, + 127, + 84, + 65, + 170, + 233, + 5, + 90, + 233, + 165, + 86, + 154, + 150, + 85, + 105, + 170, + 90, + 149, + 90, + 100, + 63, + 251, + 192, + 69, + 127, + 169, + 65, + 170, + 234, + 85, + 86, + 170, + 165, + 86, + 90, + 169, + 105, + 85, + 165, + 154, + 149, + 90, + 144, + 255, + 255, + 1, + 5, + 190, + 169, + 65, + 150, + 170, + 89, + 85, + 90, + 170, + 165, + 89, + 154, + 165, + 85, + 149, + 106, + 89, + 102, + 160, + 191, + 251, + 128, + 69, + 126, + 170, + 145, + 101, + 170, + 154, + 85, + 89, + 106, + 105, + 149, + 150, + 170, + 90, + 85, + 165, + 165, + 90, + 144, + 106, + 175, + 145, + 85, + 106, + 170, + 165, + 85, + 85, + 170, + 165, + 86, + 85, + 169, + 106, + 149, + 149, + 90, + 90, + 153, + 85, + 106, + 164, + 106, + 170, + 165, + 85, + 86, + 150, + 169, + 106, + 89, + 149, + 169, + 170, + 149, + 85, + 170, + 166, + 149, + 105, + 90, + 154, + 149, + 154, + 165, + 85, + 90, + 170, + 169, + 85, + 86, + 85, + 106, + 169, + 85, + 86, + 170, + 150, + 85, + 101, + 150, + 105, + 89, + 150, + 90, + 105, + 165, + 89, + 170, + 170, + 165, + 85, + 154, + 102, + 149, + 154, + 154, + 105, + 90, + 105, + 169, + 166, + 90, + 150, + 149, + 106, + 85, + 149, + 153, + 170, + 101, + 86, + 150, + 105, + 153, + 165, + 105, + 166, + 90, + 90, + 149, + 169, + 101, + 105, + 165, + 102, + 153, + 102, + 89, + 165, + 165, + 149, + 170, + 166, + 90, + 165, + 170, + 101, + 101, + 105, + 153, + 149, + 166, + 150, + 153, + 101, + 169, + 90, + 102, + 150, + 154, + 86, + 153, + 89, + 102, + 165, + 149, + 105, + 102, + 165, + 150, + 90, + 86, + 102, + 149, + 170, + 90, + 90, + 89, + 165, + 101, + 150, + 86, + 106, + 170, + 149, + 106, + 154, + 85, + 150, + 105, + 165, + 165, + 166, + 153, + 106, + 149, + 150, + 149, + 106, + 85, + 105, + 101, + 150, + 154, + 90, + 90, + 102, + 149, + 169, + 89, + 89, + 166, + 153, + 106, + 165, + 105, + 89, + 150, + 169, + 101, + 101, + 154, + 90, + 102, + 90, + 89, + 165, + 165, + 150, + 166, + 90, + 90, + 89, + 165, + 166, + 154, + 106, + 86, + 149, + 105, + 170, + 101, + 150, + 102, + 154, + 101, + 150, + 89, + 106, + 154, + 90, + 105, + 90, + 165, + 165, + 166, + 150, + 154, + 105, + 101, + 170, + 166, + 150, + 90, + 90, + 86, + 101, + 166, + 90, + 149, + 86, + 166, + 153, + 101, +}; +const uint8_t DPCM_six_8000[] PROGMEM = { + 153, + 153, + 90, + 105, + 165, + 150, + 102, + 101, + 90, + 105, + 105, + 154, + 89, + 165, + 166, + 102, + 101, + 153, + 101, + 150, + 169, + 90, + 169, + 153, + 102, + 149, + 166, + 89, + 102, + 149, + 169, + 154, + 101, + 89, + 102, + 101, + 165, + 102, + 105, + 165, + 101, + 153, + 150, + 105, + 102, + 86, + 101, + 153, + 153, + 166, + 86, + 90, + 89, + 90, + 153, + 166, + 101, + 101, + 102, + 89, + 153, + 166, + 102, + 149, + 149, + 153, + 150, + 101, + 154, + 150, + 102, + 105, + 153, + 153, + 166, + 89, + 153, + 105, + 150, + 106, + 105, + 150, + 154, + 106, + 101, + 154, + 154, + 89, + 153, + 150, + 153, + 166, + 102, + 105, + 153, + 102, + 102, + 89, + 153, + 150, + 102, + 169, + 102, + 153, + 154, + 102, + 89, + 153, + 153, + 150, + 101, + 153, + 153, + 150, + 102, + 102, + 106, + 101, + 153, + 153, + 154, + 101, + 153, + 150, + 102, + 102, + 153, + 153, + 153, + 102, + 105, + 153, + 166, + 102, + 102, + 102, + 153, + 153, + 153, + 102, + 153, + 150, + 102, + 102, + 102, + 153, + 153, + 153, + 165, + 102, + 105, + 153, + 150, + 102, + 101, + 154, + 153, + 166, + 89, + 154, + 166, + 102, + 153, + 153, + 153, + 150, + 102, + 154, + 90, + 102, + 85, + 153, + 150, + 105, + 154, + 101, + 153, + 153, + 102, + 102, + 166, + 154, + 154, + 166, + 153, + 153, + 101, + 150, + 102, + 106, + 89, + 102, + 101, + 153, + 166, + 150, + 89, + 153, + 169, + 102, + 154, + 154, + 106, + 102, + 89, + 101, + 153, + 153, + 90, + 90, + 101, + 154, + 105, + 154, + 101, + 102, + 166, + 89, + 105, + 153, + 166, + 105, + 154, + 101, + 154, + 102, + 105, + 153, + 153, + 153, + 105, + 101, + 153, + 86, + 106, + 102, + 102, + 89, + 153, + 153, + 150, + 105, + 105, + 166, + 106, + 105, + 153, + 150, + 153, + 166, + 154, + 106, + 105, + 150, + 89, + 102, + 150, + 154, + 153, + 149, + 166, + 102, + 105, + 153, + 154, + 166, + 86, + 101, + 166, + 169, + 153, + 101, + 150, + 90, + 105, + 150, + 166, + 169, + 154, + 144, + 181, + 111, + 95, + 148, + 144, + 85, + 106, + 171, + 230, + 148, + 85, + 106, + 170, + 102, + 101, + 86, + 160, + 217, + 239, + 63, + 16, + 192, + 141, + 63, + 123, + 129, + 64, + 105, + 255, + 170, + 5, + 86, + 151, + 234, + 117, + 39, + 12, + 207, + 227, + 241, + 56, + 28, + 207, + 219, + 48, + 32, + 143, + 175, + 117, + 65, + 70, + 188, + 245, + 102, + 26, + 33, + 205, + 252, + 60, + 19, + 67, + 205, + 248, + 177, + 5, + 14, + 223, + 215, + 64, + 32, + 190, + 174, + 14, + 6, + 151, + 15, + 59, + 240, + 208, + 76, + 46, + 59, + 195, + 128, + 80, + 246, + 255, + 44, + 5, + 71, + 219, + 164, + 161, + 89, + 111, + 195, + 199, + 252, + 48, + 51, + 15, + 79, + 241, + 209, + 64, + 60, + 255, + 158, + 5, + 86, + 234, + 229, + 100, + 101, + 185, + 124, + 60, + 47, + 195, + 130, + 16, + 248, + 254, + 110, + 9, + 10, + 219, + 219, + 160, + 96, + 169, + 189, + 104, + 26, + 90, + 138, + 220, + 56, + 127, + 207, + 15, + 2, + 242, + 246, + 232, + 52, + 58, + 175, + 154, + 65, + 86, + 166, + 165, + 165, + 101, + 106, + 153, + 156, + 60, + 63, + 207, + 7, + 2, + 162, + 246, + 224, + 32, + 40, + 126, + 170, + 70, + 6, + 150, + 154, + 150, + 85, + 101, + 181, + 182, + 67, + 79, + 235, + 224, + 224, + 225, + 190, + 125, + 24, + 2, + 71, + 235, + 229, + 80, + 21, + 170, + 170, + 85, + 85, + 86, + 170, + 170, + 67, + 203, + 207, + 224, + 176, + 240, + 250, + 190, + 28, + 9, + 26, + 171, + 219, + 130, + 65, + 166, + 250, + 169, + 85, + 85, + 170, + 170, + 102, + 86, + 154, + 154, + 90, + 149, + 150, + 166, + 166, + 160, + 240, + 240, + 240, + 240, + 36, + 30, + 31, + 31, + 14, + 10, + 6, + 150, + 230, + 225, + 145, + 149, + 165, + 169, + 169, + 105, + 89, + 105, + 86, + 154, + 170, + 154, + 85, + 85, + 170, + 170, + 89, + 85, + 101, + 169, + 169, + 90, + 90, + 90, + 90, + 85, + 149, + 165, + 169, + 165, + 89, + 105, + 169, + 169, + 85, + 89, + 170, + 166, + 149, + 85, + 106, + 170, + 106, + 85, + 90, + 106, + 106, + 86, + 149, + 166, + 165, + 150, + 89, + 90, + 170, + 170, + 89, + 101, + 106, + 165, + 165, + 165, + 150, + 154, + 90, + 85, + 89, + 170, + 150, + 85, + 149, + 166, + 165, + 165, + 85, + 90, + 170, + 85, + 85, + 150, + 170, + 105, + 105, + 89, + 106, + 106, + 153, + 86, + 86, + 153, + 165, + 169, + 106, + 89, + 85, + 106, + 170, + 165, + 149, + 90, + 90, + 90, + 101, + 149, + 149, + 106, + 170, + 153, + 165, + 165, + 150, + 166, + 169, + 105, + 105, + 106, + 170, + 90, + 90, + 86, + 105, + 105, + 165, + 150, + 89, + 102, + 154, + 90, + 85, + 154, + 102, + 149, + 165, + 165, + 166, + 105, + 89, + 86, + 86, + 90, + 90, + 90, + 86, + 95, + 70, + 149, + 169, + 106, + 90, + 86, + 149, + 166, + 106, + 85, + 46, + 90, + 85, + 165, + 165, + 169, + 105, + 90, + 86, + 149, + 165, + 165, + 105, + 101, + 102, + 169, + 90, + 90, + 165, + 149, + 86, + 90, + 150, + 170, + 89, + 101, + 89, + 105, + 86, + 149, + 165, + 169, + 109, + 90, + 86, + 165, + 170, + 86, + 90, + 90, + 86, + 169, + 165, + 90, + 90, + 90, + 149, + 165, + 169, + 86, + 150, + 165, + 90, + 90, + 153, + 154, + 101, + 170, + 154, + 153, + 150, + 150, + 149, + 101, + 102, + 90, + 89, + 154, + 150, + 86, + 102, + 150, + 154, + 89, + 101, + 165, + 101, + 150, + 153, + 89, + 166, + 150, + 150, + 105, + 102, + 170, + 149, + 169, + 165, + 166, + 150, + 101, + 105, + 169, + 154, + 90, + 102, + 105, + 89, + 154, + 166, + 154, + 101, + 154, + 150, + 90, + 89, + 86, + 149, + 166, + 101, + 90, + 102, + 165, + 153, + 153, + 90, + 102, + 149, + 106, + 106, + 89, + 149, + 106, + 105, + 105, + 166, + 153, + 166, + 102, + 89, + 105, + 105, + 166, + 90, + 105, + 165, + 153, + 89, + 166, + 102, + 90, + 102, + 153, + 166, + 89, + 153, + 166, + 85, + 153, + 102, + 153, + 86, + 166, + 89, + 169, + 150, + 102, + 89, + 106, + 101, + 153, + 150, + 102, + 89, + 154, + 86, + 89, + 102, + 102, + 89, + 149, + 154, + 90, + 101, + 101, + 150, + 105, + 106, + 102, + 149, + 169, + 105, + 150, + 153, + 105, + 105, + 166, + 138, + 105, + 102, + 166, + 89, + 105, + 102, + 150, + 154, + 105, + 166, + 102, + 85, + 89, + 165, + 150, + 89, + 153, + 166, + 150, + 105, + 101, + 102, + 150, + 154, + 101, + 169, + 170, + 86, + 150, + 153, + 105, + 105, + 166, + 90, + 89, + 166, + 154, + 89, + 105, + 166, + 138, + 105, + 101, + 150, + 150, + 101, + 101, + 150, + 153, + 105, + 150, + 170, + 90, + 105, + 89, + 150, + 86, + 90, + 90, + 102, + 153, + 153, + 102, + 165, + 165, + 102, + 154, + 150, + 89, + 165, + 150, + 90, + 105, + 166, + 150, + 85, + 169, + 106, + 102, + 153, + 153, + 154, + 90, + 154, + 105, + 101, + 102, + 154, + 154, + 165, + 166, + 90, + 89, + 105, + 86, + 105, + 101, + 154, + 106, + 153, + 90, + 86, + 150, + 86, + 105, + 154, + 150, + 85, + 105, + 150, + 86, + 165, + 165, + 154, + 105, + 165, + 165, + 105, + 90, + 165, + 154, + 90, + 101, + 165, + 86, + 165, + 165, + 166, + 154, + 105, + 101, + 170, + 90, + 105, + 165, + 170, + 90, + 90, + 165, + 165, + 150, + 149, + 165, + 150, + 90, + 86, + 165, + 169, + 90, + 105, + 165, + 154, + 90, + 166, + 150, + 106, + 89, + 101, + 90, + 154, + 101, + 166, + 153, + 166, + 154, + 154, + 106, + 153, + 101, + 86, + 105, + 105, + 169, + 101, + 170, + 166, + 154, + 89, + 105, + 154, + 86, + 149, + 169, + 89, + 150, + 169, + 106, + 86, + 165, + 166, + 154, + 149, + 102, + 90, + 105, + 170, + 90, + 85, + 166, + 90, + 85, + 165, + 90, + 150, + 89, + 89, + 150, + 150, + 102, + 154, + 153, + 166, + 166, + 90, + 89, + 149, + 153, + 106, + 90, + 86, + 102, +}; +const uint8_t DPCM_seven_8000[] PROGMEM = { + 154, + 149, + 169, + 101, + 86, + 165, + 106, + 101, + 102, + 154, + 85, + 169, + 90, + 90, + 165, + 90, + 165, + 106, + 154, + 149, + 165, + 90, + 166, + 101, + 154, + 90, + 153, + 106, + 150, + 149, + 170, + 150, + 153, + 106, + 86, + 153, + 105, + 106, + 101, + 149, + 170, + 105, + 169, + 150, + 101, + 102, + 90, + 153, + 166, + 90, + 153, + 165, + 166, + 105, + 105, + 150, + 150, + 101, + 153, + 166, + 153, + 154, + 102, + 102, + 149, + 105, + 149, + 150, + 90, + 102, + 102, + 153, + 153, + 166, + 102, + 101, + 153, + 154, + 102, + 102, + 89, + 153, + 154, + 86, + 102, + 153, + 90, + 86, + 165, + 170, + 105, + 153, + 150, + 154, + 102, + 89, + 153, + 106, + 105, + 106, + 166, + 153, + 153, + 154, + 102, + 102, + 150, + 101, + 169, + 102, + 153, + 154, + 150, + 102, + 105, + 154, + 86, + 102, + 89, + 153, + 166, + 101, + 153, + 153, + 153, + 150, + 105, + 101, + 166, + 101, + 153, + 166, + 149, + 165, + 169, + 150, + 166, + 166, + 166, + 89, + 105, + 150, + 90, + 101, + 150, + 166, + 89, + 166, + 90, + 89, + 165, + 165, + 154, + 102, + 90, + 150, + 105, + 153, + 89, + 102, + 102, + 154, + 154, + 101, + 105, + 153, + 153, + 102, + 102, + 89, + 153, + 165, + 101, + 166, + 105, + 105, + 154, + 90, + 105, + 102, + 154, + 101, + 153, + 150, + 90, + 102, + 102, + 150, + 89, + 102, + 90, + 89, + 169, + 149, + 86, + 102, + 105, + 153, + 153, + 90, + 102, + 153, + 169, + 166, + 105, + 105, + 169, + 166, + 106, + 102, + 153, + 153, + 102, + 150, + 102, + 101, + 153, + 106, + 101, + 150, + 154, + 149, + 166, + 154, + 90, + 90, + 101, + 165, + 105, + 154, + 154, + 165, + 165, + 90, + 89, + 165, + 165, + 106, + 106, + 85, + 154, + 169, + 101, + 154, + 89, + 101, + 166, + 170, + 86, + 165, + 153, + 106, + 105, + 85, + 154, + 85, + 85, + 106, + 170, + 165, + 85, + 90, + 170, + 150, + 149, + 101, + 106, + 102, + 102, + 13, + 246, + 241, + 25, + 2, + 230, + 253, + 21, + 3, + 170, + 253, + 149, + 70, + 102, + 182, + 89, + 90, + 150, + 160, + 255, + 31, + 16, + 224, + 79, + 155, + 240, + 25, + 95, + 245, + 144, + 106, + 110, + 146, + 149, + 164, + 170, + 158, + 136, + 55, + 199, + 192, + 56, + 111, + 192, + 32, + 31, + 239, + 160, + 16, + 175, + 237, + 80, + 170, + 173, + 22, + 170, + 166, + 131, + 253, + 44, + 3, + 247, + 190, + 0, + 130, + 255, + 20, + 64, + 255, + 125, + 65, + 138, + 185, + 102, + 74, + 170, + 166, + 144, + 255, + 15, + 0, + 255, + 255, + 0, + 177, + 255, + 0, + 65, + 255, + 41, + 2, + 234, + 184, + 26, + 151, + 169, + 106, + 168, + 63, + 195, + 192, + 63, + 251, + 192, + 45, + 191, + 192, + 8, + 127, + 209, + 69, + 125, + 150, + 70, + 185, + 86, + 150, + 183, + 7, + 252, + 48, + 3, + 255, + 252, + 2, + 239, + 253, + 0, + 70, + 254, + 101, + 87, + 153, + 105, + 106, + 149, + 169, + 169, + 156, + 63, + 240, + 192, + 15, + 254, + 240, + 7, + 255, + 248, + 1, + 91, + 249, + 149, + 9, + 170, + 149, + 106, + 90, + 150, + 166, + 112, + 63, + 211, + 64, + 31, + 251, + 224, + 10, + 107, + 248, + 5, + 22, + 254, + 153, + 5, + 170, + 170, + 86, + 169, + 149, + 90, + 172, + 27, + 245, + 232, + 2, + 169, + 254, + 81, + 148, + 106, + 106, + 165, + 86, + 106, + 170, + 149, + 89, + 90, + 165, + 85, + 90, + 170, + 144, + 111, + 219, + 180, + 5, + 86, + 170, + 90, + 85, + 105, + 105, + 102, + 154, + 154, + 90, + 90, + 165, + 165, + 86, + 153, + 170, + 166, + 149, + 22, + 170, + 170, + 85, + 89, + 90, + 169, + 166, + 85, + 170, + 169, + 85, + 154, + 169, + 102, + 86, + 154, + 86, + 165, + 90, + 153, + 102, + 105, + 85, + 170, + 154, + 149, + 85, + 106, + 165, + 85, + 89, + 86, + 170, + 169, + 165, + 90, + 154, + 89, + 165, + 90, + 102, + 169, + 90, + 85, + 165, + 90, + 90, + 169, + 169, + 85, + 85, + 150, + 86, + 170, + 90, + 149, + 101, + 105, + 169, + 165, + 90, + 85, + 166, + 154, + 149, + 102, + 154, + 85, + 106, + 170, + 149, + 85, + 90, + 86, + 105, + 165, + 101, + 169, + 154, + 106, + 169, + 170, + 85, + 150, + 106, + 106, + 149, + 166, + 169, + 7, + 233, + 185, + 70, + 149, + 170, + 90, + 149, + 169, + 90, + 149, + 105, + 106, + 166, + 85, + 106, + 169, + 86, + 106, + 153, + 105, + 144, + 191, + 159, + 128, + 105, + 107, + 209, + 165, + 26, + 166, + 165, + 86, + 90, + 169, + 165, + 86, + 170, + 169, + 90, + 170, + 153, + 168, + 63, + 230, + 208, + 46, + 31, + 224, + 101, + 11, + 166, + 164, + 90, + 90, + 169, + 165, + 90, + 90, + 165, + 165, + 90, + 90, + 166, + 160, + 239, + 107, + 64, + 165, + 127, + 69, + 144, + 122, + 154, + 69, + 101, + 170, + 85, + 150, + 169, + 105, + 106, + 149, + 86, + 170, + 90, + 96, + 223, + 127, + 0, + 213, + 191, + 21, + 129, + 170, + 169, + 85, + 86, + 169, + 106, + 86, + 153, + 165, + 101, + 89, + 102, + 153, + 169, + 84, + 171, + 123, + 64, + 162, + 110, + 85, + 81, + 170, + 169, + 85, + 86, + 170, + 105, + 86, + 90, + 169, + 85, + 90, + 106, + 150, + 105, + 106, + 13, + 243, + 169, + 25, + 86, + 213, + 89, + 86, + 170, + 149, + 85, + 106, + 170, + 149, + 101, + 106, + 86, + 86, + 165, + 170, + 106, + 86, + 149, + 38, + 218, + 165, + 101, + 106, + 85, + 165, + 90, + 170, + 101, + 150, + 169, + 149, + 106, + 169, + 101, + 86, + 169, + 165, + 86, + 154, + 165, + 165, + 89, + 170, + 101, + 170, + 105, + 85, + 101, + 90, + 165, + 149, + 90, + 102, + 166, + 154, + 169, + 85, + 154, + 105, + 85, + 86, + 170, + 165, + 170, + 85, + 86, + 166, + 170, + 166, + 149, + 86, + 85, + 90, + 89, + 101, + 106, + 154, + 170, + 101, + 170, + 169, + 85, + 85, + 150, + 169, + 86, + 166, + 169, + 149, + 90, + 170, + 170, + 170, + 85, + 85, + 85, + 89, + 90, + 150, + 154, + 90, + 86, + 170, + 154, + 85, + 85, + 165, + 86, + 105, + 170, + 165, + 85, + 86, + 170, + 170, + 169, + 165, + 85, + 85, + 86, + 86, + 166, + 169, + 154, + 170, + 170, + 170, + 85, + 90, + 165, + 86, + 154, + 170, + 169, + 86, + 85, + 170, + 170, + 170, + 170, + 85, + 85, + 85, + 85, + 150, + 150, + 86, + 170, + 170, + 165, + 154, + 105, + 165, + 85, + 105, + 170, + 90, + 105, + 149, + 86, + 170, + 170, + 170, + 85, + 85, + 85, + 85, + 149, + 85, + 106, + 170, + 170, + 154, + 169, + 85, + 86, + 105, + 101, + 165, + 90, + 89, + 101, + 90, + 154, + 106, + 170, + 153, + 85, + 85, + 85, + 149, + 166, + 90, + 90, + 170, + 170, + 101, + 149, + 149, + 165, + 165, + 150, + 89, + 105, + 90, + 170, + 169, + 170, + 149, + 85, + 85, + 86, + 85, + 85, + 170, + 154, + 170, + 150, + 153, + 85, + 106, + 90, + 89, + 106, + 169, + 106, + 89, + 169, + 86, + 150, + 85, + 90, + 90, + 86, + 149, + 90, + 85, + 150, + 169, + 150, + 149, + 89, + 166, + 154, + 169, + 105, + 165, + 105, + 89, + 106, + 86, + 85, + 165, + 170, + 170, + 166, + 149, + 170, + 86, + 106, + 89, + 105, + 105, + 86, + 150, + 106, + 89, + 149, + 105, + 90, + 149, + 153, + 86, + 154, + 105, + 169, + 166, + 169, + 165, + 105, + 105, + 170, + 86, + 86, + 101, + 89, + 169, + 106, + 86, + 150, + 90, + 105, + 150, + 89, + 105, + 86, + 154, + 169, + 166, + 165, + 105, +}; +const uint8_t DPCM_eight_8000[] PROGMEM = { + 153, + 170, + 149, + 165, + 105, + 154, + 85, + 150, + 165, + 102, + 89, + 106, + 90, + 90, + 150, + 86, + 154, + 150, + 166, + 90, + 86, + 170, + 90, + 150, + 101, + 170, + 149, + 149, + 154, + 89, + 105, + 101, + 150, + 105, + 105, + 166, + 154, + 102, + 90, + 89, + 170, + 90, + 90, + 102, + 90, + 90, + 101, + 153, + 101, + 165, + 89, + 166, + 165, + 101, + 150, + 170, + 105, + 102, + 89, + 89, + 105, + 154, + 90, + 90, + 90, + 90, + 102, + 150, + 150, + 150, + 149, + 165, + 165, + 153, + 149, + 101, + 166, + 169, + 105, + 101, + 154, + 154, + 101, + 153, + 154, + 154, + 169, + 105, + 150, + 87, + 89, + 149, + 170, + 101, + 165, + 165, + 165, + 164, + 165, + 105, + 101, + 166, + 153, + 90, + 102, + 135, + 26, + 134, + 139, + 101, + 210, + 195, + 102, + 101, + 166, + 153, + 108, + 182, + 105, + 90, + 153, + 99, + 86, + 154, + 213, + 101, + 169, + 108, + 56, + 177, + 100, + 229, + 154, + 41, + 105, + 154, + 89, + 105, + 166, + 89, + 105, + 105, + 154, + 93, + 101, + 151, + 92, + 60, + 247, + 193, + 0, + 58, + 255, + 144, + 0, + 191, + 249, + 160, + 21, + 122, + 229, + 149, + 26, + 94, + 60, + 160, + 156, + 60, + 62, + 188, + 8, + 15, + 191, + 73, + 6, + 15, + 175, + 70, + 9, + 126, + 219, + 69, + 25, + 90, + 174, + 104, + 151, + 11, + 63, + 254, + 192, + 2, + 191, + 250, + 128, + 2, + 255, + 229, + 146, + 135, + 154, + 229, + 145, + 85, + 229, + 242, + 240, + 248, + 60, + 255, + 251, + 4, + 8, + 191, + 251, + 4, + 9, + 191, + 167, + 12, + 44, + 167, + 106, + 88, + 34, + 95, + 92, + 112, + 212, + 12, + 60, + 60, + 76, + 45, + 61, + 237, + 13, + 30, + 46, + 94, + 47, + 29, + 45, + 110, + 106, + 89, + 24, + 163, + 203, + 204, + 48, + 255, + 252, + 48, + 16, + 159, + 255, + 116, + 0, + 25, + 127, + 125, + 201, + 4, + 57, + 250, + 161, + 85, + 138, + 167, + 228, + 114, + 255, + 252, + 176, + 0, + 89, + 254, + 249, + 80, + 0, + 122, + 249, + 233, + 96, + 97, + 235, + 234, + 100, + 85, + 168, + 248, + 48, + 255, + 253, + 112, + 0, + 4, + 190, + 253, + 224, + 0, + 38, + 254, + 255, + 89, + 4, + 42, + 190, + 169, + 96, + 84, + 244, + 60, + 255, + 255, + 60, + 0, + 17, + 126, + 255, + 244, + 0, + 21, + 191, + 253, + 124, + 96, + 161, + 186, + 245, + 149, + 41, + 122, + 12, + 59, + 255, + 76, + 16, + 16, + 30, + 255, + 249, + 0, + 4, + 127, + 254, + 245, + 145, + 69, + 171, + 250, + 149, + 85, + 170, + 203, + 61, + 255, + 219, + 4, + 4, + 7, + 127, + 255, + 68, + 0, + 10, + 191, + 187, + 73, + 70, + 26, + 110, + 169, + 85, + 29, + 172, + 48, + 238, + 255, + 48, + 128, + 0, + 61, + 255, + 250, + 80, + 0, + 102, + 251, + 233, + 226, + 130, + 138, + 171, + 86, + 137, + 42, + 176, + 243, + 207, + 253, + 242, + 128, + 0, + 119, + 255, + 250, + 128, + 1, + 70, + 251, + 234, + 198, + 21, + 149, + 151, + 105, + 102, + 85, + 227, + 12, + 51, + 255, + 204, + 48, + 16, + 77, + 127, + 254, + 232, + 0, + 16, + 110, + 251, + 173, + 36, + 96, + 150, + 170, + 166, + 154, + 103, + 176, + 195, + 63, + 252, + 195, + 2, + 4, + 211, + 255, + 238, + 129, + 1, + 10, + 239, + 186, + 214, + 70, + 9, + 90, + 170, + 106, + 150, + 103, + 112, + 199, + 243, + 184, + 193, + 67, + 0, + 203, + 247, + 234, + 128, + 66, + 82, + 207, + 171, + 154, + 69, + 70, + 90, + 94, + 106, + 102, + 86, + 107, + 12, + 115, + 51, + 204, + 36, + 48, + 76, + 191, + 126, + 172, + 68, + 17, + 28, + 191, + 190, + 100, + 129, + 86, + 170, + 234, + 166, + 86, + 74, + 170, + 166, + 160, + 195, + 195, + 207, + 241, + 224, + 194, + 134, + 186, + 246, + 226, + 145, + 64, + 162, + 234, + 234, + 229, + 85, + 85, + 86, + 169, + 234, + 166, + 85, + 89, + 102, + 106, + 169, + 166, + 106, + 80, + 195, + 195, + 215, + 228, + 112, + 112, + 181, + 190, + 126, + 45, + 25, + 6, + 86, + 150, + 150, + 166, + 165, + 101, + 89, + 102, + 90, + 154, + 154, + 89, + 105, + 165, + 170, + 85, + 102, + 165, + 166, + 169, + 105, + 105, + 166, + 154, + 89, + 90, + 102, + 150, + 150, + 101, + 150, + 150, + 149, + 165, + 170, + 101, + 85, + 86, + 90, + 90, + 89, + 101, + 149, + 106, + 154, + 106, + 85, + 101, + 150, + 150, + 154, + 154, + 90, + 90, + 105, + 150, + 90, + 154, + 90, + 85, + 154, + 86, + 150, + 154, + 105, + 166, + 85, + 149, + 150, + 105, + 105, + 101, + 86, + 86, + 106, + 105, + 165, + 149, + 85, + 90, + 166, + 166, + 165, + 165, + 102, + 169, + 165, + 165, + 154, + 85, + 150, + 101, + 166, + 85, + 169, + 170, + 101, + 149, + 170, + 101, + 166, + 86, + 86, + 102, + 154, + 165, + 86, + 154, + 170, + 165, + 149, + 89, + 102, + 90, + 106, + 85, + 105, + 90, + 106, + 169, + 86, + 86, + 90, + 106, + 90, + 165, + 101, + 149, + 86, + 105, + 169, + 101, + 149, + 85, + 165, + 170, + 170, + 149, +}; +const uint8_t DPCM_nine_8000[] PROGMEM = { + 154, + 85, + 169, + 90, + 150, + 153, + 150, + 105, + 89, + 105, + 149, + 169, + 106, + 150, + 165, + 150, + 105, + 86, + 169, + 105, + 86, + 150, + 102, + 150, + 153, + 102, + 150, + 89, + 90, + 165, + 105, + 105, + 101, + 106, + 90, + 102, + 85, + 166, + 90, + 149, + 170, + 86, + 169, + 166, + 85, + 165, + 154, + 89, + 102, + 150, + 89, + 86, + 169, + 165, + 166, + 90, + 101, + 150, + 105, + 86, + 150, + 106, + 106, + 90, + 106, + 89, + 102, + 149, + 102, + 153, + 165, + 102, + 89, + 102, + 150, + 90, + 106, + 154, + 90, + 166, + 153, + 102, + 150, + 89, + 106, + 101, + 153, + 101, + 150, + 150, + 89, + 150, + 169, + 102, + 153, + 153, + 102, + 154, + 85, + 154, + 106, + 102, + 90, + 154, + 90, + 89, + 89, + 166, + 154, + 85, + 166, + 149, + 102, + 149, + 169, + 101, + 170, + 85, + 150, + 89, + 86, + 106, + 154, + 101, + 101, + 86, + 169, + 90, + 85, + 165, + 106, + 150, + 106, + 166, + 89, + 150, + 154, + 89, + 150, + 150, + 85, + 102, + 150, + 90, + 106, + 105, + 90, + 85, + 149, + 101, + 150, + 105, + 170, + 154, + 105, + 105, + 85, + 166, + 154, + 170, + 170, + 149, + 85, + 85, + 89, + 106, + 170, + 170, + 170, + 170, + 149, + 85, + 85, + 149, + 106, + 169, + 85, + 90, + 170, + 170, + 170, + 85, + 85, + 85, + 106, + 170, + 170, + 106, + 170, + 170, + 165, + 85, + 85, + 86, + 170, + 170, + 170, + 85, + 90, + 170, + 170, + 170, + 149, + 85, + 85, + 106, + 169, + 165, + 170, + 170, + 170, + 149, + 86, + 85, + 102, + 170, + 169, + 165, + 85, + 106, + 106, + 170, + 169, + 85, + 85, + 85, + 154, + 150, + 169, + 170, + 102, + 98, + 221, + 101, + 90, + 106, + 102, + 170, + 160, + 239, + 63, + 16, + 192, + 253, + 109, + 2, + 151, + 248, + 100, + 11, + 170, + 144, + 85, + 170, + 101, + 90, + 149, + 217, + 105, + 195, + 188, + 252, + 7, + 79, + 240, + 16, + 47, + 234, + 128, + 111, + 190, + 133, + 90, + 185, + 149, + 90, + 169, + 147, + 41, + 217, + 15, + 243, + 192, + 63, + 191, + 192, + 45, + 255, + 64, + 26, + 250, + 21, + 27, + 166, + 84, + 106, + 150, + 89, + 154, + 89, + 180, + 63, + 192, + 128, + 63, + 203, + 64, + 63, + 255, + 0, + 191, + 228, + 10, + 190, + 129, + 105, + 105, + 130, + 173, + 147, + 153, + 248, + 63, + 208, + 0, + 63, + 194, + 128, + 63, + 197, + 0, + 127, + 145, + 26, + 249, + 6, + 169, + 149, + 149, + 175, + 144, + 238, + 124, + 11, + 240, + 20, + 31, + 208, + 16, + 47, + 208, + 20, + 47, + 208, + 41, + 186, + 70, + 169, + 86, + 90, + 90, + 208, + 46, + 188, + 11, + 240, + 8, + 47, + 208, + 0, + 47, + 208, + 24, + 111, + 192, + 46, + 250, + 65, + 169, + 166, + 90, + 106, + 165, + 106, + 159, + 128, + 255, + 1, + 134, + 253, + 0, + 7, + 252, + 2, + 102, + 248, + 7, + 249, + 80, + 174, + 85, + 89, + 166, + 169, + 86, + 175, + 192, + 127, + 128, + 98, + 190, + 128, + 2, + 254, + 1, + 166, + 253, + 2, + 253, + 85, + 106, + 149, + 85, + 170, + 86, + 150, + 185, + 156, + 11, + 244, + 10, + 107, + 228, + 0, + 47, + 224, + 26, + 159, + 208, + 111, + 166, + 150, + 169, + 102, + 150, + 165, + 105, + 90, + 170, + 1, + 255, + 1, + 134, + 254, + 0, + 7, + 249, + 2, + 166, + 249, + 7, + 234, + 165, + 90, + 85, + 165, + 170, + 86, + 153, + 170, + 88, + 31, + 240, + 13, + 111, + 208, + 4, + 127, + 208, + 25, + 175, + 144, + 46, + 170, + 149, + 105, + 90, + 149, + 165, + 106, + 90, + 234, + 128, + 255, + 64, + 147, + 253, + 0, + 66, + 253, + 1, + 155, + 248, + 6, + 170, + 229, + 86, + 149, + 165, + 86, + 165, + 169, + 106, + 138, + 194, + 190, + 0, + 131, + 252, + 1, + 3, + 252, + 1, + 91, + 244, + 22, + 170, + 165, + 86, + 106, + 149, + 86, + 169, + 153, + 87, + 153, + 194, + 254, + 64, + 67, + 252, + 20, + 3, + 253, + 20, + 11, + 229, + 85, + 175, + 149, + 81, + 170, + 85, + 86, + 169, + 86, + 102, + 150, + 176, + 191, + 64, + 0, + 255, + 29, + 0, + 255, + 164, + 6, + 250, + 148, + 106, + 234, + 84, + 106, + 229, + 90, + 106, + 149, + 166, + 122, + 91, + 15, + 240, + 64, + 31, + 166, + 128, + 47, + 189, + 1, + 171, + 168, + 42, + 15, + 145, + 165, + 189, + 86, + 86, + 229, + 105, + 186, + 94, + 131, + 252, + 180, + 7, + 219, + 224, + 18, + 255, + 68, + 6, + 250, + 89, + 89, + 186, + 149, + 90, + 169, + 86, + 106, + 106, + 150, + 138, + 176, + 251, + 175, + 8, + 242, + 241, + 4, + 63, + 246, + 65, + 170, + 181, + 85, + 90, + 158, + 85, + 150, + 170, + 101, + 106, + 90, + 90, + 90, + 144, + 247, + 254, + 12, + 43, + 226, + 64, + 126, + 249, + 65, + 31, + 234, + 145, + 150, + 234, + 84, + 86, + 170, + 85, + 102, + 166, + 86, + 102, + 176, + 243, + 253, + 28, + 62, + 235, + 1, + 26, + 246, + 144, + 90, + 186, + 85, + 86, + 106, + 149, + 85, + 170, + 153, + 85, + 170, + 149, + 150, + 154, + 131, + 207, + 232, + 96, + 246, + 237, + 20, + 106, + 170, + 69, + 86, + 170, + 150, + 85, + 106, + 165, + 85, + 170, + 165, + 85, + 90, + 150, + 149, + 168, + 60, + 190, + 138, + 10, + 111, + 149, + 66, + 170, + 165, + 149, + 86, + 170, + 101, + 86, + 170, + 101, + 86, + 170, + 149, + 90, + 165, + 150, + 166, + 104, + 57, + 191, + 154, + 10, + 90, + 149, + 149, + 90, + 169, + 165, + 85, + 169, + 230, + 86, + 105, + 165, + 165, + 90, + 89, + 105, + 150, + 86, + 170, + 165, + 26, + 175, + 150, + 69, + 170, + 165, + 149, + 102, + 170, + 165, + 86, + 154, + 153, + 165, + 166, + 101, + 89, + 169, + 149, + 153, + 105, + 170, + 166, + 89, + 41, + 170, + 154, + 90, + 90, + 165, + 149, + 85, + 170, + 165, + 85, + 106, + 170, + 149, + 85, + 90, + 90, + 86, + 150, + 150, + 149, + 170, + 169, + 85, + 85, + 170, + 169, + 170, + 166, + 153, + 85, + 149, + 101, + 106, + 89, + 153, + 154, + 106, + 166, + 165, + 105, + 149, + 169, + 85, + 90, + 170, + 105, + 85, + 90, + 170, + 170, + 170, + 101, + 85, + 149, + 85, + 90, + 170, + 105, + 90, + 165, + 166, + 153, + 86, + 85, + 85, + 170, + 166, + 150, + 169, + 85, + 106, + 170, + 170, + 165, + 85, + 85, + 85, + 150, + 150, + 154, + 170, + 149, + 166, + 154, + 85, + 165, + 165, + 149, + 170, + 170, + 149, + 85, + 90, + 90, + 170, + 170, + 149, + 89, + 89, + 85, + 166, + 170, + 166, + 170, + 170, + 154, + 101, + 149, + 85, + 101, + 166, + 106, + 105, + 101, + 85, + 170, + 170, + 170, + 85, + 85, + 85, + 86, + 90, + 89, + 101, + 165, + 166, + 101, + 165, + 101, + 85, + 90, + 165, + 166, + 165, + 85, + 85, + 170, + 170, + 170, + 153, + 85, + 85, + 89, + 90, + 90, + 105, + 169, + 90, + 170, + 165, + 85, + 101, + 149, + 106, + 105, + 169, + 105, + 85, + 90, + 106, + 170, + 169, + 101, + 150, + 85, + 101, + 85, + 154, + 150, + 166, + 170, + 150, + 170, + 149, + 101, + 90, + 85, + 165, + 90, + 169, + 170, + 170, + 153, + 89, + 89, + 90, + 153, + 105, + 102, + 170, + 86, + 169, + 154, + 89, + 85, + 165, + 150, + 150, + 105, + 101, + 170, + 154, + 90, + 106, + 85, + 90, + 149, + 165, + 169, + 86, + 149, + 166, + 150, + 170, + 89, + 86, + 102, + 153, + 154, + 85, + 169, + 106, + 153, + 102, + 101, + 89, + 85, + 105, + 90, + 150, + 170, + 101, + 165, + 90, + 154, + 149, + 166, + 150, + 154, + 169, + 86, + 166, + 105, + 166, + 101, + 85, + 105, + 90, + 90, + 89, + 90, + 166, + 150, + 86, + 150, + 153, + 90, + 165, + 154, + 153, + 102, + 165, + 106, + 165, + 149, + 165, + 153, + 86, + 101, + 165, + 153, + 106, + 90, + 85, + 106, + 166, + 149, + 102, + 150, + 101, + 166, + 150, + 150, + 165, + 150, + 166, + 154, + 89, + 169, + 153, + 105, + 106, + 105, + 90, + 105, + 154, + 105, + 153, + 105, + 89, + 169, + 165, + 153, + 86, + 169, + 105, + 85, + 165, + 169, + 105, + 90, + 89, + 90, + 86, + 149, + 102, + 105, + 106, + 89, + 153, + 165, + 86, + 166, + 101, + 154, + 165, + 150, + 165, + 165, + 169, + 165, + 90, + 165, + 169, + 154, + 105, + 170, + 106, + 89, + 90, + 105, + 154, + 85, + 154, + 102, + 154, + 102, + 150, + 169, + 86, + 90, + 154, + 101, + 165, + 169, + 101, + 150, + 150, + 169, + 102, + 153, + 105, + 90, + 154, + 86, + 166, + 105, + 150, + 154, + 149, + 154, + 106, + 150, + 150, + 169, + 85, + 169, + 101, + 165, + 105, + 105, + 89, + 165, + 90, + 106, + 85, + 169, + 106, + 85, + 106, + 90, + 169, + 150, + 90, + 149, + 165, + 153, + 169, + 106, + 105, + 165, + 169, + 105, + 149, + 105, + 102, + 90, + 150, + 154, + 89, + 150, + 165, + 154, + 102, + 169, + 102, + 149, + 166, + 101, + 101, + 169, + 150, + 165, + 165, + 90, + 101, + 153, + 105, + 150, + 106, + 85, + 170, + 90, + 153, + 86, + 90, + 102, + 105, + 101, + 166, + 85, + 150, + 165, + 154, + 105, + 166, + 154, + 106, + 90, + 86, + 101, + 90, + 86, + 150, + 150, + 154, + 150, + 154, + 86, + 149, + 165, + 106, + 105, + 149, + 165, + 166, + 106, + 86, + 106, + 89, + 170, + 90, + 102, + 154, + 90, + 89, + 101, + 90, + 89, + 150, + 165, + 105, + 149, +}; +const uint8_t DPCM_zero_8000[] PROGMEM = { + 153, + 153, + 106, + 89, + 154, + 101, + 90, + 105, + 106, + 89, + 165, + 101, + 150, + 153, + 101, + 90, + 154, + 86, + 165, + 89, + 150, + 102, + 153, + 165, + 153, + 105, + 86, + 169, + 106, + 86, + 90, + 90, + 149, + 170, + 165, + 150, + 85, + 90, + 102, + 150, + 149, + 90, + 165, + 165, + 105, + 166, + 90, + 106, + 149, + 169, + 106, + 150, + 105, + 105, + 85, + 101, + 150, + 154, + 166, + 90, + 102, + 106, + 150, + 149, + 169, + 86, + 149, + 106, + 105, + 169, + 150, + 165, + 169, + 169, + 166, + 102, + 105, + 153, + 85, + 89, + 153, + 166, + 105, + 102, + 90, + 150, + 165, + 101, + 106, + 102, + 101, + 105, + 169, + 153, + 153, + 165, + 165, + 106, + 101, + 166, + 101, + 150, + 153, + 169, + 166, + 105, + 154, + 85, + 101, + 102, + 86, + 105, + 154, + 149, + 105, + 166, + 106, + 105, + 149, + 154, + 105, + 102, + 153, + 86, + 170, + 106, + 169, + 150, + 90, + 101, + 170, + 89, + 150, + 149, + 165, + 106, + 101, + 105, + 170, + 166, + 86, + 90, + 165, + 169, + 153, + 153, + 150, + 166, + 102, + 105, + 153, + 150, + 101, + 169, + 102, + 153, + 150, + 102, + 102, + 153, + 153, + 153, + 166, + 166, + 106, + 106, + 89, + 165, + 105, + 150, + 101, + 153, + 150, + 153, + 165, + 102, + 166, + 105, + 105, + 153, + 149, + 170, + 85, + 169, + 106, + 86, + 105, + 105, + 153, + 153, + 166, + 102, + 154, + 102, + 89, + 166, + 101, + 153, + 101, + 153, + 102, + 102, + 153, + 170, + 102, + 102, + 101, + 153, + 153, + 150, + 90, + 105, + 86, + 153, + 150, + 102, + 102, + 89, + 89, + 153, + 166, + 105, + 89, + 105, + 153, + 86, + 106, + 89, + 102, + 102, + 154, + 170, + 101, + 153, + 153, + 154, + 102, + 166, + 102, + 106, + 105, + 85, + 150, + 153, + 105, + 101, + 90, + 90, + 106, + 105, + 130, + 163, + 169, + 173, + 106, + 86, + 74, + 86, + 165, + 169, + 165, + 102, + 105, + 150, + 150, + 90, + 86, + 162, + 154, + 176, + 193, + 243, + 60, + 44, + 50, + 2, + 199, + 150, + 184, + 244, + 164, + 101, + 105, + 106, + 106, + 90, + 9, + 90, + 154, + 15, + 63, + 60, + 195, + 211, + 200, + 120, + 60, + 253, + 119, + 13, + 6, + 22, + 151, + 231, + 214, + 145, + 149, + 233, + 224, + 243, + 242, + 206, + 60, + 60, + 54, + 27, + 207, + 195, + 132, + 144, + 161, + 230, + 245, + 232, + 100, + 24, + 157, + 190, + 79, + 15, + 15, + 51, + 192, + 226, + 208, + 234, + 61, + 60, + 40, + 25, + 95, + 111, + 143, + 83, + 69, + 150, + 235, + 208, + 243, + 240, + 226, + 48, + 12, + 61, + 30, + 111, + 139, + 195, + 131, + 195, + 231, + 245, + 164, + 112, + 120, + 125, + 189, + 15, + 15, + 15, + 11, + 0, + 192, + 176, + 252, + 252, + 124, + 25, + 10, + 95, + 171, + 149, + 130, + 65, + 170, + 250, + 180, + 60, + 60, + 63, + 15, + 7, + 0, + 240, + 248, + 188, + 60, + 28, + 74, + 247, + 229, + 224, + 96, + 165, + 186, + 182, + 144, + 240, + 240, + 252, + 60, + 8, + 3, + 242, + 248, + 244, + 56, + 30, + 107, + 244, + 240, + 112, + 124, + 107, + 151, + 214, + 194, + 240, + 248, + 60, + 12, + 2, + 164, + 253, + 61, + 8, + 2, + 107, + 239, + 166, + 65, + 86, + 170, + 174, + 90, + 86, + 192, + 240, + 252, + 63, + 0, + 80, + 63, + 15, + 131, + 128, + 105, + 111, + 195, + 208, + 180, + 47, + 154, + 224, + 180, + 110, + 128, + 252, + 63, + 15, + 64, + 125, + 15, + 195, + 240, + 21, + 7, + 249, + 249, + 20, + 85, + 186, + 106, + 85, + 90, + 111, + 144, + 188, + 31, + 130, + 208, + 15, + 195, + 244, + 61, + 1, + 229, + 191, + 150, + 144, + 26, + 91, + 213, + 101, + 26, + 155, + 164, + 15, + 195, + 240, + 56, + 1, + 252, + 63, + 2, + 128, + 43, + 234, + 248, + 6, + 66, + 249, + 169, + 85, + 86, + 174, + 170, + 64, + 252, + 63, + 0, + 144, + 15, + 241, + 252, + 5, + 65, + 191, + 91, + 192, + 105, + 111, + 165, + 89, + 10, + 150, + 170, + 149, + 3, + 240, + 252, + 1, + 64, + 127, + 195, + 240, + 25, + 27, + 253, + 11, + 65, + 249, + 190, + 85, + 89, + 91, + 150, + 165, + 104, + 7, + 240, + 252, + 2, + 64, + 191, + 71, + 192, + 42, + 107, + 244, + 45, + 11, + 230, + 249, + 5, + 165, + 185, + 106, + 85, + 160, + 15, + 195, + 240, + 6, + 82, + 254, + 15, + 0, + 175, + 155, + 208, + 164, + 47, + 170, + 148, + 6, + 154, + 229, + 105, + 101, + 96, + 31, + 195, + 224, + 7, + 227, + 252, + 25, + 1, + 190, + 95, + 64, + 110, + 75, + 209, + 228, + 26, + 166, + 244, + 41, + 106, + 112, + 63, + 199, + 208, + 11, + 211, + 248, + 5, + 65, + 254, + 105, + 64, + 110, + 170, + 165, + 85, + 90, + 170, + 164, + 85, + 170, + 172, + 7, + 249, + 228, + 2, + 249, + 255, + 64, + 84, + 127, + 165, + 84, + 106, + 170, + 90, + 85, + 106, + 154, + 165, + 69, + 170, + 253, + 128, + 127, + 238, + 128, + 62, + 175, + 244, + 5, + 90, + 254, + 85, + 85, + 110, + 164, + 105, + 90, + 165, + 106, + 85, + 86, + 190, + 86, + 0, + 255, + 86, + 0, + 127, + 255, + 208, + 6, + 171, + 245, + 69, + 86, + 171, + 149, + 101, + 106, + 149, + 165, + 90, + 149, + 186, + 149, + 128, + 191, + 209, + 64, + 31, + 251, + 240, + 2, + 171, + 254, + 65, + 86, + 171, + 229, + 70, + 170, + 153, + 86, + 150, + 153, + 90, + 165, + 168, + 7, + 253, + 16, + 0, + 255, + 190, + 64, + 42, + 190, + 164, + 21, + 170, + 170, + 80, + 110, + 170, + 149, + 106, + 149, + 165, + 106, + 165, + 100, + 31, + 252, + 64, + 0, + 255, + 229, + 64, + 27, + 234, + 165, + 6, + 170, + 90, + 85, + 110, + 149, + 85, + 106, + 165, + 101, + 90, + 165, + 164, + 27, + 253, + 64, + 1, + 255, + 229, + 65, + 91, + 234, + 90, + 85, + 90, + 170, + 165, + 85, + 102, + 170, + 166, + 149, + 86, + 169, + 90, + 168, + 6, + 255, + 164, + 0, + 107, + 249, + 85, + 86, + 170, + 150, + 85, + 90, + 170, + 169, + 85, + 105, + 166, + 170, + 85, + 106, + 170, + 85, + 102, + 128, + 111, + 250, + 0, + 5, + 190, + 169, + 85, + 85, + 170, + 150, + 165, + 90, + 165, + 86, + 170, + 85, + 149, + 106, + 149, + 165, + 85, + 106, + 160, + 27, + 254, + 144, + 5, + 111, + 165, + 85, + 101, + 170, + 149, + 90, + 150, + 170, + 85, + 170, + 165, + 85, + 106, + 165, + 86, + 86, + 170, + 164, + 6, + 255, + 144, + 5, + 90, + 169, + 85, + 170, + 150, + 169, + 86, + 170, + 101, + 86, + 170, + 149, + 86, + 170, + 149, + 86, + 169, + 86, + 170, + 65, + 111, + 229, + 85, + 85, + 170, + 149, + 90, + 149, + 106, + 149, + 106, + 149, + 90, + 165, + 85, + 150, + 170, + 149, + 90, + 166, + 106, + 101, + 85, + 170, + 165, + 90, + 149, + 106, + 165, + 86, + 169, + 86, + 169, + 86, + 169, + 86, + 165, + 149, + 170, + 85, + 86, + 105, + 86, + 169, + 89, + 86, + 170, + 149, + 90, + 170, + 89, + 85, + 106, + 169, + 86, + 149, + 106, + 165, + 86, + 169, + 86, + 170, + 85, + 170, + 149, + 90, + 165, + 90, + 165, + 90, + 169, + 86, + 90, + 150, + 149, + 90, + 165, + 85, + 170, + 105, + 169, + 85, + 170, + 149, + 90, + 169, + 85, + 170, + 85, + 170, + 85, + 106, + 149, + 90, + 170, + 86, + 169, + 106, + 85, + 90, + 170, + 85, + 170, + 165, + 85, + 170, + 169, + 86, + 170, + 101, + 86, + 169, + 86, + 169, + 85, + 106, + 149, + 86, + 170, + 85, + 102, + 170, + 90, + 150, + 101, + 86, + 170, + 165, + 86, + 170, + 105, + 102, + 165, + 90, + 169, + 85, + 106, + 149, + 90, + 169, + 85, + 106, + 165, + 85, + 90, + 165, + 85, + 170, + 149, + 106, + 169, + 85, + 106, + 170, + 85, + 106, + 106, + 153, + 85, + 170, + 165, + 86, + 170, + 165, + 154, + 170, + 85, + 85, + 170, + 149, + 165, + 169, + 86, + 106, + 169, + 86, + 106, + 149, + 89, + 169, + 90, + 165, + 90, + 169, + 85, + 154, + 165, + 85, + 154, + 169, + 85, + 106, + 165, + 89, + 106, + 165, + 101, + 170, + 165, + 86, + 106, + 165, + 90, + 153, + 90, + 170, + 90, + 105, + 165, + 90, + 150, + 85, + 170, + 149, + 90, + 169, + 85, + 166, + 169, + 85, + 170, + 149, + 86, + 90, + 149, + 89, + 170, + 105, + 89, + 149, + 170, + 166, + 86, +}; +const uint8_t DPCM_point_8000[] PROGMEM = { + 153, + 169, + 90, + 89, + 154, + 90, + 149, + 154, + 102, + 150, + 165, + 150, + 149, + 165, + 89, + 102, + 153, + 102, + 149, + 169, + 105, + 90, + 90, + 105, + 90, + 105, + 102, + 90, + 86, + 165, + 90, + 85, + 150, + 90, + 89, + 165, + 90, + 166, + 101, + 169, + 150, + 165, + 102, + 89, + 90, + 105, + 101, + 101, + 154, + 90, + 86, + 154, + 105, + 153, + 90, + 150, + 149, + 166, + 89, + 105, + 150, + 86, + 149, + 106, + 90, + 101, + 169, + 102, + 90, + 153, + 89, + 106, + 86, + 166, + 90, + 153, + 101, + 150, + 150, + 150, + 153, + 85, + 166, + 89, + 170, + 101, + 150, + 85, + 170, + 90, + 86, + 153, + 105, + 165, + 101, + 153, + 105, + 170, + 106, + 150, + 89, + 106, + 154, + 101, + 150, + 86, + 166, + 149, + 105, + 169, + 106, + 89, + 85, + 166, + 106, + 86, + 105, + 90, + 89, + 154, + 90, + 149, + 61, + 89, + 120, + 79, + 165, + 86, + 170, + 169, + 139, + 165, + 170, + 149, + 105, + 169, + 166, + 86, + 170, + 86, + 166, + 165, + 106, + 165, + 150, + 170, + 149, + 106, + 86, + 165, + 105, + 106, + 149, + 106, + 101, + 101, + 165, + 154, + 153, + 86, + 170, + 86, + 106, + 85, + 102, + 106, + 150, + 149, + 166, + 154, + 149, + 154, + 149, + 154, + 90, + 165, + 90, + 169, + 149, + 86, + 106, + 165, + 90, + 170, + 153, + 85, + 170, + 170, + 85, + 90, + 166, + 149, + 87, + 170, + 85, + 90, + 170, + 149, + 90, + 170, + 149, + 90, + 170, + 101, + 102, + 106, + 165, + 85, + 170, + 149, + 150, + 170, + 86, + 86, + 154, + 166, + 85, + 90, + 153, + 86, + 169, + 85, + 106, + 165, + 149, + 101, + 102, + 106, + 85, + 154, + 153, + 86, + 106, + 85, + 90, + 169, + 90, + 153, + 85, + 90, + 191, + 165, + 5, + 175, + 165, + 5, + 106, + 169, + 86, + 106, + 170, + 101, + 104, + 0, + 255, + 248, + 0, + 63, + 253, + 0, + 47, + 249, + 1, + 191, + 229, + 22, + 170, + 149, + 85, + 170, + 170, + 85, + 168, + 1, + 255, + 240, + 0, + 255, + 244, + 0, + 191, + 224, + 2, + 255, + 144, + 27, + 250, + 64, + 86, + 170, + 170, + 149, + 106, + 0, + 127, + 252, + 0, + 63, + 252, + 0, + 127, + 244, + 6, + 254, + 80, + 107, + 249, + 1, + 106, + 165, + 90, + 169, + 85, + 172, + 1, + 255, + 240, + 0, + 255, + 208, + 2, + 255, + 64, + 111, + 144, + 27, + 254, + 64, + 27, + 233, + 86, + 170, + 85, + 170, + 170, + 128, + 127, + 252, + 0, + 255, + 192, + 7, + 253, + 6, + 254, + 0, + 127, + 244, + 2, + 186, + 149, + 86, + 170, + 85, + 90, + 170, + 160, + 15, + 255, + 0, + 63, + 240, + 7, + 248, + 7, + 253, + 0, + 191, + 224, + 31, + 228, + 26, + 228, + 27, + 228, + 6, + 250, + 81, + 160, + 31, + 252, + 0, + 255, + 128, + 189, + 0, + 255, + 192, + 47, + 225, + 111, + 144, + 31, + 225, + 43, + 228, + 86, + 170, + 170, + 166, + 128, + 63, + 240, + 7, + 244, + 107, + 192, + 31, + 244, + 11, + 208, + 111, + 208, + 127, + 128, + 190, + 65, + 185, + 6, + 249, + 66, + 185, + 96, + 31, + 240, + 11, + 160, + 191, + 64, + 46, + 171, + 244, + 6, + 249, + 175, + 96, + 89, + 86, + 169, + 86, + 165, + 170, + 150, + 170, + 160, + 63, + 240, + 188, + 23, + 244, + 36, + 6, + 249, + 105, + 65, + 190, + 154, + 165, + 153, + 86, + 149, + 106, + 90, + 170, + 165, + 153, + 188, + 15, + 217, + 240, + 31, + 75, + 224, + 45, + 31, + 209, + 164, + 31, + 170, + 229, + 105, + 31, + 85, + 85, + 169, + 106, + 90, + 101, + 170, + 67, + 243, + 244, + 53, + 15, + 195, + 128, + 165, + 190, + 90, + 67, + 218, + 229, + 100, + 106, + 106, + 86, + 86, + 150, + 150, + 169, + 106, + 195, + 246, + 240, + 112, + 124, + 121, + 69, + 71, + 155, + 214, + 145, + 166, + 215, + 85, + 89, + 170, + 165, + 150, + 90, + 154, + 166, + 90, + 208, + 199, + 227, + 144, + 228, + 240, + 240, + 145, + 170, + 169, + 164, + 105, + 186, + 169, + 85, + 105, + 105, + 105, + 105, + 105, + 169, + 166, + 160, + 207, + 199, + 25, + 68, + 242, + 146, + 133, + 86, + 169, + 213, + 86, + 170, + 154, + 85, + 105, + 169, + 102, + 86, + 149, + 154, + 169, + 212, + 227, + 243, + 205, + 133, + 54, + 153, + 149, + 21, + 169, + 165, + 149, + 169, + 170, + 165, + 85, + 94, + 105, + 101, + 150, + 106, + 102, + 102, + 107, + 12, + 253, + 179, + 101, + 140, + 162, + 102, + 73, + 89, + 106, + 90, + 89, + 89, + 170, + 101, + 85, + 85, + 106, + 166, + 89, + 154, + 170, + 105, + 149, + 165, + 150, + 169, + 170, + 131, + 75, + 143, + 93, + 101, + 145, + 150, + 165, + 101, + 165, + 165, + 166, + 165, + 169, + 149, + 106, + 85, + 86, + 106, + 154, + 102, + 85, + 170, + 149, + 85, + 90, + 169, + 86, + 85, + 105, + 154, + 169, + 85, + 90, + 170, + 105, + 89, + 105, + 170, + 165, + 85, + 86, + 150, + 154, + 90, + 154, + 150, + 106, + 165, + 150, + 150, + 153, + 102, + 165, + 169, + 149, + 105, + 105, + 153, + 102, + 169, + 165, + 90, + 105, + 102, + 150, + 86, + 150, + 89, + 86, + 169, + 102, + 86, + 149, + 165, + 154, + 150, + 85, + 106, + 170, + 169, + 101, + 170, + 169, + 85, + 85, + 170, + 89, + 150, + 90, + 90, + 86, + 90, + 149, + 165, + 165, + 90, + 170, + 165, + 149, + 90, + 85, + 170, + 89, + 169, + 170, + 165, + 165, + 150, + 90, + 90, + 165, + 102, + 150, + 169, + 165, + 102, + 149, + 166, + 150, + 153, + 90, + 169, + 165, + 102, + 86, + 165, + 105, + 89, + 85, + 169, + 106, + 169, + 150, + 149, + 165, + 166, + 86, + 150, + 106, + 85, + 166, + 150, + 153, + 86, + 86, + 169, + 166, + 105, + 170, + 150, + 149, + 106, + 89, + 165, + 101, + 154, + 150, + 153, + 85, + 102, + 86, + 150, + 170, + 106, + 105, + 153, + 153, + 153, + 153, + 131, + 76, + 242, + 240, + 220, + 31, + 14, + 146, + 225, + 228, + 170, + 30, + 26, + 89, + 162, + 165, + 166, + 141, + 90, + 105, + 149, + 166, + 166, + 102, + 141, + 154, + 41, + 117, + 149, + 154, + 90, + 105, + 149, + 150, + 169, + 170, + 105, + 166, + 166, + 150, + 89, + 90, + 85, + 165, + 165, + 170, + 170, + 89, + 102, + 149, + 165, + 150, + 90, + 105, + 102, + 150, + 85, + 150, + 150, + 153, + 105, + 106, + 101, + 153, + 150, + 149, + 165, + 90, + 106, + 85, + 150, + 150, + 153, + 105, + 90, + 102, + 149, + 165, + 105, + 105, + 105, + 165, + 166, + 150, + 169, + 102, + 105, + 102, + 170, + 154, + 89, + 105, + 101, + 149, + 154, + 102, + 150, + 153, + 102, + 90, + 86, + 169, + 150, + 154, + 106, + 101, + 153, + 105, + 105, + 153, + 102, + 165, + 105, + 169, + 89, + 105, + 166, + 165, + 169, + 106, + 90, + 150, + 90, + 154, + 101, + 166, + 90, + 101, + 150, + 106, + 101, + 153, + 86, + 169, + 150, + 101, + 169, + 165, + 106, + 85, + 166, + 154, + 86, + 150, + 90, + 154, + 102, + 165, + 166, + 89, + 170, + 85, + 150, + 165, + 166, + 165, + 90, + 89, + 102, + 165, + 89, + 154, + 153, + 106, + 150, + 169, + 90, + 154, + 89, + 154, + 150, + 154, + 169, + 101, + 169, + 165, + 149, + 165, + 169, + 86, + 90, + 153, + 101, + 153, + 165, + 90, + 90, + 90, + 169, + 150, + 89, + 150, + 105, + 89, + 106, + 150, + 165, + 169, + 154, + 165, + 150, + 153, + 106, + 105, + 102, + 149, + 169, + 105, + 166, + 153, + 106, + 86, + 166, + 165, + 150, + 149, + 102, + 150, + 149, + 166, + 149, + 166, + 101, + 90, + 101, + 154, + 85, + 165, + 149, + 86, + 169, + 154, + 105, + 154, + 154, + 90, + 89, + 150, + 154, + 101, + 154, + 154, + 86, + 149, + 105, + 101, + 153, + 166, + 102, + 85, + 170, + 149, + 105, + 90, + 106, + 85, + 170, + 86, + 149, + 150, + 89, + 165, + 166, + 90, + 150, + 170, + 149, + 170, + 150, + 165, + 165, + 166, + 153, + 86, + 149, + 101, + 170, + 86, + 170, + 89, + 105, + 153, + 86, + 149, + 154, + 90, + 85, + 154, + 86, + 166, + 153, + 90, + 165, + 153, + 105, + 165, + 90, + 105, + 165, + 165, + 169, + 90, + 85, + 150, + 86, + 149, + 149, + 150, + 86, + 165, + 106, + 106, + 169, + 86, + 165, + 105, + 154, + 165, + 165, + 165, + 101, + 105, + 86, + 102, + 89, + 106, + 90, + 102, + 90, + 90, + 153, + 105, + 90, + 150, + 150, + 150, + 165, + 153, + 105, + 165, + 105, + 105, + 165, + 169, + 105, + 86, + 102, + 169, + 150, + 105, + 90, + 154, + 101, + 102, + 154, + 101, + 166, + 154, + 86, + 170, + 89, +}; +const uint8_t DPCM_volts_8000[] PROGMEM = { + 154, + 90, + 165, + 90, + 150, + 89, + 101, + 90, + 105, + 90, + 165, + 150, + 149, + 101, + 166, + 150, + 153, + 150, + 105, + 166, + 89, + 165, + 105, + 101, + 150, + 105, + 169, + 105, + 106, + 101, + 150, + 153, + 149, + 106, + 150, + 150, + 165, + 154, + 90, + 166, + 89, + 154, + 105, + 154, + 166, + 149, + 170, + 86, + 89, + 150, + 89, + 153, + 102, + 85, + 106, + 165, + 106, + 89, + 166, + 149, + 150, + 170, + 85, + 166, + 165, + 154, + 89, + 165, + 165, + 102, + 149, + 154, + 150, + 166, + 154, + 90, + 89, + 166, + 90, + 86, + 169, + 150, + 166, + 105, + 90, + 86, + 105, + 150, + 149, + 89, + 86, + 153, + 165, + 170, + 102, + 150, + 89, + 165, + 153, + 101, + 169, + 105, + 154, + 90, + 89, + 102, + 150, + 89, + 90, + 90, + 150, + 150, + 150, + 86, + 150, + 106, + 150, + 166, + 153, + 101, + 169, + 86, + 153, + 150, + 102, + 153, + 166, + 165, + 153, + 154, + 165, + 154, + 150, + 102, + 101, + 154, + 101, + 154, + 86, + 166, + 101, + 153, + 149, + 106, + 165, + 169, + 101, + 153, + 106, + 89, + 166, + 165, + 102, + 102, + 154, + 101, + 166, + 153, + 85, + 106, + 90, + 170, + 105, + 86, + 169, + 150, + 165, + 165, + 169, + 85, + 170, + 86, + 166, + 149, + 86, + 169, + 86, + 149, + 170, + 85, + 89, + 169, + 85, + 170, + 86, + 106, + 165, + 86, + 169, + 85, + 170, + 149, + 106, + 106, + 166, + 89, + 102, + 169, + 101, + 170, + 165, + 86, + 170, + 85, + 170, + 85, + 106, + 84, + 27, + 255, + 144, + 5, + 170, + 90, + 190, + 149, + 5, + 106, + 170, + 149, + 85, + 170, + 170, + 170, + 4, + 63, + 255, + 0, + 39, + 233, + 95, + 253, + 0, + 15, + 254, + 145, + 170, + 80, + 43, + 250, + 85, + 85, + 86, + 170, + 165, + 96, + 3, + 255, + 240, + 0, + 47, + 229, + 175, + 249, + 0, + 47, + 254, + 85, + 89, + 85, + 106, + 170, + 85, + 85, + 86, + 170, + 166, + 64, + 27, + 255, + 128, + 1, + 191, + 166, + 191, + 228, + 0, + 111, + 250, + 85, + 85, + 85, + 170, + 170, + 85, + 85, + 106, + 170, + 149, + 64, + 23, + 255, + 224, + 0, + 111, + 234, + 175, + 229, + 0, + 91, + 254, + 148, + 86, + 85, + 106, + 170, + 85, + 85, + 170, + 170, + 170, + 85, + 0, + 95, + 255, + 128, + 1, + 191, + 234, + 170, + 165, + 0, + 91, + 254, + 149, + 85, + 86, + 170, + 165, + 85, + 90, + 170, + 170, + 165, + 85, + 85, + 86, + 191, + 233, + 0, + 90, + 186, + 150, + 106, + 85, + 86, + 170, + 149, + 86, + 170, + 165, + 89, + 101, + 86, + 170, + 165, + 85, + 90, + 169, + 80, + 91, + 255, + 144, + 1, + 106, + 170, + 106, + 170, + 80, + 86, + 170, + 169, + 85, + 85, + 90, + 170, + 169, + 85, + 86, + 170, + 166, + 165, + 150, + 165, + 85, + 111, + 250, + 64, + 22, + 174, + 165, + 154, + 165, + 85, + 106, + 170, + 85, + 90, + 170, + 149, + 85, + 86, + 106, + 170, + 149, + 86, + 170, + 169, + 85, + 86, + 255, + 164, + 1, + 90, + 170, + 154, + 170, + 149, + 85, + 170, + 169, + 85, + 106, + 165, + 90, + 165, + 149, + 85, + 150, + 170, + 169, + 85, + 90, + 165, + 69, + 111, + 250, + 64, + 22, + 170, + 165, + 170, + 165, + 85, + 106, + 170, + 85, + 170, + 85, + 86, + 170, + 165, + 85, + 170, + 170, + 85, + 85, + 106, + 170, + 165, + 69, + 191, + 233, + 0, + 90, + 169, + 90, + 170, + 80, + 22, + 170, + 165, + 170, + 149, + 85, + 170, + 165, + 86, + 170, + 149, + 106, + 165, + 85, + 170, + 166, + 169, + 85, + 90, + 170, + 85, + 86, + 175, + 165, + 85, + 89, + 85, + 170, + 169, + 85, + 86, + 165, + 106, + 169, + 85, + 106, + 170, + 170, + 85, + 85, + 106, + 170, + 89, + 86, + 170, + 150, + 149, + 101, + 101, + 165, + 170, + 165, + 69, + 111, + 233, + 85, + 101, + 85, + 106, + 234, + 85, + 149, + 85, + 106, + 170, + 90, + 165, + 85, + 106, + 170, + 170, + 149, + 85, + 170, + 166, + 105, + 169, + 170, + 149, + 86, + 170, + 165, + 154, + 150, + 85, + 90, + 170, + 165, + 149, + 86, + 170, + 169, + 85, + 85, + 170, + 170, + 149, + 149, + 90, + 170, + 149, + 89, + 170, + 169, + 86, + 90, + 85, + 170, + 170, + 85, + 101, + 90, + 169, + 166, + 166, + 86, + 86, + 154, + 149, + 149, + 90, + 170, + 165, + 85, + 90, + 170, + 150, + 85, + 90, + 90, + 150, + 169, + 101, + 90, + 170, + 149, + 149, + 106, + 170, + 85, + 105, + 149, + 90, + 170, + 86, + 89, + 149, + 154, + 89, + 85, + 106, + 90, + 169, + 89, + 90, + 150, + 169, + 105, + 101, + 150, + 149, + 170, + 154, + 165, + 86, + 153, + 170, + 90, + 101, + 154, + 106, + 105, + 165, + 89, + 149, + 86, + 90, + 153, + 170, + 150, + 150, + 105, + 154, + 150, + 150, + 101, + 85, + 169, + 105, + 170, + 85, + 102, + 169, + 105, + 169, + 86, + 101, + 106, + 106, + 149, + 102, + 154, + 165, + 169, + 90, + 166, + 170, + 153, + 85, + 105, + 166, + 105, + 150, + 89, + 102, + 165, + 165, + 165, + 165, + 166, + 86, + 106, + 170, + 106, + 165, + 153, + 154, + 165, + 86, + 86, + 169, + 170, + 105, + 150, + 165, + 89, + 101, + 170, + 106, + 85, + 166, + 170, + 89, + 166, + 86, + 86, + 149, + 153, + 150, + 102, + 165, + 102, + 149, + 153, + 170, + 105, + 150, + 106, + 166, + 85, + 106, + 101, + 170, + 105, + 165, + 154, + 89, + 149, + 153, + 166, + 153, + 150, + 102, + 105, + 149, + 153, + 86, + 154, + 102, + 105, + 153, + 166, + 102, + 105, + 153, + 150, + 105, + 102, + 89, + 153, + 101, + 102, + 105, + 86, + 166, + 105, + 153, + 153, + 153, + 102, + 101, + 153, + 166, + 86, + 89, + 166, + 102, + 169, + 89, + 149, + 102, + 102, + 102, + 102, + 89, + 150, + 150, + 153, + 150, + 166, + 102, + 102, + 102, + 153, + 154, + 102, + 101, + 153, + 166, + 102, + 166, + 102, + 153, + 166, + 102, + 153, + 154, + 105, + 150, + 102, + 102, + 89, + 166, + 105, + 153, + 153, + 150, + 102, + 102, + 89, + 153, + 102, + 89, + 102, + 89, + 150, + 153, + 150, + 102, + 89, + 154, + 102, + 154, + 153, + 150, + 105, + 165, + 86, + 105, + 154, + 102, + 153, + 153, + 102, + 101, + 153, + 154, + 102, + 89, + 154, + 102, + 102, + 89, + 154, + 102, + 153, + 153, + 153, + 150, + 102, + 102, + 102, + 102, + 102, + 89, + 153, + 153, + 153, + 166, + 105, + 166, + 102, + 153, + 102, + 153, + 153, + 150, + 102, + 105, + 153, + 153, + 153, + 166, + 98, + 102, + 154, + 89, + 153, + 153, + 154, + 102, + 38, + 101, + 153, + 141, + 99, + 102, + 153, + 154, + 102, + 166, + 89, + 102, + 102, + 105, + 102, + 89, + 153, + 166, + 153, + 102, + 102, + 89, + 153, + 151, + 101, + 152, + 218, + 102, + 89, + 150, + 38, + 153, + 150, + 89, + 153, + 154, + 90, + 102, + 105, + 150, + 150, + 153, + 153, + 150, + 102, + 102, + 153, + 89, + 150, + 153, + 166, + 153, + 150, + 105, + 166, + 90, + 102, + 105, + 102, + 153, + 105, + 154, + 102, + 101, + 153, + 153, + 102, + 102, + 102, + 101, + 153, + 102, + 106, + 102, + 153, + 150, + 102, + 102, + 101, + 153, + 102, + 105, + 153, + 154, + 102, + 102, + 101, + 153, + 150, + 102, + 105, + 153, + 150, + 106, + 101, + 153, + 153, + 166, + 102, + 90, + 101, + 154, + 102, + 101, + 153, + 149, + 153, + 154, + 154, + 102, + 105, + 153, + 166, + 102, + 102, + 101, + 153, + 153, + 90, + 105, + 154, + 102, + 149, + 169, + 90, + 102, + 89, + 149, + 154, + 150, + 165, + 150, + 102, + 153, + 150, + 90, + 149, + 105, + 102, + 154, + 85, + 106, + 89, + 101, + 170, + 101, + 89, + 166, + 153, + 165, + 101, + 102, + 149, + 105, + 101, + 154, + 102, + 86, + 166, + 90, + 89, + 166, + 169, + 90, + 165, + 150, + 89, + 150, + 105, + 106, + 86, + 149, + 86, + 149, + 150, + 169, + 86, + 165, + 102, + 169, + 165, + 105, + 105, + 101, + 101, + 154, + 165, + 169, + 90, + 102, + 89, + 166, + 165, + 149, + 90, + 149, + 154, + 101, + 86, + 169, + 166, + 86, + 101, + 170, + 165, + 105, + 105, + 86, + 165, + 105, + 102, + 165, + 105, + 154, + 89, + 85, + 154, + 149, + 106, + 89, + 165, + 170, + 90, + 165, + 165, + 101, + 150, + 153, + 105, + 153, + 165, + 106, + 86, + 90, + 106, + 166, + 154, + 106, + 150, + 169, + 90, + 150, + 169, + 105, + 150, + 101, + 166, +}; +const uint8_t DPCM_talkingvoltmeter_8000[] PROGMEM = { + 153, + 89, + 170, + 106, + 101, + 86, + 89, + 102, + 165, + 154, + 90, + 169, + 105, + 105, + 154, + 101, + 90, + 102, + 169, + 90, + 153, + 166, + 154, + 165, + 149, + 106, + 150, + 106, + 90, + 165, + 154, + 90, + 154, + 105, + 169, + 86, + 165, + 90, + 101, + 149, + 102, + 154, + 165, + 169, + 90, + 101, + 165, + 86, + 169, + 169, + 90, + 86, + 149, + 169, + 86, + 90, + 90, + 86, + 101, + 106, + 165, + 86, + 165, + 102, + 153, + 169, + 102, + 165, + 154, + 102, + 150, + 106, + 90, + 105, + 166, + 154, + 149, + 169, + 153, + 170, + 102, + 153, + 153, + 165, + 153, + 165, + 89, + 102, + 154, + 101, + 154, + 101, + 150, + 106, + 154, + 166, + 101, + 153, + 101, + 154, + 106, + 153, + 102, + 86, + 89, + 150, + 89, + 101, + 169, + 105, + 165, + 149, + 106, + 89, + 165, + 105, + 165, + 105, + 153, + 149, + 150, + 166, + 90, + 165, + 165, + 150, + 106, + 150, + 90, + 101, + 170, + 154, + 150, + 169, + 90, + 101, + 165, + 169, + 149, + 165, + 149, + 166, + 149, + 106, + 150, + 169, + 169, + 86, + 169, + 169, + 101, + 149, + 105, + 154, + 150, + 101, + 102, + 101, + 102, + 106, + 102, + 153, + 152, + 250, + 102, + 86, + 102, + 154, + 94, + 142, + 102, + 86, + 101, + 169, + 154, + 89, + 150, + 166, + 106, + 105, + 150, + 154, + 101, + 166, + 85, + 101, + 166, + 154, + 149, + 154, + 166, + 165, + 101, + 85, + 102, + 153, + 153, + 150, + 105, + 101, + 150, + 154, + 105, + 106, + 166, + 86, + 102, + 165, + 90, + 106, + 165, + 90, + 105, + 101, + 154, + 106, + 85, + 170, + 105, + 86, + 169, + 86, + 86, + 169, + 154, + 90, + 149, + 166, + 89, + 106, + 102, + 102, + 149, + 105, + 170, + 150, + 65, + 191, + 148, + 0, + 191, + 153, + 1, + 190, + 149, + 90, + 186, + 85, + 90, + 165, + 85, + 154, + 122, + 192, + 255, + 64, + 0, + 127, + 240, + 1, + 111, + 249, + 1, + 186, + 149, + 22, + 186, + 85, + 90, + 170, + 85, + 90, + 168, + 15, + 252, + 0, + 183, + 255, + 128, + 31, + 250, + 164, + 7, + 249, + 85, + 86, + 169, + 86, + 169, + 169, + 90, + 165, + 111, + 192, + 127, + 192, + 15, + 234, + 252, + 0, + 255, + 150, + 64, + 111, + 144, + 106, + 165, + 149, + 90, + 165, + 105, + 106, + 149, + 175, + 0, + 255, + 64, + 63, + 155, + 244, + 3, + 253, + 74, + 65, + 190, + 69, + 170, + 149, + 85, + 170, + 85, + 165, + 106, + 149, + 175, + 128, + 63, + 192, + 15, + 234, + 248, + 2, + 254, + 86, + 65, + 127, + 149, + 105, + 170, + 81, + 170, + 149, + 85, + 169, + 101, + 107, + 148, + 7, + 253, + 0, + 87, + 255, + 64, + 110, + 170, + 84, + 106, + 165, + 86, + 170, + 149, + 90, + 165, + 106, + 149, + 105, + 106, + 166, + 129, + 175, + 233, + 0, + 110, + 229, + 85, + 150, + 165, + 90, + 149, + 170, + 85, + 165, + 85, + 86, + 170, + 165, + 26, + 165, + 169, + 91, + 129, + 190, + 90, + 149, + 169, + 90, + 85, + 169, + 90, + 150, + 169, + 86, + 170, + 149, + 90, + 149, + 169, + 90, + 154, + 85, + 105, + 86, + 150, + 149, + 102, + 170, + 165, + 86, + 106, + 85, + 106, + 165, + 85, + 170, + 150, + 149, + 106, + 166, + 150, + 170, + 86, + 169, + 169, + 86, + 165, + 85, + 90, + 105, + 85, + 106, + 165, + 106, + 165, + 86, + 105, + 170, + 85, + 106, + 149, + 85, + 170, + 149, + 85, + 170, + 149, + 106, + 149, + 105, + 86, + 166, + 86, + 169, + 86, + 170, + 149, + 106, + 150, + 165, + 90, + 169, + 85, + 170, + 169, + 85, + 90, + 169, + 90, + 169, + 101, + 86, + 106, + 149, + 154, + 85, + 170, + 85, + 86, + 169, + 154, + 85, + 106, + 149, + 89, + 153, + 105, + 90, + 90, + 90, + 150, + 150, + 149, + 170, + 165, + 85, + 166, + 165, + 101, + 102, + 89, + 90, + 149, + 149, + 153, + 89, + 90, + 150, + 102, + 61, + 41, + 89, + 106, + 105, + 105, + 105, + 105, + 152, + 116, + 165, + 165, + 169, + 105, + 165, + 150, + 150, + 90, + 90, + 105, + 166, + 101, + 153, + 169, + 165, + 165, + 165, + 166, + 150, + 86, + 154, + 102, + 90, + 153, + 150, + 89, + 106, + 101, + 101, + 166, + 154, + 105, + 165, + 165, + 166, + 150, + 90, + 106, + 106, + 89, + 89, + 106, + 105, + 101, + 165, + 150, + 154, + 90, + 105, + 85, + 150, + 154, + 154, + 90, + 85, + 90, + 105, + 105, + 165, + 165, + 166, + 170, + 144, + 195, + 195, + 203, + 15, + 29, + 45, + 105, + 105, + 89, + 105, + 166, + 166, + 150, + 154, + 86, + 89, + 102, + 90, + 105, + 160, + 240, + 243, + 195, + 199, + 15, + 30, + 109, + 101, + 89, + 106, + 105, + 165, + 150, + 154, + 89, + 89, + 101, + 166, + 166, + 166, + 161, + 203, + 219, + 43, + 25, + 89, + 102, + 101, + 149, + 150, + 170, + 89, + 89, + 106, + 165, + 149, + 150, + 154, + 89, + 154, + 90, + 134, + 43, + 125, + 169, + 150, + 166, + 90, + 153, + 90, + 166, + 85, + 90, + 170, + 170, + 165, + 85, + 89, + 169, + 86, + 170, + 153, + 85, + 122, + 169, + 169, + 169, + 85, + 101, + 149, + 166, + 150, + 85, + 106, + 170, + 170, + 101, + 85, + 149, + 150, + 154, + 154, + 70, + 90, + 106, + 105, + 169, + 85, + 105, + 89, + 101, + 165, + 85, + 106, + 170, + 166, + 149, + 85, + 85, + 166, + 170, + 164, + 101, + 166, + 170, + 154, + 149, + 86, + 150, + 154, + 86, + 89, + 90, + 170, + 106, + 105, + 101, + 85, + 166, + 170, + 168, + 101, + 165, + 166, + 166, + 165, + 85, + 153, + 154, + 149, + 85, + 86, + 170, + 170, + 89, + 89, + 101, + 166, + 165, + 101, + 85, + 170, + 170, + 166, + 149, + 85, + 89, + 102, + 149, + 85, + 86, + 170, + 170, + 165, + 85, + 85, + 169, + 150, + 149, + 85, + 106, + 170, + 170, + 169, + 85, + 86, + 85, + 101, + 165, + 166, + 154, + 170, + 90, + 85, + 85, + 90, + 106, + 89, + 149, + 86, + 154, + 170, + 154, + 170, + 85, + 85, + 90, + 150, + 101, + 106, + 170, + 165, + 101, + 150, + 169, + 170, + 86, + 150, + 85, + 85, + 170, + 154, + 170, + 169, + 85, + 85, + 150, + 149, + 106, + 90, + 90, + 90, + 149, + 169, + 169, + 150, + 149, + 85, + 86, + 170, + 106, + 101, + 169, + 85, + 151, + 149, + 105, + 85, + 170, + 149, + 106, + 149, + 101, + 105, + 166, + 169, + 86, + 102, + 165, + 106, + 149, + 89, + 150, + 165, + 89, + 154, + 154, + 165, + 90, + 101, + 101, + 150, + 86, + 90, + 170, + 170, + 169, + 86, + 90, + 85, + 90, + 90, + 150, + 165, + 106, + 165, + 90, + 154, + 85, + 149, + 170, + 166, + 169, + 105, + 101, + 149, + 106, + 86, + 165, + 150, + 90, + 149, + 154, + 149, + 165, + 85, + 170, + 149, + 106, + 149, + 85, + 166, + 86, + 170, + 85, + 90, + 154, + 86, + 149, + 170, + 148, + 22, + 255, + 149, + 85, + 85, + 90, + 170, + 85, + 106, + 149, + 85, + 170, + 165, + 86, + 170, + 64, + 47, + 253, + 1, + 105, + 69, + 191, + 233, + 85, + 90, + 170, + 85, + 170, + 149, + 106, + 224, + 11, + 255, + 0, + 27, + 229, + 111, + 249, + 0, + 90, + 191, + 164, + 22, + 234, + 70, + 180, + 3, + 255, + 128, + 7, + 249, + 11, + 253, + 0, + 107, + 254, + 164, + 6, + 186, + 69, + 120, + 2, + 255, + 208, + 2, + 254, + 70, + 254, + 64, + 31, + 254, + 149, + 86, + 186, + 149, + 100, + 3, + 255, + 192, + 7, + 254, + 10, + 254, + 64, + 31, + 249, + 65, + 90, + 169, + 86, + 0, + 127, + 252, + 0, + 127, + 225, + 175, + 224, + 2, + 255, + 148, + 86, + 169, + 85, + 144, + 15, + 255, + 64, + 11, + 249, + 91, + 253, + 0, + 127, + 229, + 22, + 186, + 65, + 80, + 31, + 255, + 128, + 7, + 249, + 91, + 253, + 0, + 111, + 233, + 22, + 250, + 85, + 64, + 47, + 255, + 64, + 10, + 249, + 95, + 252, + 0, + 111, + 245, + 86, + 169, + 84, + 0, + 255, + 248, + 0, + 110, + 85, + 191, + 224, + 1, + 255, + 149, + 106, + 148, + 80, + 11, + 255, + 192, + 6, + 249, + 87, + 254, + 64, + 11, + 249, + 86, + 254, + 64, + 0, + 255, + 252, + 0, + 90, + 85, + 191, + 228, + 1, + 191, + 149, + 106, + 149, + 64, + 15, + 255, + 128, + 6, + 165, + 95, + 254, + 0, + 42, + 230, + 170, + 165, + 85, + 0, + 255, + 248, + 0, + 89, + 22, + 255, + 208, + 2, + 170, + 170, + 233, + 89, + 148, + 1, + 255, + 240, + 5, + 80, + 27, + 254, + 80, + 85, + 22, + 254, + 149, + 105, + 85, + 97, + 15, + 255, + 65, + 84, + 2, + 191, + 229, + 169, + 64, + 91, + 234, + 170, + 85, + 106, + 149, + 4, + 255, + 101, + 165, + 0, + 63, + 230, + 174, + 144, + 26, + 165, + 106, + 169, + 170, + 165, + 102, + 153, + 85, + 85, + 102, + 170, + 85, + 170, + 85, + 169, + 85, + 106, + 86, + 170, + 170, + 165, + 85, + 106, + 166, + 170, + 149, + 86, + 170, + 169, + 85, + 85, + 105, + 170, + 170, + 85, + 86, + 170, + 165, + 105, + 85, + 90, + 170, + 85, + 90, + 170, + 90, + 169, + 85, + 90, + 170, + 149, + 85, + 105, + 150, + 106, + 165, + 85, + 106, + 154, + 106, + 165, + 85, + 86, + 170, + 170, + 154, + 85, + 90, + 170, + 85, + 90, + 85, + 90, + 170, + 149, + 106, + 170, + 149, + 86, + 170, + 86, + 165, + 149, + 149, + 170, + 105, + 86, + 169, + 89, + 170, + 86, + 105, + 85, + 170, + 166, + 170, + 149, + 106, + 169, + 85, + 170, + 85, + 86, + 105, + 166, + 85, + 169, + 89, + 170, + 85, + 106, + 89, + 106, + 149, + 90, + 101, + 170, + 149, + 86, + 86, + 170, + 169, + 170, + 85, + 86, + 169, + 90, + 170, + 85, + 106, + 105, + 170, + 165, + 86, + 149, + 90, + 169, + 90, + 169, + 89, + 102, + 150, + 90, + 150, + 90, + 86, + 170, + 85, + 90, + 89, + 149, + 106, + 101, + 86, + 85, + 165, + 105, + 106, + 106, + 170, + 89, + 85, + 149, + 86, + 86, + 170, + 90, + 170, + 149, + 86, + 85, + 101, + 106, + 149, + 101, + 106, + 170, + 169, + 85, + 101, + 85, + 149, + 170, + 154, + 170, + 154, + 85, + 89, + 89, + 85, + 170, + 105, + 90, + 90, + 106, + 105, + 105, + 101, + 85, + 150, + 169, + 85, + 90, + 170, + 170, + 105, + 85, + 101, + 149, + 85, + 106, + 169, + 169, + 169, + 165, + 166, + 149, + 85, + 102, + 150, + 170, + 154, + 149, + 90, + 86, + 85, + 102, + 165, + 154, + 150, + 170, + 90, + 89, + 89, + 22, + 106, + 170, + 106, + 149, + 165, + 105, + 90, + 86, + 150, + 165, + 165, + 169, + 105, + 153, + 84, + 101, + 126, + 170, + 85, + 90, + 154, + 105, + 165, + 150, + 154, + 89, + 105, + 169, + 165, + 165, + 101, + 29, + 46, + 109, + 41, + 86, + 90, + 150, + 150, + 90, + 90, + 89, + 154, + 170, + 105, + 169, + 165, + 45, + 125, + 185, + 101, + 90, + 90, + 154, + 25, + 25, + 106, + 165, + 90, + 105, + 101, + 166, + 85, + 52, + 206, + 215, + 25, + 29, + 102, + 151, + 73, + 89, + 101, + 165, + 150, + 153, + 101, + 165, + 148, + 56, + 125, + 249, + 149, + 89, + 105, + 166, + 150, + 74, + 45, + 105, + 165, + 169, + 120, + 165, + 148, + 52, + 185, + 245, + 165, + 85, + 105, + 185, + 101, + 149, + 215, + 154, + 89, + 105, + 109, + 121, + 164, + 40, + 124, + 249, + 165, + 25, + 105, + 174, + 105, + 40, + 105, + 169, + 165, + 102, + 151, + 90, + 105, + 13, + 46, + 126, + 105, + 9, + 90, + 107, + 154, + 70, + 75, + 90, + 90, + 90, + 149, + 214, + 150, + 67, + 135, + 235, + 149, + 129, + 145, + 170, + 245, + 161, + 149, + 230, + 166, + 169, + 169, + 120, + 117, + 144, + 226, + 251, + 169, + 100, + 37, + 106, + 174, + 29, + 25, + 42, + 154, + 154, + 90, + 151, + 150, + 149, + 29, + 109, + 170, + 82, + 70, + 70, + 165, + 229, + 165, + 86, + 89, + 153, + 170, + 165, + 101, + 105, + 81, + 217, + 234, + 118, + 105, + 85, + 86, + 166, + 166, + 165, + 85, + 105, + 105, + 154, + 166, + 101, + 150, + 105, + 166, + 89, + 90, + 106, + 169, + 149, + 89, + 86, + 90, + 89, + 105, + 105, + 166, + 154, + 89, + 105, + 165, + 149, + 165, + 106, + 170, + 154, + 85, + 149, + 85, + 105, + 106, + 150, + 89, + 101, + 166, + 154, + 86, + 90, + 105, + 166, + 150, + 154, + 169, + 105, + 85, + 85, + 165, + 166, + 154, + 90, + 89, + 101, + 165, + 154, + 74, + 6, + 166, + 249, + 169, + 21, + 70, + 171, + 234, + 168, + 100, + 101, + 170, + 150, + 150, + 74, + 90, + 169, + 15, + 15, + 230, + 160, + 36, + 42, + 175, + 214, + 145, + 194, + 170, + 174, + 90, + 88, + 57, + 106, + 80, + 244, + 252, + 106, + 1, + 193, + 250, + 254, + 70, + 66, + 214, + 249, + 169, + 26, + 15, + 135, + 208, + 62, + 47, + 6, + 128, + 120, + 63, + 175, + 144, + 144, + 185, + 189, + 90, + 70, + 195, + 225, + 228, + 15, + 139, + 208, + 224, + 15, + 15, + 231, + 224, + 84, + 47, + 95, + 145, + 149, + 180, + 189, + 41, + 3, + 240, + 248, + 40, + 2, + 226, + 253, + 185, + 6, + 71, + 231, + 228, + 105, + 110, + 31, + 75, + 128, + 124, + 63, + 6, + 64, + 124, + 127, + 91, + 129, + 96, + 189, + 126, + 6, + 150, + 210, + 245, + 104, + 15, + 195, + 240, + 100, + 7, + 199, + 245, + 164, + 22, + 15, + 214, + 160, + 105, + 105, + 110, + 74, + 64, + 252, + 63, + 6, + 64, + 125, + 127, + 90, + 64, + 85, + 190, + 110, + 70, + 150, + 146, + 229, + 165, + 3, + 211, + 240, + 36, + 2, + 225, + 248, + 120, + 5, + 90, + 170, + 169, + 105, + 105, + 31, + 91, + 144, + 63, + 15, + 194, + 128, + 31, + 79, + 210, + 208, + 6, + 155, + 230, + 165, + 102, + 145, + 245, + 105, + 65, + 244, + 252, + 25, + 0, + 184, + 126, + 26, + 0, + 90, + 110, + 90, + 85, + 89, + 27, + 150, + 148, + 15, + 195, + 240, + 164, + 7, + 210, + 245, + 180, + 5, + 166, + 185, + 105, + 89, + 100, + 185, + 110, + 84, + 47, + 31, + 130, + 64, + 27, + 91, + 234, + 149, + 21, + 90, + 150, + 165, + 106, + 70, + 225, + 185, + 80, + 125, + 191, + 85, + 64, + 106, + 107, + 170, + 85, + 86, + 170, + 170, + 150, + 169, + 26, + 86, + 165, + 145, + 185, + 254, + 85, + 80, + 105, + 110, + 170, + 85, + 85, + 153, + 170, + 170, + 149, + 105, + 90, + 90, + 84, + 105, + 110, + 90, + 85, + 86, + 170, + 169, + 165, + 85, + 86, + 170, + 165, + 101, + 105, + 106, + 169, + 85, + 106, + 170, + 169, + 85, + 90, + 170, + 154, + 165, + 85, + 86, + 169, + 105, + 89, + 86, + 154, + 165, + 149, + 86, + 170, + 169, + 105, + 85, + 90, + 170, + 86, + 85, + 170, + 106, + 154, + 165, + 85, + 105, + 90, + 85, + 150, + 166, + 169, + 105, + 90, + 90, + 149, + 169, + 106, + 106, + 170, + 165, + 105, + 86, + 154, + 90, + 165, + 90, + 166, + 90, + 149, + 86, + 169, + 86, + 166, + 101, + 106, + 169, + 165, + 89, + 86, + 165, + 106, + 170, + 86, + 169, + 90, + 106, + 90, + 149, + 170, + 90, + 154, + 165, + 170, + 154, + 165, + 90, + 86, + 165, + 170, + 86, + 90, + 149, + 170, + 85, + 85, + 102, + 169, + 89, + 150, + 166, + 169, + 86, + 165, + 89, + 90, + 106, + 85, + 106, + 150, + 166, + 149, + 90, + 149, + 150, + 85, + 153, + 86, + 169, + 105, + 105, + 86, + 166, + 169, + 105, + 106, + 153, + 106, + 101, + 90, + 105, + 90, + 85, + 170, + 86, + 166, + 149, + 102, + 86, + 149, + 165, + 154, + 105, + 150, + 149, + 106, + 86, + 149, + 154, + 166, + 86, + 170, + 86, + 101, + 89, + 106, + 89, + 153, + 90, + 150, + 170, + 86, + 150, + 166, + 170, + 89, + 86, + 150, + 150, + 149, + 101, + 90, + 165, + 169, + 106, + 150, + 89, + 169, + 102, + 90, + 85, + 149, + 169, + 86, + 169, + 150, + 149, + 149, + 165, + 105, + 165, + 165, + 169, + 105, + 101, + 165, + 149, + 85, + 105, + 90, + 149, + 166, + 105, + 90, + 150, + 85, + 166, + 153, + 166, + 105, + 166, + 154, + 166, + 89, + 86, + 166, + 169, + 105, + 105, + 101, + 106, + 90, + 153, + 106, + 90, + 149, + 169, + 86, + 169, + 166, + 89, + 149, + 106, +}; diff --git a/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/talkingVoltmeter/cornell/dpcm_1bit/Makefile b/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/talkingVoltmeter/cornell/dpcm_1bit/Makefile new file mode 100644 index 0000000..609e6e2 --- /dev/null +++ b/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/talkingVoltmeter/cornell/dpcm_1bit/Makefile @@ -0,0 +1,186 @@ + +##########------------------------------------------------------########## +########## Project-specific Details ########## +########## Check these every time you start a new project ########## +##########------------------------------------------------------########## + +MCU = atmega168 +F_CPU = 8000000 +BAUD = 9600 +## Also try BAUD = 19200 or 38400 if you're feeling lucky. + +## This is where your main() routine lives (without .c extension) +TARGET = speech_1_bit_diff +## If you've split your program into multiple .c / .h files, +## include the additional source here (without the .c or .h extension) +LOCAL_SOURCE = + +EXTRA_SOURCE_DIR = ../../../learningAVR/ +EXTRA_SOURCE_FILES = USART + +##########------------------------------------------------------########## +########## 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 = + +##########------------------------------------------------------########## +########## 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. ########## +##########------------------------------------------------------########## + +## Defined programs / locations +CC = avr-gcc +OBJCOPY = avr-objcopy +OBJDUMP = avr-objdump +AVRSIZE = avr-size +AVRDUDE = avrdude + +## Compilation options, type man avr-gcc if you're curious. +CFLAGS = -mmcu=$(MCU) -DF_CPU=$(F_CPU)UL -DBAUD=$(BAUD) -Os -I. -I$(EXTRA_SOURCE_DIR) +CFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums +CFLAGS += -Wall -Wstrict-prototypes +CFLAGS += -g -ggdb +CFLAGS += -ffunction-sections -fdata-sections -Wl,--gc-sections -Wl,--relax +CFLAGS += -std=gnu99 +## CFLAGS += -Wl,-u,vfprintf -lprintf_flt -lm ## for floating-point printf +## CFLAGS += -Wl,-u,vfprintf -lprintf_min ## for smaller printf + +## Lump target and extra source files together +TARGET = $(strip $(basename $(MAIN))) +SRC = $(TARGET).c +EXTRA_SOURCE = $(addprefix $(EXTRA_SOURCE_DIR), $(EXTRA_SOURCE_FILES)) +SRC += $(EXTRA_SOURCE) +SRC += $(LOCAL_SOURCE) + +## List of all header files +HEADERS = $(SRC:.c=.h) + +## For every .c file, compile an .o object file +OBJ = $(SRC:.c=.o) + +## Generic Makefile targets. (Only .hex file is necessary) +all: $(TARGET).hex + +%.hex: %.elf + $(OBJCOPY) -R .eeprom -O ihex $< $@ + +%.elf: $(SRC) + $(CC) $(CFLAGS) $(SRC) --output $@ + +%.eeprom: %.elf + $(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O ihex $< $@ + +debug: + @echo + @echo "Source files:" $(SRC) + @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 + +eeprom: $(TARGET).eeprom + +%.lst: %.elf + $(OBJDUMP) -S $< > $@ + +# 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 *~ + +##########------------------------------------------------------########## +########## 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 diff --git a/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/talkingVoltmeter/cornell/dpcm_1bit/Speech_1_bit_diff.m b/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/talkingVoltmeter/cornell/dpcm_1bit/Speech_1_bit_diff.m new file mode 100644 index 0000000..843a9cb --- /dev/null +++ b/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/talkingVoltmeter/cornell/dpcm_1bit/Speech_1_bit_diff.m @@ -0,0 +1,84 @@ + +% Speech encoder for Mega644/GCC decoder +clear all + +%===================================%the Encoder +[d,r] = wavread('mega644small.wav'); %4 +%scale to about unity +res = 1/(max(max(d),abs(min(d)) )); +dd = diff(d * res); +%init the code vector +ddcode = zeros(1,length(dd)); + +brkpt1= 0 ; +%quantize the first derivative +ddcode(find(dd=brkpt1)) = 1; + +%make the length of ddcode a multiple of 8 +ddcode = [ddcode,zeros(1,8-mod(length(ddcode),8))]; +length(ddcode) +index=1; +for i=1:8:length(ddcode) + packed(index)= ... + ddcode(i)*128 + ddcode(i+1)*64 + ... + ddcode(i+2)*32 + ddcode(i+3)*16 + ... + ddcode(i+4)*8 + ddcode(i+5)*4 + ... + ddcode(i+6)*2 + ddcode(i+7) ; + index=index+1; +end +%make a textfile with GCC source code in it. +fname='mega644_1_bit.h'; +fid = fopen(fname,'w'); +fprintf(fid,'const prog_uint8_t mega644[%d]={\r',length(packed)); +for i=1:length(packed)-1 + fprintf(fid,' %5d,\r',packed(i)); +end +fprintf(fid,' %5d};\r',packed(end)); +fclose(fid); + +%===================================%the Decoder +%value based on quantizer +value = [-.16, .16]; +dl(1)=0; +j=2; +highpassfactor = 1/8; +for i=1:length(packed) + p1 = round(bitand(packed(i),128)/128); + p2 = round(bitand(packed(i),64)/64); + p3 = round(bitand(packed(i),32)/32); + p4 = round(bitand(packed(i),16)/16); + p5 = round(bitand(packed(i),8)/8); + p6 = round(bitand(packed(i),4)/4); + p7 = round(bitand(packed(i),2)/2); + p8 = round(bitand(packed(i),1)/1); + + %note that a bit of highpass has been added to reduce drift + dl(j) = dl(j-1)+ value(p1+1) - highpassfactor * dl(j-1); + j=j+1; + dl(j) = dl(j-1)+ value(p2+1) - highpassfactor * dl(j-1); + j=j+1; + dl(j) = dl(j-1)+ value(p3+1) - highpassfactor * dl(j-1); + j=j+1; + dl(j) = dl(j-1)+ value(p4+1) - highpassfactor * dl(j-1); + j=j+1; + dl(j) = dl(j-1)+ value(p5+1) - highpassfactor * dl(j-1); + j=j+1; + dl(j) = dl(j-1)+ value(p6+1) - highpassfactor * dl(j-1); + j=j+1; + dl(j) = dl(j-1)+ value(p7+1) - highpassfactor * dl(j-1); + j=j+1; + dl(j) = dl(j-1)+ value(p8+1) - highpassfactor * dl(j-1); + j=j+1; +end +% dl(1)=0; +% for i=1:length(ddcode) +% dl(i) = dl(i-1)+ value(ddcode(i)+1) - .125*dl(i-1); % .* w'; +% end + +%return +%====================================%playback and graphing +% sound(dl,r); +dl = dl/(max(abs(dl))+.001); +wavwrite(dl', 'mega644_1_bit.wav'); + diff --git a/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/talkingVoltmeter/cornell/dpcm_1bit/Welcome4760small.wav b/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/talkingVoltmeter/cornell/dpcm_1bit/Welcome4760small.wav new file mode 100644 index 0000000..bc26990 Binary files /dev/null and b/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/talkingVoltmeter/cornell/dpcm_1bit/Welcome4760small.wav differ diff --git a/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/talkingVoltmeter/cornell/dpcm_1bit/mega644_1_bit.h b/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/talkingVoltmeter/cornell/dpcm_1bit/mega644_1_bit.h new file mode 100644 index 0000000..106cfc6 --- /dev/null +++ b/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/talkingVoltmeter/cornell/dpcm_1bit/mega644_1_bit.h @@ -0,0 +1 @@ +const uint8_t mega644[3280] PROGMEM ={ 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 253, 255, 255, 255, 255, 255, 255, 253, 255, 239, 238, 127, 159, 187, 223, 109, 255, 185, 238, 109, 158, 238, 230, 127, 182, 204, 230, 118, 185, 249, 111, 118, 255, 172, 227, 123, 188, 231, 56, 207, 166, 121, 190, 239, 251, 227, 188, 0, 2, 255, 252, 255, 128, 0, 12, 241, 30, 241, 238, 24, 64, 31, 48, 255, 30, 241, 198, 5, 224, 31, 32, 243, 14, 0, 30, 1, 240, 15, 128, 0, 3, 193, 175, 135, 220, 56, 32, 120, 53, 248, 56, 241, 128, 15, 193, 135, 240, 65, 224, 1, 252, 48, 63, 136, 12, 0, 63, 135, 131, 248, 0, 128, 7, 240, 240, 127, 0, 16, 1, 252, 60, 31, 192, 32, 0, 254, 30, 15, 135, 2, 0, 254, 10, 63, 135, 128, 1, 248, 48, 252, 60, 32, 7, 193, 199, 193, 199, 0, 60, 28, 124, 56, 224, 3, 193, 207, 135, 28, 0, 124, 49, 240, 199, 128, 15, 134, 62, 25, 224, 1, 240, 79, 199, 60, 0, 62, 9, 248, 231, 192, 7, 193, 63, 24, 240, 0, 240, 47, 199, 158, 0, 30, 5, 248, 243, 192, 3, 193, 190, 31, 112, 0, 248, 55, 195, 231, 0, 15, 2, 252, 62, 0, 1, 240, 111, 195, 224, 128, 15, 2, 252, 31, 0, 0, 120, 31, 224, 248, 6, 1, 224, 127, 131, 248, 28, 3, 224, 255, 135, 240, 60, 3, 224, 255, 195, 252, 30, 0, 248, 61, 240, 127, 195, 224, 7, 131, 207, 135, 158, 15, 12, 15, 7, 159, 7, 159, 15, 14, 1, 240, 121, 240, 120, 248, 60, 120, 1, 248, 120, 248, 60, 124, 28, 124, 56, 1, 248, 60, 124, 30, 63, 15, 159, 131, 1, 65, 248, 124, 124, 30, 31, 135, 207, 131, 129, 193, 239, 224, 249, 248, 62, 127, 15, 199, 131, 130, 163, 239, 224, 249, 248, 63, 127, 159, 227, 224, 208, 240, 184, 127, 7, 135, 224, 125, 255, 127, 253, 6, 165, 98, 248, 63, 224, 245, 254, 15, 255, 245, 250, 92, 89, 63, 47, 135, 248, 29, 63, 129, 255, 253, 127, 171, 194, 219, 233, 124, 159, 248, 125, 255, 139, 255, 253, 127, 79, 133, 233, 250, 63, 199, 255, 234, 255, 250, 191, 255, 253, 126, 173, 175, 253, 250, 255, 15, 255, 255, 191, 253, 253, 223, 95, 239, 235, 254, 255, 239, 255, 255, 255, 255, 191, 255, 181, 255, 219, 239, 251, 245, 255, 255, 191, 255, 215, 254, 255, 223, 239, 239, 223, 255, 191, 223, 215, 255, 219, 253, 255, 255, 251, 127, 254, 254, 191, 223, 255, 239, 223, 255, 239, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 247, 255, 251, 253, 253, 127, 255, 255, 255, 191, 158, 20, 83, 248, 254, 135, 195, 252, 0, 3, 225, 237, 15, 144, 248, 111, 199, 252, 120, 115, 66, 8, 60, 123, 195, 180, 62, 83, 225, 124, 24, 79, 15, 112, 246, 159, 73, 255, 62, 1, 192, 0, 120, 223, 13, 225, 254, 91, 207, 240, 128, 12, 120, 79, 16, 212, 57, 79, 161, 240, 0, 12, 124, 39, 145, 204, 55, 147, 240, 124, 0, 12, 125, 39, 195, 124, 7, 195, 248, 152, 128, 0, 60, 134, 225, 62, 151, 232, 255, 35, 48, 3, 55, 33, 152, 78, 196, 127, 63, 248, 12, 0, 230, 123, 31, 208, 236, 2, 243, 51, 57, 0, 0, 103, 236, 63, 1, 252, 15, 230, 119, 25, 128, 204, 103, 200, 86, 1, 184, 71, 225, 187, 76, 192, 48, 141, 242, 45, 225, 58, 199, 249, 230, 28, 160, 8, 83, 190, 229, 233, 11, 118, 247, 255, 125, 225, 128, 0, 1, 255, 238, 76, 152, 145, 55, 255, 249, 144, 32, 0, 159, 255, 114, 68, 204, 137, 255, 255, 196, 0, 0, 0, 31, 255, 251, 48, 32, 101, 255, 255, 240, 0, 0, 127, 255, 230, 96, 128, 143, 255, 255, 246, 0, 0, 0, 127, 249, 147, 230, 15, 62, 124, 241, 195, 6, 0, 240, 71, 156, 60, 225, 159, 56, 227, 142, 97, 196, 120, 7, 216, 63, 192, 125, 131, 252, 11, 192, 0, 120, 7, 248, 39, 200, 63, 129, 254, 0, 128, 117, 15, 240, 15, 240, 31, 224, 254, 0, 0, 29, 99, 190, 65, 158, 33, 255, 255, 253, 32, 0, 223, 255, 243, 252, 128, 23, 255, 255, 64, 0, 31, 255, 214, 109, 128, 5, 255, 255, 168, 0, 11, 255, 255, 126, 128, 5, 255, 255, 224, 0, 27, 255, 223, 125, 128, 7, 255, 253, 224, 0, 251, 29, 226, 57, 71, 127, 223, 225, 0, 7, 17, 252, 39, 152, 206, 103, 185, 140, 0, 112, 63, 136, 236, 55, 185, 220, 196, 0, 57, 15, 228, 118, 19, 241, 254, 204, 0, 59, 31, 196, 118, 39, 201, 252, 0, 0, 232, 127, 35, 240, 156, 15, 224, 32, 14, 135, 242, 59, 15, 192, 254, 0, 3, 33, 252, 143, 194, 240, 63, 0, 3, 33, 252, 207, 195, 240, 61, 128, 14, 7, 241, 157, 143, 226, 120, 0, 58, 31, 198, 116, 63, 200, 248, 0, 96, 63, 152, 236, 55, 49, 220, 64, 56, 143, 227, 61, 142, 225, 127, 223, 96, 15, 145, 254, 39, 192, 127, 143, 255, 159, 193, 228, 3, 254, 63, 229, 190, 103, 223, 127, 251, 232, 7, 223, 255, 255, 253, 170, 187, 255, 255, 239, 191, 255, 255, 255, 255, 255, 255, 255, 255, 255, 254, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 191, 255, 255, 255, 255, 255, 255, 125, 255, 255, 254, 222, 219, 239, 255, 251, 255, 255, 253, 189, 239, 119, 191, 255, 255, 255, 255, 251, 239, 168, 0, 5, 127, 255, 255, 176, 0, 0, 1, 127, 255, 201, 153, 18, 127, 255, 128, 0, 127, 255, 221, 248, 1, 47, 255, 96, 0, 31, 152, 127, 131, 28, 115, 207, 120, 0, 0, 248, 135, 56, 115, 141, 249, 159, 0, 0, 31, 136, 243, 15, 112, 223, 25, 230, 24, 0, 120, 71, 152, 115, 15, 241, 222, 1, 192, 3, 194, 60, 35, 220, 125, 199, 188, 195, 0, 60, 33, 210, 30, 97, 230, 30, 113, 192, 9, 210, 159, 8, 240, 215, 143, 120, 112, 0, 31, 15, 240, 122, 131, 227, 15, 12, 48, 0, 120, 123, 224, 240, 131, 225, 249, 195, 192, 1, 240, 254, 208, 248, 61, 120, 126, 30, 16, 8, 120, 31, 10, 31, 7, 195, 135, 224, 248, 16, 15, 129, 240, 43, 248, 126, 30, 159, 131, 224, 0, 62, 7, 193, 161, 224, 126, 94, 31, 135, 224, 0, 62, 7, 192, 248, 224, 124, 31, 248, 62, 0, 124, 15, 129, 245, 224, 124, 63, 248, 62, 0, 24, 15, 193, 241, 224, 124, 63, 233, 252, 0, 224, 62, 131, 203, 128, 243, 255, 255, 240, 3, 1, 253, 62, 124, 2, 15, 255, 255, 0, 0, 15, 255, 251, 227, 1, 127, 255, 248, 0, 0, 127, 255, 248, 112, 0, 127, 255, 128, 0, 15, 255, 255, 204, 0, 31, 255, 224, 0, 0, 255, 255, 251, 0, 6, 255, 252, 0, 0, 127, 255, 255, 96, 1, 127, 255, 0, 0, 15, 255, 255, 232, 0, 47, 255, 208, 0, 3, 255, 255, 233, 0, 3, 255, 248, 0, 0, 255, 255, 252, 225, 2, 127, 255, 0, 0, 59, 255, 220, 48, 65, 159, 255, 192, 0, 15, 255, 247, 4, 16, 247, 255, 248, 0, 19, 255, 254, 233, 129, 55, 255, 232, 0, 159, 251, 119, 108, 81, 223, 248, 0, 57, 167, 60, 227, 156, 227, 156, 192, 1, 200, 119, 136, 102, 31, 198, 240, 0, 57, 14, 241, 60, 199, 243, 28, 128, 14, 97, 252, 79, 112, 126, 103, 32, 7, 224, 103, 35, 123, 19, 185, 0, 6, 200, 110, 198, 127, 38, 118, 0, 59, 35, 59, 35, 63, 3, 50, 1, 187, 51, 127, 34, 127, 32, 0, 27, 178, 38, 254, 64, 239, 192, 1, 178, 100, 205, 255, 64, 155, 32, 19, 102, 205, 155, 50, 118, 109, 32, 1, 127, 255, 221, 160, 77, 181, 186, 140, 191, 255, 255, 250, 183, 117, 170, 165, 191, 255, 255, 237, 86, 251, 239, 118, 223, 252, 155, 52, 217, 50, 108, 255, 100, 147, 118, 219, 124, 221, 255, 178, 79, 246, 4, 137, 255, 252, 153, 191, 144, 0, 2, 246, 100, 6, 255, 192, 79, 240, 0, 69, 217, 128, 223, 224, 79, 254, 1, 244, 0, 238, 64, 63, 240, 31, 248, 7, 244, 0, 123, 128, 79, 224, 27, 252, 7, 254, 0, 30, 226, 63, 248, 7, 252, 3, 200, 0, 223, 160, 29, 246, 1, 190, 224, 0, 5, 186, 196, 51, 204, 35, 253, 132, 33, 25, 190, 97, 31, 239, 49, 220, 66, 18, 177, 79, 252, 115, 223, 60, 243, 214, 41, 214, 153, 127, 252, 235, 94, 251, 93, 58, 225, 92, 74, 170, 213, 85, 90, 175, 85, 106, 210, 169, 170, 90, 206, 173, 86, 181, 84, 170, 214, 213, 86, 170, 170, 213, 115, 214, 245, 171, 85, 84, 183, 89, 111, 42, 106, 170, 197, 85, 74, 170, 170, 175, 85, 234, 171, 212, 170, 85, 42, 210, 171, 92, 186, 171, 117, 95, 106, 234, 170, 213, 85, 43, 85, 85, 87, 74, 170, 219, 122, 170, 170, 170, 165, 170, 169, 170, 234, 234, 84, 170, 173, 85, 170, 213, 106, 170, 149, 125, 212, 214, 173, 85, 165, 90, 154, 173, 86, 154, 102, 150, 234, 165, 107, 85, 85, 42, 147, 105, 182, 219, 81, 140, 247, 123, 236, 82, 0, 0, 127, 152, 79, 243, 13, 254, 128, 0, 25, 196, 3, 117, 136, 255, 224, 32, 1, 187, 72, 142, 236, 39, 119, 51, 32, 51, 35, 19, 59, 153, 155, 217, 128, 6, 236, 204, 142, 238, 255, 238, 213, 84, 141, 255, 255, 182, 255, 255, 237, 119, 223, 191, 255, 255, 255, 247, 223, 251, 255, 255, 127, 255, 255, 223, 247, 206, 111, 127, 206, 102, 63, 216, 206, 99, 185, 230, 115, 155, 204, 103, 56, 206, 99, 57, 157, 53, 42, 227, 85, 206, 117, 87, 86, 170, 174, 170, 170, 171, 91, 149, 212, 85, 110, 170, 173, 85, 85, 170, 169, 37, 173, 87, 43, 154, 227, 21, 170, 213, 107, 86, 173, 107, 90, 181, 45, 89, 106, 212, 173, 87, 58, 116, 157, 162, 121, 156, 107, 218, 183, 172, 178, 89, 181, 75, 172, 248, 117, 173, 203, 220, 158, 239, 243, 126, 239, 119, 109, 205, 251, 109, 110, 191, 255, 191, 247, 127, 255, 127, 251, 238, 251, 222, 191, 111, 255, 127, 255, 255, 255, 255, 255, 251, 255, 255, 255, 250, 251, 247, 190, 219, 254, 255, 255, 255, 255, 255, 255, 255, 237, 223, 127, 247, 241, 247, 250, 243, 255, 179, 247, 203, 239, 151, 246, 191, 247, 223, 254, 254, 128, 0, 31, 254, 3, 254, 60, 0, 0, 127, 128, 124, 255, 128, 254, 0, 3, 252, 7, 247, 248, 15, 240, 0, 31, 224, 62, 191, 192, 127, 0, 0, 255, 1, 227, 252, 7, 254, 0, 7, 248, 14, 31, 192, 125, 252, 0, 31, 192, 96, 254, 7, 7, 192, 0, 63, 7, 7, 240, 240, 124, 0, 0, 127, 6, 7, 227, 193, 241, 224, 224, 126, 28, 31, 158, 7, 63, 128, 0, 126, 0, 31, 254, 0, 254, 0, 0, 126, 112, 28, 249, 193, 199, 128, 0, 62, 120, 193, 231, 140, 30, 112, 192, 15, 56, 227, 159, 60, 99, 140, 113, 0, 115, 206, 115, 24, 198, 51, 156, 198, 32, 239, 255, 243, 19, 62, 247, 32, 0, 110, 243, 200, 103, 125, 152, 198, 32, 111, 242, 37, 191, 200, 143, 252, 0, 6, 108, 32, 79, 255, 36, 127, 96, 3, 38, 246, 71, 111, 252, 137, 89, 1, 183, 114, 68, 110, 221, 137, 3, 0, 109, 221, 153, 51, 254, 64, 9, 128, 25, 54, 102, 100, 221, 217, 17, 52, 1, 179, 102, 68, 111, 236, 128, 77, 128, 27, 187, 96, 153, 255, 64, 2, 108, 0, 63, 254, 224, 31, 255, 242, 80, 204, 34, 11, 255, 242, 23, 255, 188, 204, 214, 101, 108, 255, 255, 251, 185, 219, 119, 54, 77, 231, 158, 115, 103, 223, 216, 217, 237, 141, 198, 51, 91, 153, 250, 230, 246, 198, 103, 58, 205, 175, 253, 246, 253, 236, 239, 127, 191, 239, 63, 245, 179, 111, 255, 255, 251, 239, 247, 191, 247, 255, 255, 183, 255, 255, 255, 255, 255, 255, 255, 255, 255, 239, 127, 255, 191, 251, 255, 255, 255, 255, 255, 254, 255, 215, 255, 127, 247, 157, 239, 219, 47, 187, 187, 189, 231, 238, 255, 127, 190, 235, 127, 185, 220, 255, 54, 189, 140, 117, 191, 248, 157, 254, 253, 223, 98, 251, 123, 252, 253, 255, 122, 20, 5, 255, 248, 63, 232, 0, 7, 240, 31, 31, 240, 31, 192, 3, 254, 3, 247, 248, 7, 244, 0, 127, 192, 62, 255, 192, 127, 128, 3, 254, 1, 255, 255, 0, 254, 0, 31, 240, 7, 255, 192, 7, 240, 0, 255, 128, 63, 255, 0, 255, 128, 15, 248, 3, 255, 240, 15, 240, 0, 255, 192, 63, 255, 0, 254, 0, 7, 252, 7, 255, 248, 31, 192, 0, 63, 192, 126, 127, 1, 248, 28, 1, 254, 0, 231, 240, 31, 199, 192, 3, 248, 15, 143, 224, 126, 62, 3, 15, 224, 120, 126, 7, 195, 224, 48, 63, 7, 3, 240, 240, 252, 8, 1, 252, 8, 63, 128, 15, 231, 0, 7, 192, 193, 249, 224, 241, 240, 0, 124, 112, 62, 248, 60, 120, 0, 3, 199, 3, 223, 0, 14, 112, 0, 121, 225, 15, 206, 17, 252, 0, 30, 56, 195, 255, 8, 127, 192, 7, 156, 96, 247, 140, 63, 224, 1, 206, 16, 123, 206, 12, 240, 0, 123, 194, 28, 255, 2, 254, 0, 7, 252, 97, 255, 208, 7, 224, 4, 124, 224, 199, 254, 96, 201, 18, 100, 183, 109, 219, 107, 164, 182, 149, 173, 218, 221, 182, 75, 117, 109, 42, 219, 90, 202, 210, 210, 82, 218, 214, 182, 182, 169, 106, 212, 173, 90, 210, 149, 43, 82, 82, 181, 169, 90, 212, 169, 94, 219, 93, 106, 149, 74, 181, 74, 173, 85, 165, 106, 173, 74, 165, 171, 93, 74, 171, 85, 170, 169, 77, 82, 173, 171, 86, 169, 86, 165, 170, 85, 106, 181, 93, 172, 173, 106, 211, 165, 74, 182, 202, 90, 150, 150, 214, 221, 187, 102, 125, 157, 183, 154, 246, 219, 183, 238, 221, 183, 251, 183, 255, 255, 255, 119, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 253, 255, 255, 255, 255, 253, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 223, 255, 255, 255, 251, 57, 153, 206, 128, 103, 255, 176, 1, 51, 253, 137, 159, 255, 0, 55, 111, 224, 77, 131, 102, 202, 76, 155, 249, 178, 0, 79, 255, 81, 36, 19, 100, 212, 201, 191, 246, 72, 5, 155, 242, 6, 252, 27, 100, 220, 137, 55, 118, 192, 36, 223, 250, 36, 222, 4, 155, 103, 68, 187, 252, 1, 182, 221, 186, 38, 217, 0, 147, 109, 186, 55, 233, 34, 72, 95, 255, 255, 255, 255, 100, 1, 47, 255, 255, 253, 222, 253, 213, 187, 255, 255, 255, 253, 247, 191, 255, 255, 255, 255, 109, 228, 219, 109, 191, 178, 233, 146, 89, 124, 146, 218, 73, 119, 178, 219, 220, 150, 214, 211, 219, 47, 73, 201, 182, 77, 236, 153, 159, 164, 155, 70, 201, 191, 118, 77, 255, 233, 111, 216, 145, 55, 38, 236, 200, 159, 255, 242, 101, 2, 76, 158, 201, 132, 223, 192, 223, 255, 204, 200, 1, 129, 187, 1, 187, 201, 159, 217, 159, 248, 15, 0, 3, 178, 27, 248, 19, 248, 31, 252, 15, 251, 9, 180, 128, 14, 200, 77, 196, 223, 196, 254, 200, 255, 223, 219, 236, 54, 0, 63, 35, 126, 3, 254, 7, 254, 151, 254, 223, 253, 159, 144, 32, 63, 50, 126, 76, 222, 25, 255, 250, 127, 247, 191, 223, 255, 238, 255, 253, 191, 233, 32, 3, 255, 239, 255, 251, 230, 223, 255, 239, 187, 255, 255, 255, 250, 0, 3, 255, 237, 203, 239, 130, 175, 255, 247, 87, 255, 251, 255, 255, 127, 239, 255, 255, 255, 255, 255, 255, 253, 181, 104, 0, 127, 255, 250, 255, 236, 219, 255, 255, 183, 127, 247, 255, 254, 200, 4, 11, 255, 255, 247, 219, 183, 255, 255, 95, 255, 255, 254, 255, 191, 255, 119, 255, 238, 253, 254, 255, 255, 235, 191, 255, 255, 255, 191, 255, 190, 247, 111, 191, 255, 254, 253, 251, 239, 255, 255, 255, 251, 255, 255, 253, 255, 255, 219, 255, 255, 255, 255, 255, 253, 253, 253, 255, 255, 255, 253, 255, 255, 255, 254, 255, 253, 255, 127, 255, 255, 253, 255, 255, 247, 239, 255, 255, 255, 190, 255, 255, 255, 251, 255, 251, 255, 255, 255, 255, 255, 254, 253, 255, 223, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 191, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 254}; \ No newline at end of file diff --git a/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/talkingVoltmeter/cornell/dpcm_1bit/speech_1_bit_diff.c b/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/talkingVoltmeter/cornell/dpcm_1bit/speech_1_bit_diff.c new file mode 100644 index 0000000..881954b --- /dev/null +++ b/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/talkingVoltmeter/cornell/dpcm_1bit/speech_1_bit_diff.c @@ -0,0 +1,113 @@ +//Voice decompressor example +#include +#include +#include +#include +#include +#include "USART.h" + +#define TableSize 3280 //refers to the following incl file +//Contains the packed 1-bit codes for syntehsis +#include "mega644_1_bit.h" + +//reconstruction differentials +//volatile signed char PCMvalue[4] = {-78, -16, 16, 78}; +volatile signed char PCMvalue[2] = {-10, 10}; + +volatile unsigned int outI, tableI; //indexes +volatile unsigned char cycle ; //decode phase counter +volatile signed char out, lastout, newvalue; //output values +volatile unsigned char p1,p2,p3,p4,p5,p6,p7,p8; //hold 8 differentials +volatile unsigned char packed ; //byte containing 4 2-bit values +volatile uint8_t speaking; /* flag for whether currently speaking or not */ + +//generate waveform at 7812 scamples/sec +ISR (TIMER2_COMPA_vect){ + //compute next sample + cycle = outI & 7; // outI modulo 8 + if (cycle==0) //do we need to unpack more data? + { + if (tableI>3); + //update outputs + OCR0A = out + 128; + lastout = out; + outI++; + //at end, turn off TCCRO + if (tableI==TableSize) TCCR2B = 0; +} //ISR + +int main(void){ + DDRD |= (1<0){}; + OCR0A = 128 ; + _delay_ms(500); + PORTB ^= (1<=p(1) & dd=p(2) & dd=p(3)))=3; + +dl = zeros(1,length(ddcode)); +for i=2:length(ddcode) + dl(i) = dl(i-1)+ p(ddcode(i)+4) - .125*dl(i-1); % .* w'; +end + +fit = mean((d(2:end)-dl').^2); \ No newline at end of file diff --git a/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/talkingVoltmeter/cornell/dpcm_2bit/Makefile b/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/talkingVoltmeter/cornell/dpcm_2bit/Makefile new file mode 100644 index 0000000..09f3b4d --- /dev/null +++ b/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/talkingVoltmeter/cornell/dpcm_2bit/Makefile @@ -0,0 +1,186 @@ + +##########------------------------------------------------------########## +########## Project-specific Details ########## +########## Check these every time you start a new project ########## +##########------------------------------------------------------########## + +MCU = atmega168 +F_CPU = 8000000 +BAUD = 9600 +## Also try BAUD = 19200 or 38400 if you're feeling lucky. + +## This is where your main() routine lives (without .c extension) +TARGET = speech_644_GCC +## If you've split your program into multiple .c / .h files, +## include the additional source here (without the .c or .h extension) +LOCAL_SOURCE = + +EXTRA_SOURCE_DIR = ../../../learningAVR/ +EXTRA_SOURCE_FILES = USART + +##########------------------------------------------------------########## +########## 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 = + +##########------------------------------------------------------########## +########## 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. ########## +##########------------------------------------------------------########## + +## Defined programs / locations +CC = avr-gcc +OBJCOPY = avr-objcopy +OBJDUMP = avr-objdump +AVRSIZE = avr-size +AVRDUDE = avrdude + +## Compilation options, type man avr-gcc if you're curious. +CFLAGS = -mmcu=$(MCU) -DF_CPU=$(F_CPU)UL -DBAUD=$(BAUD) -Os -I. -I$(EXTRA_SOURCE_DIR) +CFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums +CFLAGS += -Wall -Wstrict-prototypes +CFLAGS += -g -ggdb +CFLAGS += -ffunction-sections -fdata-sections -Wl,--gc-sections -Wl,--relax +CFLAGS += -std=gnu99 +## CFLAGS += -Wl,-u,vfprintf -lprintf_flt -lm ## for floating-point printf +## CFLAGS += -Wl,-u,vfprintf -lprintf_min ## for smaller printf + +## Lump target and extra source files together +TARGET = $(strip $(basename $(MAIN))) +SRC = $(TARGET).c +EXTRA_SOURCE = $(addprefix $(EXTRA_SOURCE_DIR), $(EXTRA_SOURCE_FILES)) +SRC += $(EXTRA_SOURCE) +SRC += $(LOCAL_SOURCE) + +## List of all header files +HEADERS = $(SRC:.c=.h) + +## For every .c file, compile an .o object file +OBJ = $(SRC:.c=.o) + +## Generic Makefile targets. (Only .hex file is necessary) +all: $(TARGET).hex + +%.hex: %.elf + $(OBJCOPY) -R .eeprom -O ihex $< $@ + +%.elf: $(SRC) + $(CC) $(CFLAGS) $(SRC) --output $@ + +%.eeprom: %.elf + $(OBJCOPY) -j .eeprom --change-section-lma .eeprom=0 -O ihex $< $@ + +debug: + @echo + @echo "Source files:" $(SRC) + @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 + +eeprom: $(TARGET).eeprom + +%.lst: %.elf + $(OBJDUMP) -S $< > $@ + +# 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 *~ + +##########------------------------------------------------------########## +########## 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 diff --git a/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/talkingVoltmeter/cornell/dpcm_2bit/Speech_encode_GCC.m b/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/talkingVoltmeter/cornell/dpcm_2bit/Speech_encode_GCC.m new file mode 100644 index 0000000..f7a6eab --- /dev/null +++ b/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/talkingVoltmeter/cornell/dpcm_2bit/Speech_encode_GCC.m @@ -0,0 +1,83 @@ + +% Speech encoder for Mega644/GCC decoder +clear all + +%===================================%the Encoder +[d,r] = wavread('Z:\EEdocs\4760\ece4760labstuff\GCCmega644\Speech\AllDigits8khz.WAV'); %4 +%scale to about unity +res = 1/(max(max(d),abs(min(d)) )); +dd = diff(d * res); +%init the code vector +ddcode = zeros(1,length(dd)); + +% brkpt1=-0.5; brkpt2=0 ; brkpt3=0.5; +% value = [-.75, -.25 .25 .75]; +%breakpoints are changeable +brkpt1=-0.05; brkpt2=0 ; brkpt3=0.05; +%quantize the first derivative +ddcode(find(dd=brkpt1 & dd=brkpt2 & dd=brkpt3))=3; + +%make the length of ddcode a multiple of 4 +ddcode = [ddcode,zeros(1,4-mod(length(ddcode),4))]; +length(ddcode) +index=1; +for i=1:4:length(ddcode) + packed(index)=ddcode(i)*64 + ddcode(i+1)*16 + ... + ddcode(i+2)*4 + ddcode(i+3) ; + index=index+1; +end +%make a textfile with GCC source code in it. +fname='DPCMAllDigits.h'; +fid = fopen(fname,'w'); +fprintf(fid,'const prog_uint8_t DPCMAllDigits[%d]={\r',length(packed)); +for i=1:length(packed)-1 + fprintf(fid,' %5d,\r',packed(i)); +end +fprintf(fid,' %5d};\r',packed(end)); +fclose(fid); + +%===================================%the Decoder +%value based on quantizer +value = [-.16, -.026 .026 .16]; +dl(1)=0; +j=2; +for i=1:length(packed) + p1 = fix(bitand(packed(i),192)/64); + p2 = fix(bitand(packed(i),48)/16); + p3 = fix(bitand(packed(i), 12)/4); + p4 = fix(bitand(packed(i),3)); + %note that a bit of highpass has been added to reduce drift + dl(j) = dl(j-1)+ value(p1+1) - .125*dl(j-1); + j=j+1; + dl(j) = dl(j-1)+ value(p2+1) - .125*dl(j-1); + j=j+1; + dl(j) = dl(j-1)+ value(p3+1) - .125*dl(j-1); + j=j+1; + dl(j) = dl(j-1)+ value(p4+1) - .125*dl(j-1); + j=j+1; +end +% dl(1)=0; +% for i=1:length(ddcode) +% dl(i) = dl(i-1)+ value(ddcode(i)+1) - .125*dl(i-1); % .* w'; +% end + +%====================================%playback and graphing +soundsc(dl,r); +% Compare the spectrograms +figure(1);clf + +subplot(211) +spectrogram(d,256,r); +colormap gray +title('Original'); + +subplot(212) +spectrogram(dl,256,r); +colormap gray +title('synth'); + + + diff --git a/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/talkingVoltmeter/cornell/dpcm_2bit/downsample.m b/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/talkingVoltmeter/cornell/dpcm_2bit/downsample.m new file mode 100644 index 0000000..b175c87 --- /dev/null +++ b/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/talkingVoltmeter/cornell/dpcm_2bit/downsample.m @@ -0,0 +1,5 @@ +clear all +%reads a file at 16kHz and outputs it a 8kHz, 8 bits +[d,r] = wavread('test6.WAV'); +y = resample(d,1,2); +wavwrite(y,8000,8,'test6small.wav') \ No newline at end of file diff --git a/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/talkingVoltmeter/cornell/dpcm_2bit/sineWave.py b/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/talkingVoltmeter/cornell/dpcm_2bit/sineWave.py new file mode 100644 index 0000000..ed0c618 --- /dev/null +++ b/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/talkingVoltmeter/cornell/dpcm_2bit/sineWave.py @@ -0,0 +1,38 @@ +## Demo python program to create sine wave + +from struct import pack, unpack +from math import sin, pi +import wave +import os +import random + + +RATE = 44100 + +## GENERATE MONO FILE ## +wv = wave.open('test_mono.wav', 'w') +wv.setparams((1, 2, RATE, 0, 'NONE', 'not compressed')) +maxVol=2**15-1.0 #maximum amplitude +wvData="" +for i in range(0, RATE*3): + wvData+=pack('h', maxVol*sin(2*pi*i*440.0/RATE)) + +wv.writeframes(wvData) +wv.close() +os.system("mplayer test_mono.wav") + +## GENERATE STEREO FILE ## +wv = wave.open('test_stereo.wav', 'w') +wv.setparams((2, 2, RATE, 0, 'NONE', 'not compressed')) +maxVol=2**15-1.0 #maximum amplitude +waveData = "" +for i in range(0, RATE*3): + t = 2*pi*i/RATE # time-step in radians*sec + waveData+=pack('h', maxVol*sin(t*440.0)) #440Hz left + waveData+=pack('h', maxVol*sin(t*220.0)) #220Hz right + + +wv.writeframes(waveData) +wv.close() +os.system("mplayer test_stereo.wav") + diff --git a/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/talkingVoltmeter/cornell/dpcm_2bit/speech_644_GCC.c b/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/talkingVoltmeter/cornell/dpcm_2bit/speech_644_GCC.c new file mode 100644 index 0000000..130fddb --- /dev/null +++ b/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/talkingVoltmeter/cornell/dpcm_2bit/speech_644_GCC.c @@ -0,0 +1,109 @@ +// Talking Voltmeter Example + +#include +#include +#include +#include +#include +#include "pinDefines.h" + +#include "DPCM_rich_hello_world_8000.h" +#define TABLE_NAME DPCM_rich_hello_world_8000 + +volatile uint16_t sampleNumber; // sample index +volatile int8_t out, lastout; // output values +volatile uint8_t p1, p2, p3, p4; // hold 4 differentials +const int8_t PCMvalue[4] = {-20, -4, 4, 20}; + +ISR (TIMER2_COMPA_vect){ + /* Timer 2 controls sampling speed -- + ISR reads new data, loads PWM output, OCR0A */ + /* Since we can decode 4 2-bit values at once, need to know where + we are in the 4-step mini-cycle. */ + uint8_t cycle = sampleNumber & 0b00000011; /* our 4 steps */ + uint16_t tableEntry = sampleNumber >> 2; /* 4 steps per table byte */ + uint8_t packedData; /* the new byte */ + switch(cycle){ + case 0: // Start of the cycle, unpack next byte of samples + if (tableEntry < sizeof(TABLE_NAME)){ + packedData = pgm_read_byte(&TABLE_NAME[tableEntry]) ; + p1 = (packedData>>6) & 3 ; + p2 = (packedData>>4) & 3 ; + p3 = (packedData>>2) & 3 ; + p4 = (packedData & 3); + } + /* Add in the next PCM differential value */ + out = lastout + PCMvalue[p1] - (lastout>>3) ; + break; + case 1: + out = lastout + PCMvalue[p2] - (lastout>>3) ; + break; + case 2: + out = lastout + PCMvalue[p3] - (lastout>>3) ; + break; + case 3: + out = lastout + PCMvalue[p4] - (lastout>>3) ; + break; + } + /* Update PWM audio output */ + OCR0A = out + 128; /* re-center for 0-255 PWM */ + lastout = out; /* update last value */ + sampleNumber++; /* on to next sample */ + + /* When done, turn off PWM, Timer0 */ + if (sampleNumber == 4*sizeof(TABLE_NAME)-1) { + TCCR2B = 0; /* disable sample-playback clock */ + OCR0A = 128; /* idle at mid-voltage */ + lastout = 0; /* start at 0 next time */ + } +} // end ISR + +void initTimer0(void){ + // Timer 0 Configured for free-running PWM Audio Output + TCCR0A |= (1< +#include +#include +#include +#include +#include +#include "pinDefines.h" +#include "USART.h" + +#include "talkingVoltmeter.h" + +void startSampleTimer(void) { + sampleNumber = 0; /* back to start of sample table */ + TCCR2B = (1 << CS21); /* turn on timer clock */ + /* Two clock options above end up ~8kHz on 8MHz system */ +} +void stopSampleTimer(void) { + TCCR2B = 0; /* disable sample-playback clock */ + OCR0A = 128; /* idle PWM at mid-voltage */ + lastout = 0; /* start at 0 next time */ +} +void speak(void) { + startSampleTimer(); + loop_until_bit_is_clear(TCCR2B, CS21); /* Wait until done */ +} + +void updatePWMAudio(void) { + OCR0A = out + 128; /* re-center for 0-255 PWM */ + lastout = out; /* update last value */ + sampleNumber++; /* on to next sample */ +} +void unpackByte(uint8_t dataByte) { + /* Select pairs of bits from byte, save out */ + differentials[0] = (dataByte >> 6) & 0b00000011; + differentials[1] = (dataByte >> 4) & 0b00000011; + differentials[2] = (dataByte >> 2) & 0b00000011; + differentials[3] = (dataByte & 0b00000011); +} + +/* Timer 2 controls sampling speed. + ISR reads new data, loads PWM values into OCR0A */ +ISR(TIMER2_COMPA_vect) { + /* Since we can decode 4 2-bit values at once, need to know where + we are in the 4-step mini-cycle. */ + uint8_t cycle = sampleNumber & 0b00000011; /* keep last 2 bits */ + uint16_t tableEntry; + uint8_t packedData; + + if (cycle == 0) { /* at first sample, re-load */ + tableEntry = sampleNumber >> 2; /* where we are in table */ + if (tableEntry < thisTableLength) { + /* read the next byte from the selected table */ + packedData = pgm_read_byte(&thisTableP[tableEntry]); + unpackByte(packedData); /* split up byte into differentials[] */ + } + else { /* at end of table, done. */ + stopSampleTimer(); + } + } + /* Decode the differences: current value = last + difference */ + out = lastout + dpcmWeights[differentials[cycle]] - (lastout >> 4); + updatePWMAudio(); +} // end ISR (TIMER2_COMPA_vect) + +void printString_Progmem(const char *stringP) { + char oneLetter; + while ((oneLetter = pgm_read_byte(stringP))) { + transmitByte(oneLetter); + stringP++; + } +} + + +int main(void) { + uint16_t voltage; + uint8_t volts; + uint8_t tenths; + uint8_t vcc = 51; /* 10x VCC, in volts */ + + clock_prescale_set(clock_div_1); /* 8 MHz */ + initTimer0(); + initTimer2(); + sei(); /* for timer2 ISR */ + initADC(); + initUSART(); + + printString_Progmem(PSTR("\r\n--=( Talking Voltmeter )=--\r\n")); + + selectTable(INTRO); + speak(); + + while (1) { + + ADCSRA |= (1 << ADSC); /* start ADC */ + loop_until_bit_is_clear(ADCSRA, ADSC); + + voltage = ADC * vcc + vcc / 2; /* vcc/2 to make rounding work */ + voltage = voltage >> 10; /* divide by 10-bits for ADC */ + /* "voltage" is now actually 10x real-world voltage */ + volts = voltage / 10; + tenths = voltage % 10; + + transmitByte('0' + volts); /* serial output as well */ + selectTable(volts); /* 0 points to ZERO_TABLE, etc */ + speak(); + + transmitByte('.'); + selectTable(POINT); + speak(); + + transmitByte('0' + tenths); + selectTable(tenths); + speak(); + + printString_Progmem(PSTR(" volts\r\n")); + selectTable(VOLTS); + speak(); + + _delay_ms(SPEECH_DELAY); + + } /* end while */ + return 0; +} diff --git a/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/talkingVoltmeter/talkingVoltmeter.h b/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/talkingVoltmeter/talkingVoltmeter.h new file mode 100644 index 0000000..6014a3f --- /dev/null +++ b/Make AVR Examples/Chapter18_Using-Flash-Program-Memory/talkingVoltmeter/talkingVoltmeter.h @@ -0,0 +1,91 @@ +/* Include file with DPCM data in it */ +#include "allDigits.h" +#include + +// Now define sample-table names used in digits file +// From here on, no matter what you call the samples, +// you can refer to them as "ONE_TABLE", etc. +#define ONE_TABLE DPCM_one_8000 +#define TWO_TABLE DPCM_two_8000 +#define THREE_TABLE DPCM_three_8000 +#define FOUR_TABLE DPCM_four_8000 +#define FIVE_TABLE DPCM_five_8000 +#define SIX_TABLE DPCM_six_8000 +#define SEVEN_TABLE DPCM_seven_8000 +#define EIGHT_TABLE DPCM_eight_8000 +#define NINE_TABLE DPCM_nine_8000 +#define ZERO_TABLE DPCM_zero_8000 +#define POINT_TABLE DPCM_point_8000 +#define VOLTS_TABLE DPCM_volts_8000 +#define INTRO_TABLE DPCM_talkingvoltmeter_8000 + +#define SPEECH_DELAY 2000 /* milliseconds */ + +/* --------------- Globals used by the ISR -------------- */ +volatile uint8_t* thisTableP; /* points at the current speech table */ +volatile uint16_t thisTableLength; /* length of current speech table */ + +volatile uint16_t sampleNumber; // sample index +volatile int8_t out, lastout; // output values +volatile uint8_t differentials[4] = {0,0,0,0}; +const int8_t dpcmWeights[4] = {-12, -3, 3, 12}; + + +/* These arrays let us choose a table (and its length) numerically */ +const uint16_t tableLengths[] = { /* all sample tables are 8-bit */ + sizeof(ZERO_TABLE), sizeof(ONE_TABLE), sizeof(TWO_TABLE), + sizeof(THREE_TABLE), sizeof(FOUR_TABLE), sizeof(FIVE_TABLE), + sizeof(SIX_TABLE), sizeof(SEVEN_TABLE), sizeof(EIGHT_TABLE), + sizeof(NINE_TABLE), sizeof(POINT_TABLE), sizeof(VOLTS_TABLE), + sizeof(INTRO_TABLE) +}; + +// Create an indexing table of all of the start addresses for +// each spoken digit. And then store this index in PROGMEM. +const uint8_t* const tablePointers[] PROGMEM = { + ZERO_TABLE, ONE_TABLE, TWO_TABLE, THREE_TABLE, FOUR_TABLE, + FIVE_TABLE, SIX_TABLE, SEVEN_TABLE, EIGHT_TABLE, NINE_TABLE, + POINT_TABLE, VOLTS_TABLE, INTRO_TABLE +}; + +void selectTable(uint8_t whichTable){ + /* Set up global table pointer, lengths */ + uint16_t pointerAddress; + thisTableLength = tableLengths[whichTable]; + pointerAddress = (uint16_t) &tablePointers[whichTable]; + thisTableP = (uint8_t*) pgm_read_word(pointerAddress); +} + +/* Extra defines for the non-numeric values */ +#define POINT 10 +#define VOLTS 11 +#define INTRO 12 + + +///----------------- Init functions -------------------/// + +void initTimer0(void){ + // Timer 0 Configured for free-running PWM Audio Output + TCCR0A |= (1< $@ + +## 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 diff --git a/Make AVR Examples/Chapter19_EEPROM/eememDemo/eememDemo.c b/Make AVR Examples/Chapter19_EEPROM/eememDemo/eememDemo.c new file mode 100644 index 0000000..df219d1 --- /dev/null +++ b/Make AVR Examples/Chapter19_EEPROM/eememDemo/eememDemo.c @@ -0,0 +1,39 @@ +#include +#include +#include +/* #include */ +/* #include */ + +#define STRING_LEN 80 + +// Define EEMEM variables +uint8_t eepromCounter EEMEM = 0; +char eepromString[STRING_LEN] EEMEM = "Welcome to the EEMEM Demo.\r\n"; +uint16_t eepromWord EEMEM = 12345; + +int main(void) { + + initUSART(); + char ramString[STRING_LEN]; + uint8_t counter; + + while (1) { + printString("\r\n------------------\r\n"); + eeprom_read_block(ramString, eepromString, STRING_LEN); + printString(ramString); + + printString("\r\nThe counter reads: "); + counter = eeprom_read_byte(&eepromCounter); + printByte(counter); + + printString("\r\nMy uint16_t value is: "); + printWord(eeprom_read_word(&eepromWord)); + + printString("\r\n Enter a new introduction string below:\r\n"); + readString(ramString, STRING_LEN); + eeprom_update_block(ramString, eepromString, STRING_LEN); + counter++; + eeprom_update_byte(&eepromCounter, counter); + } + return 0; +} diff --git a/Make AVR Examples/Chapter19_EEPROM/favoriteColor/.gitignore b/Make AVR Examples/Chapter19_EEPROM/favoriteColor/.gitignore new file mode 100644 index 0000000..c286bce --- /dev/null +++ b/Make AVR Examples/Chapter19_EEPROM/favoriteColor/.gitignore @@ -0,0 +1,3 @@ +*.eeprom +*.raw + diff --git a/Make AVR Examples/Chapter19_EEPROM/favoriteColor/Makefile b/Make AVR Examples/Chapter19_EEPROM/favoriteColor/Makefile new file mode 100644 index 0000000..3d9e544 --- /dev/null +++ b/Make AVR Examples/Chapter19_EEPROM/favoriteColor/Makefile @@ -0,0 +1,196 @@ + +##########------------------------------------------------------########## +########## Project-specific Details ########## +########## Check these every time you start a new project ########## +##########------------------------------------------------------########## + +MCU = atmega168p +F_CPU = 1000000UL +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 diff --git a/Make AVR Examples/Chapter19_EEPROM/favoriteColor/favoriteColor.c b/Make AVR Examples/Chapter19_EEPROM/favoriteColor/favoriteColor.c new file mode 100644 index 0000000..53032f7 --- /dev/null +++ b/Make AVR Examples/Chapter19_EEPROM/favoriteColor/favoriteColor.c @@ -0,0 +1,38 @@ +#include +#include +#include + +#define STRING_MAXLEN 0x20 /* 32 characters */ +#define STRING_ADDRESS 0x20 +#define COUNTER_ADDRESS 0x10 + + +int main(void) { + + char myString[STRING_MAXLEN]; + char *eepromAddress = (char *) STRING_ADDRESS; + uint16_t counter; + + initUSART(); + + while (1) { + // Read from EEPROM and print out + eeprom_read_block(myString, eepromAddress, STRING_MAXLEN); + counter = eeprom_read_word((uint16_t *) COUNTER_ADDRESS); + printString("\r\nYour old favorite color is: "); + printString(myString); + + // Take input, store in EEPROM + printString("\r\n\r\n Type your new favorite color. "); + readString(myString, sizeof(myString)); + /* pass by address, function will change its values */ + eeprom_update_block(myString, eepromAddress, STRING_MAXLEN); + counter++; + printString("Thanks! \r\nYou've answered the same question "); + printWord(counter); + printString(" times. \r\n"); + eeprom_update_word((uint16_t *) COUNTER_ADDRESS, counter); + } + + return 0; +} diff --git a/Make AVR Examples/Chapter19_EEPROM/quickDemo/.gitignore b/Make AVR Examples/Chapter19_EEPROM/quickDemo/.gitignore new file mode 100644 index 0000000..c286bce --- /dev/null +++ b/Make AVR Examples/Chapter19_EEPROM/quickDemo/.gitignore @@ -0,0 +1,3 @@ +*.eeprom +*.raw + diff --git a/Make AVR Examples/Chapter19_EEPROM/quickDemo/Makefile b/Make AVR Examples/Chapter19_EEPROM/quickDemo/Makefile new file mode 100644 index 0000000..3d9e544 --- /dev/null +++ b/Make AVR Examples/Chapter19_EEPROM/quickDemo/Makefile @@ -0,0 +1,196 @@ + +##########------------------------------------------------------########## +########## Project-specific Details ########## +########## Check these every time you start a new project ########## +##########------------------------------------------------------########## + +MCU = atmega168p +F_CPU = 1000000UL +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 diff --git a/Make AVR Examples/Chapter19_EEPROM/quickDemo/quickDemo.c b/Make AVR Examples/Chapter19_EEPROM/quickDemo/quickDemo.c new file mode 100644 index 0000000..3184ee1 --- /dev/null +++ b/Make AVR Examples/Chapter19_EEPROM/quickDemo/quickDemo.c @@ -0,0 +1,19 @@ +#include +#include + +int main(void) { + + // Store the 8-bit value 5 in EEPROM slot 0 + uint8_t *address = (uint8_t *) 0; + eeprom_update_byte(address, 5); + + // Store the 16-bit value 12345 in EEPROM slots 5 and 6: + eeprom_update_word((uint16_t *) 5, 12345); + + // Store a character array (string) in EEPROM slots 16-28: + char *stringPointer = (char *) 16; + char myString[] = "hello world."; + eeprom_update_block(myString, stringPointer, sizeof(myString)); + + return 0; +} diff --git a/Make AVR Examples/Chapter19_EEPROM/vigenereCipher/.gitignore b/Make AVR Examples/Chapter19_EEPROM/vigenereCipher/.gitignore new file mode 100644 index 0000000..9b3c2e9 --- /dev/null +++ b/Make AVR Examples/Chapter19_EEPROM/vigenereCipher/.gitignore @@ -0,0 +1 @@ +vigenereCipher.eeprom diff --git a/Make AVR Examples/Chapter19_EEPROM/vigenereCipher/Makefile b/Make AVR Examples/Chapter19_EEPROM/vigenereCipher/Makefile new file mode 100644 index 0000000..3d9e544 --- /dev/null +++ b/Make AVR Examples/Chapter19_EEPROM/vigenereCipher/Makefile @@ -0,0 +1,196 @@ + +##########------------------------------------------------------########## +########## Project-specific Details ########## +########## Check these every time you start a new project ########## +##########------------------------------------------------------########## + +MCU = atmega168p +F_CPU = 1000000UL +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 diff --git a/Make AVR Examples/Chapter19_EEPROM/vigenereCipher/vigenereCipher.c b/Make AVR Examples/Chapter19_EEPROM/vigenereCipher/vigenereCipher.c new file mode 100644 index 0000000..267aa15 --- /dev/null +++ b/Make AVR Examples/Chapter19_EEPROM/vigenereCipher/vigenereCipher.c @@ -0,0 +1,146 @@ +/* +Vigenere Cipher encoder / decoder demo +And an excuse to play around with EEPROM memory + */ + +#include "vigenereCipher.h" + +// -------- Functions --------- // + +void printFromEEPROM(char *eepromString) { + uint8_t letter; + do { + letter = eeprom_read_byte((uint8_t *) eepromString); + transmitByte(letter); + eepromString++; + } while (letter); +} + +void enterText(char text[]) { + uint8_t i = 0; + char letter; + do { + letter = receiveByte(); + transmitByte(letter); /* echo */ + text[i] = letter; + i++; + } while (!(letter == '\r') && (i < (MAX_TEXT_LENGTH - 1))); + text[i - 1] = 0; +} + +void displayCodes(void) { + uint8_t i; + for (i = 0; i < 4; i++) { + transmitByte(' '); + transmitByte('0' + i); + printString(": "); + printFromEEPROM(codePointers[i]); + printString("\r\n"); + } +} + +void changeCode(char codeBuffer[]) { + char input; + char *codeAddress; + printString(" -- Choose code phrase to replace:\r\n"); + do { + displayCodes(); + input = receiveByte(); + } while ((input < '0') || (input > '3')); + codeAddress = codePointers[input - '0']; + printString(" -- Enter new code: "); + enterText(codeBuffer); + eeprom_update_block(codeBuffer, codeAddress, CODE_LEN); +} + +void selectCode(char code[]) { + char input; + char *codeAddress; + printFromEEPROM(promptSelectCode); + do { + displayCodes(); + input = receiveByte(); + } while ((input < '0') || (input > '3')); + codeAddress = codePointers[input - '0']; + eeprom_read_block(code, codeAddress, CODE_LEN); +} + +void encodeVigenere(char text[], char code[]) { + uint8_t codePosition = 0; + uint8_t textPosition = 0; + do { + if (code[codePosition] == 0) { /* loop when at end of code phrase */ + codePosition = 0; + } + text[textPosition] += code[codePosition] - 32; + if (text[textPosition] > 126) { + text[textPosition] -= 95; + } /* keeps within printing characters */ + codePosition++; /* and move on to the next */ + textPosition++; + } while (text[textPosition]); /* until zero at the end of string */ +} + +void decodeVigenere(char text[], char code[]) { + uint8_t codePosition = 0; + uint8_t textPosition = 0; + do { + if (code[codePosition] == 0) { + codePosition = 0; + } + if (code[codePosition] > text[textPosition]) { + text[textPosition] += 95; + } /* keeps within printing characters */ + text[textPosition] -= code[codePosition] - 32; + codePosition++; + textPosition++; + } while (text[textPosition]); +} + +int main(void) { + // -------- Inits --------- // + + char textBuffer[MAX_TEXT_LENGTH]; + char codeString[CODE_LEN]; + char input; + initUSART(); + + // ------ Event loop ------ // + while (1) { + + // Menu + printFromEEPROM(welcomeString); + printFromEEPROM(promptText); + printString(textBuffer); + printString("\r\n"); + printFromEEPROM(promptCode); + printString(codeString); + printString("\r\n\r\n ---------------------\r\n"); + printFromEEPROM(menuEnterText); + printFromEEPROM(menuCodeText); + printFromEEPROM(menuChangeCode); + printFromEEPROM(menuEncode); + printFromEEPROM(menuDecode); + input = receiveByte(); + + switch (input) { + case 'e': // encode + encodeVigenere(textBuffer, codeString); + break; + case 'd': // decode + decodeVigenere(textBuffer, codeString); + break; + case 'n': // new text + printFromEEPROM(promptTypeText); + enterText(textBuffer); + break; + case 'c': // choose code + selectCode(codeString); + break; + case 'x': // change code + changeCode(codeString); + break; + } + } /* End event loop */ + return 0; /* This line is never reached */ +} diff --git a/Make AVR Examples/Chapter19_EEPROM/vigenereCipher/vigenereCipher.c_outline b/Make AVR Examples/Chapter19_EEPROM/vigenereCipher/vigenereCipher.c_outline new file mode 100644 index 0000000..c9c1941 --- /dev/null +++ b/Make AVR Examples/Chapter19_EEPROM/vigenereCipher/vigenereCipher.c_outline @@ -0,0 +1,55 @@ +/* +Vigenere Cipher encoder / decoder demo +And an excuse to play around with EEPROM memory + */ + +#include "vigenereCipher.h" + +// -------- Functions --------- // + +void printFromEEPROM(char *eepromString); +void enterText(char text[]); +void displayCodes(void); +void changeCode(char codeBuffer[]); +void selectCode(char code[]); +void encodeVigenere(char text[], char code[]); +void decodeVigenere(char text[], char code[]); + +int main(void) { + // -------- Inits --------- // + + char textBuffer[MAX_TEXT_LENGTH]; + char codeString[CODE_LEN]; + char input; + initUSART(); + + // ------ Event loop ------ // + while (1) { + + // Menu + printFromEEPROM(welcomeString); + // .... more fany menu printing + printFromEEPROM(menuDecode); + input = receiveByte(); + + switch (input) { + case 'e': // encode + encodeVigenere(textBuffer, codeString); + break; + case 'd': // decode + decodeVigenere(textBuffer, codeString); + break; + case 'n': // new text + printFromEEPROM(promptTypeText); + enterText(textBuffer); + break; + case 'c': // choose code + selectCode(codeString); + break; + case 'x': // change code + changeCode(codeString); + break; + } + } /* End event loop */ + return (0); /* This line is never reached */ +} diff --git a/Make AVR Examples/Chapter19_EEPROM/vigenereCipher/vigenereCipher.h b/Make AVR Examples/Chapter19_EEPROM/vigenereCipher/vigenereCipher.h new file mode 100644 index 0000000..aed537c --- /dev/null +++ b/Make AVR Examples/Chapter19_EEPROM/vigenereCipher/vigenereCipher.h @@ -0,0 +1,50 @@ + +#include +#include +#include "USART.h" + +#define MAX_TEXT_LENGTH 256 +#define CODE_LEN 64 + +// -------- Global Variables --------- // +char EEMEM code0[CODE_LEN] = "ettubrute"; +char EEMEM code1[CODE_LEN] = "attackatdawn"; +char EEMEM code2[CODE_LEN] = "theraininspainfallsmainlyontheplain"; +char EEMEM code3[CODE_LEN] = "ablewasiereisawelba"; +char *codePointers[] = { code0, code1, code2, code3 }; + +// Menu strings. Why not store them in EEPROM? +char EEMEM welcomeString[] = "\r\n--== Vigenere Cipher ==--\r\n"; +char EEMEM menuEncode[] = " [e] to encode text\r\n"; +char EEMEM menuDecode[] = " [d] to decode text\r\n\r\n"; +char EEMEM menuEnterText[] = " [n] to enter new text\r\n"; +char EEMEM menuCodeText[] = " [c] to select your code phrase\r\n"; +char EEMEM menuChangeCode[] = " [x] to modify code phrases\r\n"; +char EEMEM promptCode[] = "code: "; +char EEMEM promptText[] = "\r\ntext: "; + +char EEMEM promptSelectCode[] = "Select codephrase:\r\n\r\n"; +char EEMEM promptTypeText[] = "Type your text: "; + +// Given the address of an EEPROM string, prints it out +// Used for menu items +void printFromEEPROM(char *eepromString); + +// Takes input from serial, stores it in the text array +void enterText(char text[]); + +// Reads code phrases out of EEPROM and prints them. +// Uses the codeBuffer for temporary storage +void displayCodes(void); + +// Changes a code phrase, both in EEPROM and the current code +void changeCode(char codeBuffer[]); + +// Pick a code phrase from EEPROM +void selectCode(char code[]); + +// Encodes the passed text string, in place +void encodeVigenere(char text[], char code[]); + +// Decodes the passed text string, in place +void decodeVigenere(char text[], char code[]); diff --git a/Make AVR Examples/LICENSE.txt b/Make AVR Examples/LICENSE.txt new file mode 100644 index 0000000..c9d461b --- /dev/null +++ b/Make AVR Examples/LICENSE.txt @@ -0,0 +1,24 @@ +MIT License + +Copyright (c) 2014 Elliot Williams + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +Have fun! + diff --git a/Make AVR Examples/README.md b/Make AVR Examples/README.md new file mode 100644 index 0000000..f48a201 --- /dev/null +++ b/Make AVR Examples/README.md @@ -0,0 +1,107 @@ +Code and Examples from "Make: AVR Programming" +============================================== + +Welcome! +-------- + +Here you'll find all the code (and more!) for the Maker Media book + [Make: AVR Programming](http://shop.oreilly.com/product/0636920028161.do). + + +Getting Started +--------------- + +* First, download the contents of this repository onto your hard-drive. The easiest way + is with the "Download ZIP" button above and on the right-hand side of this very web + page. Extract the zip file wherever is comfy. (Feel free to clone the repo if you + are comfortable with Git.) + +* Most of the projects share a common set of pin defines and a common simple + USART serial library in the **AVR-Programming-Library** directory. The + makefiles I've included depend on the directory structure here by default, so + don't go moving the folders around unless you also change the path to + included files in the makefile. + +* If you're using the Arduino IDE, you'll want to copy the **AVR-Programming-Library** directory + into your **sketchbook/libraries** folder. If you don't know where this is, you can find out in the + "File...Settings" dialog within Arduino. Now you can link your code to use this library simply + using "Sketch...Import Library" and selecting the library from the the menu. + +* Now you will be set to open the code, edit it, and flash it into the AVR following the directions + in the book. + + +Repo Layout +----------- + +All of the project code is organized by the chapters in the book. So if +you're looking for an example of some SPI code, see the "Chapter16_SPI" folder for +SPI-related projects. That's obvious. + +But a bunch of the projects are interesting in addition to the topic covered in +the chapter. For instance, "Chapter05_Serial-IO" includes a project that uses +the serial communication between your desktop computer and the AVR to turn your +computer keyboard into a musical keyboard that plays notes generated on the +AVR, turning it into a serial-port-controlled organ. You wouldn't think to +go looking in the Serial I/O chapter unles you were following along in the book. + +So for an overview all the projects, the file [allProjectsList](https://github.com/hexagon5un/AVR-Programming/blob/master/allProjectsList) lists them all out by name. + +setupProject +------------ + +If you'd like a blank template to start writing your own AVR code, +have a look in the **setupProject** directory that I've included here. Inside, you'll find +**main.c** and **main.h** files that are essentially blank and ready to go. **main.c** +makes use of my simple USART library, which is also included an linked in by the **Makefile**. +In short, you could copy this directory, rename files, and start using it in your own projects. + +But you don't have to do that manually. Running *python setupProject.py +myProjectName* will create a directory called **myProjectName** for you, copy +the blank main files, renaming them as appropriate, and set up the Makefile +accordingly. All that's left for you to do is the hard part -- actually +coding. + +If you use this setup a lot, you'll want to personalize the **Makefile** and +the two **main** files to suit your own preferences. That way, whenever you +start up a new project, it'll include a customized **Makefile** that has your +programmer, chip type, and favorite baud rate already set. + +Finally, if you like to map out your pin definitions in macro definitions, run +*python createPinDefines.py*. The program will ask you what you'd like to call +each pin macro (e.g. "LED0") and then which pin on the AVR you'd like to +associate with it (e.g. "PB1"). When you're done entering your pin layout, +it'll create a "pinDefines.h" file with (I hope) nicely-named macros. Move +this file into the right directory, and include it in your code. Calling +LED0_SET_HIGH will turn your LED on. + + +More! +----- + +You've read the book, you've built the projects, you've worked through the code. +But still you hunger for more projects, more examples, more, more, more! +If I may toot my own horn, you should visit [LittleHacks.org](http://littlehacks.org) +where I blog about whatever microcontroller projects I'm currently up to. + +In particular, if you're reading + [Make: AVR Programming](http://shop.oreilly.com/product/0636920028161.do), and +you're interested in fully-elaborated versions of the projects with more +photos, videos, and explanation than could fit in a book, head on over to + [LittleHacks.org's AVR-Programming Section](http://littlehacks.org/AVR-Programming). + +Once you've exhausted all of these resources, you should *definitely* head over +to [The Cornell University ECE 4760 Final +Projects](http://people.ece.cornell.edu/land/courses/ece4760/FinalProjects/) +list page. It's an awe-inspiring collection of applications, and sure to spark +some creative thoughts. It's all well-documented and there's tons of source +code in C. [Professor Land's links section] +(http://people.ece.cornell.edu/land/courses/ece4760/#links) is also top-notch, +and his lectures on YouTube are also worth a look if you're getting serious +about this whole AVR deal. + + + + + + diff --git a/Make AVR Examples/allProjectsList b/Make AVR Examples/allProjectsList new file mode 100644 index 0000000..8a8436e --- /dev/null +++ b/Make AVR Examples/allProjectsList @@ -0,0 +1,69 @@ +Chapter02_Programming-AVRs/blinkLED +Chapter02_Programming-AVRs/blinkLED_AVR_style + +Chapter03_Digital-Output/povToy + +Chapter04_Bit-Twiddling/cylonEyes +Chapter04_Bit-Twiddling/showingOffBits + +Chapter05_Serial-IO/serialLoopback +Chapter05_Serial-IO/serialOrgan + +Chapter06_Digital-Input/avrMusicBox +Chapter06_Digital-Input/bossButton +Chapter06_Digital-Input/debouncer +Chapter06_Digital-Input/simpleButton +Chapter06_Digital-Input/toggleButton + +Chapter07_Analog-to-Digital-Conversion-I/lightSensor +Chapter07_Analog-to-Digital-Conversion-I/nightLight +Chapter07_Analog-to-Digital-Conversion-I/slowScope + +Chapter08_Hardware-Interrupts/capSense +Chapter08_Hardware-Interrupts/helloInterrupt + +Chapter09_Introduction-to-Timer-Counter-Hardware/amRadio +Chapter09_Introduction-to-Timer-Counter-Hardware/reactionTimer +Chapter09_Introduction-to-Timer-Counter-Hardware/timerAudio + +Chapter10_Pulse-Width-Modulation/bruteForcePWM +Chapter10_Pulse-Width-Modulation/pwm +Chapter10_Pulse-Width-Modulation/pwmOnAnyPin +Chapter10_Pulse-Width-Modulation/pwmTimers + +Chapter11_Driving-Servo-Motors/servoSundial +Chapter11_Driving-Servo-Motors/servoWorkout + +Chapter12_Analog-to-Digital-Conversion-II/footstepDetector +Chapter12_Analog-to-Digital-Conversion-II/voltmeter + +Chapter13_Advanced-PWM-Tricks/adsr +Chapter13_Advanced-PWM-Tricks/arpeggiator +Chapter13_Advanced-PWM-Tricks/dds +Chapter13_Advanced-PWM-Tricks/dds_interrupts +Chapter13_Advanced-PWM-Tricks/dds_saw15 +Chapter13_Advanced-PWM-Tricks/dialTone +Chapter13_Advanced-PWM-Tricks/fatSaw + +Chapter14_Switches/dcMotorWorkout + +Chapter15_Advanced-Motors/hBridgeWorkout +Chapter15_Advanced-Motors/stepperWorkout + +Chapter16_SPI/spiEEPROMDemo + +Chapter17_I2C/i2cThermometer +Chapter17_I2C/loggingThermometer + +Chapter18_Using-Flash-Program-Memory/progmemDemo1 +Chapter18_Using-Flash-Program-Memory/progmemDemo2 +Chapter18_Using-Flash-Program-Memory/progmemDemo3 +Chapter18_Using-Flash-Program-Memory/progmemDemo4 +Chapter18_Using-Flash-Program-Memory/progmemDemo5 +Chapter18_Using-Flash-Program-Memory/talkingVoltmeter + +Chapter19_EEPROM/eememDemo +Chapter19_EEPROM/favoriteColor +Chapter19_EEPROM/quickDemo +Chapter19_EEPROM/vigenereCipher + diff --git a/Make AVR Examples/setupProject/Makefile b/Make AVR Examples/setupProject/Makefile new file mode 100644 index 0000000..5c588dc --- /dev/null +++ b/Make AVR Examples/setupProject/Makefile @@ -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 diff --git a/Make AVR Examples/setupProject/USART.c b/Make AVR Examples/setupProject/USART.c new file mode 100644 index 0000000..7e8139a --- /dev/null +++ b/Make AVR Examples/setupProject/USART.c @@ -0,0 +1,137 @@ + +/* + Quick and dirty functions that make serial communications work. + + Note that receiveByte() blocks -- it sits and waits _forever_ for + a byte to come in. If you're doing anything that's more interesting, + you'll want to implement this with interrupts. + + initUSART requires BAUDRATE to be defined in order to calculate + the bit-rate multiplier. 9600 is a reasonable default. + + May not work with some of the older chips: + Tiny2313, Mega8, Mega16, Mega32 have different pin macros + If you're using these chips, see (e.g.) iom8.h for how it's done. + These old chips don't specify UDR0 vs UDR1. + Correspondingly, the macros will just be defined as UDR. +*/ + +#include +#include "USART.h" +#include + +void initUSART(void) { /* requires BAUD */ + UBRR0H = UBRRH_VALUE; /* defined in setbaud.h */ + UBRR0L = UBRRL_VALUE; +#if USE_2X + UCSR0A |= (1 << U2X0); +#else + UCSR0A &= ~(1 << U2X0); +#endif + /* Enable USART transmitter/receiver */ + UCSR0B = (1 << TXEN0) | (1 << RXEN0); + UCSR0C = (1 << UCSZ01) | (1 << UCSZ00); /* 8 data bits, 1 stop bit */ +} + + +void transmitByte(uint8_t data) { + /* Wait for empty transmit buffer */ + loop_until_bit_is_set(UCSR0A, UDRE0); + UDR0 = data; /* send data */ +} + +uint8_t receiveByte(void) { + loop_until_bit_is_set(UCSR0A, RXC0); /* Wait for incoming data */ + return UDR0; /* return register value */ +} + + + /* Here are a bunch of useful printing commands */ + +void printString(const char myString[]) { + uint8_t i = 0; + while (myString[i]) { + transmitByte(myString[i]); + i++; + } +} + +void readString(char myString[], uint8_t maxLength) { + char response; + uint8_t i; + i = 0; + while (i < (maxLength - 1)) { /* prevent over-runs */ + response = receiveByte(); + transmitByte(response); /* echo */ + if (response == '\r') { /* enter marks the end */ + break; + } + else { + myString[i] = response; /* add in a letter */ + i++; + } + } + myString[i] = 0; /* terminal NULL character */ +} + +void printByte(uint8_t byte) { + /* Converts a byte to a string of decimal text, sends it */ + transmitByte('0' + (byte / 100)); /* Hundreds */ + transmitByte('0' + ((byte / 10) % 10)); /* Tens */ + transmitByte('0' + (byte % 10)); /* Ones */ +} + +void printWord(uint16_t word) { + transmitByte('0' + (word / 10000)); /* Ten-thousands */ + transmitByte('0' + ((word / 1000) % 10)); /* Thousands */ + transmitByte('0' + ((word / 100) % 10)); /* Hundreds */ + transmitByte('0' + ((word / 10) % 10)); /* Tens */ + transmitByte('0' + (word % 10)); /* Ones */ +} + +void printBinaryByte(uint8_t byte) { + /* Prints out a byte as a series of 1's and 0's */ + uint8_t bit; + for (bit = 7; bit < 255; bit--) { + if (bit_is_set(byte, bit)) + transmitByte('1'); + else + transmitByte('0'); + } +} + +char nibbleToHexCharacter(uint8_t nibble) { + /* Converts 4 bits into hexadecimal */ + if (nibble < 10) { + return ('0' + nibble); + } + else { + return ('A' + nibble - 10); + } +} + +void printHexByte(uint8_t byte) { + /* Prints a byte as its hexadecimal equivalent */ + uint8_t nibble; + nibble = (byte & 0b11110000) >> 4; + transmitByte(nibbleToHexCharacter(nibble)); + nibble = byte & 0b00001111; + transmitByte(nibbleToHexCharacter(nibble)); +} + +uint8_t getNumber(void) { + // Gets a numerical 0-255 from the serial port. + // Converts from string to number. + char hundreds = '0'; + char tens = '0'; + char ones = '0'; + char thisChar = '0'; + do { /* shift over */ + hundreds = tens; + tens = ones; + ones = thisChar; + thisChar = receiveByte(); /* get a new character */ + transmitByte(thisChar); /* echo */ + } while (thisChar != '\r'); /* until type return */ + return (100 * (hundreds - '0') + 10 * (tens - '0') + ones - '0'); +} diff --git a/Make AVR Examples/setupProject/USART.h b/Make AVR Examples/setupProject/USART.h new file mode 100644 index 0000000..c3f18eb --- /dev/null +++ b/Make AVR Examples/setupProject/USART.h @@ -0,0 +1,44 @@ +/* Functions to initialize, send, receive over USART + + initUSART requires BAUD to be defined in order to calculate + the bit-rate multiplier. + */ + +#ifndef BAUD /* if not defined in Makefile... */ +#define BAUD 9600 /* set a safe default baud rate */ +#endif + + /* These are defined for convenience */ +#define USART_HAS_DATA bit_is_set(UCSR0A, RXC0) +#define USART_READY bit_is_set(UCSR0A, UDRE0) + +/* Takes the defined BAUD and F_CPU, + calculates the bit-clock multiplier, + and configures the hardware USART */ +void initUSART(void); + +/* Blocking transmit and receive functions. + When you call receiveByte() your program will hang until + data comes through. We'll improve on this later. */ +void transmitByte(uint8_t data); +uint8_t receiveByte(void); + +void printString(const char myString[]); + /* Utility function to transmit an entire string from RAM */ +void readString(char myString[], uint8_t maxLength); +/* Define a string variable, pass it to this function + The string will contain whatever you typed over serial */ + +void printByte(uint8_t byte); + /* Prints a byte out as its 3-digit ascii equivalent */ +void printWord(uint16_t word); + /* Prints a word (16-bits) out as its 5-digit ascii equivalent */ + +void printBinaryByte(uint8_t byte); + /* Prints a byte out in 1s and 0s */ +char nibbleToHex(uint8_t nibble); +void printHexByte(uint8_t byte); + /* Prints a byte out in hexadecimal */ +uint8_t getNumber(void); +/* takes in up to three ascii digits, + converts them to a byte when press enter */ diff --git a/Make AVR Examples/setupProject/createPinDefines.py b/Make AVR Examples/setupProject/createPinDefines.py new file mode 100644 index 0000000..e0f555d --- /dev/null +++ b/Make AVR Examples/setupProject/createPinDefines.py @@ -0,0 +1,61 @@ +#! /usr/bin/env python + +## Given a list of names and pin defines, runs through a routine that +## sets up a standard set of macros which can then be cut/paste or +## otherwise included. + +import sys +try: + outputFilename = sys.argv[1] +except: + print + print 'No filename passed, saving to "pinDefinitions.h" in this directory.' + outputFilename = "pinDefinitions.h" + +print +print +print "First we need to define macro names and the coresponding AVR pins." +print "None of the names are case-sensitive -- all are converted to uppercase." +print "When you're done, hit [enter] for the macro name." +print " " + + +nicknames = [] +pinouts = [] +while(True): + nickname = raw_input("\nPin Macro Name: ").strip().upper() + if (nickname == ""): + break + nicknames.append(nickname) + pinout = raw_input("AVR Pinout: ").strip().upper() + pinouts.append(pinout) + +output = "// ---------------\n// Pin Defines \n// ---------------\n\n" +for (nickname, pinout) in zip(nicknames, pinouts): + output += "#define %s %s\n" % (nickname, pinout) + output += "#define %s_PORT PORT%s\n" % (nickname, pinout[1]) + output += "#define %s_PIN PIN%s\n" % (nickname, pinout[1]) + output += "#define %s_DDR DDR%s\n" % (nickname, pinout[1]) + output += "#define %s_SET_OUTPUT %s_DDR |= _BV(%s)\n" %\ + (nickname, nickname, nickname) + output += "#define %s_SET_INPUT %s_DDR &= ~_BV(%s)\n" %\ + (nickname, nickname, nickname) + output += "#define %s_SET_HIGH %s_PORT |= _BV(%s)\n" % \ + (nickname, nickname, nickname) + output += "#define %s_SET_LOW %s_PORT &= ~_BV(%s)\n" % \ + (nickname, nickname, nickname) + output += "#define %s_TOGGLE %s_PORT ^= _BV(%s)\n" % \ + (nickname, nickname, nickname) + output += "#define %s_IS_HIGH (%s_PIN & _BV(%s))\n" % \ + (nickname, nickname, nickname) + output += "#define %s_IS_LOW !(%s_PIN & _BV(%s))\n" % \ + (nickname, nickname, nickname) + output += "\n" + + +try: + outfile = open(outputFilename, "w") + outfile.write(output) + outfile.close() +except NameError: + print output diff --git a/Make AVR Examples/setupProject/macros.h b/Make AVR Examples/setupProject/macros.h new file mode 100644 index 0000000..7987b22 --- /dev/null +++ b/Make AVR Examples/setupProject/macros.h @@ -0,0 +1,26 @@ + +/* Standard Macros */ +/* You can totally get by without these, but why? */ + +/* Make sure we've already got io / sfr / pindefs loaded */ +#ifndef _AVR_IO_H_ +#include +#endif + +/* Reminder: the following useful bit-twiddling macros are + always included in avr/sfr_defs.h, which is called from + avr/io.h + + bit_is_set(sfr, bit) + bit_is_clear(sfr, bit) + loop_until_bit_is_set(sfr, bit) + loop_until_bit_is_clear(sfr, bit) + +*/ + +/* Define up the full complement of bit-twiddling macros */ +#define BV(bit) (1 << bit) +#define set_bit(sfr, bit) (_SFR_BYTE(sfr) |= BV(bit)) // old sbi() +#define clear_bit(sfr, bit) (_SFR_BYTE(sfr) &= ~BV(bit)) // old cbi() +#define toggle_bit(sfr, bit) (_SFR_BYTE(sfr) ^= BV(bit)) + diff --git a/Make AVR Examples/setupProject/main.c b/Make AVR Examples/setupProject/main.c new file mode 100644 index 0000000..4e08b2d --- /dev/null +++ b/Make AVR Examples/setupProject/main.c @@ -0,0 +1,22 @@ + +#include "main.h" + +// -------- Global Variables --------- // + +// -------- Functions --------- // + +int main(void) { + // -------- Inits --------- // + + // clock_prescale_set(clock_div_1); /* CPU Clock: 8 MHz */ + initUSART(); + printString("OK"); + + // ------ Event loop ------ // + while (1) { + + + + } /* End event loop */ + return (0); /* This line is never reached */ +} diff --git a/Make AVR Examples/setupProject/main.h b/Make AVR Examples/setupProject/main.h new file mode 100644 index 0000000..d505039 --- /dev/null +++ b/Make AVR Examples/setupProject/main.h @@ -0,0 +1,19 @@ + +// Standard AVR includes +#include +#include +#include +#include +#include +#include +#include +#include + +// Standard includes +#include + +// These are optional, but nice to have around. +// Feel free to comment them out if you don't use them. +#include "USART.h" +#include "macros.h" + diff --git a/Make AVR Examples/setupProject/setupProject.py b/Make AVR Examples/setupProject/setupProject.py new file mode 100644 index 0000000..5bf6c73 --- /dev/null +++ b/Make AVR Examples/setupProject/setupProject.py @@ -0,0 +1,41 @@ +#! /usr/bin/env python +## Simple Python routine for creating a new AVR project +## Feel free to extend this to meet your needs + +## In particular, main.h includes nearly every AVR library you'll ever need +## which is no problem, b/c the linker will just ignore the unused ones. +## But if you're not using them, it might cause confusion later. +## Trim them down to fit? + +## Or, if you're feeling DIY, you can just copy the Makefile, main.c and main.h +## into a new directory yourself. The other files are optional, but handy. + +import os +import shutil +import sys + +## Get command-line input +class UsageError(Exception): + pass +try: + newProjectName = sys.argv[1] +except IndexError: + raise(UsageError("Please specify a project name on the command-line.\n")) + +## Create new project directory... +## ... in parent directory +## relativeDirectory = os.path.join(os.path.pardir, newProjectName) +## ... or in this directory, and you get to move it yourself. +relativeDirectory = newProjectName +os.mkdir(relativeDirectory) + +## Files copied directly over... +def copyToNewDirectory(whichFile, newDirectory): + shutil.copy(whichFile, newDirectory) +## ... these ones. +for filename in ["Makefile", "main.c", "main.h", "USART.h", "USART.c", "macros.h"]: + copyToNewDirectory(filename, relativeDirectory) + +print "Copied Makefile, main.c, and main.h into %s." % relativeDirectory +print "Time to start coding." +