diff --git a/lambda-test/Makefile b/lambda-test/Makefile index 232a947..d5fcefd 100644 --- a/lambda-test/Makefile +++ b/lambda-test/Makefile @@ -17,12 +17,8 @@ LOCAL_SOURCE = avrjunit.c ## Here you can link to one more directory (and multiple .c files) -EXTRA_SOURCE_DIR = /data/Work/AVR/AVR-Programming-master/AVR-Programming-Library/ -EXTRA_SOURCE_FILES = USART.c - -## Link to directory of project under test and its source files -TEST_SOURCE_DIR = /data/Job/git/lambda-avr/lambda/ -TEST_SOURCE_FILES = adc.c sensors.c integers.c +EXTRA_SOURCE_DIR = ../lambda/ +EXTRA_SOURCE_FILES = USART.c adc.c sensors.c integers.c ##########------------------------------------------------------########## ########## Programmer Defaults ########## @@ -51,7 +47,7 @@ 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) -I$(TEST_SOURCE_DIR) +CFLAGS = -mmcu=$(MCU) -DF_CPU=$(F_CPU)UL -DBAUD=$(BAUD) -Os -I. -I$(EXTRA_SOURCE_DIR) -I$(TEST_SOURCE_DIR) CFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums CFLAGS += -Wall -Wstrict-prototypes CFLAGS += -g -ggdb @@ -64,9 +60,7 @@ TARGET = $(strip $(basename $(MAIN))) SRC = $(TARGET).c EXTRA_SOURCE = $(addprefix $(EXTRA_SOURCE_DIR), $(EXTRA_SOURCE_FILES)) -TEST_SOURCE = $(addprefix $(TEST_SOURCE_DIR), $(TEST_SOURCE_FILES)) SRC += $(EXTRA_SOURCE) -SRC += $(TEST_SOURCE) SRC += $(LOCAL_SOURCE) ## List of all header files diff --git a/lambda-test/avrjunit.c b/lambda-test/avrjunit.c index ee80b64..1c48a5c 100644 --- a/lambda-test/avrjunit.c +++ b/lambda-test/avrjunit.c @@ -4,6 +4,27 @@ * Created on: 06.03.2015 * Author: dode@luniks.net * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Alternatively, the contents of this file may be used under the terms of the + * GNU Lesser General Public License Version 2.1 (the "LGPL License"), in which + * case the provisions of the LGPL License are applicable instead of those + * above. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU (Lesser) General Public License for more details. + * + * DISCLAIMER: I'm experienced with Java, but new to C. + * + * ATTRIBUTION: This project includes the module USART and the Makefile from + * the code accompanying the book Make: AVR Programming by Elliot Williams, + * a great book and a pleasant read, that helped me tremendously to get + * started with AVR programming. */ #include #include "USART.h" diff --git a/lambda-test/avrjunit.h b/lambda-test/avrjunit.h index ca32573..dc77ac9 100644 --- a/lambda-test/avrjunit.h +++ b/lambda-test/avrjunit.h @@ -3,6 +3,28 @@ * * Created on: 06.03.2015 * Author: dode@luniks.net + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Alternatively, the contents of this file may be used under the terms of the + * GNU Lesser General Public License Version 2.1 (the "LGPL License"), in which + * case the provisions of the LGPL License are applicable instead of those + * above. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU (Lesser) General Public License for more details. + * + * DISCLAIMER: I'm experienced with Java, but new to C. + * + * ATTRIBUTION: This project includes the module USART and the Makefile from + * the code accompanying the book Make: AVR Programming by Elliot Williams, + * a great book and a pleasant read, that helped me tremendously to get + * started with AVR programming. */ /** diff --git a/lambda-test/lambda-test.c b/lambda-test/lambda-test.c index 989e040..9778ffd 100644 --- a/lambda-test/lambda-test.c +++ b/lambda-test/lambda-test.c @@ -4,6 +4,27 @@ * Created on: 04.03.2015 * Author: dode@luniks.net * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Alternatively, the contents of this file may be used under the terms of the + * GNU Lesser General Public License Version 2.1 (the "LGPL License"), in which + * case the provisions of the LGPL License are applicable instead of those + * above. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU (Lesser) General Public License for more details. + * + * DISCLAIMER: I'm experienced with Java, but new to C. + * + * ATTRIBUTION: This project includes the module USART and the Makefile from + * the code accompanying the book Make: AVR Programming by Elliot Williams, + * a great book and a pleasant read, that helped me tremendously to get + * started with AVR programming. */ #include @@ -51,7 +72,7 @@ setupSleepMode(); // enable pull-up resistor so the measured voltage - // should be (close to?) to AREF + // should be (close to?) AREF PORTC |= (1 << PC1); // PORTC = 0xff; diff --git a/lambda/Makefile b/lambda/Makefile index 9f7d982..9b58805 100644 --- a/lambda/Makefile +++ b/lambda/Makefile @@ -14,11 +14,11 @@ ## If you've split your program into multiple .c / .h files, ## include the additional source (in same directory) here -LOCAL_SOURCE = adc.c sensors.c integers.c +LOCAL_SOURCE = USART.c adc.c sensors.c integers.c ## Here you can link to one more directory (and multiple .c files) -EXTRA_SOURCE_DIR = /data/Work/AVR/AVR-Programming-master/AVR-Programming-Library/ -EXTRA_SOURCE_FILES = USART.c +EXTRA_SOURCE_DIR = +EXTRA_SOURCE_FILES = ##########------------------------------------------------------########## ########## Programmer Defaults ########## diff --git a/lambda/USART.c b/lambda/USART.c new file mode 100644 index 0000000..7e8139a --- /dev/null +++ b/lambda/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/lambda/USART.h b/lambda/USART.h new file mode 100644 index 0000000..c3f18eb --- /dev/null +++ b/lambda/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/lambda/adc.c b/lambda/adc.c index da570ee..9820c68 100644 --- a/lambda/adc.c +++ b/lambda/adc.c @@ -4,6 +4,27 @@ * Created on: 11.03.2015 * Author: dode@luniks.net * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Alternatively, the contents of this file may be used under the terms of the + * GNU Lesser General Public License Version 2.1 (the "LGPL License"), in which + * case the provisions of the LGPL License are applicable instead of those + * above. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU (Lesser) General Public License for more details. + * + * DISCLAIMER: I'm experienced with Java, but new to C. + * + * ATTRIBUTION: This project includes the module USART and the Makefile from + * the code accompanying the book Make: AVR Programming by Elliot Williams, + * a great book and a pleasant read, that helped me tremendously to get + * started with AVR programming. */ #include #include @@ -15,19 +36,26 @@ // #define TEMPO_OP_OFFSET_MV 441 #define TEMPO_OP_OFFSET_MV 454 +// TODO what exactly does this do? EMPTY_INTERRUPT(ADC_vect); void setupADC(void) { - ADMUX |= (1 << REFS0); // use AVCC as reference voltage - // ADCSRA |= (1 << ADPS1) | (1 << ADPS2); // ADC clock prescaler /64 - ADCSRA |= (1 << ADPS2); // ADC clock prescaler /16 - ADCSRA |= (1 << ADEN); // enable ADC + // use AVCC as reference voltage + ADMUX |= (1 << REFS0); + // ADC clock prescaler/64 + // ADCSRA |= (1 << ADPS1) | (1 << ADPS2); + // ADC clock prescaler/16 + ADCSRA |= (1 << ADPS2); + // enable ADC + ADCSRA |= (1 << ADEN); } void setupSleepMode(void) { set_sleep_mode(SLEEP_MODE_ADC); - ADCSRA |= (1 << ADIE); // enable ADC interrupt - sei(); // enable global interrupts + // enable ADC interrupt + ADCSRA |= (1 << ADIE); + // enable global interrupts + sei(); } int16_t getVoltage(uint8_t port) { diff --git a/lambda/adc.h b/lambda/adc.h index 7fa8742..0919830 100644 --- a/lambda/adc.h +++ b/lambda/adc.h @@ -3,6 +3,28 @@ * * Created on: 11.03.2015 * Author: dode@luniks.net + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Alternatively, the contents of this file may be used under the terms of the + * GNU Lesser General Public License Version 2.1 (the "LGPL License"), in which + * case the provisions of the LGPL License are applicable instead of those + * above. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU (Lesser) General Public License for more details. + * + * DISCLAIMER: I'm experienced with Java, but new to C. + * + * ATTRIBUTION: This project includes the module USART and the Makefile from + * the code accompanying the book Make: AVR Programming by Elliot Williams, + * a great book and a pleasant read, that helped me tremendously to get + * started with AVR programming. */ /** diff --git a/lambda/integers.c b/lambda/integers.c index 19b74fe..c7c6b4f 100644 --- a/lambda/integers.c +++ b/lambda/integers.c @@ -3,6 +3,28 @@ * * Created on: 07.03.2015 * Author: dode@luniks.net + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Alternatively, the contents of this file may be used under the terms of the + * GNU Lesser General Public License Version 2.1 (the "LGPL License"), in which + * case the provisions of the LGPL License are applicable instead of those + * above. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU (Lesser) General Public License for more details. + * + * DISCLAIMER: I'm experienced with Java, but new to C. + * + * ATTRIBUTION: This project includes the module USART and the Makefile from + * the code accompanying the book Make: AVR Programming by Elliot Williams, + * a great book and a pleasant read, that helped me tremendously to get + * started with AVR programming. */ #include #include "integers.h" diff --git a/lambda/integers.h b/lambda/integers.h index 3e5227c..b328c4e 100644 --- a/lambda/integers.h +++ b/lambda/integers.h @@ -3,6 +3,28 @@ * * Created on: 07.03.2015 * Author: dode@luniks.net + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Alternatively, the contents of this file may be used under the terms of the + * GNU Lesser General Public License Version 2.1 (the "LGPL License"), in which + * case the provisions of the LGPL License are applicable instead of those + * above. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU (Lesser) General Public License for more details. + * + * DISCLAIMER: I'm experienced with Java, but new to C. + * + * ATTRIBUTION: This project includes the module USART and the Makefile from + * the code accompanying the book Make: AVR Programming by Elliot Williams, + * a great book and a pleasant read, that helped me tremendously to get + * started with AVR programming. */ /** @@ -19,4 +41,4 @@ int32_t divRoundUp(int32_t num, int32_t den); // TODO function to divide and return result as decimal string? -// char* toDecimalString(int32_t num, int32_t den); +// char* divToDecimalString(int32_t num, int32_t den); diff --git a/lambda/lambda.c b/lambda/lambda.c index 0052704..99e36b2 100644 --- a/lambda/lambda.c +++ b/lambda/lambda.c @@ -4,8 +4,27 @@ * Created on: 22.02.2015 * Author: dode@luniks.net * - * TODO attribution - * TODO have a look at string.h + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Alternatively, the contents of this file may be used under the terms of the + * GNU Lesser General Public License Version 2.1 (the "LGPL License"), in which + * case the provisions of the LGPL License are applicable instead of those + * above. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU (Lesser) General Public License for more details. + * + * DISCLAIMER: I'm experienced with Java, but new to C. + * + * ATTRIBUTION: This project includes the module USART and the Makefile from + * the code accompanying the book Make: AVR Programming by Elliot Williams, + * a great book and a pleasant read, that helped me tremendously to get + * started with AVR programming. */ #include #include @@ -36,6 +55,6 @@ _delay_ms(1000); } - // will never be reached + // never reached return 0; } diff --git a/lambda/sensors.c b/lambda/sensors.c index 8f8b745..0a23512 100644 --- a/lambda/sensors.c +++ b/lambda/sensors.c @@ -4,6 +4,27 @@ * Created on: 02.03.2015 * Author: dode@luniks.net * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Alternatively, the contents of this file may be used under the terms of the + * GNU Lesser General Public License Version 2.1 (the "LGPL License"), in which + * case the provisions of the LGPL License are applicable instead of those + * above. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU (Lesser) General Public License for more details. + * + * DISCLAIMER: I'm experienced with Java, but new to C. + * + * ATTRIBUTION: This project includes the module USART and the Makefile from + * the code accompanying the book Make: AVR Programming by Elliot Williams, + * a great book and a pleasant read, that helped me tremendously to get + * started with AVR programming. */ #include #include @@ -16,7 +37,7 @@ /** * Table used to look up the lambda value at 12 V heater voltage * and 220°C exhaust gas temperature. Most values are approximated - * from the characteristic curve in the datasheet. + * from the characteristic curve in the data sheet. * TODO real data? */ static const tableEntry lambdaTable[] = { diff --git a/lambda/sensors.h b/lambda/sensors.h index 58a7e68..8ec8c9c 100644 --- a/lambda/sensors.h +++ b/lambda/sensors.h @@ -3,6 +3,28 @@ * * Created on: 02.03.2015 * Author: dode@luniks.net + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * Alternatively, the contents of this file may be used under the terms of the + * GNU Lesser General Public License Version 2.1 (the "LGPL License"), in which + * case the provisions of the LGPL License are applicable instead of those + * above. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU (Lesser) General Public License for more details. + * + * DISCLAIMER: I'm experienced with Java, but new to C. + * + * ATTRIBUTION: This project includes the module USART and the Makefile from + * the code accompanying the book Make: AVR Programming by Elliot Williams, + * a great book and a pleasant read, that helped me tremendously to get + * started with AVR programming. */ #define LEAN "Mager"