1
0
Kaynağa Gözat

LCD library is working

kbere 2 yıl önce
ebeveyn
işleme
5ab73c648c
6 değiştirilmiş dosya ile 99 ekleme ve 79 silme
  1. 65 65
      soft/HD44780.c
  2. 9 12
      soft/HD44780.h
  3. 1 1
      soft/I2C.c
  4. 1 1
      soft/I2C.h
  5. 18 0
      soft/expander.c
  6. 5 0
      soft/expander.h

+ 65 - 65
soft/HD44780.c

@@ -1,13 +1,14 @@
-//-------------------------------------------------------------------------------------------------
-// Wyœwietlacz alfanumeryczny ze sterownikiem HD44780
-// Sterowanie w trybie 8-bitowym z odczytem flagi zajêtoœci
-// Plik : HD44780.c	
-// Mikrokontroler : Atmel AVR
-// Kompilator : avr-gcc
-// Autor : Rados³aw Kwiecieñ
-// �ród³o : http://radzio.dxp.pl/hd44780/
-// Data : 24.03.2007
-//-------------------------------------------------------------------------------------------------
+/*-------------------------------------------------------------------------------------------------
+ * Wyœwietlacz alfanumeryczny ze sterownikiem HD44780
+ * Sterowanie w trybie 8-bitowym z odczytem flagi zajêtoœci
+ * Plik : HD44780.c	
+ * Mikrokontroler : Atmel AVR
+ * Kompilator : avr-gcc
+ * Autor : Rados³aw Kwiecieñ
+ * �ród³o : http://radzio.dxp.pl/hd44780/
+ * Data : 24.03.2007
+ * Modified: k4be, 2022 (I2C access mode)
+ * ------------------------------------------------------------------------------------------------ */
 
 #include "HD44780.h"
 #include <avr/pgmspace.h>
@@ -20,12 +21,13 @@
 //-------------------------------------------------------------------------------------------------
 void _LCD_Write(unsigned char dataToWrite)
 {
-LCD_DATA_DIR = 0xFF;
+	LCD_DATA_OUTPUT();
 
-LCD_RW_PORT &= ~LCD_RW;
-LCD_E_PORT |= LCD_E;
-LCD_DATA_PORT = dataToWrite;
-LCD_E_PORT &= ~LCD_E;
+	expander_set_bit_no_send(LCD_RW_PORT, LCD_RW, 0);
+	expander_set_bit_no_send(LCD_E_PORT, LCD_E, 1);
+	exp_output[0] = dataToWrite;
+	expander_write(0);
+	expander_set_bit(LCD_E_PORT, LCD_E, 0);
 }
 //-------------------------------------------------------------------------------------------------
 //
@@ -35,16 +37,15 @@ LCD_E_PORT &= ~LCD_E;
 
 unsigned char _LCD_Read(void)
 {
-unsigned char tmp = 0;
+	unsigned char tmp = 0;
 
-LCD_DATA_DIR = 0x00;
+	LCD_DATA_INPUT();
 
-LCD_RW_PORT |= LCD_RW;
-LCD_E_PORT |= LCD_E;
-_delay_us(1); //tu zmieni³em
-tmp = LCD_DATA_PIN;
-LCD_E_PORT &= ~LCD_E;
-return tmp;
+	expander_set_bit_no_send(LCD_RW_PORT, LCD_RW, 1);
+	expander_set_bit(LCD_E_PORT, LCD_E, 1);
+	tmp = expander_read_byte(0, 0);
+	expander_set_bit(LCD_E_PORT, LCD_E, 0);
+	return tmp;
 }
 
 //-------------------------------------------------------------------------------------------------
@@ -54,9 +55,9 @@ return tmp;
 //-------------------------------------------------------------------------------------------------
 void LCD_WriteCommand(unsigned char commandToWrite)
 {
-LCD_RS_PORT &= ~LCD_RS;
-_LCD_Write(commandToWrite);
-while(LCD_ReadStatus()&0x80);
+	expander_set_bit(LCD_RS_PORT, LCD_RS, 0);
+	_LCD_Write(commandToWrite);
+	while(LCD_ReadStatus()&0x80);
 }
 
 //-------------------------------------------------------------------------------------------------
@@ -66,9 +67,9 @@ while(LCD_ReadStatus()&0x80);
 //-------------------------------------------------------------------------------------------------
 unsigned char LCD_ReadStatus(void)
 {
-LCD_RS_PORT &= ~LCD_RS;
-return _LCD_Read();
-return 0;
+	expander_set_bit(LCD_RS_PORT, LCD_RS, 0);
+	return _LCD_Read();
+	return 0;
 }
 //-------------------------------------------------------------------------------------------------
 //
@@ -77,9 +78,9 @@ return 0;
 //-------------------------------------------------------------------------------------------------
 void LCD_WriteData(unsigned char dataToWrite)
 {
-LCD_RS_PORT |= LCD_RS;
-_LCD_Write(dataToWrite);
-while(LCD_ReadStatus()&0x80);
+	expander_set_bit(LCD_RS_PORT, LCD_RS, 1);
+	_LCD_Write(dataToWrite);
+	while(LCD_ReadStatus()&0x80);
 }
 //-------------------------------------------------------------------------------------------------
 //
@@ -88,8 +89,8 @@ while(LCD_ReadStatus()&0x80);
 //-------------------------------------------------------------------------------------------------
 unsigned char LCD_ReadData(void)
 {
-LCD_RS_PORT |= LCD_RS;
-return _LCD_Read();
+	expander_set_bit(LCD_RS_PORT, LCD_RS, 1);
+	return _LCD_Read();
 }
 //-------------------------------------------------------------------------------------------------
 //
@@ -98,15 +99,15 @@ return _LCD_Read();
 //-------------------------------------------------------------------------------------------------
 void LCD_WriteText(const char * text)
 {
-while(*text)
-  LCD_WriteData(*text++);
+	while(*text)
+	  LCD_WriteData(*text++);
 }
 
 void LCD_WriteTextP(const char *text)
 {
-char a;
-while((a = pgm_read_byte(text++)))
-  LCD_WriteData(a);
+	char a;
+	while((a = pgm_read_byte(text++)))
+	  LCD_WriteData(a);
 }
 //-------------------------------------------------------------------------------------------------
 //
@@ -115,7 +116,7 @@ while((a = pgm_read_byte(text++)))
 //-------------------------------------------------------------------------------------------------
 void LCD_GoTo(unsigned char x, unsigned char y)
 {
-LCD_WriteCommand(HD44780_DDRAM_SET | (x + (0x40 * y)));
+	LCD_WriteCommand(HD44780_DDRAM_SET | (x + (0x40 * y)));
 }
 //-------------------------------------------------------------------------------------------------
 //
@@ -124,8 +125,8 @@ LCD_WriteCommand(HD44780_DDRAM_SET | (x + (0x40 * y)));
 //-------------------------------------------------------------------------------------------------
 void LCD_Clear(void)
 {
-LCD_WriteCommand(HD44780_CLEAR);
-_delay_ms(2);
+	LCD_WriteCommand(HD44780_CLEAR);
+	_delay_ms(2);
 }
 //-------------------------------------------------------------------------------------------------
 //
@@ -134,8 +135,8 @@ _delay_ms(2);
 //-------------------------------------------------------------------------------------------------
 void LCD_Home(void)
 {
-LCD_WriteCommand(HD44780_HOME);
-_delay_ms(2);
+	LCD_WriteCommand(HD44780_HOME);
+	_delay_ms(2);
 }
 //-------------------------------------------------------------------------------------------------
 //
@@ -144,29 +145,28 @@ _delay_ms(2);
 //-------------------------------------------------------------------------------------------------
 void LCD_Initalize(void)
 {
-unsigned char i;
-LCD_DATA_DIR = 0xFF;
-LCD_E_DIR 	|= LCD_E;   //
-LCD_RS_DIR 	|= LCD_RS;  //
-LCD_RW_DIR	|= LCD_RW;
-LCD_RS_PORT &= ~LCD_RS; // wyzerowanie linii RS
-LCD_E_PORT &= ~LCD_E;  // wyzerowanie linii E
-LCD_RW_PORT  &= ~LCD_RW;
-_delay_ms(100); // oczekiwanie na ustalibizowanie siê napiecia zasilajacego
+	unsigned char i;
+	LCD_DATA_OUTPUT();
+
+	expander_set_bit_no_send(LCD_RS_PORT, LCD_RS, 0); // wyzerowanie linii RS
+	expander_set_bit_no_send(LCD_E_PORT, LCD_E, 0);  // wyzerowanie linii E
+	expander_set_bit(LCD_RW_PORT, LCD_RW, 0);
+	_delay_ms(100); // oczekiwanie na ustalibizowanie siê napiecia zasilajacego
 
-for(i = 0; i < 3; i++) // trzykrotne powtórzenie bloku instrukcji
-  {
-  LCD_E_PORT |= LCD_E;
-  LCD_DATA_PORT = 0x3F;
-  LCD_E_PORT &= ~LCD_E;
-  _delay_ms(5); // czekaj 5ms
-  }
+	exp_output[0] = 0x3F;
+	expander_write(0);
+	for(i = 0; i < 3; i++) // trzykrotne powtórzenie bloku instrukcji
+	{
+	  expander_set_bit(LCD_E, LCD_E, 1);
+	  expander_set_bit(LCD_E, LCD_E, 0);
+	  _delay_ms(5); // czekaj 5ms
+	}
 
-LCD_WriteCommand(HD44780_FUNCTION_SET | HD44780_FONT5x7 | HD44780_TWO_LINE | HD44780_8_BIT); // interfejs 4-bity, 2-linie, znak 5x7
-LCD_WriteCommand(HD44780_DISPLAY_ONOFF | HD44780_DISPLAY_OFF); // wy³¹czenie wyswietlacza
-//LCD_WriteCommand(HD44780_CLEAR); // czyszczenie zawartosæi pamieci DDRAM
-LCD_WriteCommand(HD44780_ENTRY_MODE | HD44780_EM_SHIFT_CURSOR | HD44780_EM_INCREMENT);// inkrementaja adresu i przesuwanie kursora
-LCD_WriteCommand(HD44780_DISPLAY_ONOFF | HD44780_DISPLAY_ON | HD44780_CURSOR_OFF | HD44780_CURSOR_NOBLINK); // w³¹cz LCD, bez kursora i mrugania
+	LCD_WriteCommand(HD44780_FUNCTION_SET | HD44780_FONT5x7 | HD44780_TWO_LINE | HD44780_8_BIT); // interfejs 4-bity, 2-linie, znak 5x7
+	LCD_WriteCommand(HD44780_DISPLAY_ONOFF | HD44780_DISPLAY_OFF); // wy³¹czenie wyswietlacza
+	//LCD_WriteCommand(HD44780_CLEAR); // czyszczenie zawartosæi pamieci DDRAM
+	LCD_WriteCommand(HD44780_ENTRY_MODE | HD44780_EM_SHIFT_CURSOR | HD44780_EM_INCREMENT);// inkrementaja adresu i przesuwanie kursora
+	LCD_WriteCommand(HD44780_DISPLAY_ONOFF | HD44780_DISPLAY_ON | HD44780_CURSOR_OFF | HD44780_CURSOR_NOBLINK); // w³¹cz LCD, bez kursora i mrugania
 }
 
 //-------------------------------------------------------------------------------------------------

+ 9 - 12
soft/HD44780.h

@@ -11,6 +11,7 @@
 
 #include <avr/io.h>
 #include <util/delay.h>
+#include "expander.h"
 
 //-------------------------------------------------------------------------------------------------
 //
@@ -18,21 +19,17 @@
 // Mo¿na zmieniæ stosownie do potrzeb.
 //
 //-------------------------------------------------------------------------------------------------
-#define LCD_RS_DIR		DDRA
-#define LCD_RS_PORT 	PORTA
-#define LCD_RS			(1 << PA2)
+#define LCD_RS_PORT 	1
+#define LCD_RS			(1 << 3)
 
-#define LCD_RW_DIR		DDRA
-#define LCD_RW_PORT		PORTA
-#define LCD_RW			(1 << PA1)
+#define LCD_RW_PORT		1
+#define LCD_RW			(1 << 2)
 
-#define LCD_E_DIR		DDRA
-#define LCD_E_PORT		PORTA
-#define LCD_E			(1 << PA0)
+#define LCD_E_PORT		1
+#define LCD_E			(1 << 1)
 
-#define LCD_DATA_DIR	DDRB
-#define LCD_DATA_PORT	PORTB
-#define LCD_DATA_PIN	PINB
+#define LCD_DATA_OUTPUT() 	expander_set_dir(0, 0x00, 0x00)
+#define LCD_DATA_INPUT() 	expander_set_dir(0, 0xFF, 0x00)
 
 //-------------------------------------------------------------------------------------------------
 //

+ 1 - 1
soft/I2C.c

@@ -184,7 +184,7 @@ unsigned int I2C_WaitForTWInt(void){
 	unsigned char count = 20;
 	while (count && !(TWCR & (1<<TWINT))){
 		count--;
-		_delay_us(10);
+		_delay_us(6);
 	}
 	if(count == 0) return ERROR_I2C_TIMEOUT;
 

+ 1 - 1
soft/I2C.h

@@ -23,6 +23,6 @@ unsigned int I2C_SendOneCommandByte(unsigned char command);
 unsigned int I2C_ReceiveOneCommandByte(unsigned char *command, unsigned char ack);
 unsigned int I2C_WaitForTWInt(void);
 unsigned int I2C_Receive_n_bytes(unsigned char address, unsigned char command, unsigned char *data, unsigned int length);
-unsigned int I2C_Send_n_bytes(unsigned char address,unsigned char command, unsigned char *data, unsigned int length);
+unsigned int I2C_Send_n_bytes(unsigned char address,unsigned char command, const unsigned char *data, unsigned int length);
 
 #endif /* I2C_H_ */

+ 18 - 0
soft/expander.c

@@ -16,6 +16,10 @@ unsigned char exp_output[EXPANDER_COUNT*2];
 void expander_init(unsigned char addr, unsigned char p1in, unsigned char p2in){
 	addr += EXPANDER_ADDR;
 	expander_write_all();
+	expander_set_dir(addr, p1in, p2in);
+}
+
+void expander_set_dir(unsigned char addr, unsigned char p1in, unsigned char p2in){
 	System.global_error |= I2C_SendCommand3byte(addr, CMD_PORT0_CONFIG, p1in, p2in);
 }
 
@@ -26,6 +30,12 @@ unsigned int expander_read(unsigned char addr){
 	return (unsigned int)low | (((unsigned int) high)<<8);
 }
 
+unsigned int expander_read_byte(unsigned char addr, unsigned char reg){
+	unsigned char result;
+	System.global_error |= I2C_ReceiveCommand(addr, reg?CMD_PORT1_INPUT:CMD_PORT0_INPUT, &result);
+	return result;
+}
+
 void expander_write(unsigned char expaddr){
 	unsigned char addr = EXPANDER_ADDR + expaddr;
 	System.global_error |= I2C_SendCommand3byte(addr, CMD_PORT0_OUTPUT, exp_output[expaddr*2], exp_output[expaddr*2 + 1]);
@@ -49,3 +59,11 @@ void expander_set_bit(unsigned char port, unsigned char val, unsigned char on){
 		expander_write(port / 2);
 //	}
 }
+
+void expander_set_bit_no_send(unsigned char port, unsigned char val, unsigned char on){
+	if(on){
+		exp_output[port] |= val;
+	} else {
+		exp_output[port] &= ~val;
+	}
+}

+ 5 - 0
soft/expander.h

@@ -44,10 +44,15 @@
 
 #define EXPANDER_COUNT 1 // number of expanders (contiguous addresses)
 
+extern unsigned char exp_output[EXPANDER_COUNT*2];
+
 void expander_init(unsigned char addr, unsigned char p1in, unsigned char p2in);
+void expander_set_dir(unsigned char addr, unsigned char p1in, unsigned char p2in);
 unsigned int expander_read(unsigned char addr);
+unsigned int expander_read_byte(unsigned char addr, unsigned char reg);
 void expander_write(unsigned char addr);
 void expander_write_all(void);
 void expander_set_bit(unsigned char port, unsigned char val, unsigned char on);
+void expander_set_bit_no_send(unsigned char port, unsigned char val, unsigned char on);
 
 #endif /* EXPANDER_H_ */