Fix hd44780 formatting and add modifications descriptions to files headers

This commit is contained in:
Silver Kits 2016-10-10 11:38:01 +03:00
parent 51951fcea1
commit 6dac14ab59
4 changed files with 951 additions and 907 deletions

550
lib/hd44780_111/hd44780.c Executable file → Normal file
View File

@ -2,8 +2,10 @@
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>
@ -14,44 +16,44 @@ Version: 1.11
#if (USE_ADELAY_LIBRARY==1)
#include "adelay.h"
#include "adelay.h"
#else
#define Delay_ns(__ns) \
#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) \
#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) \
#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) \
#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.
#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.
#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.
#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.
#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.
#error LCD_DISPLAYS is not defined or not valid.
#endif
// Constants/Macros
@ -61,7 +63,7 @@ Version: 1.11
//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)
#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)
@ -74,7 +76,7 @@ Version: 1.11
#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)
#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)
@ -87,7 +89,7 @@ Version: 1.11
#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();
#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();
@ -111,7 +113,7 @@ Version: 1.11
//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)
#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)
@ -124,7 +126,7 @@ Version: 1.11
#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)
#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)
@ -137,7 +139,7 @@ Version: 1.11
#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();
#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();
@ -149,103 +151,131 @@ Version: 1.11
#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;
static unsigned char PrevCmdInvolvedAddressCounter = 0;
#endif
#if (LCD_DISPLAYS>1)
static unsigned char ActiveDisplay=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);
#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);
#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);
#endif
#if (LCD_DISPLAYS==4)
case 4 :
LCD_E4_PORT &= ~_BV(LCD_E4_PIN);
break;
#endif
#endif
default :
#endif
LCD_E_PORT&=~_BV(LCD_E_PIN);
#if (LCD_DISPLAYS>1)
#endif
LCD_E_PORT &= ~_BV(LCD_E_PIN);
#if (LCD_DISPLAYS>1)
}
#endif
#endif
}
static inline void lcd_e_port_high()
{
#if (LCD_DISPLAYS>1)
switch (ActiveDisplay)
{
case 2 : LCD_E2_PORT|=_BV(LCD_E2_PIN);
#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);
#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);
#endif
#if (LCD_DISPLAYS==4)
case 4 :
LCD_E4_PORT |= _BV(LCD_E4_PIN);
break;
#endif
#endif
default :
#endif
LCD_E_PORT|=_BV(LCD_E_PIN);
#if (LCD_DISPLAYS>1)
#endif
LCD_E_PORT |= _BV(LCD_E_PIN);
#if (LCD_DISPLAYS>1)
}
#endif
#endif
}
static inline void lcd_e_ddr_low()
{
#if (LCD_DISPLAYS>1)
switch (ActiveDisplay)
{
case 2 : DDR(LCD_E2_PORT)&=~_BV(LCD_E2_PIN);
#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);
#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);
#endif
#if (LCD_DISPLAYS==4)
case 4 :
DDR(LCD_E4_PORT) &= ~_BV(LCD_E4_PIN);
break;
#endif
#endif
default :
#endif
DDR(LCD_E_PORT)&=~_BV(LCD_E_PIN);
#if (LCD_DISPLAYS>1)
#endif
DDR(LCD_E_PORT) &= ~_BV(LCD_E_PIN);
#if (LCD_DISPLAYS>1)
}
#endif
#endif
}
static inline void lcd_e_ddr_high()
{
#if (LCD_DISPLAYS>1)
switch (ActiveDisplay)
{
case 2 : DDR(LCD_E2_PORT)|=_BV(LCD_E2_PIN);
#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);
#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);
#endif
#if (LCD_DISPLAYS==4)
case 4 :
DDR(LCD_E4_PORT) |= _BV(LCD_E4_PIN);
break;
#endif
#endif
default :
#endif
DDR(LCD_E_PORT)|=_BV(LCD_E_PIN);
#if (LCD_DISPLAYS>1)
#endif
DDR(LCD_E_PORT) |= _BV(LCD_E_PIN);
#if (LCD_DISPLAYS>1)
}
#endif
#endif
}
@ -256,13 +286,16 @@ loops while lcd is busy, returns address counter
static uint8_t lcd_read(uint8_t rs);
static void lcd_waitbusy(void)
{
{
register uint8_t c;
unsigned int ul1=0;
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
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
@ -274,64 +307,56 @@ 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 (WAIT_MODE==1 && RW_LINE_IMPLEMENTED==1)
if (rs)
if (rs) {
lcd_waitbusy();
if (PrevCmdInvolvedAddressCounter)
{
}
if (PrevCmdInvolvedAddressCounter) {
Delay_us(5);
PrevCmdInvolvedAddressCounter=0;
PrevCmdInvolvedAddressCounter = 0;
}
#endif
if (rs)
{
#endif
if (rs) {
lcd_rs_port_high(); // RS=1: Read Data
#if (WAIT_MODE==1 && RW_LINE_IMPLEMENTED==1)
PrevCmdInvolvedAddressCounter=1;
#endif
#if (WAIT_MODE==1 && RW_LINE_IMPLEMENTED==1)
PrevCmdInvolvedAddressCounter = 1;
#endif
} else {
lcd_rs_port_low(); // RS=0: Read Busy Flag
}
else lcd_rs_port_low(); // RS=0: Read Busy Flag
lcd_rw_port_high(); // RW=1: Read Mode
#if LCD_BITS==4
#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 |
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 |
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
#else //using 8-Bit-Mode
lcd_db7_ddr_low(); // Configure Data Pins as Input
lcd_db6_ddr_low();
lcd_db5_ddr_low();
@ -340,17 +365,13 @@ static uint8_t lcd_read(uint8_t rs)
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 |
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();
@ -359,7 +380,6 @@ static uint8_t lcd_read(uint8_t rs)
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();
@ -368,22 +388,24 @@ static uint8_t lcd_read(uint8_t rs)
lcd_db2_port_high();
lcd_db1_port_high();
lcd_db0_port_high();
#endif
#endif
lcd_rw_port_low();
#if (WAIT_MODE==0 || RW_LINE_IMPLEMENTED==0)
#if (WAIT_MODE==0 || RW_LINE_IMPLEMENTED==0)
if (rs)
if (rs) {
Delay_us(40);
else Delay_us(1);
#endif
return data;
} else {
Delay_us(1);
}
#endif
return data;
}
uint8_t lcd_getc()
{
{
return lcd_read(1);
}
}
#endif
@ -394,75 +416,64 @@ Input: data byte to write to LCD
0: write instruction
Returns: none
*************************************************************************/
static void lcd_write(uint8_t data,uint8_t rs)
{
#if (WAIT_MODE==1 && RW_LINE_IMPLEMENTED==1)
static void lcd_write(uint8_t data, uint8_t rs)
{
#if (WAIT_MODE==1 && RW_LINE_IMPLEMENTED==1)
lcd_waitbusy();
if (PrevCmdInvolvedAddressCounter)
{
if (PrevCmdInvolvedAddressCounter) {
Delay_us(5);
PrevCmdInvolvedAddressCounter=0;
PrevCmdInvolvedAddressCounter = 0;
}
#endif
if (rs)
{
#endif
if (rs) {
lcd_rs_port_high(); // RS=1: Write Character
#if (WAIT_MODE==1 && RW_LINE_IMPLEMENTED==1)
PrevCmdInvolvedAddressCounter=1;
#endif
}
else
{
#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 (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));
#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));
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));
#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();
@ -471,24 +482,28 @@ static void lcd_write(uint8_t data,uint8_t rs)
lcd_db2_port_high();
lcd_db1_port_high();
lcd_db0_port_high();
#endif
#endif
#if (WAIT_MODE==0 || RW_LINE_IMPLEMENTED==0)
#if (WAIT_MODE==0 || RW_LINE_IMPLEMENTED==0)
if (!rs && data<=((1<<LCD_CLR) | (1<<LCD_HOME))) // Is command clrscr or home?
if (!rs &&
data <= ((1 << LCD_CLR) | (1 << LCD_HOME))) { // Is command clrscr or home?
Delay_us(1640);
else Delay_us(40);
#endif
} 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);
}
{
lcd_write(cmd, 0);
}
/*************************************************************************
Set cursor to specified position
@ -496,11 +511,11 @@ 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);
}
lcd_command((1 << LCD_DDRAM) + pos);
}
/*************************************************************************
@ -509,9 +524,9 @@ Input: none
Returns: none
*************************************************************************/
void lcd_clrscr()
{
lcd_command(1<<LCD_CLR);
}
{
lcd_command(1 << LCD_CLR);
}
/*************************************************************************
@ -520,9 +535,9 @@ Input: none
Returns: none
*************************************************************************/
void lcd_home()
{
lcd_command(1<<LCD_HOME);
}
{
lcd_command(1 << LCD_HOME);
}
/*************************************************************************
@ -531,9 +546,9 @@ Input: character to be displayed
Returns: none
*************************************************************************/
void lcd_putc(char c)
{
lcd_write(c,1);
}
{
lcd_write(c, 1);
}
/*************************************************************************
@ -542,12 +557,13 @@ Input: string to be displayed
Returns: none
*************************************************************************/
void lcd_puts(const char *s)
{
{
register char c;
while ((c=*s++))
while ((c = *s++)) {
lcd_putc(c);
}
}
/*************************************************************************
@ -556,12 +572,13 @@ 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++)))
while ((c = pgm_read_byte(progmem_s++))) {
lcd_putc(c);
}
}
/*************************************************************************
Initialize display
@ -569,46 +586,42 @@ 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
#if RW_LINE_IMPLEMENTED==1
lcd_rw_ddr_high();
#endif
#endif
lcd_db7_ddr_high();
lcd_db6_ddr_high();
lcd_db5_ddr_high();
lcd_db4_ddr_high();
#if LCD_BITS==8
#if LCD_BITS==8
lcd_db3_ddr_high();
lcd_db2_ddr_high();
lcd_db1_ddr_high();
lcd_db0_ddr_high();
#endif
#endif
//Set All Control Lines Low
lcd_e_port_low();
lcd_rs_port_low();
#if RW_LINE_IMPLEMENTED==1
#if RW_LINE_IMPLEMENTED==1
lcd_rw_port_low();
#endif
#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
#if LCD_BITS==8
lcd_db3_port_high();
lcd_db2_port_high();
lcd_db1_port_high();
lcd_db0_port_high();
#endif
#endif
//Startup Delay
Delay_ms(DELAY_RESET);
//Initialize Display
lcd_db7_port_low();
lcd_db6_port_low();
@ -616,110 +629,131 @@ void lcd_init()
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)
#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_DISPLAYS==1)
if (LCD_DISPLAY_LINES>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
#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
#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
#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;
{
if (ADisplay >= 1 && ADisplay <= LCD_DISPLAYS) {
ActiveDisplay = ADisplay;
}
}
#endif
@ -729,10 +763,10 @@ 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(' ');
}
}
}

View File

@ -2,8 +2,10 @@
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

View File

@ -1,3 +1,11 @@
/*****************************************************************************
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