////////////////////////////////////////////////////////// // Created by : Kerem Yollu // Project : Multiprise conectée // Nom : lcd.c // Header : lcd.h //_________________________Info_________________________ // // Libraire pour le control d'un ecran lcd stadard (HD44780) // controlée par un expandeur de port PCF8574 en I2C. // ////////////////////////////////////////////////////////// #include "pf8574lcd.h" I2C *i2c_pf8574; // Fonction pour initialiser l'écran vide en mode 4 bits int lcd_init(I2C* i2c) { i2c_pf8574 = i2c; lcd_write(0x03,CMD_MODE); // Mise en mode 4 bit avec 4 essai conssecutif lcd_write(0x03,CMD_MODE); lcd_write(0x03,CMD_MODE); lcd_write(0x02,CMD_MODE); lcd_write(LCD_FUNCTIONSET | LCD_2LINE | LCD_5x8DOTS | LCD_4BITMODE ,CMD_MODE); lcd_write(LCD_DISPLAYCONTROL| LCD_DISPLAYON ,CMD_MODE); lcd_write(LCD_CLEARDISPLAY ,CMD_MODE); lcd_write(LCD_ENTRYMODESET | LCD_ENTRYLEFT ,CMD_MODE); return 1; } // Fonction qui vas afficher une pharse sur la ligne et colone qui lui est indiquée // Attention la fonction est capable de calculer la liongeure d0ubn phrase mais il ne // faut pas dépasser la limite d'une ligne totale qui est de 20 charactères max // // LCD 20x4 // ------------------------------------------- // ||# # # # # # # # # # # # # # # # # # # #|| // ||# # # # # # # # # # # # # # # # # # # #|| // ||# # # # # # # # # # # # # # # # # # # #|| // ||# # # # # # # # # # # # # # # # # # # #|| // ------------------------------------------- void lcd_display_string(unsigned char line, unsigned char pos, char* charvalue, unsigned char lenght) { unsigned char setPosition = 0; unsigned char i, S_length = 0; S_length = strlen(charvalue); if (lenght < S_length) { if (lenght > 0) { S_length = lenght; } } if (S_length > TOTAL_CHAR_CAP) { printf("LCD.C :\t La phrase est trop longue => %d MAX: %d \n", S_length, TOTAL_CHAR_CAP); exit(1); } else { if(line == 1) // Selection de la ligne d'écriture { setPosition = pos; } else if(line ==2) { setPosition = 0x40 + pos; } else if(line ==3) { setPosition = 0x14 + pos; } else if(line ==4) { setPosition = 0x54 + pos; } else { setPosition = -1; } if(setPosition >= 0) { lcd_write(LCD_SETDDRAMADDR + setPosition, CMD_MODE); for(i = 0; i < S_length; i++ ) { lcd_write(charvalue[i],RS); } } else { printf("LCD.C :\t Mauvais numéro de ligne => %d MAX:4 Min:1 \n", line); exit(1); } } } // Cette focntion nous pernet de faire pulser la ppin EN du lcd afin qu'il puisse // enregistrer les donées qui lui sont envoyées void ldc_pulse_En(unsigned char data) { i2c_pf8574->writeByte(LCD_ADDRS,data | EN | LCD_BACKLIGHT); usleep(100); i2c_pf8574->writeByte(LCD_ADDRS,((data & ~EN) | LCD_BACKLIGHT)); usleep(450); } // Cette fonction nous permet d'envoyer un information de 8 bits sous format // de 2x4 bites. Celà est necessaire du au fonctionnement de l'expendeur de port PCF8574 // qui est branché sur l'écran de facon a ce qu'il communiquer en 4 bits. void lcd_write(unsigned char cmd, unsigned char mode) { lcd_write_4bits(mode | (cmd & 0xF0)); lcd_write_4bits(mode | ((cmd << 4) & 0xF0)); } // Fonction nous permettant d'nevoyer 4 bits dinformation sur le PC8574 ainsi que // le rétroéclairage. void lcd_write_4bits(unsigned char data) { i2c_pf8574->writeByte(LCD_ADDRS,data | LCD_BACKLIGHT); ldc_pulse_En(data); }