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 Title : HD44780 Library
Author : SA Development Author : SA Development
Version: 1.11 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/pgmspace.h>
#include <avr/sfr_defs.h> #include <avr/sfr_defs.h>
#include <inttypes.h> #include <inttypes.h>
@ -14,44 +16,44 @@ Version: 1.11
#if (USE_ADELAY_LIBRARY==1) #if (USE_ADELAY_LIBRARY==1)
#include "adelay.h" #include "adelay.h"
#else #else
#define Delay_ns(__ns) \ #define Delay_ns(__ns) \
if((unsigned long) (F_CPU/1000000000.0 * __ns) != F_CPU/1000000000.0 * __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);\ __builtin_avr_delay_cycles((unsigned long) ( F_CPU/1000000000.0 * __ns)+1);\
else __builtin_avr_delay_cycles((unsigned long) ( F_CPU/1000000000.0 * __ns)) 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)\ 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);\ __builtin_avr_delay_cycles((unsigned long) ( F_CPU/1000000.0 * __us)+1);\
else __builtin_avr_delay_cycles((unsigned long) ( F_CPU/1000000.0 * __us)) 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)\ 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);\ __builtin_avr_delay_cycles((unsigned long) ( F_CPU/1000.0 * __ms)+1);\
else __builtin_avr_delay_cycles((unsigned long) ( F_CPU/1000.0 * __ms)) 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)\ 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);\ __builtin_avr_delay_cycles((unsigned long) ( F_CPU/1.0 * __s)+1);\
else __builtin_avr_delay_cycles((unsigned long) ( F_CPU/1.0 * __s)) else __builtin_avr_delay_cycles((unsigned long) ( F_CPU/1.0 * __s))
#endif #endif
#if !defined(LCD_BITS) || (LCD_BITS!=4 && LCD_BITS!=8) #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 #endif
#if !defined(WAIT_MODE) || (WAIT_MODE!=0 && WAIT_MODE!=1) #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 #endif
#if !defined(RW_LINE_IMPLEMENTED) || (RW_LINE_IMPLEMENTED!=0 && RW_LINE_IMPLEMENTED!=1) #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 #endif
#if (WAIT_MODE==1 && RW_LINE_IMPLEMENTED!=1) #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 #endif
#if !defined(LCD_DISPLAYS) || (LCD_DISPLAYS<1) || (LCD_DISPLAYS>4) #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 #endif
// Constants/Macros // Constants/Macros
@ -61,7 +63,7 @@ Version: 1.11
//PORT defines //PORT defines
#define lcd_rs_port_low() LCD_RS_PORT&=~_BV(LCD_RS_PIN) #define lcd_rs_port_low() LCD_RS_PORT&=~_BV(LCD_RS_PIN)
#if RW_LINE_IMPLEMENTED==1 #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 #endif
#define lcd_db0_port_low() LCD_DB0_PORT&=~_BV(LCD_DB0_PIN) #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_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) #define lcd_rs_port_high() LCD_RS_PORT|=_BV(LCD_RS_PIN)
#if RW_LINE_IMPLEMENTED==1 #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 #endif
#define lcd_db0_port_high() LCD_DB0_PORT|=_BV(LCD_DB0_PIN) #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_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(); #define lcd_rs_port_set(value) if (value) lcd_rs_port_high(); else lcd_rs_port_low();
#if RW_LINE_IMPLEMENTED==1 #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 #endif
#define lcd_db0_port_set(value) if (value) lcd_db0_port_high(); else lcd_db0_port_low(); #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_db1_port_set(value) if (value) lcd_db1_port_high(); else lcd_db1_port_low();
@ -111,7 +113,7 @@ Version: 1.11
//DDR defines //DDR defines
#define lcd_rs_ddr_low() DDR(LCD_RS_PORT)&=~_BV(LCD_RS_PIN) #define lcd_rs_ddr_low() DDR(LCD_RS_PORT)&=~_BV(LCD_RS_PIN)
#if RW_LINE_IMPLEMENTED==1 #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 #endif
#define lcd_db0_ddr_low() DDR(LCD_DB0_PORT)&=~_BV(LCD_DB0_PIN) #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_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) #define lcd_rs_ddr_high() DDR(LCD_RS_PORT)|=_BV(LCD_RS_PIN)
#if RW_LINE_IMPLEMENTED==1 #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 #endif
#define lcd_db0_ddr_high() DDR(LCD_DB0_PORT)|=_BV(LCD_DB0_PIN) #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_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(); #define lcd_rs_ddr_set(value) if (value) lcd_rs_ddr_high(); else lcd_rs_ddr_low();
#if RW_LINE_IMPLEMENTED==1 #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 #endif
#define lcd_db0_ddr_set(value) if (value) lcd_db0_ddr_high(); else lcd_db0_ddr_low(); #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_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(); #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) #if (WAIT_MODE==1 && RW_LINE_IMPLEMENTED==1)
static unsigned char PrevCmdInvolvedAddressCounter=0; static unsigned char PrevCmdInvolvedAddressCounter = 0;
#endif #endif
#if (LCD_DISPLAYS>1) #if (LCD_DISPLAYS>1)
static unsigned char ActiveDisplay=1; static unsigned char ActiveDisplay = 1;
#endif #endif
static inline void lcd_e_port_low() static inline void lcd_e_port_low()
{ {
#if (LCD_DISPLAYS>1) #if (LCD_DISPLAYS>1)
switch (ActiveDisplay)
{ switch (ActiveDisplay) {
case 2 : LCD_E2_PORT&=~_BV(LCD_E2_PIN); case 2 :
LCD_E2_PORT &= ~_BV(LCD_E2_PIN);
break; break;
#if (LCD_DISPLAYS>=3) #if (LCD_DISPLAYS>=3)
case 3 : LCD_E3_PORT&=~_BV(LCD_E3_PIN);
case 3 :
LCD_E3_PORT &= ~_BV(LCD_E3_PIN);
break; break;
#endif #endif
#if (LCD_DISPLAYS==4) #if (LCD_DISPLAYS==4)
case 4 : LCD_E4_PORT&=~_BV(LCD_E4_PIN);
case 4 :
LCD_E4_PORT &= ~_BV(LCD_E4_PIN);
break; break;
#endif #endif
default : default :
#endif #endif
LCD_E_PORT&=~_BV(LCD_E_PIN); LCD_E_PORT &= ~_BV(LCD_E_PIN);
#if (LCD_DISPLAYS>1) #if (LCD_DISPLAYS>1)
} }
#endif
#endif
} }
static inline void lcd_e_port_high() static inline void lcd_e_port_high()
{ {
#if (LCD_DISPLAYS>1) #if (LCD_DISPLAYS>1)
switch (ActiveDisplay)
{ switch (ActiveDisplay) {
case 2 : LCD_E2_PORT|=_BV(LCD_E2_PIN); case 2 :
LCD_E2_PORT |= _BV(LCD_E2_PIN);
break; break;
#if (LCD_DISPLAYS>=3) #if (LCD_DISPLAYS>=3)
case 3 : LCD_E3_PORT|=_BV(LCD_E3_PIN);
case 3 :
LCD_E3_PORT |= _BV(LCD_E3_PIN);
break; break;
#endif #endif
#if (LCD_DISPLAYS==4) #if (LCD_DISPLAYS==4)
case 4 : LCD_E4_PORT|=_BV(LCD_E4_PIN);
case 4 :
LCD_E4_PORT |= _BV(LCD_E4_PIN);
break; break;
#endif #endif
default : default :
#endif #endif
LCD_E_PORT|=_BV(LCD_E_PIN); LCD_E_PORT |= _BV(LCD_E_PIN);
#if (LCD_DISPLAYS>1) #if (LCD_DISPLAYS>1)
} }
#endif
#endif
} }
static inline void lcd_e_ddr_low() static inline void lcd_e_ddr_low()
{ {
#if (LCD_DISPLAYS>1) #if (LCD_DISPLAYS>1)
switch (ActiveDisplay)
{ switch (ActiveDisplay) {
case 2 : DDR(LCD_E2_PORT)&=~_BV(LCD_E2_PIN); case 2 :
DDR(LCD_E2_PORT) &= ~_BV(LCD_E2_PIN);
break; break;
#if (LCD_DISPLAYS>=3) #if (LCD_DISPLAYS>=3)
case 3 : DDR(LCD_E3_PORT)&=~_BV(LCD_E3_PIN);
case 3 :
DDR(LCD_E3_PORT) &= ~_BV(LCD_E3_PIN);
break; break;
#endif #endif
#if (LCD_DISPLAYS==4) #if (LCD_DISPLAYS==4)
case 4 : DDR(LCD_E4_PORT)&=~_BV(LCD_E4_PIN);
case 4 :
DDR(LCD_E4_PORT) &= ~_BV(LCD_E4_PIN);
break; break;
#endif #endif
default : default :
#endif #endif
DDR(LCD_E_PORT)&=~_BV(LCD_E_PIN); DDR(LCD_E_PORT) &= ~_BV(LCD_E_PIN);
#if (LCD_DISPLAYS>1) #if (LCD_DISPLAYS>1)
} }
#endif
#endif
} }
static inline void lcd_e_ddr_high() static inline void lcd_e_ddr_high()
{ {
#if (LCD_DISPLAYS>1) #if (LCD_DISPLAYS>1)
switch (ActiveDisplay)
{ switch (ActiveDisplay) {
case 2 : DDR(LCD_E2_PORT)|=_BV(LCD_E2_PIN); case 2 :
DDR(LCD_E2_PORT) |= _BV(LCD_E2_PIN);
break; break;
#if (LCD_DISPLAYS>=3) #if (LCD_DISPLAYS>=3)
case 3 : DDR(LCD_E3_PORT)|=_BV(LCD_E3_PIN);
case 3 :
DDR(LCD_E3_PORT) |= _BV(LCD_E3_PIN);
break; break;
#endif #endif
#if (LCD_DISPLAYS==4) #if (LCD_DISPLAYS==4)
case 4 : DDR(LCD_E4_PORT)|=_BV(LCD_E4_PIN);
case 4 :
DDR(LCD_E4_PORT) |= _BV(LCD_E4_PIN);
break; break;
#endif #endif
default : default :
#endif #endif
DDR(LCD_E_PORT)|=_BV(LCD_E_PIN); DDR(LCD_E_PORT) |= _BV(LCD_E_PIN);
#if (LCD_DISPLAYS>1) #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 uint8_t lcd_read(uint8_t rs);
static void lcd_waitbusy(void) static void lcd_waitbusy(void)
{ {
register uint8_t c; 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++; ul1++;
} }
}
#endif #endif
@ -274,64 +307,56 @@ Returns: byte read from LCD controller
*************************************************************************/ *************************************************************************/
#if RW_LINE_IMPLEMENTED==1 #if RW_LINE_IMPLEMENTED==1
static uint8_t lcd_read(uint8_t rs) static uint8_t lcd_read(uint8_t rs)
{ {
uint8_t data; 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(); lcd_waitbusy();
if (PrevCmdInvolvedAddressCounter) }
{
if (PrevCmdInvolvedAddressCounter) {
Delay_us(5); Delay_us(5);
PrevCmdInvolvedAddressCounter=0; PrevCmdInvolvedAddressCounter = 0;
} }
#endif
if (rs) #endif
{
if (rs) {
lcd_rs_port_high(); // RS=1: Read Data lcd_rs_port_high(); // RS=1: Read Data
#if (WAIT_MODE==1 && RW_LINE_IMPLEMENTED==1) #if (WAIT_MODE==1 && RW_LINE_IMPLEMENTED==1)
PrevCmdInvolvedAddressCounter=1; PrevCmdInvolvedAddressCounter = 1;
#endif #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 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_db7_ddr_low(); // Configure Data Pins as Input
lcd_db6_ddr_low(); lcd_db6_ddr_low();
lcd_db5_ddr_low(); lcd_db5_ddr_low();
lcd_db4_ddr_low(); lcd_db4_ddr_low();
lcd_e_port_high(); // Read High Nibble First lcd_e_port_high(); // Read High Nibble First
Delay_ns(500); 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_db6_pin_get() << 6 | lcd_db7_pin_get() << 7;
lcd_e_port_low(); lcd_e_port_low();
Delay_ns(500); Delay_ns(500);
lcd_e_port_high(); // Read Low Nibble lcd_e_port_high(); // Read Low Nibble
Delay_ns(500); 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_db6_pin_get() << 2 | lcd_db7_pin_get() << 3;
lcd_e_port_low(); lcd_e_port_low();
lcd_db7_ddr_high(); // Configure Data Pins as Output lcd_db7_ddr_high(); // Configure Data Pins as Output
lcd_db6_ddr_high(); lcd_db6_ddr_high();
lcd_db5_ddr_high(); lcd_db5_ddr_high();
lcd_db4_ddr_high(); lcd_db4_ddr_high();
lcd_db7_port_high(); // Pins High (Inactive) lcd_db7_port_high(); // Pins High (Inactive)
lcd_db6_port_high(); lcd_db6_port_high();
lcd_db5_port_high(); lcd_db5_port_high();
lcd_db4_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_db7_ddr_low(); // Configure Data Pins as Input
lcd_db6_ddr_low(); lcd_db6_ddr_low();
lcd_db5_ddr_low(); lcd_db5_ddr_low();
@ -340,17 +365,13 @@ static uint8_t lcd_read(uint8_t rs)
lcd_db2_ddr_low(); lcd_db2_ddr_low();
lcd_db1_ddr_low(); lcd_db1_ddr_low();
lcd_db0_ddr_low(); lcd_db0_ddr_low();
lcd_e_port_high(); lcd_e_port_high();
Delay_ns(500); 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_db5_pin_get() << 5 | lcd_db4_pin_get() << 4 |
lcd_db3_pin_get() << 3 | lcd_db2_pin_get() << 2 | lcd_db3_pin_get() << 3 | lcd_db2_pin_get() << 2 |
lcd_db1_pin_get() << 1 | lcd_db0_pin_get(); lcd_db1_pin_get() << 1 | lcd_db0_pin_get();
lcd_e_port_low(); lcd_e_port_low();
lcd_db7_ddr_high(); // Configure Data Pins as Output lcd_db7_ddr_high(); // Configure Data Pins as Output
lcd_db6_ddr_high(); lcd_db6_ddr_high();
lcd_db5_ddr_high(); lcd_db5_ddr_high();
@ -359,7 +380,6 @@ static uint8_t lcd_read(uint8_t rs)
lcd_db2_ddr_high(); lcd_db2_ddr_high();
lcd_db1_ddr_high(); lcd_db1_ddr_high();
lcd_db0_ddr_high(); lcd_db0_ddr_high();
lcd_db7_port_high(); // Pins High (Inactive) lcd_db7_port_high(); // Pins High (Inactive)
lcd_db6_port_high(); lcd_db6_port_high();
lcd_db5_port_high(); lcd_db5_port_high();
@ -368,22 +388,24 @@ static uint8_t lcd_read(uint8_t rs)
lcd_db2_port_high(); lcd_db2_port_high();
lcd_db1_port_high(); lcd_db1_port_high();
lcd_db0_port_high(); lcd_db0_port_high();
#endif #endif
lcd_rw_port_low(); 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); Delay_us(40);
else Delay_us(1); } else {
#endif Delay_us(1);
return data;
} }
#endif
return data;
}
uint8_t lcd_getc() uint8_t lcd_getc()
{ {
return lcd_read(1); return lcd_read(1);
} }
#endif #endif
@ -394,75 +416,64 @@ Input: data byte to write to LCD
0: write instruction 0: write instruction
Returns: none Returns: none
*************************************************************************/ *************************************************************************/
static void lcd_write(uint8_t data,uint8_t rs) static void lcd_write(uint8_t data, uint8_t rs)
{ {
#if (WAIT_MODE==1 && RW_LINE_IMPLEMENTED==1) #if (WAIT_MODE==1 && RW_LINE_IMPLEMENTED==1)
lcd_waitbusy(); lcd_waitbusy();
if (PrevCmdInvolvedAddressCounter)
{ if (PrevCmdInvolvedAddressCounter) {
Delay_us(5); Delay_us(5);
PrevCmdInvolvedAddressCounter=0; PrevCmdInvolvedAddressCounter = 0;
} }
#endif
if (rs) #endif
{
if (rs) {
lcd_rs_port_high(); // RS=1: Write Character lcd_rs_port_high(); // RS=1: Write Character
#if (WAIT_MODE==1 && RW_LINE_IMPLEMENTED==1) #if (WAIT_MODE==1 && RW_LINE_IMPLEMENTED==1)
PrevCmdInvolvedAddressCounter=1; PrevCmdInvolvedAddressCounter = 1;
#endif #endif
} } else {
else
{
lcd_rs_port_low(); // RS=0: Write Command lcd_rs_port_low(); // RS=0: Write Command
#if (WAIT_MODE==1 && RW_LINE_IMPLEMENTED==1) #if (WAIT_MODE==1 && RW_LINE_IMPLEMENTED==1)
PrevCmdInvolvedAddressCounter=0; PrevCmdInvolvedAddressCounter = 0;
#endif #endif
} }
#if LCD_BITS==4 #if LCD_BITS==4
lcd_db7_port_set(data&_BV(7)); //Output High Nibble lcd_db7_port_set(data & _BV(7)); //Output High Nibble
lcd_db6_port_set(data&_BV(6)); lcd_db6_port_set(data & _BV(6));
lcd_db5_port_set(data&_BV(5)); lcd_db5_port_set(data & _BV(5));
lcd_db4_port_set(data&_BV(4)); lcd_db4_port_set(data & _BV(4));
Delay_ns(100); Delay_ns(100);
lcd_e_port_high(); lcd_e_port_high();
Delay_ns(500); Delay_ns(500);
lcd_e_port_low(); lcd_e_port_low();
lcd_db7_port_set(data & _BV(3)); //Output High Nibble
lcd_db7_port_set(data&_BV(3)); //Output High Nibble lcd_db6_port_set(data & _BV(2));
lcd_db6_port_set(data&_BV(2)); lcd_db5_port_set(data & _BV(1));
lcd_db5_port_set(data&_BV(1)); lcd_db4_port_set(data & _BV(0));
lcd_db4_port_set(data&_BV(0));
Delay_ns(100); Delay_ns(100);
lcd_e_port_high(); lcd_e_port_high();
Delay_ns(500); Delay_ns(500);
lcd_e_port_low(); lcd_e_port_low();
lcd_db7_port_high(); // All Data Pins High (Inactive) lcd_db7_port_high(); // All Data Pins High (Inactive)
lcd_db6_port_high(); lcd_db6_port_high();
lcd_db5_port_high(); lcd_db5_port_high();
lcd_db4_port_high(); lcd_db4_port_high();
#else //using 8-Bit_Mode
#else //using 8-Bit_Mode lcd_db7_port_set(data & _BV(7)); //Output High Nibble
lcd_db7_port_set(data&_BV(7)); //Output High Nibble lcd_db6_port_set(data & _BV(6));
lcd_db6_port_set(data&_BV(6)); lcd_db5_port_set(data & _BV(5));
lcd_db5_port_set(data&_BV(5)); lcd_db4_port_set(data & _BV(4));
lcd_db4_port_set(data&_BV(4)); lcd_db3_port_set(data & _BV(3)); //Output High Nibble
lcd_db3_port_set(data&_BV(3)); //Output High Nibble lcd_db2_port_set(data & _BV(2));
lcd_db2_port_set(data&_BV(2)); lcd_db1_port_set(data & _BV(1));
lcd_db1_port_set(data&_BV(1)); lcd_db0_port_set(data & _BV(0));
lcd_db0_port_set(data&_BV(0));
Delay_ns(100); Delay_ns(100);
lcd_e_port_high(); lcd_e_port_high();
Delay_ns(500); Delay_ns(500);
lcd_e_port_low(); lcd_e_port_low();
lcd_db7_port_high(); // All Data Pins High (Inactive) lcd_db7_port_high(); // All Data Pins High (Inactive)
lcd_db6_port_high(); lcd_db6_port_high();
lcd_db5_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_db2_port_high();
lcd_db1_port_high(); lcd_db1_port_high();
lcd_db0_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 &&
if (!rs && data<=((1<<LCD_CLR) | (1<<LCD_HOME))) // Is command clrscr or home? data <= ((1 << LCD_CLR) | (1 << LCD_HOME))) { // Is command clrscr or home?
Delay_us(1640); Delay_us(1640);
else Delay_us(40); } else {
#endif Delay_us(40);
} }
#endif
}
/************************************************************************* /*************************************************************************
Send LCD controller instruction command Send LCD controller instruction command
Input: instruction to send to LCD controller, see HD44780 data sheet Input: instruction to send to LCD controller, see HD44780 data sheet
Returns: none Returns: none
*************************************************************************/ *************************************************************************/
void lcd_command(uint8_t cmd) void lcd_command(uint8_t cmd)
{ {
lcd_write(cmd,0); lcd_write(cmd, 0);
} }
/************************************************************************* /*************************************************************************
Set cursor to specified position Set cursor to specified position
@ -496,11 +511,11 @@ Input: pos position
Returns: none Returns: none
*************************************************************************/ *************************************************************************/
void lcd_goto(uint8_t pos) void lcd_goto(uint8_t pos)
{ {
//Do not go outside of screen limits //Do not go outside of screen limits
assert(pos < LCD_COLS_MAX); 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 Returns: none
*************************************************************************/ *************************************************************************/
void lcd_clrscr() void lcd_clrscr()
{ {
lcd_command(1<<LCD_CLR); lcd_command(1 << LCD_CLR);
} }
/************************************************************************* /*************************************************************************
@ -520,9 +535,9 @@ Input: none
Returns: none Returns: none
*************************************************************************/ *************************************************************************/
void lcd_home() void lcd_home()
{ {
lcd_command(1<<LCD_HOME); lcd_command(1 << LCD_HOME);
} }
/************************************************************************* /*************************************************************************
@ -531,9 +546,9 @@ Input: character to be displayed
Returns: none Returns: none
*************************************************************************/ *************************************************************************/
void lcd_putc(char c) void lcd_putc(char c)
{ {
lcd_write(c,1); lcd_write(c, 1);
} }
/************************************************************************* /*************************************************************************
@ -542,12 +557,13 @@ Input: string to be displayed
Returns: none Returns: none
*************************************************************************/ *************************************************************************/
void lcd_puts(const char *s) void lcd_puts(const char *s)
{ {
register char c; register char c;
while ((c=*s++)) while ((c = *s++)) {
lcd_putc(c); lcd_putc(c);
} }
}
/************************************************************************* /*************************************************************************
@ -556,12 +572,13 @@ Input: string to be displayed
Returns: none Returns: none
*************************************************************************/ *************************************************************************/
void lcd_puts_P(const char *progmem_s) void lcd_puts_P(const char *progmem_s)
{ {
register char c; register char c;
while ((c=pgm_read_byte(progmem_s++))) while ((c = pgm_read_byte(progmem_s++))) {
lcd_putc(c); lcd_putc(c);
} }
}
/************************************************************************* /*************************************************************************
Initialize display Initialize display
@ -569,46 +586,42 @@ Input: none
Returns: none Returns: none
*************************************************************************/ *************************************************************************/
void lcd_init() void lcd_init()
{ {
//Set All Pins as Output //Set All Pins as Output
lcd_e_ddr_high(); lcd_e_ddr_high();
lcd_rs_ddr_high(); lcd_rs_ddr_high();
#if RW_LINE_IMPLEMENTED==1 #if RW_LINE_IMPLEMENTED==1
lcd_rw_ddr_high(); lcd_rw_ddr_high();
#endif #endif
lcd_db7_ddr_high(); lcd_db7_ddr_high();
lcd_db6_ddr_high(); lcd_db6_ddr_high();
lcd_db5_ddr_high(); lcd_db5_ddr_high();
lcd_db4_ddr_high(); lcd_db4_ddr_high();
#if LCD_BITS==8 #if LCD_BITS==8
lcd_db3_ddr_high(); lcd_db3_ddr_high();
lcd_db2_ddr_high(); lcd_db2_ddr_high();
lcd_db1_ddr_high(); lcd_db1_ddr_high();
lcd_db0_ddr_high(); lcd_db0_ddr_high();
#endif #endif
//Set All Control Lines Low //Set All Control Lines Low
lcd_e_port_low(); lcd_e_port_low();
lcd_rs_port_low(); lcd_rs_port_low();
#if RW_LINE_IMPLEMENTED==1 #if RW_LINE_IMPLEMENTED==1
lcd_rw_port_low(); lcd_rw_port_low();
#endif #endif
//Set All Data Lines High //Set All Data Lines High
lcd_db7_port_high(); lcd_db7_port_high();
lcd_db6_port_high(); lcd_db6_port_high();
lcd_db5_port_high(); lcd_db5_port_high();
lcd_db4_port_high(); lcd_db4_port_high();
#if LCD_BITS==8 #if LCD_BITS==8
lcd_db3_port_high(); lcd_db3_port_high();
lcd_db2_port_high(); lcd_db2_port_high();
lcd_db1_port_high(); lcd_db1_port_high();
lcd_db0_port_high(); lcd_db0_port_high();
#endif #endif
//Startup Delay //Startup Delay
Delay_ms(DELAY_RESET); Delay_ms(DELAY_RESET);
//Initialize Display //Initialize Display
lcd_db7_port_low(); lcd_db7_port_low();
lcd_db6_port_low(); lcd_db6_port_low();
@ -616,110 +629,131 @@ void lcd_init()
lcd_e_port_high(); lcd_e_port_high();
Delay_ns(500); Delay_ns(500);
lcd_e_port_low(); lcd_e_port_low();
Delay_us(4100); Delay_us(4100);
lcd_e_port_high(); lcd_e_port_high();
Delay_ns(500); Delay_ns(500);
lcd_e_port_low(); lcd_e_port_low();
Delay_us(100); Delay_us(100);
lcd_e_port_high(); lcd_e_port_high();
Delay_ns(500); Delay_ns(500);
lcd_e_port_low(); lcd_e_port_low();
Delay_us(40); Delay_us(40);
//Init differs between 4-bit and 8-bit from here //Init differs between 4-bit and 8-bit from here
#if (LCD_BITS==4) #if (LCD_BITS==4)
lcd_db4_port_low(); lcd_db4_port_low();
Delay_ns(100); Delay_ns(100);
lcd_e_port_high(); lcd_e_port_high();
Delay_ns(500); Delay_ns(500);
lcd_e_port_low(); lcd_e_port_low();
Delay_us(40); Delay_us(40);
lcd_db4_port_low(); lcd_db4_port_low();
Delay_ns(100); Delay_ns(100);
lcd_e_port_high(); lcd_e_port_high();
Delay_ns(500); Delay_ns(500);
lcd_e_port_low(); lcd_e_port_low();
Delay_ns(500); 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(); 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); Delay_ns(100);
lcd_e_port_high(); lcd_e_port_high();
Delay_ns(500); Delay_ns(500);
lcd_e_port_low(); lcd_e_port_low();
Delay_us(40); Delay_us(40);
#else #else
#if (LCD_DISPLAYS==1) #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
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(); lcd_db2_port_low();
Delay_ns(100); Delay_ns(100);
lcd_e_port_high(); lcd_e_port_high();
Delay_ns(500); Delay_ns(500);
lcd_e_port_low(); lcd_e_port_low();
Delay_us(40); Delay_us(40);
#endif #endif
//Display Off //Display Off
lcd_command(_BV(LCD_DISPLAYMODE)); lcd_command(_BV(LCD_DISPLAYMODE));
//Display Clear //Display Clear
lcd_clrscr(); lcd_clrscr();
//Entry Mode Set //Entry Mode Set
lcd_command(_BV(LCD_ENTRY_MODE) | _BV(LCD_ENTRY_INC)); lcd_command(_BV(LCD_ENTRY_MODE) | _BV(LCD_ENTRY_INC));
//Display On //Display On
lcd_command(_BV(LCD_DISPLAYMODE) | _BV(LCD_DISPLAYMODE_ON)); lcd_command(_BV(LCD_DISPLAYMODE) | _BV(LCD_DISPLAYMODE_ON));
} }
#if (LCD_DISPLAYS>1) #if (LCD_DISPLAYS>1)
void lcd_use_display(int ADisplay) void lcd_use_display(int ADisplay)
{ {
if (ADisplay>=1 && ADisplay<=LCD_DISPLAYS) if (ADisplay >= 1 && ADisplay <= LCD_DISPLAYS) {
ActiveDisplay=ADisplay; ActiveDisplay = ADisplay;
} }
}
#endif #endif
@ -729,10 +763,10 @@ Input: start position and lentgh
Returns: none Returns: none
*************************************************************************/ *************************************************************************/
void lcd_clr(uint8_t pos, uint8_t len) void lcd_clr(uint8_t pos, uint8_t len)
{ {
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
lcd_goto(pos + i); lcd_goto(pos + i);
lcd_putc(' '); lcd_putc(' ');
} }
} }

View File

@ -2,8 +2,10 @@
Title : HD44780 Library Title : HD44780 Library
Author : SA Development Author : SA Development
Version: 1.11 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 #ifndef HD44780_H
#define 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 #ifndef HD44780_SETTINGS_H
#define HD44780_SETTINGS_H #define HD44780_SETTINGS_H