1
0
Fork 0
This commit is contained in:
Arti Zirk 2016-11-02 14:13:58 +02:00
commit 5d4b4a1e6d
14 changed files with 3116 additions and 57 deletions

View File

@ -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
size:
$(AVRSIZE) $(AVRSIZEARGS) $(ELF)
.PHONY: clean install format size

View File

@ -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.
<div class=pagebreak></div>
## 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)
<div class=pagebreak></div>
## 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 |

Binary file not shown.

After

Width:  |  Height:  |  Size: 119 KiB

View File

@ -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 |

View File

@ -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.

56
lib/andygock_avr-uart/README.md Executable file
View File

@ -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.

1466
lib/andygock_avr-uart/uart.c Executable file

File diff suppressed because it is too large Load Diff

415
lib/andygock_avr-uart/uart.h Executable file
View File

@ -0,0 +1,415 @@
#ifndef UART_H
#define UART_H
/************************************************************************
Title: Interrupt UART library with receive/transmit circular buffers
Author: Andy Gock
Software: AVR-GCC 4.1, AVR Libc 1.4
Hardware: any AVR with built-in UART, tested on AT90S8515 & ATmega8 at 4 Mhz
License: GNU General Public License
Usage: see Doxygen manual
Based on original library by Peter Fluery, Tim Sharpe, Nicholas Zambetti.
https://github.com/andygock/avr-uart
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.h - 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
************************************************************************/
/************************************************************************
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/12/2009 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!
************************************************************************/
/**
* @defgroup avr-uart UART Library
* @code #include <uart.h> @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 <andy@gock.net>
* @note Based on original library by Peter Fleury and Tim Sharpe.
*/
/**@{*/
#include <stdint.h>
#include <avr/io.h>
#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
* <br>no receive data available
* - \b UART_BUFFER_OVERFLOW
* <br>Receive ringbuffer overflow.
* We are not reading the receive buffer fast enough,
* one or more received character have been dropped
* - \b UART_OVERRUN_ERROR
* <br>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
* <br>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
* <br>no receive data available
* - \b UART_BUFFER_OVERFLOW
* <br>Receive ringbuffer overflow.
* We are not reading the receive buffer fast enough,
* one or more received character have been dropped
* - \b UART_OVERRUN_ERROR
* <br>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
* <br>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

772
lib/hd44780_111/hd44780.c Normal file
View File

@ -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 <silver.kits@eesti.ee> October 2016
*****************************************************************************/
#include <avr/pgmspace.h>
#include <avr/sfr_defs.h>
#include <inttypes.h>
#define __ASSERT_USE_STDERR
#include <assert.h>
#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(' ');
}
}

75
lib/hd44780_111/hd44780.h Executable file
View File

@ -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 <silver.kits@eesti.ee> 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

155
lib/hd44780_111/hd44780.txt Executable file
View File

@ -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.

View File

@ -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 <silver.kits@eesti.ee> 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 */

View File

@ -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

View File

@ -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