Added files from first programming course at OSU

This commit is contained in:
2019-07-09 18:14:25 -07:00
parent 4a591de966
commit 8c92e250d9
48 changed files with 2858 additions and 0 deletions

View File

@@ -0,0 +1,336 @@
#----------------------------------------------------------------------------
# Wunderboard Makefile Template written by Dan Albert
#----------------------------------------------------------------------------
#
# make all = Make software.
#
# make clean = Clean out built project files.
#
# make filename.s = Just compile filename.c into the assembler code only.
#
# To rebuild project do "make clean" then "make all".
#----------------------------------------------------------------------------
# Target file name.
TARGET = main
# List C source files here.
SRC = $(TARGET).c \
adc.c
# MCU name, you MUST set this to match the board you are using
# type "make clean" after changing this, so all files will be rebuilt
MCU = at90usb646
# Detect platform
UNAME = $(shell uname)
ifeq ($(UNAME),Linux)
ENV=unix
endif
ifeq ($(UNAME),Darwin)
ENV=unix
endif
ifndef ENV
ENV=windows
endif
ifeq ($(ENV),unix)
PROGRAM = dfu-programmer
PROGRAMFLAGS = $(MCU)
ERASE = erase
FLASH = flash "$(TARGET).hex"
START = start
else
PROGRAM = "C:/Program Files (x86)/Atmel/Flip 3.4.1/bin/batchisp.exe"
PROGRAMFLAGS = -device at90usb646 -hardware USB
ERASE = -operation erase F
FLASH = -operation loadbuffer "$(TARGET).hex" program verify
START = -operation start noreset 0
endif
# Processor frequency.
# Normally the first thing your program should do is set the clock prescaler,
# so your program will run at the correct speed. You should also set this
# variable to same clock speed. The _delay_ms() macro uses this, and many
# examples use this variable to calculate timings. Do not add a "UL" here.
F_CPU = 8000000
# Output format. (can be srec, ihex, binary)
FORMAT = ihex
# Object files directory
# To put object files in current directory, use a dot (.), do NOT make
# this an empty or blank macro!
OBJDIR = obj
# Optimization level, can be [0, 1, 2, 3, s].
# 0 = turn off optimization. s = optimize for size.
# (Note: 3 is not always the best optimization level. See avr-libc FAQ.)
OPT = s
# List any extra directories to look for include files here.
# Each directory must be seperated by a space.
# Use forward slashes for directory separators.
# For a directory that has spaces, enclose it in quotes.
EXTRAINCDIRS =
# Compiler flag to set the C Standard level.
# c89 = "ANSI" C
# gnu89 = c89 plus GCC extensions
# c99 = ISO C99 standard (not yet fully implemented)
# gnu99 = c99 plus GCC extensions
CSTANDARD = -std=gnu99
# Place -D or -U options here for C sources
CDEFS = -DF_CPU=$(F_CPU)UL
#---------------- Compiler Options C ----------------
# -g*: generate debugging information
# -O*: optimization level
# -f...: tuning, see GCC manual and avr-libc documentation
# -Wall...: warning level
# -Wa,...: tell GCC to pass this to the assembler.
# -adhlns...: create assembler listing
CFLAGS += $(CDEFS)
CFLAGS += -O$(OPT)
CFLAGS += -funsigned-char
CFLAGS += -funsigned-bitfields
CFLAGS += -ffunction-sections
CFLAGS += -fpack-struct
CFLAGS += -fshort-enums
CFLAGS += -Wall
CFLAGS += -Wstrict-prototypes
#CFLAGS += -mshort-calls
#CFLAGS += -fno-unit-at-a-time
#CFLAGS += -Wundef
#CFLAGS += -Wunreachable-code
#CFLAGS += -Wsign-compare
CFLAGS += -Wa,-adhlns=$(<:%.c=$(OBJDIR)/%.lst)
CFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS))
CFLAGS += $(CSTANDARD)
#---------------- Library Options ----------------
# Minimalistic printf version
PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min
# Floating point printf version (requires MATH_LIB = -lm below)
PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt
# If this is left blank, then it will use the Standard printf version.
PRINTF_LIB =
#PRINTF_LIB = $(PRINTF_LIB_MIN)
#PRINTF_LIB = $(PRINTF_LIB_FLOAT)
# Minimalistic scanf version
SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min
# Floating point + %[ scanf version (requires MATH_LIB = -lm below)
SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt
# If this is left blank, then it will use the Standard scanf version.
SCANF_LIB =
#SCANF_LIB = $(SCANF_LIB_MIN)
#SCANF_LIB = $(SCANF_LIB_FLOAT)
MATH_LIB = -lm
# List any extra directories to look for libraries here.
# Each directory must be seperated by a space.
# Use forward slashes for directory separators.
# For a directory that has spaces, enclose it in quotes.
EXTRALIBDIRS =
#---------------- Linker Options ----------------
# -Wl,...: tell GCC to pass this to linker.
# -Map: create map file
# --cref: add cross reference to map file
LDFLAGS = -Wl,-Map=$(TARGET).map,--cref
LDFLAGS += -Wl,--relax
LDFLAGS += -Wl,--gc-sections
LDFLAGS += $(patsubst %,-L%,$(EXTRALIBDIRS))
LDFLAGS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)
#LDFLAGS += -T linker_script.x
#============================================================================
# Define programs and commands.
SHELL = sh
CC = avr-gcc
OBJCOPY = avr-objcopy
OBJDUMP = avr-objdump
SIZE = avr-size
AR = avr-ar rcs
NM = avr-nm
REMOVE = rm -f
REMOVEDIR = rm -rf
COPY = cp
WINSHELL = cmd
# Define Messages
# English
MSG_ERRORS_NONE = Errors: none
MSG_BEGIN = -------- begin --------
MSG_END = -------- end --------
MSG_SIZE_BEFORE = Size before:
MSG_SIZE_AFTER = Size after:
MSG_FLASH = Creating load file for Flash:
MSG_EEPROM = Creating load file for EEPROM:
MSG_EXTENDED_LISTING = Creating Extended Listing:
MSG_SYMBOL_TABLE = Creating Symbol Table:
MSG_LINKING = Linking:
MSG_COMPILING = Compiling C:
MSG_ASSEMBLING = Assembling:
MSG_CLEANING = Cleaning project:
MSG_CREATING_LIBRARY = Creating library:
# Define all object files.
OBJ = $(SRC:%.c=$(OBJDIR)/%.o)
# Define all listing files.
LST = $(SRC:%.c=$(OBJDIR)/%.lst)
# Compiler flags to generate dependency files.
GENDEPFLAGS = -MMD -MP -MF .dep/$(@F).d
# Combine all necessary flags and optional flags.
# Add target processor to flags.
ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS) $(GENDEPFLAGS)
# Default target.
all: begin gccversion sizebefore build sizeafter end
# Change the build target to build a HEX file or a library.
build: elf hex eep lss sym
#build: lib
elf: $(TARGET).elf
hex: $(TARGET).hex
eep: $(TARGET).eep
lss: $(TARGET).lss
sym: $(TARGET).sym
LIBNAME=lib$(TARGET).a
lib: $(LIBNAME)
# Eye candy.
# AVR Studio 3.x does not check make's exit code but relies on
# the following magic strings to be generated by the compile job.
begin:
@echo
@echo $(MSG_BEGIN)
end:
@echo $(MSG_END)
@echo
# Display size of file.
HEXSIZE = $(SIZE) --target=$(FORMAT) $(TARGET).hex
ELFSIZE = $(SIZE) --mcu=$(MCU) --format=avr $(TARGET).elf
sizebefore:
@if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_BEFORE); $(ELFSIZE); \
2>/dev/null; echo; fi
sizeafter:
@if test -f $(TARGET).elf; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE); \
2>/dev/null; echo; fi
# Display compiler version information.
gccversion :
@$(CC) --version
# Create final output files (.hex, .eep) from ELF output file.
%.hex: %.elf
@echo
@echo $(MSG_FLASH) $@
$(OBJCOPY) -O $(FORMAT) -R .eeprom -R .fuse -R .lock -R .signature $< $@
%.eep: %.elf
@echo
@echo $(MSG_EEPROM) $@
-$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
--change-section-lma .eeprom=0 --no-change-warnings -O $(FORMAT) $< $@ || exit 0
# Create extended listing file from ELF output file.
%.lss: %.elf
@echo
@echo $(MSG_EXTENDED_LISTING) $@
$(OBJDUMP) -h -S -z $< > $@
# Create a symbol table from ELF output file.
%.sym: %.elf
@echo
@echo $(MSG_SYMBOL_TABLE) $@
$(NM) -n $< > $@
# Create library from object files.
.SECONDARY : $(TARGET).a
.PRECIOUS : $(OBJ)
%.a: $(OBJ)
@echo
@echo $(MSG_CREATING_LIBRARY) $@
$(AR) $@ $(OBJ)
# Link: create ELF output file from object files.
.SECONDARY : $(TARGET).elf
.PRECIOUS : $(OBJ)
%.elf: $(OBJ)
@echo
@echo $(MSG_LINKING) $@
$(CC) $(ALL_CFLAGS) $^ --output $@ $(LDFLAGS)
# Compile: create object files from C source files.
$(OBJDIR)/%.o : %.c
@echo
@echo $(MSG_COMPILING) $<
$(CC) -c $(ALL_CFLAGS) $< -o $@
# Compile: create assembler files from C source files.
%.s : %.c
$(CC) -S $(ALL_CFLAGS) $< -o $@
## Program
.PHONY: program
program: all
$(PROGRAM) $(PROGRAMFLAGS) $(ERASE)
$(PROGRAM) $(PROGRAMFLAGS) $(FLASH)
$(PROGRAM) $(PROGRAMFLAGS) $(START)
doxygen:
doxygen
# Target: clean project.
clean: begin clean_list end
clean_list :
@echo
@echo $(MSG_CLEANING)
$(REMOVE) $(TARGET).hex
$(REMOVE) $(TARGET).eep
$(REMOVE) $(TARGET).elf
$(REMOVE) $(TARGET).map
$(REMOVE) $(TARGET).sym
$(REMOVE) $(TARGET).lss
$(REMOVE) $(SRC:%.c=$(OBJDIR)/%.o)
$(REMOVE) $(SRC:%.c=$(OBJDIR)/%.lst)
$(REMOVE) $(SRC:.c=.s)
$(REMOVE) $(SRC:.c=.d)
$(REMOVEDIR) .dep
$(REMOVEDIR) $(OBJDIR)
# Create object files directory
$(shell mkdir $(OBJDIR) 2>/dev/null)
# Include the dependency files.
-include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*)
# Listing of phony targets.
.PHONY : all begin finish end sizebefore sizeafter gccversion \
build elf hex eep lss sym \
clean clean_list

View File

@@ -0,0 +1,28 @@
/**
@file adc.c
@brief Wunderboard ADC Helper Functions
@version .01
@section intro Code Overview
This is the code for the Wunderboard ADC helper functions.
*/
#include "adc.h"
unsigned char read_adc(uint8_t channel){
unsigned char test;
ADMUX = 0x60 | channel; // Set the channel to the one we want
ADCSRA = 0b11000110; // Start a new sample.
while ((ADCSRA & 0b00010000) == 0 ); // Wait for a Valid Sample
ADCSRA |= 0b00010000; // Tell ADC you have the sample you want.
ADCSRA |= 0b01000000; // Start a new sample.
while ((ADCSRA & 0b00010000) == 0 ); // Wait for a Valid Sample
ADCSRA |= 0b00010000; // Tell ADC you have the sample you want.
test = ADCH;
ADCSRA = 0x00; // Disable the ADC
return (test);
}

View File

@@ -0,0 +1,19 @@
/**
@file adc.h
@brief Wunderboard ADC Helper Functions
@version .01
@section intro Code Overview
This is the code for the Wunderboard ADC helper functions.
*/
#include <avr/io.h>
/**
The read_adc() function allows for taking 8 bit measurements from any of the single ended ADC inputs. The function call is blocking. eg. read_adc(5) would read from ADC channel 5.
@param channel This is the channel to be read.
@return This is the return value from the ADC.
*/
unsigned char read_adc(uint8_t channel);

View File

@@ -0,0 +1,228 @@
/**
@file main.c
@brief Lab 8 Starter Code
@version .01
@mainpage Lab 8 Starter Code
@section intro Code Overview
@section hw Hardware Pin Out
Port A:
A0 - A3 : Push Buttons
A4 - A7 : Slide Switches
Port B:
B0 - B3 : SPI (SD Card)
B4 : Nothing
B5 : Audio Out
B6 : Red Enable
B7 : Green Enable
Port C:
C0 - C7 : LED Array (Row)
Port D:
D0 - D1 : Nothing
D2 : Serial RX
D3 : Serial TX
D4 - D7 : Nothing
Port E:
E0 - E2 : LED Array (Column)
E3 : USB (UID)
E4 - E5 : Nothing
E6 : Relay
E7 : Nothing
Port F:
F0 : ADC Channel 0
F1 : ADC Channel 1
F2 : ADC Channel 2
F3 : ADC Channel 3
F4 : ADC Channel 4 (Audio In)
F5 : ADC Channel 5 (Accel X Axis)
F6 : ADC Channel 6 (Accel Y Axis)
F7 : ADC Channel 7 (Accel Z Axis (if installed))
*/
/** Includes */
#include <avr/io.h>
#include <util/delay.h>
#include "adc.h"
#include <stdlib.h>
#include <math.h>
/** Constants */
#define F_CPU 1000000U
#define DELAY .02
#define YMIN 180.0
#define STEPSIZE 2.75
#define SLOW 50
#define FAST 25
#define FASTEST 10
/** Global Variables */
/** Functions */
/** The initialize() function initializes all of the Data Direction Registers for the Wunderboard. Before making changes to DDRx registers, ensure that you have read the peripherals section of the Wunderboard user guide.*/
void initialize (void) {
/** Port A is the switches and buttons. They should always be inputs. ( 0 = Input and 1 = Output )*/
DDRA=0b00000000;
/** Port B has the LED Array color control, SD card, and audio-out on it. Leave DDRB alone. ( 0 = Input and 1 = Output )*/
DDRB=0b11000000;
/** Port C is for the 'row' of the LED array. They should always be outputs. ( 0 = Input and 1 = Output )*/
DDRC=0b11111111;
/** Port D has the Serial on it. Leave DDRB alone. ( 0 = Input and 1 = Output )*/
DDRD=0b00000000;
/** Port E has the LED Array Column control out on it. Leave DDRE alone. ( 0 = Input and 1 = Output )*/
DDRE=0b00000111;
/** Port F has the accelerometer and audio-in on it. Leave DDRF alone. ( 0 = Input and 1 = Output )*/
DDRF=0b00000000;
}
void clearArray(void)
{
PORTB &= ~((1 << PB6) | (1 << PB7)); // Disable latches
PORTC = 0x00;
PORTB |= (1 << PB6) | (1 << PB7); // Enable latches
PORTB &= ~((1 << PB6) | (1 << PB7)); // Disable latches
}
int readadcval(void){
char adcread = read_adc(5);
if(adcread <= 181){
return 0;
}else if((adcread == 182) | (adcread == 183)){
return 1;
}else if((adcread == 184) | (adcread == 185) | (adcread == 186)){
return 2;
}else if((adcread >= 187) & (adcread <= 191)){
return 4;
}else if((adcread == 192) | (adcread == 193) | (adcread == 194)){
return 6;
}else if((adcread == 195) | (adcread == 196) | (adcread == 197)){
return 7;
}else if(adcread >= 198){
return 8;
}
}
void printscreen(int loop, int loop3, const int *Red, const int *Green){
int arrval;
clearArray();
PORTE = loop3;
arrval = (loop+loop3);
PORTB = 0b10000000;
PORTC = Green[arrval];
_delay_ms(DELAY);
clearArray();
PORTB= 0b01000000;
PORTC = Red[arrval];
_delay_ms(DELAY);
clearArray();
}
/** Main Function */
int main (void) {
/** Local Varibles */
int loop = 0;
int *looppoint = &loop;
int loop2;
int loop3;
int speed = 20;
int adcreturn;
const int Red[136] = {0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00001000,0b00000000,0b00001000,0b00101000,0b00001000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00001000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000100,0b00000100,0b00010000,0b00010000,0b00010000,0b00010000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00010000,0b00010100,0b00000000,0b00000000,0b00001000,0b00001000,0b00000000,0b00000000,0b00001000,0b00000000,0b00101000,0b00000000,0b00001000,0b00000000,0b00000000,0b00100000,0b00100000,0b00000000,0b00000000,0b00101000,0b00100000,0b00000000,0b00000000,0b00000010,0b00000110,0b00001110,0b00000000,0b00001110,0b00000110,0b00000010,0b0000000,0b00000000,0b00000010,0b00000110,0b00001110,0b00001110,0b00000000,0b00001110,0b00000110,0b00000010,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00001000,0b00001000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000010,0b00000110,0b00001110,0b00011110,0b00011110,0b00000000,0b00000000,0b00000000,0b00000010,0b00000000,0b00000000,0b00001110,0b00111110,0b00011000,0b00111110,0b00001110,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000};
const int Green[136] = {0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000001,0b00000001,0b01000001,0b11000001,0b01000001,0b00000001,0b00000001,0b00001001,0b00000001,0b00001001,0b00101001,0b00001001,0b00000001,0b00000001,0b00000001,0b00000001,0b10000111,0b11000111,0b01000001,0b00000001,0b00000001,0b00001111,0b00001111,0b00000001,0b00000001,0b00001111,0b00001111,0b00000001,0b00000001,0b00001111,0b00001111,0b00000001,0b00000001,0b00001001,0b00000001,0b00000001,0b01000001,0b11100001,0b11100000,0b01000000,0b00000001,0b00000101,0b00000101,0b00010001,0b00010001,0b00010001,0b00010001,0b00000001,0b00000001,0b00000000,0b00000000,0b00000000,0b00010001,0b00010101,0b00000001,0b00000001,0b01001001,0b11001001,0b11000001,0b10000001,0b00001001,0b00000001,0b00101001,0b00000001,0b00001001,0b00000001,0b00000001,0b00100001,0b00100001,0b00000001,0b00000001,0b00101001,0b00100001,0b00000001,0b00000001,0b00000011,0b00000111,0b00001111,0b00000001,0b00001111,0b00000111,0b01000011,0b11000001,0b01000001,0b00000011,0b00000111,0b00001111,0b00001111,0b00000000,0b00001111,0b00000111,0b00000011,0b00000001,0b00000111,0b00000111,0b00000001,0b00000001,0b00001001,0b00001001,0b00000001,0b00000001,0b00000001,0b00000111,0b00000111,0b00000011,0b00000111,0b00001111,0b00011111,0b00011111,0b00000001,0b01000001,0b11100001,0b11100011,0b01000001,0b00000001,0b00000001,0b00000001,0b00000001,0b00000001,0b00000001,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000,0b00000000};
initialize();
clearArray();
while(1){
adcreturn = readadcval();
if(adcreturn == 4){
for(loop2 = 0; loop2 < 50 ; loop2++){
for(loop3 = 7 ; loop3 > -1 ; loop3--){
printscreen(loop, loop3, Red, Green);
}
}
}else if(adcreturn < 4){
for(loop ; loop >= 0 ; loop--){
if(adcreturn == 2){
speed = SLOW;
}else if(adcreturn == 1){
speed = FAST;
}else if(adcreturn == 0){
speed = FASTEST;
}
for(loop2 = 0 ; loop2 < speed ; loop2++){
for(loop3 = 7 ; loop3 > -1 ; loop3--){
printscreen(loop, loop3, Red, Green);
if(loop == 0){
loop = 128;
}
}
}
adcreturn = readadcval();
if((adcreturn < 4) != 1){
break;
}
}
}else if(adcreturn > 4){
for(loop ; loop <= 128 ; loop++){
if(adcreturn == 6){
speed = SLOW;
}else if(adcreturn == 7){
speed = FAST;
}else if(adcreturn == 8){
speed = FASTEST;
}
for(loop2 = 0 ; loop2 < speed ; loop2++){
for(loop3 = 7 ; loop3 > -1 ; loop3--){
printscreen(loop, loop3, Red, Green);
if(loop == 128){
loop = 0;
}
}
}
adcreturn = readadcval();
if((adcreturn > 4) != 1){
break;
}
}
}
}
}