diff --git a/Makefile b/Makefile index 5f4fe7a..fb399fb 100644 --- a/Makefile +++ b/Makefile @@ -8,6 +8,7 @@ CC = avr-gcc OBJCOPY = avr-objcopy AVRDUDE = avrdude CODE_FORMATTER = tooling/format-code.sh +AVRSIZE = avr-size BOARD = atmega2560 @@ -22,7 +23,8 @@ ELF = $(BINDIR)/$(BOARD)-user-code.elf # Source files. wildard "uses" all .c files in src directory SRCDIR = src -SRC = $(wildcard $(SRCDIR)/*.c) +BUILD_LIBS_DIR = lib +SRC = $(wildcard $(SRCDIR)/*.c $(BUILD_LIBS_DIR)/*/*.c) # Define object files from .c files defined above OBJ=$(SRC:.c=.o) @@ -41,13 +43,19 @@ CFLAGS = -Wall \ -Werror \ -Wfatal-errors \ -Os \ + -flto \ + -fdata-sections \ + -ffunction-sections \ -mmcu=$(BOARD) \ -DF_CPU=16000000UL \ -DGIT_DESCR=\"$(shell git describe --abbrev=6 --dirty --always --tags --long)\" \ + -ffreestanding \ -std=c11 # Linker flags -LDFLAGS = -mmcu=$(BOARD) +LDFLAGS = -mmcu=$(BOARD) \ + -flto \ + -Wl,-gc-sections OBJCOPYARGS = -O ihex \ -R .eeprom @@ -61,6 +69,9 @@ AVRDUDEARGS = -p $(BOARD) \ -V \ -D +AVRSIZEARGS = -C \ + --mcu=$(BOARD) + all: $(ELF) $(TARGET) %.o : %.c @@ -76,11 +87,15 @@ clean: #Do not remove .placeholder in BINDIR find $(BINDIR) -type f -not -name '.placeholder' -print0 | xargs -0 rm -f -- rm -f $(SRCDIR)/*.o + rm -fr $(BUILD_LIBS_DIR)/*/*.o install: $(AVRDUDE) $(AVRDUDEARGS) -U flash:w:$(TARGET) format: - $(CODE_FORMATTER) $(SRC) + $(CODE_FORMATTER) $(SRCDIR)/*.c -.PHONY: clean install format \ No newline at end of file +size: + $(AVRSIZE) $(AVRSIZEARGS) $(ELF) + +.PHONY: clean install format size diff --git a/doc/Arduino-Mega-LCD1602-keypad-shield-wiring.markdown b/doc/Arduino-Mega-LCD1602-keypad-shield-wiring.markdown new file mode 100644 index 0000000..48389f4 --- /dev/null +++ b/doc/Arduino-Mega-LCD1602-keypad-shield-wiring.markdown @@ -0,0 +1,41 @@ +# Arduino Mega Arduino LCD1602 Keypad shield wiring + +## Introduction + +This shield consists of three logical parts: + +- 1602LCD with HD4780 Dot Matrix Liquid Crystal Display Controller/Driver. +- 6 button keypad. +- Analog pins, 5V and GND pass through. + +
+ +## Wiring illustration + +![Arduino Mega LCD1602 Keypad shield wiring LCD part.png](Arduino-Mega-LCD1602-keypad-shield-wiring.png) + +Author: [Lauri Võsandi](http://lauri.võsandi.com/arduino/lcd1602-key-shield.html#hd44780) + +
+ +## Wiring table + +| Signal | ATMega2560 port and pin | Arduino Mega 2560 pin | LCD 1602 Keypad shield | 1602 LCD pin | HD44780 pin | +| --- | --- | --- | --- | --- | --- | +| **LCD** | | | | | +| Data bus DB4 | PORTG pin 5 | Digital pin 4 | 4 | 11 | DB4 | +| Data bus DB5 | PORTE pin 3 | Digital pin 5 | 5 | 12 | DB5 | +| Data bus DB6 | PORTH pin 3 | Digital pin 6 | 6 | 13 | DB6 | +| Data bus DB7 | PORTH pin 4 | Digital pin 7 | 7 | 14 | DB7 | +| Select register RS | PORTH pin 5 | Digital pin 8 | 8 | 4 | RS | +| Start read/write E | PORTH pin 6 | Digital pin 9 | 9 | 6 | E | +| Backlight control | PORTB pin 4 | Digital pin 10 | 10 | 16 | - | +| Backlight 5V (via variable resistor ) | - | - | - | 3 | - | +| **Keypad** | | | | | +| Buttons (select, up, right, down and left) | | Analog pin 0 | 0 | - | +| Reset button | - | RESET | RESET | - | +| **Pass through** | | | | | +| Analog A1 .. A5 | | Analog pin 1 .. 5 | Analog pin 1 .. 5 | - | +| **Common** | | | | | +| 5V | - | 5V | VCC | 2 and 15 | Vcc | +| Ground (GND) | GND | GND | pin 1 | GND | GND | diff --git a/doc/Arduino-Mega-LCD1602-keypad-shield-wiring.png b/doc/Arduino-Mega-LCD1602-keypad-shield-wiring.png new file mode 100644 index 0000000..8e6c765 Binary files /dev/null and b/doc/Arduino-Mega-LCD1602-keypad-shield-wiring.png differ diff --git a/doc/Arduino-Mega-USB-UART-wiring.markdown b/doc/Arduino-Mega-USB-UART-wiring.markdown index 8bfb0f9..6d47b44 100644 --- a/doc/Arduino-Mega-USB-UART-wiring.markdown +++ b/doc/Arduino-Mega-USB-UART-wiring.markdown @@ -12,6 +12,6 @@ This wiring schema uses only Tx from Arduino and is suitable to be used as stand | Signal | ATMega2560 port and pin | Arduino Mega 2560 pin | USB UART converter pin | | --- | --- | --- | --- | -| Ground (GND) | - | GND | GND | -| Transmit data from Arduino (TxD) | PORTJ 1 (TXD3) | 14 (TX3) | TxD | +| Ground (GND) | GND | GND | GND | +| Transmit data from Arduino (TxD) | PORTJ pin 1 (TXD3) | Digital pin 14 (TX3) | TxD | diff --git a/lib/andygock_avr-uart/LICENSE.txt b/lib/andygock_avr-uart/LICENSE.txt new file mode 100755 index 0000000..bf70028 --- /dev/null +++ b/lib/andygock_avr-uart/LICENSE.txt @@ -0,0 +1,11 @@ +Copyright (C) 2012 Andy Gock + +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 +any later version. + +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 General Public License for more details. \ No newline at end of file diff --git a/lib/andygock_avr-uart/README.md b/lib/andygock_avr-uart/README.md new file mode 100755 index 0000000..6ddc382 --- /dev/null +++ b/lib/andygock_avr-uart/README.md @@ -0,0 +1,56 @@ +avr-uart +======== + +An interrupt driven UART Library for 8-bit AVR microcontrollers + +Maintained by Andy Gock + +https://github.com/andygock/avr-uart + +Derived from original library by Peter Fleury + +Interrupt UART library using the built-in UART with transmit and receive circular buffers. + +An interrupt is generated when the UART has finished transmitting or +receiving a byte. The interrupt handling routines use circular buffers +for buffering received and transmitted data. + +## Setting up + +The `UART_RXn_BUFFER_SIZE` and `UART_TXn_BUFFER_SIZE` constants define +the size of the circular buffers in bytes. Note that these constants must be a power of 2. +You may need to adapt this constants to your target and your application by adding to your +compiler options: + + -DUART_RXn_BUFFER_SIZE=nn -DUART_TXn_BUFFER_SIZE=nn + +`RXn` and `TXn` refer to UART number, for UART3 with 128 byte buffers, add: + + -DUART_RX3_BUFFER_SIZE=128 -DUART_TX3_BUFFER_SIZE=128 + +UART0 is always enabled by default, to enable the other available UARTs, add the following +to your compiler options (or symbol options), for the relevant USART number: + + -DUSART1_ENABLED -DUSART2_ENABLED -DUSART3_ENABLED + +To enable large buffer support (over 256 bytes, up to 2^16 bytes) use: + + -DUSARTn_LARGE_BUFFER + +Where n = USART number. + +Supports AVR devices with up to 4 hardware USARTs. + +## Documentation + +Doxygen based documentation will be coming soon. + +## Notes + +### Buffer overflow behaviour + +When the RX circular buffer is full, and it receives further data from the UART, a buffer overflow condition occurs. Any new data is dropped. The RX buffer must be read before any more incoming data from the UART is placed into the RX buffer. + +If the TX buffer is full, and new data is sent to it using one of the `uartN_put*()` functions, this function will loop and wait until the buffer is not full any more. It is important to make sure you have not disabled your UART transmit interrupts (`TXEN*`) elsewhere in your application (e.g with `cli()`) before calling the `uartN_put*()` functions, as the application will lock up. The UART interrupts are automatically enabled when you use the `uartN_init()` functions. This is probably not the idea behaviour, I'll probably fix this some time. + +For now, make sure `TXEN*` interrupts are enabled when calling `uartN_put*()` functions. This should not be an issue unless you have code elsewhere purposely turning it off. diff --git a/lib/andygock_avr-uart/uart.c b/lib/andygock_avr-uart/uart.c new file mode 100755 index 0000000..ae71b50 --- /dev/null +++ b/lib/andygock_avr-uart/uart.c @@ -0,0 +1,1466 @@ +/************************************************************************* + + Updated UART library (this one) by Andy Gock + https://github.com/andygock/avr-uart + + Based on updated UART library (this one) by Tim Sharpe + http://beaststwo.org/avr-uart/index.shtml + + Based on original library by Peter Fluery + http://homepage.hispeed.ch/peterfleury/avr-software.html + +*************************************************************************/ + +/************************************************************************* +Title: Interrupt UART library with receive/transmit circular buffers +Author: Peter Fleury http://jump.to/fleury +Software: AVR-GCC 4.1, AVR Libc 1.4.6 or higher +Hardware: any AVR with built-in UART, +License: GNU General Public License + +DESCRIPTION: + An interrupt is generated when the UART has finished transmitting or + receiving a byte. The interrupt handling routines use circular buffers + for buffering received and transmitted data. + + The UART_RX_BUFFERn_SIZE and UART_TX_BUFFERn_SIZE variables define + the buffer size in bytes. Note that these variables must be a + power of 2. + +USAGE: + Refer to the header file uart.h for a description of the routines. + See also example test_uart.c. + +NOTES: + Based on original library by Peter Fluery, Tim Sharpe, Nicholas Zambetti. + Based on Atmel Application Note AVR306 + +LICENSE: + Copyright (C) 2012 Andy Gock + + 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 + any later version. + + 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 General Public License for more details. + +LICENSE: + Copyright (C) 2006 Peter Fleury + + 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 + any later version. + + 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 General Public License for more details. + +*************************************************************************/ + +/************************************************************************ +uart_available, uart_flush, uart1_available, and uart1_flush functions +were adapted from the Arduino HardwareSerial.h library by Tim Sharpe on +11 Jan 2009. The license info for HardwareSerial.h is as follows: + + HardwareSerial.cpp - Hardware serial library for Wiring + Copyright (c) 2006 Nicholas Zambetti. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library 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. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + + Modified 23 November 2006 by David A. Mellis +************************************************************************/ + +/************************************************************************ +Changelog for modifications made by Tim Sharpe, starting with the current + library version on his Web site as of 05/01/2009. + +Date Description +========================================================================= +05/11/2009 Changed all existing UARTx_RECEIVE_INTERRUPT and UARTx_TRANSMIT_INTERRUPT + macros to use the "_vect" format introduced in AVR-Libc + v1.4.0. Had to split the 3290 and 6490 out of their existing + macro due to an inconsistency in the UART0_RECEIVE_INTERRUPT + vector name (seems like a typo: USART_RX_vect for the 3290/6490 + vice USART0_RX_vect for the others in the macro). + Verified all existing macro register names against the device + header files in AVR-Libc v1.6.6 to catch any inconsistencies. +05/12/2009 Added support for 48P, 88P, 168P, and 328P by adding them to the + existing 48/88/168 macro. + Added Arduino-style available() and flush() functions for both + supported UARTs. Really wanted to keep them out of the library, so + that it would be as close as possible to Peter Fleury's original + library, but has scoping issues accessing internal variables from + another program. Go C! +05/13/2009 Changed Interrupt Service Routine label from the old "SIGNAL" to + the "ISR" format introduced in AVR-Libc v1.4.0. + +************************************************************************/ + +/************************************************************************ +Changelog for modifications made by Andy Gock, starting with the current + library version by Tim Sharpe as of 05/13/2009. + +Date Description +========================================================================= +2013-05-19 + - You can now use ring buffers over 256 bytes in size + - Used "uint16_t" instead of "unsigned int" etc + +2012-09-06 + - Added peek functions + - Updated URLs in source and README + - Cleaned up some indenting to be more readable + - Changed uart_functions() to uart0_functions + - Added macros to allow legacy naming + +2012-03-01 + - Fixed errors in ISR vector names for various devices + - Added USART2 and USART3 support to those devices with 4x USARTS. + - Selective enabling of USART0,1,2,3 as required. (set in uart.h) +************************************************************************/ + +#include +#include +#include +#include "uart.h" + +/* + * constants and macros + */ + +/* size of RX/TX buffers */ +#define UART_RX0_BUFFER_MASK ( UART_RX0_BUFFER_SIZE - 1) +#define UART_RX1_BUFFER_MASK ( UART_RX1_BUFFER_SIZE - 1) +#define UART_RX2_BUFFER_MASK ( UART_RX2_BUFFER_SIZE - 1) +#define UART_RX3_BUFFER_MASK ( UART_RX3_BUFFER_SIZE - 1) + +#define UART_TX0_BUFFER_MASK ( UART_TX0_BUFFER_SIZE - 1) +#define UART_TX1_BUFFER_MASK ( UART_TX1_BUFFER_SIZE - 1) +#define UART_TX2_BUFFER_MASK ( UART_TX2_BUFFER_SIZE - 1) +#define UART_TX3_BUFFER_MASK ( UART_TX3_BUFFER_SIZE - 1) + +#if ( UART_RX0_BUFFER_SIZE & UART_RX0_BUFFER_MASK ) + #error RX0 buffer size is not a power of 2 +#endif +#if ( UART_TX0_BUFFER_SIZE & UART_TX0_BUFFER_MASK ) + #error TX0 buffer size is not a power of 2 +#endif + +#if ( UART_RX1_BUFFER_SIZE & UART_RX1_BUFFER_MASK ) + #error RX1 buffer size is not a power of 2 +#endif +#if ( UART_TX1_BUFFER_SIZE & UART_TX1_BUFFER_MASK ) + #error TX1 buffer size is not a power of 2 +#endif + +#if ( UART_RX2_BUFFER_SIZE & UART_RX2_BUFFER_MASK ) + #error RX2 buffer size is not a power of 2 +#endif +#if ( UART_TX2_BUFFER_SIZE & UART_TX2_BUFFER_MASK ) + #error TX2 buffer size is not a power of 2 +#endif + +#if ( UART_RX3_BUFFER_SIZE & UART_RX3_BUFFER_MASK ) + #error RX3 buffer size is not a power of 2 +#endif +#if ( UART_TX3_BUFFER_SIZE & UART_TX3_BUFFER_MASK ) + #error TX3 buffer size is not a power of 2 +#endif + +#if defined(__AVR_AT90S2313__) \ + || defined(__AVR_AT90S4414__) || defined(__AVR_AT90S4434__) \ + || defined(__AVR_AT90S8515__) || defined(__AVR_AT90S8535__) \ + || defined(__AVR_ATmega103__) + /* old AVR classic or ATmega103 with one UART */ + #define AT90_UART + #define UART0_RECEIVE_INTERRUPT UART_RX_vect + #define UART0_TRANSMIT_INTERRUPT UART_UDRE_vect + #define UART0_STATUS USR + #define UART0_CONTROL UCR + #define UART0_DATA UDR + #define UART0_UDRIE UDRIE +#elif defined(__AVR_AT90S2333__) || defined(__AVR_AT90S4433__) + /* old AVR classic with one UART */ + #define AT90_UART + #define UART0_RECEIVE_INTERRUPT UART_RX_vect + #define UART0_TRANSMIT_INTERRUPT UART_UDRE_vect + #define UART0_STATUS UCSRA + #define UART0_CONTROL UCSRB + #define UART0_DATA UDR + #define UART0_UDRIE UDRIE +#elif defined(__AVR_ATmega8__) || defined(__AVR_ATmega16__) || defined(__AVR_ATmega32__) \ + || defined(__AVR_ATmega323__) + /* ATmega with one USART */ + #define ATMEGA_USART + #define UART0_RECEIVE_INTERRUPT USART_RXC_vect + #define UART0_TRANSMIT_INTERRUPT USART_UDRE_vect + #define UART0_STATUS UCSRA + #define UART0_CONTROL UCSRB + #define UART0_DATA UDR + #define UART0_UDRIE UDRIE +#elif defined(__AVR_ATmega8U2__) || defined(__AVR_ATmega16U2__) || defined(__AVR_ATmega16U4__) || \ + defined(__AVR_ATmega32U2__) || defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega32U6__) + /* ATmega with one USART, but is called USART1 (untested) */ + #define ATMEGA_USART1 + #define UART1_RECEIVE_INTERRUPT USART1_RX_vect + #define UART1_TRANSMIT_INTERRUPT USART1_UDRE_vect + #define UART1_STATUS UCSR1A + #define UART1_CONTROL UCSR1B + #define UART1_DATA UDR1 + #define UART1_UDRIE UDRIE1 +#elif defined(__AVR_ATmega8515__) || defined(__AVR_ATmega8535__) + /* ATmega with one USART */ + #define ATMEGA_USART + #define UART0_RECEIVE_INTERRUPT USART_RX_vect + #define UART0_TRANSMIT_INTERRUPT USART_UDRE_vect + #define UART0_STATUS UCSRA + #define UART0_CONTROL UCSRB + #define UART0_DATA UDR + #define UART0_UDRIE UDRIE +#elif defined(__AVR_ATmega163__) + /* ATmega163 with one UART */ + #define ATMEGA_UART + #define UART0_RECEIVE_INTERRUPT UART_RX_vect + #define UART0_TRANSMIT_INTERRUPT UART_UDRE_vect + #define UART0_STATUS UCSRA + #define UART0_CONTROL UCSRB + #define UART0_DATA UDR + #define UART0_UDRIE UDRIE +#elif defined(__AVR_ATmega162__) + /* ATmega with two USART */ + #define ATMEGA_USART0 + #define ATMEGA_USART1 + #define UART0_RECEIVE_INTERRUPT USART0_RXC_vect + #define UART1_RECEIVE_INTERRUPT USART1_RXC_vect + #define UART0_TRANSMIT_INTERRUPT USART0_UDRE_vect + #define UART1_TRANSMIT_INTERRUPT USART1_UDRE_vect + #define UART0_STATUS UCSR0A + #define UART0_CONTROL UCSR0B + #define UART0_DATA UDR0 + #define UART0_UDRIE UDRIE0 + #define UART1_STATUS UCSR1A + #define UART1_CONTROL UCSR1B + #define UART1_DATA UDR1 + #define UART1_UDRIE UDRIE1 +#elif defined(__AVR_ATmega64__) || defined(__AVR_ATmega128__) + /* ATmega with two USART */ + #define ATMEGA_USART0 + #define ATMEGA_USART1 + #define UART0_RECEIVE_INTERRUPT USART0_RX_vect + #define UART1_RECEIVE_INTERRUPT USART1_RX_vect + #define UART0_TRANSMIT_INTERRUPT USART0_UDRE_vect + #define UART1_TRANSMIT_INTERRUPT USART1_UDRE_vect + #define UART0_STATUS UCSR0A + #define UART0_CONTROL UCSR0B + #define UART0_DATA UDR0 + #define UART0_UDRIE UDRIE0 + #define UART1_STATUS UCSR1A + #define UART1_CONTROL UCSR1B + #define UART1_DATA UDR1 + #define UART1_UDRIE UDRIE1 +#elif defined(__AVR_ATmega161__) + /* ATmega with UART */ + #error "AVR ATmega161 currently not supported by this libaray !" +#elif defined(__AVR_ATmega169__) + /* ATmega with one USART */ + #define ATMEGA_USART + #define UART0_RECEIVE_INTERRUPT USART0_RX_vect + #define UART0_TRANSMIT_INTERRUPT USART0_UDRE_vect + #define UART0_STATUS UCSRA + #define UART0_CONTROL UCSRB + #define UART0_DATA UDR + #define UART0_UDRIE UDRIE +#elif defined(__AVR_ATmega48__) ||defined(__AVR_ATmega88__) || defined(__AVR_ATmega168__) || \ + defined(__AVR_ATmega48P__) ||defined(__AVR_ATmega88P__) || defined(__AVR_ATmega168P__) || \ + defined(__AVR_ATmega328P__) + /* TLS-Added 48P/88P/168P/328P */ + /* ATmega with one USART */ + #define ATMEGA_USART0 + #define UART0_RECEIVE_INTERRUPT USART_RX_vect + #define UART0_TRANSMIT_INTERRUPT USART_UDRE_vect + #define UART0_STATUS UCSR0A + #define UART0_CONTROL UCSR0B + #define UART0_DATA UDR0 + #define UART0_UDRIE UDRIE0 +#elif defined(__AVR_ATtiny2313__) || defined(__AVR_ATtiny2313A__) || defined(__AVR_ATtiny4313__) + #define ATMEGA_USART + #define UART0_RECEIVE_INTERRUPT USART_RX_vect + #define UART0_TRANSMIT_INTERRUPT USART_UDRE_vect + #define UART0_STATUS UCSRA + #define UART0_CONTROL UCSRB + #define UART0_DATA UDR + #define UART0_UDRIE UDRIE +#elif defined(__AVR_ATmega329__) ||\ + defined(__AVR_ATmega649__) ||\ + defined(__AVR_ATmega325__) ||defined(__AVR_ATmega3250__) ||\ + defined(__AVR_ATmega645__) ||defined(__AVR_ATmega6450__) + /* ATmega with one USART */ + #define ATMEGA_USART0 + #define UART0_RECEIVE_INTERRUPT USART0_RX_vect + #define UART0_TRANSMIT_INTERRUPT USART0_UDRE_vect + #define UART0_STATUS UCSR0A + #define UART0_CONTROL UCSR0B + #define UART0_DATA UDR0 + #define UART0_UDRIE UDRIE0 +#elif defined(__AVR_ATmega3290__) ||\ + defined(__AVR_ATmega6490__) + /* TLS-Separated these two from the previous group because of inconsistency in the USART_RX */ + /* ATmega with one USART */ + #define ATMEGA_USART0 + #define UART0_RECEIVE_INTERRUPT USART_RX_vect + #define UART0_TRANSMIT_INTERRUPT USART0_UDRE_vect + #define UART0_STATUS UCSR0A + #define UART0_CONTROL UCSR0B + #define UART0_DATA UDR0 + #define UART0_UDRIE UDRIE0 +#elif defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega640__) + /* ATmega with four USART */ + #define ATMEGA_USART0 + #define ATMEGA_USART1 + #define ATMEGA_USART2 + #define ATMEGA_USART3 + #define UART0_RECEIVE_INTERRUPT USART0_RX_vect + #define UART1_RECEIVE_INTERRUPT USART1_RX_vect + #define UART2_RECEIVE_INTERRUPT USART2_RX_vect + #define UART3_RECEIVE_INTERRUPT USART3_RX_vect + #define UART0_TRANSMIT_INTERRUPT USART0_UDRE_vect + #define UART1_TRANSMIT_INTERRUPT USART1_UDRE_vect + #define UART2_TRANSMIT_INTERRUPT USART2_UDRE_vect + #define UART3_TRANSMIT_INTERRUPT USART3_UDRE_vect + #define UART0_STATUS UCSR0A + #define UART0_CONTROL UCSR0B + #define UART0_DATA UDR0 + #define UART0_UDRIE UDRIE0 + #define UART1_STATUS UCSR1A + #define UART1_CONTROL UCSR1B + #define UART1_DATA UDR1 + #define UART1_UDRIE UDRIE1 + #define UART2_STATUS UCSR2A + #define UART2_CONTROL UCSR2B + #define UART2_DATA UDR2 + #define UART2_UDRIE UDRIE2 + #define UART3_STATUS UCSR3A + #define UART3_CONTROL UCSR3B + #define UART3_DATA UDR3 + #define UART3_UDRIE UDRIE3 +#elif defined(__AVR_ATmega644__) + /* ATmega with one USART */ + #define ATMEGA_USART0 + #define UART0_RECEIVE_INTERRUPT USART0_RX_vect + #define UART0_TRANSMIT_INTERRUPT USART0_UDRE_vect + #define UART0_STATUS UCSR0A + #define UART0_CONTROL UCSR0B + #define UART0_DATA UDR0 + #define UART0_UDRIE UDRIE0 +#elif defined(__AVR_ATmega164P__) || defined(__AVR_ATmega324P__) || defined(__AVR_ATmega644P__) || \ + defined(__AVR_ATmega1284P__) + /* ATmega with two USART */ + #define ATMEGA_USART0 + #define ATMEGA_USART1 + #define UART0_RECEIVE_INTERRUPT USART0_RX_vect + #define UART1_RECEIVE_INTERRUPT USART1_RX_vect + #define UART0_TRANSMIT_INTERRUPT USART0_UDRE_vect + #define UART1_TRANSMIT_INTERRUPT USART1_UDRE_vect + #define UART0_STATUS UCSR0A + #define UART0_CONTROL UCSR0B + #define UART0_DATA UDR0 + #define UART0_UDRIE UDRIE0 + #define UART1_STATUS UCSR1A + #define UART1_CONTROL UCSR1B + #define UART1_DATA UDR1 + #define UART1_UDRIE UDRIE1 +#else + #error "no UART definition for MCU available" +#endif + +/* + * Module global variables + */ + +#if defined( USART0_ENABLED ) + #if defined( ATMEGA_USART ) || defined( ATMEGA_USART0 ) + static volatile uint8_t UART_TxBuf[UART_TX0_BUFFER_SIZE]; + static volatile uint8_t UART_RxBuf[UART_RX0_BUFFER_SIZE]; + + #if defined( USART0_LARGE_BUFFER ) + static volatile uint16_t UART_TxHead; + static volatile uint16_t UART_TxTail; + static volatile uint16_t UART_RxHead; + static volatile uint16_t UART_RxTail; + static volatile uint8_t UART_LastRxError; + #else + static volatile uint8_t UART_TxHead; + static volatile uint8_t UART_TxTail; + static volatile uint8_t UART_RxHead; + static volatile uint8_t UART_RxTail; + static volatile uint8_t UART_LastRxError; + #endif + + #endif +#endif + +#if defined( USART1_ENABLED ) + #if defined( ATMEGA_USART1 ) + static volatile uint8_t UART1_TxBuf[UART_TX1_BUFFER_SIZE]; + static volatile uint8_t UART1_RxBuf[UART_RX1_BUFFER_SIZE]; + + #if defined( USART1_LARGE_BUFFER ) + static volatile uint16_t UART1_TxHead; + static volatile uint16_t UART1_TxTail; + static volatile uint16_t UART1_RxHead; + static volatile uint16_t UART1_RxTail; + static volatile uint8_t UART1_LastRxError; + #else + static volatile uint8_t UART1_TxHead; + static volatile uint8_t UART1_TxTail; + static volatile uint8_t UART1_RxHead; + static volatile uint8_t UART1_RxTail; + static volatile uint8_t UART1_LastRxError; + #endif + #endif +#endif + +#if defined( USART2_ENABLED ) + #if defined( ATMEGA_USART2 ) + static volatile uint8_t UART2_TxBuf[UART_TX2_BUFFER_SIZE]; + static volatile uint8_t UART2_RxBuf[UART_RX2_BUFFER_SIZE]; + + #if defined( USART2_LARGE_BUFFER ) + static volatile uint16_t UART2_TxHead; + static volatile uint16_t UART2_TxTail; + static volatile uint16_t UART2_RxHead; + static volatile uint16_t UART2_RxTail; + static volatile uint8_t UART2_LastRxError; + #else + static volatile uint8_t UART2_TxHead; + static volatile uint8_t UART2_TxTail; + static volatile uint8_t UART2_RxHead; + static volatile uint8_t UART2_RxTail; + static volatile uint8_t UART2_LastRxError; + #endif + #endif +#endif + +#if defined( USART3_ENABLED ) + #if defined( ATMEGA_USART3 ) + static volatile uint8_t UART3_TxBuf[UART_TX3_BUFFER_SIZE]; + static volatile uint8_t UART3_RxBuf[UART_RX3_BUFFER_SIZE]; + + #if defined( USART3_LARGE_BUFFER ) + static volatile uint16_t UART3_TxHead; + static volatile uint16_t UART3_TxTail; + static volatile uint16_t UART3_RxHead; + static volatile uint16_t UART3_RxTail; + static volatile uint8_t UART3_LastRxError; + #else + static volatile uint8_t UART3_TxHead; + static volatile uint8_t UART3_TxTail; + static volatile uint8_t UART3_RxHead; + static volatile uint8_t UART3_RxTail; + static volatile uint8_t UART3_LastRxError; + #endif + + #endif +#endif + +#if defined(AT90_UART) || defined(ATMEGA_USART) || defined(ATMEGA_USART0) + +ISR(UART0_RECEIVE_INTERRUPT) +/************************************************************************* +Function: UART Receive Complete interrupt +Purpose: called when the UART has received a character +**************************************************************************/ +{ + uint16_t tmphead; + uint8_t data; + uint8_t usr; + uint8_t lastRxError; + + /* read UART status register and UART data register */ + usr = UART0_STATUS; + data = UART0_DATA; + + /* */ +#if defined( AT90_UART ) + lastRxError = (usr & (_BV(FE)|_BV(DOR)) ); +#elif defined( ATMEGA_USART ) + lastRxError = (usr & (_BV(FE)|_BV(DOR)) ); +#elif defined( ATMEGA_USART0 ) + lastRxError = (usr & (_BV(FE0)|_BV(DOR0)) ); +#elif defined ( ATMEGA_UART ) + lastRxError = (usr & (_BV(FE)|_BV(DOR)) ); +#endif + + /* calculate buffer index */ + tmphead = ( UART_RxHead + 1) & UART_RX0_BUFFER_MASK; + + if ( tmphead == UART_RxTail ) { + /* error: receive buffer overflow */ + lastRxError = UART_BUFFER_OVERFLOW >> 8; + } else { + /* store new index */ + UART_RxHead = tmphead; + /* store received data in buffer */ + UART_RxBuf[tmphead] = data; + } + UART_LastRxError = lastRxError; +} + + +ISR(UART0_TRANSMIT_INTERRUPT) +/************************************************************************* +Function: UART Data Register Empty interrupt +Purpose: called when the UART is ready to transmit the next byte +**************************************************************************/ +{ + uint16_t tmptail; + + if ( UART_TxHead != UART_TxTail) { + /* calculate and store new buffer index */ + tmptail = (UART_TxTail + 1) & UART_TX0_BUFFER_MASK; + UART_TxTail = tmptail; + /* get one byte from buffer and write it to UART */ + UART0_DATA = UART_TxBuf[tmptail]; /* start transmission */ + } else { + /* tx buffer empty, disable UDRE interrupt */ + UART0_CONTROL &= ~_BV(UART0_UDRIE); + } +} + + +/************************************************************************* +Function: uart0_init() +Purpose: initialize UART and set baudrate +Input: baudrate using macro UART_BAUD_SELECT() +Returns: none +**************************************************************************/ +void uart0_init(uint16_t baudrate) +{ + UART_TxHead = 0; + UART_TxTail = 0; + UART_RxHead = 0; + UART_RxTail = 0; + +#if defined( AT90_UART ) + /* set baud rate */ + UBRR = (uint8_t)baudrate; + + /* enable UART receiver and transmitter and receive complete interrupt */ + UART0_CONTROL = _BV(RXCIE)|_BV(RXEN)|_BV(TXEN); + +#elif defined (ATMEGA_USART) + /* Set baud rate */ + if ( baudrate & 0x8000 ) { + UART0_STATUS = (1<>8); + UBRRL = (uint8_t) baudrate; + + /* Enable USART receiver and transmitter and receive complete interrupt */ + UART0_CONTROL = _BV(RXCIE)|(1<>8); + UBRR0L = (uint8_t) baudrate; + + /* Enable USART receiver and transmitter and receive complete interrupt */ + UART0_CONTROL = _BV(RXCIE0)|(1<>8); + UBRR = (uint8_t) baudrate; + + /* Enable UART receiver and transmitter and receive complete interrupt */ + UART0_CONTROL = _BV(RXCIE)|(1<> 8; + } else { + /* store new index */ + UART1_RxHead = tmphead; + /* store received data in buffer */ + UART1_RxBuf[tmphead] = data; + } + UART1_LastRxError = lastRxError; +} + + +ISR(UART1_TRANSMIT_INTERRUPT) +/************************************************************************* +Function: UART1 Data Register Empty interrupt +Purpose: called when the UART1 is ready to transmit the next byte +**************************************************************************/ +{ + uint16_t tmptail; + + if ( UART1_TxHead != UART1_TxTail) { + /* calculate and store new buffer index */ + tmptail = (UART1_TxTail + 1) & UART_TX1_BUFFER_MASK; + UART1_TxTail = tmptail; + /* get one byte from buffer and write it to UART */ + UART1_DATA = UART1_TxBuf[tmptail]; /* start transmission */ + } else { + /* tx buffer empty, disable UDRE interrupt */ + UART1_CONTROL &= ~_BV(UART1_UDRIE); + } +} + + +/************************************************************************* +Function: uart1_init() +Purpose: initialize UART1 and set baudrate +Input: baudrate using macro UART_BAUD_SELECT() +Returns: none +**************************************************************************/ +void uart1_init(uint16_t baudrate) +{ + UART1_TxHead = 0; + UART1_TxTail = 0; + UART1_RxHead = 0; + UART1_RxTail = 0; + + /* Set baud rate */ + if ( baudrate & 0x8000 ) { + UART1_STATUS = (1<>8); + UBRR1L = (uint8_t) baudrate; + + /* Enable USART receiver and transmitter and receive complete interrupt */ + UART1_CONTROL = _BV(RXCIE1)|(1<> 8; + } else { + /* store new index */ + UART2_RxHead = tmphead; + /* store received data in buffer */ + UART2_RxBuf[tmphead] = data; + } + UART2_LastRxError = lastRxError; +} + + +ISR(UART2_TRANSMIT_INTERRUPT) +/************************************************************************* +Function: UART2 Data Register Empty interrupt +Purpose: called when the UART2 is ready to transmit the next byte +**************************************************************************/ +{ + uint16_t tmptail; + + + if ( UART2_TxHead != UART2_TxTail) { + /* calculate and store new buffer index */ + tmptail = (UART2_TxTail + 1) & UART_TX2_BUFFER_MASK; + UART2_TxTail = tmptail; + /* get one byte from buffer and write it to UART */ + UART2_DATA = UART2_TxBuf[tmptail]; /* start transmission */ + } else { + /* tx buffer empty, disable UDRE interrupt */ + UART2_CONTROL &= ~_BV(UART2_UDRIE); + } +} + + +/************************************************************************* +Function: uart2_init() +Purpose: initialize UART2 and set baudrate +Input: baudrate using macro UART_BAUD_SELECT() +Returns: none +**************************************************************************/ +void uart2_init(uint16_t baudrate) +{ + UART2_TxHead = 0; + UART2_TxTail = 0; + UART2_RxHead = 0; + UART2_RxTail = 0; + + + /* Set baud rate */ + if ( baudrate & 0x8000 ) { + UART2_STATUS = (1<>8); + UBRR2L = (uint8_t) baudrate; + + /* Enable USART receiver and transmitter and receive complete interrupt */ + UART2_CONTROL = _BV(RXCIE2)|(1<> 8; + } else { + /* store new index */ + UART3_RxHead = tmphead; + /* store received data in buffer */ + UART3_RxBuf[tmphead] = data; + } + UART3_LastRxError = lastRxError; +} + + +ISR(UART3_TRANSMIT_INTERRUPT) +/************************************************************************* +Function: UART3 Data Register Empty interrupt +Purpose: called when the UART3 is ready to transmit the next byte +**************************************************************************/ +{ + uint16_t tmptail; + + + if ( UART3_TxHead != UART3_TxTail) { + /* calculate and store new buffer index */ + tmptail = (UART3_TxTail + 1) & UART_TX3_BUFFER_MASK; + UART3_TxTail = tmptail; + /* get one byte from buffer and write it to UART */ + UART3_DATA = UART3_TxBuf[tmptail]; /* start transmission */ + } else { + /* tx buffer empty, disable UDRE interrupt */ + UART3_CONTROL &= ~_BV(UART3_UDRIE); + } +} + + +/************************************************************************* +Function: uart3_init() +Purpose: initialize UART3 and set baudrate +Input: baudrate using macro UART_BAUD_SELECT() +Returns: none +**************************************************************************/ +void uart3_init(uint16_t baudrate) +{ + UART3_TxHead = 0; + UART3_TxTail = 0; + UART3_RxHead = 0; + UART3_RxTail = 0; + + /* Set baud rate */ + if ( baudrate & 0x8000 ) { + UART3_STATUS = (1<>8); + UBRR3L = (uint8_t) baudrate; + + /* Enable USART receiver and transmitter and receive complete interrupt */ + UART3_CONTROL = _BV(RXCIE3)|(1< @endcode + * + * @brief Interrupt UART library using the built-in UART with transmit and receive circular buffers. + * + * This library can be used to transmit and receive data through the built in UART. + * + * An interrupt is generated when the UART has finished transmitting or + * receiving a byte. The interrupt handling routines use circular buffers + * for buffering received and transmitted data. + * + * The UART_RXn_BUFFER_SIZE and UART_TXn_BUFFER_SIZE constants define + * the size of the circular buffers in bytes. Note that these constants must be a power of 2. + * + * You need to define these buffer sizes in uart.h + * + * @note Based on Atmel Application Note AVR306 + * @author Andy Gock + * @note Based on original library by Peter Fleury and Tim Sharpe. + */ + +/**@{*/ +#include +#include + +#if (__GNUC__ * 100 + __GNUC_MINOR__) < 304 +#error "This library requires AVR-GCC 3.4 or later, update to newer AVR-GCC compiler !" +#endif + +/* + * constants and macros + */ + +/* Enable USART 1, 2, 3 as required */ +#define USART0_ENABLED +//#define USART1_ENABLED +//#define USART2_ENABLED +#define USART3_ENABLED + +/* Set size of receive and transmit buffers */ + +#ifndef UART_RX0_BUFFER_SIZE + #define UART_RX0_BUFFER_SIZE 128 /**< Size of the circular receive buffer, must be power of 2 */ +#endif +#ifndef UART_RX1_BUFFER_SIZE + #define UART_RX1_BUFFER_SIZE 128 /**< Size of the circular receive buffer, must be power of 2 */ +#endif +#ifndef UART_RX2_BUFFER_SIZE + #define UART_RX2_BUFFER_SIZE 128 /**< Size of the circular receive buffer, must be power of 2 */ +#endif +#ifndef UART_RX3_BUFFER_SIZE + #define UART_RX3_BUFFER_SIZE 128 /**< Size of the circular receive buffer, must be power of 2 */ +#endif + +#ifndef UART_TX0_BUFFER_SIZE + #define UART_TX0_BUFFER_SIZE 128 /**< Size of the circular transmit buffer, must be power of 2 */ +#endif +#ifndef UART_TX1_BUFFER_SIZE + #define UART_TX1_BUFFER_SIZE 128 /**< Size of the circular transmit buffer, must be power of 2 */ +#endif +#ifndef UART_TX2_BUFFER_SIZE + #define UART_TX2_BUFFER_SIZE 128 /**< Size of the circular transmit buffer, must be power of 2 */ +#endif +#ifndef UART_TX3_BUFFER_SIZE + #define UART_TX3_BUFFER_SIZE 128 /**< Size of the circular transmit buffer, must be power of 2 */ +#endif + +/* Check buffer sizes are not too large for 8-bit positioning */ + +#if (UART_RX0_BUFFER_SIZE > 256 & !defined(USART0_LARGE_BUFFER)) + #error "Buffer too large, please use -DUSART0_LARGE_BUFFER switch in compiler options" +#endif + +#if (UART_RX1_BUFFER_SIZE > 256 & !defined(USART1_LARGE_BUFFER)) + #error "Buffer too large, please use -DUSART1_LARGE_BUFFER switch in compiler options" +#endif + +#if (UART_RX2_BUFFER_SIZE > 256 & !defined(USART2_LARGE_BUFFER)) + #error "Buffer too large, please use -DUSART2_LARGE_BUFFER switch in compiler options" +#endif + +#if (UART_RX3_BUFFER_SIZE > 256 & !defined(USART3_LARGE_BUFFER)) + #error "Buffer too large, please use -DUSART3_LARGE_BUFFER switch in compiler options" +#endif + +/* Check buffer sizes are not too large for *_LARGE_BUFFER operation (16-bit positioning) */ + +#if (UART_RX0_BUFFER_SIZE > 65536) + #error "Buffer too large, maximum allowed is 65536 bytes" +#endif + +#if (UART_RX1_BUFFER_SIZE > 65536) + #error "Buffer too large, maximum allowed is 65536 bytes" +#endif + +#if (UART_RX2_BUFFER_SIZE > 65536) + #error "Buffer too large, maximum allowed is 65536 bytes" +#endif + +#if (UART_RX3_BUFFER_SIZE > 65536) + #error "Buffer too large, maximum allowed is 65536 bytes" +#endif + +/** @brief UART Baudrate Expression + * @param xtalCpu system clock in Mhz, e.g. 4000000L for 4Mhz + * @param baudRate baudrate in bps, e.g. 1200, 2400, 9600 + */ +#define UART_BAUD_SELECT(baudRate,xtalCpu) (((xtalCpu)+8UL*(baudRate))/(16UL*(baudRate))-1UL) + +/** @brief UART Baudrate Expression for ATmega double speed mode + * @param xtalCpu system clock in Mhz, e.g. 4000000L for 4Mhz + * @param baudRate baudrate in bps, e.g. 1200, 2400, 9600 + */ +#define UART_BAUD_SELECT_DOUBLE_SPEED(baudRate,xtalCpu) ((((xtalCpu)+4UL*(baudRate))/(8UL*(baudRate))-1)|0x8000) + +/* test if the size of the circular buffers fits into SRAM */ + +#if defined(USART0_ENABLED) && ( (UART_RX0_BUFFER_SIZE+UART_TX0_BUFFER_SIZE) >= (RAMEND-0x60 ) ) +#error "size of UART_RX0_BUFFER_SIZE + UART_TX0_BUFFER_SIZE larger than size of SRAM" +#endif + +#if defined(USART1_ENABLED) && ( (UART_RX1_BUFFER_SIZE+UART_TX1_BUFFER_SIZE) >= (RAMEND-0x60 ) ) +#error "size of UART_RX1_BUFFER_SIZE + UART_TX1_BUFFER_SIZE larger than size of SRAM" +#endif + +#if defined(USART2_ENABLED) && ( (UART_RX2_BUFFER_SIZE+UART_RX2_BUFFER_SIZE) >= (RAMEND-0x60 ) ) +#error "size of UART_RX2_BUFFER_SIZE + UART_TX2_BUFFER_SIZE larger than size of SRAM" +#endif + +#if defined(USART3_ENABLED) && ( (UART_RX3_BUFFER_SIZE+UART_RX3_BUFFER_SIZE) >= (RAMEND-0x60 ) ) +#error "size of UART_RX3_BUFFER_SIZE + UART_TX3_BUFFER_SIZE larger than size of SRAM" +#endif + +/* +** high byte error return code of uart_getc() +*/ +#define UART_FRAME_ERROR 0x0800 /**< Framing Error by UART */ +#define UART_OVERRUN_ERROR 0x0400 /**< Overrun condition by UART */ +#define UART_BUFFER_OVERFLOW 0x0200 /**< receive ringbuffer overflow */ +#define UART_NO_DATA 0x0100 /**< no receive data available */ + +/* Macros, to allow use of legacy names */ + +#define uart_init(b) uart0_init(b) +#define uart_getc() uart0_getc() +#define uart_putc(d) uart0_putc(d) +#define uart_puts(s) uart0_puts(s) +#define uart_puts_p(s) uart0_puts_p(s) +#define uart_available() uart0_available() +#define uart_flush() uart0_flush() + +/* +** function prototypes +*/ + +/** + @brief Initialize UART and set baudrate + @param baudrate Specify baudrate using macro UART_BAUD_SELECT() + @return none +*/ +extern void uart0_init(uint16_t baudrate); + + +/** + * @brief Get received byte from ringbuffer + * + * Returns in the lower byte the received character and in the + * higher byte the last receive error. + * UART_NO_DATA is returned when no data is available. + * + * @return lower byte: received byte from ringbuffer + * @return higher byte: last receive status + * - \b 0 successfully received data from UART + * - \b UART_NO_DATA + *
no receive data available + * - \b UART_BUFFER_OVERFLOW + *
Receive ringbuffer overflow. + * We are not reading the receive buffer fast enough, + * one or more received character have been dropped + * - \b UART_OVERRUN_ERROR + *
Overrun condition by UART. + * A character already present in the UART UDR register was + * not read by the interrupt handler before the next character arrived, + * one or more received characters have been dropped. + * - \b UART_FRAME_ERROR + *
Framing Error by UART + */ +extern uint16_t uart0_getc(void); + +/** + * @brief Peek at next byte in ringbuffer + * + * Returns the next byte (character) of incoming UART data without removing it from the + * internal ring buffer. That is, successive calls to uartN_peek() will return the same + * character, as will the next call to uartN_getc(). + * + * UART_NO_DATA is returned when no data is available. + * + * @return lower byte: next byte in ringbuffer + * @return higher byte: last receive status + * - \b 0 successfully received data from UART + * - \b UART_NO_DATA + *
no receive data available + * - \b UART_BUFFER_OVERFLOW + *
Receive ringbuffer overflow. + * We are not reading the receive buffer fast enough, + * one or more received character have been dropped + * - \b UART_OVERRUN_ERROR + *
Overrun condition by UART. + * A character already present in the UART UDR register was + * not read by the interrupt handler before the next character arrived, + * one or more received characters have been dropped. + * - \b UART_FRAME_ERROR + *
Framing Error by UART + */ +extern uint16_t uart0_peek(void); + +/** + * @brief Put byte to ringbuffer for transmitting via UART + * @param data byte to be transmitted + * @return none + */ +extern void uart0_putc(uint8_t data); + + +/** + * @brief Put string to ringbuffer for transmitting via UART + * + * The string is buffered by the uart library in a circular buffer + * and one character at a time is transmitted to the UART using interrupts. + * Blocks if it can not write the whole string into the circular buffer. + * + * @param s string to be transmitted + * @return none + */ +extern void uart0_puts(const char *s ); + + +/** + * @brief Put string from program memory to ringbuffer for transmitting via UART. + * + * The string is buffered by the uart library in a circular buffer + * and one character at a time is transmitted to the UART using interrupts. + * Blocks if it can not write the whole string into the circular buffer. + * + * @param s program memory string to be transmitted + * @return none + * @see uart0_puts_P + */ +extern void uart0_puts_p(const char *s ); + +/** + * @brief Macro to automatically put a string constant into program memory + * \param __s string in program memory + */ +#define uart_puts_P(__s) uart0_puts_p(PSTR(__s)) +#define uart0_puts_P(__s) uart0_puts_p(PSTR(__s)) + +/** + * @brief Return number of bytes waiting in the receive buffer + * @return bytes waiting in the receive buffer + */ +extern uint16_t uart0_available(void); + +/** + * @brief Flush bytes waiting in receive buffer + */ +extern void uart0_flush(void); + + +/** @brief Initialize USART1 (only available on selected ATmegas) @see uart_init */ +extern void uart1_init(uint16_t baudrate); +/** @brief Get received byte of USART1 from ringbuffer. (only available on selected ATmega) @see uart_getc */ +extern uint16_t uart1_getc(void); +/** @brief Peek at next byte in USART1 ringbuffer */ +extern uint16_t uart1_peek(void); +/** @brief Put byte to ringbuffer for transmitting via USART1 (only available on selected ATmega) @see uart_putc */ +extern void uart1_putc(uint8_t data); +/** @brief Put string to ringbuffer for transmitting via USART1 (only available on selected ATmega) @see uart_puts */ +extern void uart1_puts(const char *s ); +/** @brief Put string from program memory to ringbuffer for transmitting via USART1 (only available on selected ATmega) @see uart_puts_p */ +extern void uart1_puts_p(const char *s ); +/** @brief Macro to automatically put a string constant into program memory */ +#define uart1_puts_P(__s) uart1_puts_p(PSTR(__s)) +/** @brief Return number of bytes waiting in the receive buffer */ +extern uint16_t uart1_available(void); +/** @brief Flush bytes waiting in receive buffer */ +extern void uart1_flush(void); + + +/** @brief Initialize USART2 (only available on selected ATmegas) @see uart_init */ +extern void uart2_init(uint16_t baudrate); +/** @brief Get received byte of USART2 from ringbuffer. (only available on selected ATmega) @see uart_getc */ +extern uint16_t uart2_getc(void); +/** @brief Peek at next byte in USART2 ringbuffer */ +extern uint16_t uart2_peek(void); +/** @brief Put byte to ringbuffer for transmitting via USART2 (only available on selected ATmega) @see uart_putc */ +extern void uart2_putc(uint8_t data); +/** @brief Put string to ringbuffer for transmitting via USART2 (only available on selected ATmega) @see uart_puts */ +extern void uart2_puts(const char *s ); +/** @brief Put string from program memory to ringbuffer for transmitting via USART2 (only available on selected ATmega) @see uart_puts_p */ +extern void uart2_puts_p(const char *s ); +/** @brief Macro to automatically put a string constant into program memory */ +#define uart2_puts_P(__s) uart2_puts_p(PSTR(__s)) +/** @brief Return number of bytes waiting in the receive buffer */ +extern uint16_t uart2_available(void); +/** @brief Flush bytes waiting in receive buffer */ +extern void uart2_flush(void); + + +/** @brief Initialize USART3 (only available on selected ATmegas) @see uart_init */ +extern void uart3_init(uint16_t baudrate); +/** @brief Get received byte of USART3 from ringbuffer. (only available on selected ATmega) @see uart_getc */ +extern uint16_t uart3_getc(void); +/** @brief Peek at next byte in USART3 ringbuffer */ +extern uint16_t uart3_peek(void); +/** @brief Put byte to ringbuffer for transmitting via USART3 (only available on selected ATmega) @see uart_putc */ +extern void uart3_putc(uint8_t data); +/** @brief Put string to ringbuffer for transmitting via USART3 (only available on selected ATmega) @see uart_puts */ +extern void uart3_puts(const char *s ); +/** @brief Put string from program memory to ringbuffer for transmitting via USART3 (only available on selected ATmega) @see uart_puts_p */ +extern void uart3_puts_p(const char *s ); +/** @brief Macro to automatically put a string constant into program memory */ +#define uart3_puts_P(__s) uart3_puts_p(PSTR(__s)) +/** @brief Return number of bytes waiting in the receive buffer */ +extern uint16_t uart3_available(void); +/** @brief Flush bytes waiting in receive buffer */ +extern void uart3_flush(void); + +/**@}*/ + +#endif // UART_H + diff --git a/lib/hd44780_111/hd44780.c b/lib/hd44780_111/hd44780.c new file mode 100644 index 0000000..e4a1efd --- /dev/null +++ b/lib/hd44780_111/hd44780.c @@ -0,0 +1,772 @@ +/***************************************************************************** +Title : HD44780 Library +Author : SA Development +Version: 1.11 +Modifications for: Arduino Mega 2560 + Itead Studio Arduino 1602 LED Keypad Shield +Modified by: Silver Kits October 2016 +*****************************************************************************/ +#include +#include +#include +#define __ASSERT_USE_STDERR +#include +#include "hd44780.h" +#include "hd44780_settings.h" + + +#if (USE_ADELAY_LIBRARY==1) +#include "adelay.h" +#else +#define Delay_ns(__ns) \ + if((unsigned long) (F_CPU/1000000000.0 * __ns) != F_CPU/1000000000.0 * __ns)\ + __builtin_avr_delay_cycles((unsigned long) ( F_CPU/1000000000.0 * __ns)+1);\ + else __builtin_avr_delay_cycles((unsigned long) ( F_CPU/1000000000.0 * __ns)) +#define Delay_us(__us) \ + if((unsigned long) (F_CPU/1000000.0 * __us) != F_CPU/1000000.0 * __us)\ + __builtin_avr_delay_cycles((unsigned long) ( F_CPU/1000000.0 * __us)+1);\ + else __builtin_avr_delay_cycles((unsigned long) ( F_CPU/1000000.0 * __us)) +#define Delay_ms(__ms) \ + if((unsigned long) (F_CPU/1000.0 * __ms) != F_CPU/1000.0 * __ms)\ + __builtin_avr_delay_cycles((unsigned long) ( F_CPU/1000.0 * __ms)+1);\ + else __builtin_avr_delay_cycles((unsigned long) ( F_CPU/1000.0 * __ms)) +#define Delay_s(__s) \ + if((unsigned long) (F_CPU/1.0 * __s) != F_CPU/1.0 * __s)\ + __builtin_avr_delay_cycles((unsigned long) ( F_CPU/1.0 * __s)+1);\ + else __builtin_avr_delay_cycles((unsigned long) ( F_CPU/1.0 * __s)) +#endif + +#if !defined(LCD_BITS) || (LCD_BITS!=4 && LCD_BITS!=8) +#error LCD_BITS is not defined or not valid. +#endif + +#if !defined(WAIT_MODE) || (WAIT_MODE!=0 && WAIT_MODE!=1) +#error WAIT_MODE is not defined or not valid. +#endif + +#if !defined(RW_LINE_IMPLEMENTED) || (RW_LINE_IMPLEMENTED!=0 && RW_LINE_IMPLEMENTED!=1) +#error RW_LINE_IMPLEMENTED is not defined or not valid. +#endif + +#if (WAIT_MODE==1 && RW_LINE_IMPLEMENTED!=1) +#error WAIT_MODE=1 requires RW_LINE_IMPLEMENTED=1. +#endif + +#if !defined(LCD_DISPLAYS) || (LCD_DISPLAYS<1) || (LCD_DISPLAYS>4) +#error LCD_DISPLAYS is not defined or not valid. +#endif + +// Constants/Macros +#define PIN(x) (*(&x - 2)) // Address of Data Direction Register of Port X +#define DDR(x) (*(&x - 1)) // Address of Input Register of Port X + +//PORT defines +#define lcd_rs_port_low() LCD_RS_PORT&=~_BV(LCD_RS_PIN) +#if RW_LINE_IMPLEMENTED==1 +#define lcd_rw_port_low() LCD_RW_PORT&=~_BV(LCD_RW_PIN) +#endif +#define lcd_db0_port_low() LCD_DB0_PORT&=~_BV(LCD_DB0_PIN) +#define lcd_db1_port_low() LCD_DB1_PORT&=~_BV(LCD_DB1_PIN) +#define lcd_db2_port_low() LCD_DB2_PORT&=~_BV(LCD_DB2_PIN) +#define lcd_db3_port_low() LCD_DB3_PORT&=~_BV(LCD_DB3_PIN) +#define lcd_db4_port_low() LCD_DB4_PORT&=~_BV(LCD_DB4_PIN) +#define lcd_db5_port_low() LCD_DB5_PORT&=~_BV(LCD_DB5_PIN) +#define lcd_db6_port_low() LCD_DB6_PORT&=~_BV(LCD_DB6_PIN) +#define lcd_db7_port_low() LCD_DB7_PORT&=~_BV(LCD_DB7_PIN) + +#define lcd_rs_port_high() LCD_RS_PORT|=_BV(LCD_RS_PIN) +#if RW_LINE_IMPLEMENTED==1 +#define lcd_rw_port_high() LCD_RW_PORT|=_BV(LCD_RW_PIN) +#endif +#define lcd_db0_port_high() LCD_DB0_PORT|=_BV(LCD_DB0_PIN) +#define lcd_db1_port_high() LCD_DB1_PORT|=_BV(LCD_DB1_PIN) +#define lcd_db2_port_high() LCD_DB2_PORT|=_BV(LCD_DB2_PIN) +#define lcd_db3_port_high() LCD_DB3_PORT|=_BV(LCD_DB3_PIN) +#define lcd_db4_port_high() LCD_DB4_PORT|=_BV(LCD_DB4_PIN) +#define lcd_db5_port_high() LCD_DB5_PORT|=_BV(LCD_DB5_PIN) +#define lcd_db6_port_high() LCD_DB6_PORT|=_BV(LCD_DB6_PIN) +#define lcd_db7_port_high() LCD_DB7_PORT|=_BV(LCD_DB7_PIN) + +#define lcd_rs_port_set(value) if (value) lcd_rs_port_high(); else lcd_rs_port_low(); +#if RW_LINE_IMPLEMENTED==1 +#define lcd_rw_port_set(value) if (value) lcd_rw_port_high(); else lcd_rw_port_low(); +#endif +#define lcd_db0_port_set(value) if (value) lcd_db0_port_high(); else lcd_db0_port_low(); +#define lcd_db1_port_set(value) if (value) lcd_db1_port_high(); else lcd_db1_port_low(); +#define lcd_db2_port_set(value) if (value) lcd_db2_port_high(); else lcd_db2_port_low(); +#define lcd_db3_port_set(value) if (value) lcd_db3_port_high(); else lcd_db3_port_low(); +#define lcd_db4_port_set(value) if (value) lcd_db4_port_high(); else lcd_db4_port_low(); +#define lcd_db5_port_set(value) if (value) lcd_db5_port_high(); else lcd_db5_port_low(); +#define lcd_db6_port_set(value) if (value) lcd_db6_port_high(); else lcd_db6_port_low(); +#define lcd_db7_port_set(value) if (value) lcd_db7_port_high(); else lcd_db7_port_low(); + +//PIN defines +#define lcd_db0_pin_get() (((PIN(LCD_DB0_PORT) & _BV(LCD_DB0_PIN))==0)?0:1) +#define lcd_db1_pin_get() (((PIN(LCD_DB1_PORT) & _BV(LCD_DB1_PIN))==0)?0:1) +#define lcd_db2_pin_get() (((PIN(LCD_DB2_PORT) & _BV(LCD_DB2_PIN))==0)?0:1) +#define lcd_db3_pin_get() (((PIN(LCD_DB3_PORT) & _BV(LCD_DB3_PIN))==0)?0:1) +#define lcd_db4_pin_get() (((PIN(LCD_DB4_PORT) & _BV(LCD_DB4_PIN))==0)?0:1) +#define lcd_db5_pin_get() (((PIN(LCD_DB5_PORT) & _BV(LCD_DB5_PIN))==0)?0:1) +#define lcd_db6_pin_get() (((PIN(LCD_DB6_PORT) & _BV(LCD_DB6_PIN))==0)?0:1) +#define lcd_db7_pin_get() (((PIN(LCD_DB7_PORT) & _BV(LCD_DB7_PIN))==0)?0:1) + +//DDR defines +#define lcd_rs_ddr_low() DDR(LCD_RS_PORT)&=~_BV(LCD_RS_PIN) +#if RW_LINE_IMPLEMENTED==1 +#define lcd_rw_ddr_low() DDR(LCD_RW_PORT)&=~_BV(LCD_RW_PIN) +#endif +#define lcd_db0_ddr_low() DDR(LCD_DB0_PORT)&=~_BV(LCD_DB0_PIN) +#define lcd_db1_ddr_low() DDR(LCD_DB1_PORT)&=~_BV(LCD_DB1_PIN) +#define lcd_db2_ddr_low() DDR(LCD_DB2_PORT)&=~_BV(LCD_DB2_PIN) +#define lcd_db3_ddr_low() DDR(LCD_DB3_PORT)&=~_BV(LCD_DB3_PIN) +#define lcd_db4_ddr_low() DDR(LCD_DB4_PORT)&=~_BV(LCD_DB4_PIN) +#define lcd_db5_ddr_low() DDR(LCD_DB5_PORT)&=~_BV(LCD_DB5_PIN) +#define lcd_db6_ddr_low() DDR(LCD_DB6_PORT)&=~_BV(LCD_DB6_PIN) +#define lcd_db7_ddr_low() DDR(LCD_DB7_PORT)&=~_BV(LCD_DB7_PIN) + +#define lcd_rs_ddr_high() DDR(LCD_RS_PORT)|=_BV(LCD_RS_PIN) +#if RW_LINE_IMPLEMENTED==1 +#define lcd_rw_ddr_high() DDR(LCD_RW_PORT)|=_BV(LCD_RW_PIN) +#endif +#define lcd_db0_ddr_high() DDR(LCD_DB0_PORT)|=_BV(LCD_DB0_PIN) +#define lcd_db1_ddr_high() DDR(LCD_DB1_PORT)|=_BV(LCD_DB1_PIN) +#define lcd_db2_ddr_high() DDR(LCD_DB2_PORT)|=_BV(LCD_DB2_PIN) +#define lcd_db3_ddr_high() DDR(LCD_DB3_PORT)|=_BV(LCD_DB3_PIN) +#define lcd_db4_ddr_high() DDR(LCD_DB4_PORT)|=_BV(LCD_DB4_PIN) +#define lcd_db5_ddr_high() DDR(LCD_DB5_PORT)|=_BV(LCD_DB5_PIN) +#define lcd_db6_ddr_high() DDR(LCD_DB6_PORT)|=_BV(LCD_DB6_PIN) +#define lcd_db7_ddr_high() DDR(LCD_DB7_PORT)|=_BV(LCD_DB7_PIN) + +#define lcd_rs_ddr_set(value) if (value) lcd_rs_ddr_high(); else lcd_rs_ddr_low(); +#if RW_LINE_IMPLEMENTED==1 +#define lcd_rw_ddr_set(value) if (value) lcd_rw_ddr_high(); else lcd_rw_ddr_low(); +#endif +#define lcd_db0_ddr_set(value) if (value) lcd_db0_ddr_high(); else lcd_db0_ddr_low(); +#define lcd_db1_ddr_set(value) if (value) lcd_db1_ddr_high(); else lcd_db1_ddr_low(); +#define lcd_db2_ddr_set(value) if (value) lcd_db2_ddr_high(); else lcd_db2_ddr_low(); +#define lcd_db3_ddr_set(value) if (value) lcd_db3_ddr_high(); else lcd_db3_ddr_low(); +#define lcd_db4_ddr_set(value) if (value) lcd_db4_ddr_high(); else lcd_db4_ddr_low(); +#define lcd_db5_ddr_set(value) if (value) lcd_db5_ddr_high(); else lcd_db5_ddr_low(); +#define lcd_db6_ddr_set(value) if (value) lcd_db6_ddr_high(); else lcd_db6_ddr_low(); +#define lcd_db7_ddr_set(value) if (value) lcd_db7_ddr_high(); else lcd_db7_ddr_low(); + +#if (WAIT_MODE==1 && RW_LINE_IMPLEMENTED==1) +static unsigned char PrevCmdInvolvedAddressCounter = 0; +#endif + +#if (LCD_DISPLAYS>1) +static unsigned char ActiveDisplay = 1; +#endif + +static inline void lcd_e_port_low() +{ +#if (LCD_DISPLAYS>1) + + switch (ActiveDisplay) { + case 2 : + LCD_E2_PORT &= ~_BV(LCD_E2_PIN); + break; +#if (LCD_DISPLAYS>=3) + + case 3 : + LCD_E3_PORT &= ~_BV(LCD_E3_PIN); + break; +#endif +#if (LCD_DISPLAYS==4) + + case 4 : + LCD_E4_PORT &= ~_BV(LCD_E4_PIN); + break; +#endif + + default : +#endif + LCD_E_PORT &= ~_BV(LCD_E_PIN); +#if (LCD_DISPLAYS>1) + } + +#endif +} + +static inline void lcd_e_port_high() +{ +#if (LCD_DISPLAYS>1) + + switch (ActiveDisplay) { + case 2 : + LCD_E2_PORT |= _BV(LCD_E2_PIN); + break; +#if (LCD_DISPLAYS>=3) + + case 3 : + LCD_E3_PORT |= _BV(LCD_E3_PIN); + break; +#endif +#if (LCD_DISPLAYS==4) + + case 4 : + LCD_E4_PORT |= _BV(LCD_E4_PIN); + break; +#endif + + default : +#endif + LCD_E_PORT |= _BV(LCD_E_PIN); +#if (LCD_DISPLAYS>1) + } + +#endif +} + +static inline void lcd_e_ddr_low() +{ +#if (LCD_DISPLAYS>1) + + switch (ActiveDisplay) { + case 2 : + DDR(LCD_E2_PORT) &= ~_BV(LCD_E2_PIN); + break; +#if (LCD_DISPLAYS>=3) + + case 3 : + DDR(LCD_E3_PORT) &= ~_BV(LCD_E3_PIN); + break; +#endif +#if (LCD_DISPLAYS==4) + + case 4 : + DDR(LCD_E4_PORT) &= ~_BV(LCD_E4_PIN); + break; +#endif + + default : +#endif + DDR(LCD_E_PORT) &= ~_BV(LCD_E_PIN); +#if (LCD_DISPLAYS>1) + } + +#endif +} + +static inline void lcd_e_ddr_high() +{ +#if (LCD_DISPLAYS>1) + + switch (ActiveDisplay) { + case 2 : + DDR(LCD_E2_PORT) |= _BV(LCD_E2_PIN); + break; +#if (LCD_DISPLAYS>=3) + + case 3 : + DDR(LCD_E3_PORT) |= _BV(LCD_E3_PIN); + break; +#endif +#if (LCD_DISPLAYS==4) + + case 4 : + DDR(LCD_E4_PORT) |= _BV(LCD_E4_PIN); + break; +#endif + + default : +#endif + DDR(LCD_E_PORT) |= _BV(LCD_E_PIN); +#if (LCD_DISPLAYS>1) + } + +#endif +} + + +/************************************************************************* +loops while lcd is busy, returns address counter +*************************************************************************/ +#if (WAIT_MODE==1 && RW_LINE_IMPLEMENTED==1) +static uint8_t lcd_read(uint8_t rs); + +static void lcd_waitbusy(void) +{ + register uint8_t c; + unsigned int ul1 = 0; + + while ( ((c = lcd_read(0)) & (1 << LCD_BUSY)) && + ul1 < ((F_CPU / 16384 >= 16) ? F_CPU / 16384 : + 16)) { // Wait Until Busy Flag is Cleared + ul1++; + } +} +#endif + + +/************************************************************************* +Low-level function to read byte from LCD controller +Input: rs 1: read data + 0: read busy flag / address counter +Returns: byte read from LCD controller +*************************************************************************/ +#if RW_LINE_IMPLEMENTED==1 +static uint8_t lcd_read(uint8_t rs) +{ + uint8_t data; +#if (WAIT_MODE==1 && RW_LINE_IMPLEMENTED==1) + + if (rs) { + lcd_waitbusy(); + } + + if (PrevCmdInvolvedAddressCounter) { + Delay_us(5); + PrevCmdInvolvedAddressCounter = 0; + } + +#endif + + if (rs) { + lcd_rs_port_high(); // RS=1: Read Data +#if (WAIT_MODE==1 && RW_LINE_IMPLEMENTED==1) + PrevCmdInvolvedAddressCounter = 1; +#endif + } else { + lcd_rs_port_low(); // RS=0: Read Busy Flag + } + + lcd_rw_port_high(); // RW=1: Read Mode +#if LCD_BITS==4 + lcd_db7_ddr_low(); // Configure Data Pins as Input + lcd_db6_ddr_low(); + lcd_db5_ddr_low(); + lcd_db4_ddr_low(); + lcd_e_port_high(); // Read High Nibble First + Delay_ns(500); + data = lcd_db4_pin_get() << 4 | lcd_db5_pin_get() << 5 | + lcd_db6_pin_get() << 6 | lcd_db7_pin_get() << 7; + lcd_e_port_low(); + Delay_ns(500); + lcd_e_port_high(); // Read Low Nibble + Delay_ns(500); + data |= lcd_db4_pin_get() << 0 | lcd_db5_pin_get() << 1 | + lcd_db6_pin_get() << 2 | lcd_db7_pin_get() << 3; + lcd_e_port_low(); + lcd_db7_ddr_high(); // Configure Data Pins as Output + lcd_db6_ddr_high(); + lcd_db5_ddr_high(); + lcd_db4_ddr_high(); + lcd_db7_port_high(); // Pins High (Inactive) + lcd_db6_port_high(); + lcd_db5_port_high(); + lcd_db4_port_high(); +#else //using 8-Bit-Mode + lcd_db7_ddr_low(); // Configure Data Pins as Input + lcd_db6_ddr_low(); + lcd_db5_ddr_low(); + lcd_db4_ddr_low(); + lcd_db3_ddr_low(); + lcd_db2_ddr_low(); + lcd_db1_ddr_low(); + lcd_db0_ddr_low(); + lcd_e_port_high(); + Delay_ns(500); + data = lcd_db7_pin_get() << 7 | lcd_db6_pin_get() << 6 | + lcd_db5_pin_get() << 5 | lcd_db4_pin_get() << 4 | + lcd_db3_pin_get() << 3 | lcd_db2_pin_get() << 2 | + lcd_db1_pin_get() << 1 | lcd_db0_pin_get(); + lcd_e_port_low(); + lcd_db7_ddr_high(); // Configure Data Pins as Output + lcd_db6_ddr_high(); + lcd_db5_ddr_high(); + lcd_db4_ddr_high(); + lcd_db3_ddr_high(); + lcd_db2_ddr_high(); + lcd_db1_ddr_high(); + lcd_db0_ddr_high(); + lcd_db7_port_high(); // Pins High (Inactive) + lcd_db6_port_high(); + lcd_db5_port_high(); + lcd_db4_port_high(); + lcd_db3_port_high(); + lcd_db2_port_high(); + lcd_db1_port_high(); + lcd_db0_port_high(); +#endif + lcd_rw_port_low(); +#if (WAIT_MODE==0 || RW_LINE_IMPLEMENTED==0) + + if (rs) { + Delay_us(40); + } else { + Delay_us(1); + } + +#endif + return data; +} + +uint8_t lcd_getc() +{ + return lcd_read(1); +} + +#endif + +/************************************************************************* +Low-level function to write byte to LCD controller +Input: data byte to write to LCD + rs 1: write data + 0: write instruction +Returns: none +*************************************************************************/ +static void lcd_write(uint8_t data, uint8_t rs) +{ +#if (WAIT_MODE==1 && RW_LINE_IMPLEMENTED==1) + lcd_waitbusy(); + + if (PrevCmdInvolvedAddressCounter) { + Delay_us(5); + PrevCmdInvolvedAddressCounter = 0; + } + +#endif + + if (rs) { + lcd_rs_port_high(); // RS=1: Write Character +#if (WAIT_MODE==1 && RW_LINE_IMPLEMENTED==1) + PrevCmdInvolvedAddressCounter = 1; +#endif + } else { + lcd_rs_port_low(); // RS=0: Write Command +#if (WAIT_MODE==1 && RW_LINE_IMPLEMENTED==1) + PrevCmdInvolvedAddressCounter = 0; +#endif + } + +#if LCD_BITS==4 + lcd_db7_port_set(data & _BV(7)); //Output High Nibble + lcd_db6_port_set(data & _BV(6)); + lcd_db5_port_set(data & _BV(5)); + lcd_db4_port_set(data & _BV(4)); + Delay_ns(100); + lcd_e_port_high(); + Delay_ns(500); + lcd_e_port_low(); + lcd_db7_port_set(data & _BV(3)); //Output High Nibble + lcd_db6_port_set(data & _BV(2)); + lcd_db5_port_set(data & _BV(1)); + lcd_db4_port_set(data & _BV(0)); + Delay_ns(100); + lcd_e_port_high(); + Delay_ns(500); + lcd_e_port_low(); + lcd_db7_port_high(); // All Data Pins High (Inactive) + lcd_db6_port_high(); + lcd_db5_port_high(); + lcd_db4_port_high(); +#else //using 8-Bit_Mode + lcd_db7_port_set(data & _BV(7)); //Output High Nibble + lcd_db6_port_set(data & _BV(6)); + lcd_db5_port_set(data & _BV(5)); + lcd_db4_port_set(data & _BV(4)); + lcd_db3_port_set(data & _BV(3)); //Output High Nibble + lcd_db2_port_set(data & _BV(2)); + lcd_db1_port_set(data & _BV(1)); + lcd_db0_port_set(data & _BV(0)); + Delay_ns(100); + lcd_e_port_high(); + Delay_ns(500); + lcd_e_port_low(); + lcd_db7_port_high(); // All Data Pins High (Inactive) + lcd_db6_port_high(); + lcd_db5_port_high(); + lcd_db4_port_high(); + lcd_db3_port_high(); + lcd_db2_port_high(); + lcd_db1_port_high(); + lcd_db0_port_high(); +#endif +#if (WAIT_MODE==0 || RW_LINE_IMPLEMENTED==0) + + if (!rs && + data <= ((1 << LCD_CLR) | (1 << LCD_HOME))) { // Is command clrscr or home? + Delay_us(1640); + } else { + Delay_us(40); + } + +#endif +} + +/************************************************************************* +Send LCD controller instruction command +Input: instruction to send to LCD controller, see HD44780 data sheet +Returns: none +*************************************************************************/ +void lcd_command(uint8_t cmd) +{ + lcd_write(cmd, 0); +} + +/************************************************************************* +Set cursor to specified position +Input: pos position +Returns: none +*************************************************************************/ +void lcd_goto(uint8_t pos) +{ + //Do not go outside of screen limits + assert(pos < LCD_COLS_MAX); + lcd_command((1 << LCD_DDRAM) + pos); +} + + +/************************************************************************* +Clear screen +Input: none +Returns: none +*************************************************************************/ +void lcd_clrscr() +{ + lcd_command(1 << LCD_CLR); +} + + +/************************************************************************* +Return home +Input: none +Returns: none +*************************************************************************/ +void lcd_home() +{ + lcd_command(1 << LCD_HOME); +} + + +/************************************************************************* +Display character +Input: character to be displayed +Returns: none +*************************************************************************/ +void lcd_putc(char c) +{ + lcd_write(c, 1); +} + + +/************************************************************************* +Display string +Input: string to be displayed +Returns: none +*************************************************************************/ +void lcd_puts(const char *s) +{ + register char c; + + while ((c = *s++)) { + lcd_putc(c); + } +} + + +/************************************************************************* +Display string from flash +Input: string to be displayed +Returns: none +*************************************************************************/ +void lcd_puts_P(const char *progmem_s) +{ + register char c; + + while ((c = pgm_read_byte(progmem_s++))) { + lcd_putc(c); + } +} + +/************************************************************************* +Initialize display +Input: none +Returns: none +*************************************************************************/ +void lcd_init() +{ + //Set All Pins as Output + lcd_e_ddr_high(); + lcd_rs_ddr_high(); +#if RW_LINE_IMPLEMENTED==1 + lcd_rw_ddr_high(); +#endif + lcd_db7_ddr_high(); + lcd_db6_ddr_high(); + lcd_db5_ddr_high(); + lcd_db4_ddr_high(); +#if LCD_BITS==8 + lcd_db3_ddr_high(); + lcd_db2_ddr_high(); + lcd_db1_ddr_high(); + lcd_db0_ddr_high(); +#endif + //Set All Control Lines Low + lcd_e_port_low(); + lcd_rs_port_low(); +#if RW_LINE_IMPLEMENTED==1 + lcd_rw_port_low(); +#endif + //Set All Data Lines High + lcd_db7_port_high(); + lcd_db6_port_high(); + lcd_db5_port_high(); + lcd_db4_port_high(); +#if LCD_BITS==8 + lcd_db3_port_high(); + lcd_db2_port_high(); + lcd_db1_port_high(); + lcd_db0_port_high(); +#endif + //Startup Delay + Delay_ms(DELAY_RESET); + //Initialize Display + lcd_db7_port_low(); + lcd_db6_port_low(); + Delay_ns(100); + lcd_e_port_high(); + Delay_ns(500); + lcd_e_port_low(); + Delay_us(4100); + lcd_e_port_high(); + Delay_ns(500); + lcd_e_port_low(); + Delay_us(100); + lcd_e_port_high(); + Delay_ns(500); + lcd_e_port_low(); + Delay_us(40); + //Init differs between 4-bit and 8-bit from here +#if (LCD_BITS==4) + lcd_db4_port_low(); + Delay_ns(100); + lcd_e_port_high(); + Delay_ns(500); + lcd_e_port_low(); + Delay_us(40); + lcd_db4_port_low(); + Delay_ns(100); + lcd_e_port_high(); + Delay_ns(500); + lcd_e_port_low(); + Delay_ns(500); +#if (LCD_DISPLAYS==1) + + if (LCD_DISPLAY_LINES > 1) { + lcd_db7_port_high(); + } + +#else + unsigned char c; + + switch (ActiveDisplay) { + case 1 : + c = LCD_DISPLAY_LINES; + break; + + case 2 : + c = LCD_DISPLAY2_LINES; + break; +#if (LCD_DISPLAYS>=3) + + case 3 : + c = LCD_DISPLAY3_LINES; + break; +#endif +#if (LCD_DISPLAYS==4) + + case 4 : + c = LCD_DISPLAY4_LINES; + break; +#endif + } + + if (c > 1) { + lcd_db7_port_high(); + } + +#endif + Delay_ns(100); + lcd_e_port_high(); + Delay_ns(500); + lcd_e_port_low(); + Delay_us(40); +#else +#if (LCD_DISPLAYS==1) + + if (LCD_DISPLAY_LINES < 2) { + lcd_db3_port_low(); + } + +#else + unsigned char c; + + switch (ActiveDisplay) { + case 1 : + c = LCD_DISPLAY_LINES; + break; + + case 2 : + c = LCD_DISPLAY2_LINES; + break; +#if (LCD_DISPLAYS>=3) + + case 3 : + c = LCD_DISPLAY3_LINES; + break; +#endif +#if (LCD_DISPLAYS==4) + + case 4 : + c = LCD_DISPLAY4_LINES; + break; +#endif + } + + if (c < 2) { + lcd_db3_port_low(); + } + +#endif + lcd_db2_port_low(); + Delay_ns(100); + lcd_e_port_high(); + Delay_ns(500); + lcd_e_port_low(); + Delay_us(40); +#endif + //Display Off + lcd_command(_BV(LCD_DISPLAYMODE)); + //Display Clear + lcd_clrscr(); + //Entry Mode Set + lcd_command(_BV(LCD_ENTRY_MODE) | _BV(LCD_ENTRY_INC)); + //Display On + lcd_command(_BV(LCD_DISPLAYMODE) | _BV(LCD_DISPLAYMODE_ON)); +} + +#if (LCD_DISPLAYS>1) +void lcd_use_display(int ADisplay) +{ + if (ADisplay >= 1 && ADisplay <= LCD_DISPLAYS) { + ActiveDisplay = ADisplay; + } +} +#endif + + +/************************************************************************* +Clear characters at position until length +Input: start position and lentgh +Returns: none +*************************************************************************/ +void lcd_clr(uint8_t pos, uint8_t len) +{ + for (int i = 0; i < len; i++) { + lcd_goto(pos + i); + lcd_putc(' '); + } +} + diff --git a/lib/hd44780_111/hd44780.h b/lib/hd44780_111/hd44780.h new file mode 100755 index 0000000..4e6cad1 --- /dev/null +++ b/lib/hd44780_111/hd44780.h @@ -0,0 +1,75 @@ +/***************************************************************************** +Title : HD44780 Library +Author : SA Development +Version: 1.11 +Modifications for: Arduino Mega 2560 + Itead Studio Arduino 1602 LED Keypad Shield +Modified by: Silver Kits October 2016 +*****************************************************************************/ +#ifndef HD44780_H +#define HD44780_H + + +//LCD Commands for HD44780 +#define LCD_CLR 0 // DB0: clear display + +#define LCD_HOME 1 // DB1: return to home position + +#define LCD_ENTRY_MODE 2 // DB2: set entry mode +#define LCD_ENTRY_INC 1 // DB1: 1=increment, 0=decrement +#define LCD_ENTRY_SHIFT 0 // DB0: 1=display shift on + +#define LCD_DISPLAYMODE 3 // DB3: turn lcd/cursor on +#define LCD_DISPLAYMODE_ON 2 // DB2: turn display on +#define LCD_DISPLAYMODE_CURSOR 1 // DB1: turn cursor on +#define LCD_DISPLAYMODE_BLINK 0 // DB0: blinking cursor + +#define LCD_MOVE 4 // DB4: move cursor/display +#define LCD_MOVE_DISP 3 // DB3: move display (0-> cursor) +#define LCD_MOVE_RIGHT 2 // DB2: move right (0-> left) + +#define LCD_FUNCTION 5 // DB5: function set +#define LCD_FUNCTION_8BIT 4 // DB4: set 8BIT mode (0->4BIT mode) +#define LCD_FUNCTION_2LINES 3 // DB3: two lines (0->one line) +#define LCD_FUNCTION_10DOTS 2 // DB2: 5x10 font (0->5x7 font) + +#define LCD_CGRAM 6 // DB6: set CG RAM address +#define LCD_DDRAM 7 // DB7: set DD RAM address + +#define LCD_BUSY 7 // DB7: LCD is busy + +// LCD columns and rows definitions +#define LCD_ROW_1_START 0 +#define LCD_ROW_2_START 64 +#define LCD_ROW_1_LAST_VISIBLE_COL 15 +#define LCD_ROW_1_LAST_COL 39 +#define LCD_ROW_2_LAST_VISIBLE_COL 79 +#define LCD_ROW_2_LAST_COL 103 +#define LCD_COLS_MAX 103 +#define LCD_VISIBLE_COLS 16 + +// Maximum character what can be displayed with 1 byte +#define LCD_MAX_CARACTER 255 + + +void lcd_init(); +void lcd_command(uint8_t cmd); + +void lcd_clrscr(); +void lcd_clr(uint8_t pos, uint8_t len); +void lcd_home(); +void lcd_goto(uint8_t pos); + +#if RW_LINE_IMPLEMENTED==1 +uint8_t lcd_getc(); +#endif + +void lcd_putc(char c); +void lcd_puts(const char *s); +void lcd_puts_P(const char *progmem_s); +#if (LCD_DISPLAYS>1) +void lcd_use_display(int ADisplay); +#endif + +#endif + diff --git a/lib/hd44780_111/hd44780.txt b/lib/hd44780_111/hd44780.txt new file mode 100755 index 0000000..8840ee9 --- /dev/null +++ b/lib/hd44780_111/hd44780.txt @@ -0,0 +1,155 @@ +Title : HD44780 Library +Author : SA Development +Version: 1.11 + +Parts of this code have been created or modified by Peter Fleury, Martin Thomas, and Andreas Heinzen as well. I went through it line by line and modified or improved it as necessary. This library has been cut down to only what was necessary to communicate with the LCD and does not include scrolling or wrapping features. See the libraries for the mentioned authors to get those features if you need them. + + +INSTALLATION: +------------- + +Three files are provided: + +hd44780.c - Main code file, you must add this to your project under "Source Files". + +hd44780.h - Main include file, you must include this in any files you wish to use the library. + +hd44780_settings_example.h - This is an example of the hd44780_settings.h file that the library requires (and will try to include). The settings that are intended to be customized for each project are located in this file. + +The advantage to this is that the main C/H files are unmodified and can be updated to a new version without losing custom per project settings. Another advantage is that since they are unmodified, you can put them in a shared or library directory and use them in multiple separate projects. Then you only have one place to update them instead of multiple project directories. + +Two ways you can implement this: + +Non-shared method: + +1. Copy these files into your project directory. +2. Rename "hd44780_settings_example.h" to "hd44780_settings.h". +3. Set the values appropriate to your project in "hd44780_settings.h". +4. Add the hd44780.c to your project. +5. Put "#include "hd44780.h" in any of your C files that need to use the functions. + + +Shared method: + +1. Create a shared directory. +2. Copy these files into this directory. + +To use it with a project: + +1. Copy "hd44780_settings_example.h" to your project directory as "hd44780_settings.h". NOTE THE "_example" was dropped from the filename. +2. Set the values appropriate to your project in "hd44780_settings.h". +3. Add the hd44780.c to your project. +4. Put "#include "..\shared\hd44780.h" in any of your C files that need to use the functions. You may have to modify this to point to your shared directory. +5. Project -> Configuration Options -> Include Directories -> New -> Add your project directory. It should put a ".\" in the list. This step is necessary because when the library tries to include "hd44780_settings.h", it will look in your project directory and grab the one customized for that particular project. This is why it is important NOT to have a hd44780_settings.h in your shared directory and why I have this file named hd44780_settings_example.h instead. You can leave the example file in the shared directory as a file to copy and rename when starting a new project. + +This library will work with my Advanced Delay Library as well by changing the USE_ADELAY_LIBRARY value from 0 to 1. By default it will use the __builtin_avr_delay_cycles function. My only gripe about this built in function is that if you are debugging at the assembly level it does not match C code lines to the assembly lines properly. Other than this it is exceptional. My Advanced Delay Library accomplishes the same thing while also adding additional delay functions that can expect a variable instead of a constant to be supplied and they don't suffer the C to assembly alignment bug that the built in ones do. + + +HOW TO USE: +----------- + +Supports LCD communications on as few as 6 pins or as many as 11 pins depending on configuration. + +The first choice you must make is whether you want to use 4 bit or 8 bit mode. Honestly this isn't a hard choice as I've tested both on my scope to see how the performance differed and both were very close to the same under all clock speeds I tested (16khz to 16mhz). I don't see the point in wasting 4 uC pins for 8 bit mode as it seems to have no advantage. Use the LCD_BITS parameter to set this: + +LCD_BITS=4 // 4 for 4 Bit I/O Mode +LCD_BITS=8 // 8 for 8 Bit I/O Mode + +The next choice is whether to implement a RW signal or not. If you don't need to read anything back from the LCD, then you can skip implementing it and simply connect the RW signal to ground. This is nice because it doesn't take up a uC pin this way. If however, you need to read something back from the LCD, you will need to implement RW. Use the RW_LINE_IMPLEMENTED parameter to set this: + +RW_LINE_IMPLEMENTED=0 //0 for no RW line (RW on LCD tied to ground) +RW_LINE_IMPLEMENTED=1 //1 for RW line present + +The last big decision is which WAIT_MODE to use. You can select between Delay Mode or Check Busy Mode. Delay Mode will delay after each LCD command to make sure that there is time for the LCD to execute the command before the next one can be issued. Check Busy Mode will read the check busy flag from the LCD to see if the LCD is still busy or ready for the next command. Check Busy Mode requires the RW line to be implemented, however you can implement an RW line (RW_LINE_IMPLEMENTED=1) and use Delay Mode (WAIT_MODE=0). You might think that the Check Busy Mode technique would be faster, but it is actually slower when running a clock below 10Mhz. This is because the extra code is takes to check it takes up more time that the Delay Mode would have. At 10Mhz or above, Check Busy Mode will be faster. At 16Mhz, it was 20% faster than Delay Mode, but at 8Mhz Delay Mode was 10% faster. Use the WAIT_MODE parameter to set this: + +WAIT_MODE=0 // 0=Use Delay Method (Faster if running <10Mhz) +WAIT_MODE=1 // 1=Use Check Busy Flag (Faster if running >10Mhz) ***Requires RW Line*** + + +This version implements multiple LCD display support for up to 4 devices. All devices will share their data/RS/RW(if implemented) pins. Each device will have its own E(enable) pin. You can use the command lcd_use_display(x) to choose which display commands will execute on. You will need to lcd_init() each one individually. This not only allows you to run 4 independent LCD display, but some displays like the 40 character x 4 line display are actually implemented with 2 lcd controllers. They will have an E and E2 pin so you will need this multiple display functionallity to use a display like this. + + +To init the display, clear the screen, and output "Hello World...": + lcd_init(); + lcd_clrscr(); + lcd_puts("Hello World..."); + +To put a character: + lcd_putc('A'); + +To turn off the display: + lcd_command(_BV(LCD_DISPLAYMODE)); + +To turn on the display: + lcd_command(_BV(LCD_DISPLAYMODE) | _BV(LCD_DISPLAYMODE_ON)); + +To turn on the display AND display an underline cursor: + lcd_command(_BV(LCD_DISPLAYMODE) | _BV(LCD_DISPLAYMODE_ON) | _BV(LCD_DISPLAYMODE_CURSOR)); + +To turn on the display AND display a blinking cursor: + lcd_command(_BV(LCD_DISPLAYMODE) | _BV(LCD_DISPLAYMODE_ON) | _BV(LCD_DISPLAYMODE_BLINK)); + +To move the cursor to the left: + lcd_command(_BV(LCD_MOVE)); + +To move the cursor to the right: + lcd_command(_BV(LCD_MOVE) | _BV(LCD_MOVE_RIGHT)); + +To move the cursor to a specific location: + lcd_goto(0x40); //0x40 is often the beginning of the second line + //each LCD display will have its memory mapped + //differently + +To create a custom character: + lcd_command(_BV(LCD_CGRAM)+0*8); //The 0 on this line may be 0-7 + lcd_putc(0b00000); //5x8 bitmap of character, in this example a backslash + lcd_putc(0b10000); + lcd_putc(0b01000); + lcd_putc(0b00100); + lcd_putc(0b00010); + lcd_putc(0b00001); + lcd_putc(0b00000); + lcd_putc(0b00000); + lcd_goto(0); //DO NOT FORGET to issue a GOTO command to go back to writing to the LCD + //ddram OR you will spend hours like me thinking the LCD is locked up + //when it working just fine and you are outputting to cgram instead of + //ddram! + +To display this custom character: + lcd_putc(0); //Displays custom character 0 + +To shift the display so that the characters on screen are pushed to the left: + lcd_command(_BV(LCD_MOVE) | _BV(LCD_MOVE_DISP)); + +To shift the display so that the characters on screen are pushed to the left: + lcd_command(_BV(LCD_MOVE) | _BV(LCD_MOVE_DISP) | _BV(LCD_MOVE_RIGHT)); + + +VERSION HISTORY: +---------------- + +1.00 - Initial version. + +1.02 - Delay_ns, Delay_us, and Delay_ms added via a new included file "delay.h". All of these functions support values from 1-65535 so you can delay 65.535 seconds using Delay_ms, or Delay_ns(1) to delay 1ns. Realize that a delay of 1ns would only be possible if you were running at 1ghz, but asking for 1ns delay will get you a single clock delay. At 8mhz this is 125ns. The delays will get you "at least" what you ask for with as little more as possible. The reason the delay functions were added is because the LCD library I based this on "assumed" that 2 clocks were enough for a 500ns wait. This is TRUE if you are running at less than 2mhz, but not true if you are running faster. I modified these functions to use the new Delay_ns function above so it will ALWAYS wait 500ns on the enable line now. + +1.03 - No longer includes my delay functions, but instead uses the internal builtin_avr_delay_cycles instead. You can still use it with my Advanced Delay Library, check the C file for info. This version also adds a clrscr in the init function. I was experiencing issues where a reset would corrupt part of the screen so this was necessary to make sure it starts clear. + +1.05 - Reorganized all code to follow the standard C and H file techniques. + +1.10 - Multiple LCD display support (Up to 4) added. + + Bugs in the read command and 8 bit modes fixed and tested. + + You are now able to put any pins on any pin and port. The data pins are no longer required to be on 0-3 or 0-7. This gives you full freedom to put these pins anywhere. + + All pin changes are now done through SBI CBI instructions meaning there will be zero problems with interrupts of other things occuring on pins of the same port as the LCD pins. + + Checkbusy used to end up in an infinite loop if the LCD didn't response with "not busy". I have put a 3ms maximum time on it (or 16 attempts minimum). Since all LCD commands should run with 1.64ms, this should be more than enough and will allow the processor to continue on instead of being permanently stuck. The delay however at 3ms everytime a call is made to the LCD will probably slow things down too much anyway, but I figured having this limit was better than nothing. + +1.11 - A big issue in the LCD init code has been corrected which will now allow 4-bit mode to work properly below 2mhz. I've tested both 4-bit and 8-bit modes from 16khz to 16mhz with no issues. + + Many commands have been marked as static if you don't need to access them, the only change is that lcd_read(x) is no longer available. You must use lcd_getc() instead. + + RW_LINE_IMPLEMENTED has been added which allows you to indicate whether you are implementing the RW line or not. This used to be part of the WAIT_MODE, but having this option now allows you to implement the RW line so you can read from the LCD, but still use WAIT_MODE=0 for delays instead of using the check busy flag. + + Check Busy has had an additional 6us delay added to it when the previous command involved a read or write that changes the address pointer. This is due to the check busy flag going low before this pointer is updated and is to ensure the LCD is ready for another command. diff --git a/lib/hd44780_111/hd44780_settings.h b/lib/hd44780_111/hd44780_settings.h new file mode 100755 index 0000000..af41641 --- /dev/null +++ b/lib/hd44780_111/hd44780_settings.h @@ -0,0 +1,38 @@ +/***************************************************************************** +Title : HD44780 Library +Author : SA Development +Version: 1.11 +Modifications for: Arduino Mega 2560 + Itead Studio Arduino 1602 LED Keypad Shield +Modified by: Silver Kits October 2016 +*****************************************************************************/ +#ifndef HD44780_SETTINGS_H +#define HD44780_SETTINGS_H + +#define USE_ADELAY_LIBRARY 0 +#define LCD_BITS 4 +#define RW_LINE_IMPLEMENTED 0 +#define WAIT_MODE 0 +#define DELAY_RESET 15 + +// Pin and port definitions for Arduino Mega 2560 +#define LCD_DB4_PORT PORTG +#define LCD_DB4_PIN 5 +#define LCD_DB5_PORT PORTE +#define LCD_DB5_PIN 3 +#define LCD_DB6_PORT PORTH +#define LCD_DB6_PIN 3 +#define LCD_DB7_PORT PORTH +#define LCD_DB7_PIN 4 + +#define LCD_RS_PORT PORTH +#define LCD_RS_PIN 5 + +#define LCD_DISPLAYS 1 + +#define LCD_DISPLAY_LINES 2 + +#define LCD_E_PORT PORTH +#define LCD_E_PIN 6 + +#endif /* HD44780_SETTINGS_H */ diff --git a/lib/hd44780_111/hd44780_settings_example.h b/lib/hd44780_111/hd44780_settings_example.h new file mode 100755 index 0000000..21cca52 --- /dev/null +++ b/lib/hd44780_111/hd44780_settings_example.h @@ -0,0 +1,66 @@ +#ifndef HD44780_SETTINGS_H +#define HD44780_SETTINGS_H + +#define F_CPU 8000000 // Set Clock Frequency + +#define USE_ADELAY_LIBRARY 0 // Set to 1 to use my ADELAY library, 0 to use internal delay functions +#define LCD_BITS 4 // 4 for 4 Bit I/O Mode, 8 for 8 Bit I/O Mode +#define RW_LINE_IMPLEMENTED 0 // 0 for no RW line (RW on LCD tied to ground), 1 for RW line present +#define WAIT_MODE 0 // 0=Use Delay Method (Faster if running <10Mhz) + // 1=Use Check Busy Flag (Faster if running >10Mhz) ***Requires RW Line*** +#define DELAY_RESET 15 // in mS + +#if (LCD_BITS==8) // If using 8 bit mode, you must configure DB0-DB7 + #define LCD_DB0_PORT PORTC + #define LCD_DB0_PIN 0 + #define LCD_DB1_PORT PORTC + #define LCD_DB1_PIN 1 + #define LCD_DB2_PORT PORTC + #define LCD_DB2_PIN 2 + #define LCD_DB3_PORT PORTC + #define LCD_DB3_PIN 3 +#endif +#define LCD_DB4_PORT PORTC // If using 4 bit omde, yo umust configure DB4-DB7 +#define LCD_DB4_PIN 4 +#define LCD_DB5_PORT PORTC +#define LCD_DB5_PIN 5 +#define LCD_DB6_PORT PORTC +#define LCD_DB6_PIN 6 +#define LCD_DB7_PORT PORTC +#define LCD_DB7_PIN 7 + +#define LCD_RS_PORT PORTC // Port for RS line +#define LCD_RS_PIN 4 // Pin for RS line + +#define LCD_RW_PORT PORTC // Port for RW line (ONLY used if RW_LINE_IMPLEMENTED=1) +#define LCD_RW_PIN 6 // Pin for RW line (ONLY used if RW_LINE_IMPLEMENTED=1) + +#define LCD_DISPLAYS 1 // Up to 4 LCD displays can be used at one time + // All pins are shared between displays except for the E + // pin which each display will have its own + + // Display 1 Settings - if you only have 1 display, YOU MUST SET THESE +#define LCD_DISPLAY_LINES 2 // Number of Lines, Only Used for Set I/O Mode Command +#define LCD_E_PORT PORTC // Port for E line +#define LCD_E_PIN 5 // Pin for E line + +#if (LCD_DISPLAYS>=2) // If you have 2 displays, set these and change LCD_DISPLAYS=2 + #define LCD_DISPLAY2_LINES 2 // Number of Lines, Only Used for Set I/O Mode Command + #define LCD_E2_PORT PORTC // Port for E line + #define LCD_E2_PIN 5 // Pin for E line +#endif + +#if (LCD_DISPLAYS>=3) // If you have 3 displays, set these and change LCD_DISPLAYS=3 + #define LCD_DISPLAY3_LINES 2 // Number of Lines, Only Used for Set I/O Mode Command + #define LCD_E3_PORT PORTC // Port for E line + #define LCD_E3_PIN 5 // Pin for E line +#endif + +#if (LCD_DISPLAYS>=4) // If you have 4 displays, set these and change LCD_DISPLAYS=4 + #define LCD_DISPLAY4_LINES 2 // Number of Lines, Only Used for Set I/O Mode Command + #define LCD_E4_PORT PORTC // Port for E line + #define LCD_E4_PIN 5 // Pin for E line +#endif + +#endif + diff --git a/tooling/package-and-deliver.sh b/tooling/package-and-deliver.sh deleted file mode 100755 index 0ed33d0..0000000 --- a/tooling/package-and-deliver.sh +++ /dev/null @@ -1,51 +0,0 @@ -#!/bin/bash - -echo "Test project build" - -make clean && make - -if [ $? -ne 0 ] ; then - echo "Build failed!" - exit 1 -else - echo "Build OK!" -fi - - -echo "Format code" -make format - -# Test if there are changed files that are not commited - -if [ -n "$(git status --porcelain)" ] ; then - echo "Uncommited files detected" - git status - exit 1 -else - echo "OK" -fi - - -echo "Currently set tags on this project" -git tag -echo -n "Are the required tags added? (Y/n)" -read ANSWER -if [ "$ANSWER" == "n" ]; then - echo "Please add required tags" - exit 1 -fi - - -echo "Packaging the project" - -make clean && make - -TEMP_DIR=$(mktemp -d) -cp bin/atmega2560-user-code.ihx $TEMP_DIR -make clean -git archive --format=tar.gz -o $TEMP_DIR/$(git describe --abbrev=6 --dirty --always --tags --long).tar.gz HEAD -mv $TEMP_DIR/* bin/ -rm -rf $TEMP_DIR - -echo "Project packaging succeeded" -exit 0