mirror of
https://github.com/caperren/school_archives.git
synced 2025-11-09 21:51:15 +00:00
Added VERY old code. Very cringy to look at, but hey, we all had to start somewhere...
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
# 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)
|
||||
CC = g++
|
||||
BOOSTFLAGS = -lboost_system
|
||||
else
|
||||
CC = g++
|
||||
BOOSTFLAGS = -lboost_system -lws2_32 -D _WIN32_WINNT=0x0501
|
||||
endif
|
||||
|
||||
all:
|
||||
$(CC) lab5.cpp -Wall $(BOOSTFLAGS)
|
||||
|
||||
clean:
|
||||
rm -f *.exe
|
||||
rm -f *.o
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
#include <boost/asio.hpp>
|
||||
|
||||
/** This code requires 'boost' to be successfully installed and when compiled will require the boost libaries to be included.
|
||||
The original code source as of this writting was found here:
|
||||
|
||||
http://www.webalice.it/fede.tft/serial_port/serial_port.html
|
||||
|
||||
*/
|
||||
|
||||
class SimpleSerial
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* Constructor.
|
||||
* \param port device name, example "/dev/ttyUSB0" or "COM4"
|
||||
* \param baud_rate communication speed, example 9600 or 115200
|
||||
* \throws boost::system::system_error if cannot open the
|
||||
* serial device
|
||||
*/
|
||||
SimpleSerial(std::string port, unsigned int baud_rate)
|
||||
: io(), serial(io,port)
|
||||
{
|
||||
serial.set_option(boost::asio::serial_port_base::baud_rate(baud_rate));
|
||||
}
|
||||
|
||||
/**
|
||||
* Write a string to the serial device.
|
||||
* \param s string to write
|
||||
* \throws boost::system::system_error on failure
|
||||
*/
|
||||
void writeString(std::string s)
|
||||
{
|
||||
boost::asio::write(serial,boost::asio::buffer(s.c_str(),s.size()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Blocks until a line is received from the serial device. A line is denoted by a final '\n' character
|
||||
* Eventual '\n' or '\r\n' characters at the end of the string are removed.
|
||||
* \return a string containing the received line
|
||||
* \throws boost::system::system_error on failure
|
||||
*/
|
||||
std::string readLine()
|
||||
{
|
||||
//Reading data char by char, code is optimized for simplicity, not speed
|
||||
using namespace boost;
|
||||
char c;
|
||||
std::string result;
|
||||
for(;;)
|
||||
{
|
||||
asio::read(serial,asio::buffer(&c,1));
|
||||
switch(c)
|
||||
{
|
||||
case '\r':
|
||||
break;
|
||||
case '\n':
|
||||
return result;
|
||||
default:
|
||||
result+=c;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
boost::asio::io_service io;
|
||||
boost::asio::serial_port serial;
|
||||
};
|
||||
Binary file not shown.
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* conio.h
|
||||
* This file has no copyright assigned and is placed in the Public Domain.
|
||||
* This file is a part of the mingw-runtime package.
|
||||
* No warranty is given; refer to the file DISCLAIMER within the package.
|
||||
*
|
||||
* Low level console I/O functions. Pretty please try to use the ANSI
|
||||
* standard ones if you are writing new code.
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef _CONIO_H_
|
||||
#define _CONIO_H_
|
||||
|
||||
/* All the headers include this file. */
|
||||
#include <_mingw.h>
|
||||
|
||||
#ifndef RC_INVOKED
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
_CRTIMP char* __cdecl __MINGW_NOTHROW _cgets (char*);
|
||||
_CRTIMP int __cdecl __MINGW_NOTHROW _cprintf (const char*, ...);
|
||||
_CRTIMP int __cdecl __MINGW_NOTHROW _cputs (const char*);
|
||||
_CRTIMP int __cdecl __MINGW_NOTHROW _cscanf (char*, ...);
|
||||
|
||||
_CRTIMP int __cdecl __MINGW_NOTHROW _getch (void);
|
||||
_CRTIMP int __cdecl __MINGW_NOTHROW _getche (void);
|
||||
_CRTIMP int __cdecl __MINGW_NOTHROW _kbhit (void);
|
||||
_CRTIMP int __cdecl __MINGW_NOTHROW _putch (int);
|
||||
_CRTIMP int __cdecl __MINGW_NOTHROW _ungetch (int);
|
||||
|
||||
|
||||
#ifndef _NO_OLDNAMES
|
||||
|
||||
_CRTIMP int __cdecl __MINGW_NOTHROW getch (void);
|
||||
_CRTIMP int __cdecl __MINGW_NOTHROW getche (void);
|
||||
_CRTIMP int __cdecl __MINGW_NOTHROW kbhit (void);
|
||||
_CRTIMP int __cdecl __MINGW_NOTHROW putch (int);
|
||||
_CRTIMP int __cdecl __MINGW_NOTHROW ungetch (int);
|
||||
|
||||
#endif /* Not _NO_OLDNAMES */
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* Not RC_INVOKED */
|
||||
|
||||
#endif /* Not _CONIO_H_ */
|
||||
@@ -0,0 +1,35 @@
|
||||
// g++ serial.c++ -lboost_system -lws2_32 -D _WIN32_WINNT=0x0501
|
||||
|
||||
#include <iostream>
|
||||
#include "SimpleSerial.h"
|
||||
#include "conio.h"
|
||||
|
||||
using namespace std;
|
||||
using namespace boost;
|
||||
|
||||
int main(int argc, char* argv[])
|
||||
{
|
||||
|
||||
string inputstr;
|
||||
cout << "Lab 5 Code Started" << endl;
|
||||
SimpleSerial wunderboard("COM5", 9600);
|
||||
|
||||
while(1){
|
||||
try {
|
||||
cout << wunderboard.readLine() << endl;
|
||||
if(kbhit()){
|
||||
cin >> inputstr;
|
||||
wunderboard.writeString(inputstr);
|
||||
if((inputstr.compare("Exit") == 0) | (inputstr.compare("EXIT") == 0) | (inputstr.compare("exit") == 0)){
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
} catch(boost::system::system_error& e)
|
||||
{
|
||||
cout<<"Error: "<<e.what()<<endl;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,335 @@
|
||||
#----------------------------------------------------------------------------
|
||||
# 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 \
|
||||
|
||||
# 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
|
||||
@@ -0,0 +1,289 @@
|
||||
/**
|
||||
@file main.`
|
||||
@brief Lab 3 Starter Code
|
||||
@version .01
|
||||
@mainpage Lab 3 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 <avr/interrupt.h>
|
||||
|
||||
|
||||
/** Constants */
|
||||
#define F_CPU 1000000UL
|
||||
#define DEBUG 1
|
||||
#ifdef DEBUG == 1
|
||||
#define SENDSPEED 3 //If less than .12, there are problems
|
||||
#endif
|
||||
|
||||
/** Global Variables */
|
||||
volatile unsigned char loop = 0;
|
||||
volatile unsigned char throwaway = 0;
|
||||
volatile unsigned char loopdir = 0;
|
||||
/** Functions */
|
||||
|
||||
/** Prototypes **/
|
||||
unsigned char InitializeUART(void);
|
||||
unsigned char SendByteUART (unsigned char data);
|
||||
unsigned char SendStringUART (unsigned char *data);
|
||||
void initialize(void);
|
||||
void clearArray(void);
|
||||
unsigned char initializeTIMER0(void);
|
||||
unsigned char setTIMER0(unsigned char clock, unsigned char count);
|
||||
ISR(TIMER0_COMPA_vect);
|
||||
unsigned char myitoa(unsigned char received, unsigned char *send);
|
||||
|
||||
|
||||
|
||||
#ifdef DEBUG == 1
|
||||
/** This function needs to setup the variables used by the UART to enable the UART and tramsmit at 9600bps. This
|
||||
function should always return 0. Remember, by defualt the Wunderboard runs at 1mHz for its system clock.*/
|
||||
unsigned char InitializeUART (void){
|
||||
unsigned char baud = ((F_CPU/(9600*8UL))-1);
|
||||
|
||||
/* Set baud rate */
|
||||
UBRR1H = (baud >> 8);
|
||||
UBRR1L = baud;
|
||||
/* Set the U2X1 bit */
|
||||
UCSR1A = (1 << U2X1);
|
||||
/* Enable transmitter */
|
||||
UCSR1B = ((1 << TXEN1) | (1 << RXEN1));
|
||||
/* Set frame format: 8data, 1stop bit */
|
||||
UCSR1C = (3 << UCSZ10);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/** This function needs to write a single byte to the UART. It must check that the UART is ready for a new byte
|
||||
and return a 1 if the byte was not sent.
|
||||
@param [in] data This is the data byte to be sent.
|
||||
@return The function returns a 1 or error and 0 on successful completion.*/
|
||||
|
||||
unsigned char SendByteUART (unsigned char data){
|
||||
|
||||
/*
|
||||
** Checks the buffer empty bit so that data can either be placed in the buffer
|
||||
** or else reuturn a 1
|
||||
*/
|
||||
if((UCSR1A & (1 << UDRE1)) == (1 << UDRE1)){
|
||||
UDR1 = data; //Places data into the UDR1 8-bit data buffer
|
||||
}else {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/** This function needs to writes a string to the UART. It must check that the UART is ready for a new byte and
|
||||
return a 1 if the string was not sent.
|
||||
@param [in] data This is a pointer to the data to be sent.
|
||||
@return The function returns a 1 or error and 0 on successful completion.*/
|
||||
|
||||
unsigned char SendStringUART (unsigned char *data){
|
||||
|
||||
for (int loop = 0 ; data[loop] != '\0'; loop++){
|
||||
|
||||
/* Sends the current byte based on the loop umber to SendByte */
|
||||
while(SendByteUART(data[loop]));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
unsigned char GetByteUART(){
|
||||
if (UCSR1A & (1 << RXC1)){
|
||||
return(UDR1);
|
||||
} else{
|
||||
return(-1);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/** 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=0b11000111;
|
||||
|
||||
/** 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=0b00001000;
|
||||
|
||||
/** 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){
|
||||
PORTC = 0x00;
|
||||
PORTB |= (1 << PB6) | (1 << PB7); /** Enable latches*/
|
||||
PORTB &= ~((1 << PB6) | (1 << PB7)); /** Disable latches*/
|
||||
}
|
||||
|
||||
/** This function needs to setup the variables used by TIMER0 Compare Match (CTC) mode with
|
||||
a base clock frequency of clk/1024. This function should return a 1 if it fails and a 0 if it
|
||||
does not. Remember, by default the Wunderboard runs at 1mHz for its system clock.
|
||||
@return This function returns a 1 is unsuccessful, else return 0.*/
|
||||
unsigned char initializeTIMER0(void){
|
||||
/* Set the CTC mode */
|
||||
TCCR0A = (1 << WGM01);
|
||||
|
||||
/* Set the Clock Frequency */
|
||||
TCCR0B = ((1 << CS02) | (1 << CS00));
|
||||
|
||||
/* Set initial count value */
|
||||
OCR0A = 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/** This function takes two values, clock and count. The value of count should be copied into OCR0A and the value of clock should be used to set CS02:0. The TCNT0 variable should also be reset to 0 so that the new timer rate starts from 0.
|
||||
@param [in] clock Insert Comment
|
||||
@param [in] count Insert Comment
|
||||
@return The function returns a 1 or error and 0 on successful completion.*/
|
||||
unsigned char setTIMER0(unsigned char clock, unsigned char count){
|
||||
|
||||
if(clock > 7){
|
||||
return 1;
|
||||
}
|
||||
TCCR0B &= 0b11111000; //Zeros out last three bits
|
||||
TCCR0B |= clock;
|
||||
OCR0A = count;
|
||||
TIMSK0 = 2;
|
||||
TCNT0 = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
ISR(TIMER0_COMPA_vect){
|
||||
if(throwaway == 1){
|
||||
volatile unsigned char send[4];
|
||||
|
||||
myitoa(loop, &send);
|
||||
SendStringUART(send);
|
||||
SendStringUART("\n");
|
||||
throwaway = 0;
|
||||
|
||||
if(loop == 50){
|
||||
loopdir = 1;
|
||||
}else if(loop == 0){
|
||||
loopdir = 0;
|
||||
}
|
||||
|
||||
if(loopdir == 0){
|
||||
loop++;
|
||||
}else{
|
||||
loop--;
|
||||
}
|
||||
}else{
|
||||
throwaway++;
|
||||
}
|
||||
}
|
||||
|
||||
unsigned char myitoa(unsigned char received, unsigned char *send){
|
||||
unsigned char tmp;
|
||||
|
||||
if(received > 99){
|
||||
|
||||
tmp = (received / 100);
|
||||
send[0] = (tmp+48);
|
||||
tmp = ((received % 100)/10);
|
||||
send[1] = (tmp+48);
|
||||
tmp = ((received % 100) % 10);
|
||||
send[2] = (tmp+48);
|
||||
send[3] = '\0';
|
||||
return 0;
|
||||
|
||||
}else if(received > 9){
|
||||
|
||||
tmp = ((received % 100)/10);
|
||||
send[0] = (tmp+48);
|
||||
tmp = ((received % 100) % 10);
|
||||
send[1] = (tmp+48);
|
||||
send[2] = '\0';
|
||||
return 0;
|
||||
|
||||
}else if(received < 10){
|
||||
tmp = ((received % 100) % 10);
|
||||
send[0] = (tmp+48);
|
||||
send[1] = '\0';
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/** Main Function */
|
||||
int main (void){
|
||||
|
||||
enum states{
|
||||
setup,
|
||||
running
|
||||
} state = setup;
|
||||
|
||||
while(1){
|
||||
|
||||
switch(state){
|
||||
case setup:
|
||||
|
||||
initialize();
|
||||
initializeTIMER0();
|
||||
InitializeUART();
|
||||
setTIMER0(5,120);
|
||||
clearArray();
|
||||
sei();
|
||||
state = running;
|
||||
SendStringUART("Initialized...\n");
|
||||
break;
|
||||
|
||||
case running:
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user