1
0
Fork 0

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

246
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>
@ -159,92 +161,120 @@ static unsigned char ActiveDisplay=1;
static inline void lcd_e_port_low()
{
#if (LCD_DISPLAYS>1)
switch (ActiveDisplay)
{
case 2 : LCD_E2_PORT&=~_BV(LCD_E2_PIN);
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);
case 3 :
LCD_E3_PORT &= ~_BV(LCD_E3_PIN);
break;
#endif
#if (LCD_DISPLAYS==4)
case 4 : LCD_E4_PORT&=~_BV(LCD_E4_PIN);
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);
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);
case 3 :
LCD_E3_PORT |= _BV(LCD_E3_PIN);
break;
#endif
#if (LCD_DISPLAYS==4)
case 4 : LCD_E4_PORT|=_BV(LCD_E4_PIN);
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);
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);
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);
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);
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);
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);
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
}
@ -260,9 +290,12 @@ 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
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
@ -276,57 +309,49 @@ Returns: byte read from LCD controller
static uint8_t lcd_read(uint8_t rs)
{
uint8_t data;
#if (WAIT_MODE==1 && RW_LINE_IMPLEMENTED==1)
if (rs)
if (rs) {
lcd_waitbusy();
if (PrevCmdInvolvedAddressCounter)
{
}
if (PrevCmdInvolvedAddressCounter) {
Delay_us(5);
PrevCmdInvolvedAddressCounter = 0;
}
#endif
if (rs)
{
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
}
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();
@ -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 |
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();
@ -369,13 +389,15 @@ static uint8_t lcd_read(uint8_t rs)
lcd_db1_port_high();
lcd_db0_port_high();
#endif
lcd_rw_port_low();
#if (WAIT_MODE==0 || RW_LINE_IMPLEMENTED==0)
if (rs)
if (rs) {
Delay_us(40);
else Delay_us(1);
} else {
Delay_us(1);
}
#endif
return data;
}
@ -398,22 +420,20 @@ 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;
}
#endif
if (rs)
{
if (rs) {
lcd_rs_port_high(); // RS=1: Write Character
#if (WAIT_MODE==1 && RW_LINE_IMPLEMENTED==1)
PrevCmdInvolvedAddressCounter = 1;
#endif
}
else
{
} else {
lcd_rs_port_low(); // RS=0: Write Command
#if (WAIT_MODE==1 && RW_LINE_IMPLEMENTED==1)
PrevCmdInvolvedAddressCounter = 0;
@ -425,29 +445,22 @@ static void lcd_write(uint8_t data,uint8_t rs)
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));
@ -457,12 +470,10 @@ static void lcd_write(uint8_t data,uint8_t rs)
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();
@ -472,11 +483,15 @@ static void lcd_write(uint8_t data,uint8_t rs)
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?
if (!rs &&
data <= ((1 << LCD_CLR) | (1 << LCD_HOME))) { // Is command clrscr or home?
Delay_us(1640);
else Delay_us(40);
} else {
Delay_us(40);
}
#endif
}
@ -545,9 +560,10 @@ void lcd_puts(const char *s)
{
register char c;
while ((c=*s++))
while ((c = *s++)) {
lcd_putc(c);
}
}
/*************************************************************************
@ -559,9 +575,10 @@ 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
@ -586,14 +603,12 @@ void lcd_init()
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();
@ -605,10 +620,8 @@ void lcd_init()
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();
@ -616,21 +629,15 @@ 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)
lcd_db4_port_low();
@ -639,34 +646,48 @@ void lcd_init()
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)
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;
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;
case 3 :
c = LCD_DISPLAY3_LINES;
break;
#endif
#if (LCD_DISPLAYS==4)
case 4 : c=LCD_DISPLAY4_LINES; break;
case 4 :
c = LCD_DISPLAY4_LINES;
break;
#endif
}
if (c>1)
lcd_db7_port_high();
#endif
if (c > 1) {
lcd_db7_port_high();
}
#endif
Delay_ns(100);
lcd_e_port_high();
Delay_ns(500);
@ -674,25 +695,41 @@ void lcd_init()
Delay_us(40);
#else
#if (LCD_DISPLAYS==1)
if (LCD_DISPLAY_LINES<2)
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;
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;
case 3 :
c = LCD_DISPLAY3_LINES;
break;
#endif
#if (LCD_DISPLAYS==4)
case 4 : c=LCD_DISPLAY4_LINES; break;
case 4 :
c = LCD_DISPLAY4_LINES;
break;
#endif
}
if (c<2)
lcd_db3_port_low();
#endif
if (c < 2) {
lcd_db3_port_low();
}
#endif
lcd_db2_port_low();
Delay_ns(100);
lcd_e_port_high();
@ -700,16 +737,12 @@ void lcd_init()
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));
}
@ -717,9 +750,10 @@ void lcd_init()
#if (LCD_DISPLAYS>1)
void lcd_use_display(int ADisplay)
{
if (ADisplay>=1 && ADisplay<=LCD_DISPLAYS)
if (ADisplay >= 1 && ADisplay <= LCD_DISPLAYS) {
ActiveDisplay = ADisplay;
}
}
#endif

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