//-----------------------------------------------------------------------------------------// // Archivo: xlcd_pluma.h (BETA 0.7: Julio 2012) // // Nota: Incluir en el programa principal: delays.h // // Descripción: Archivo modificado del original xlcd.h de Microchip para adaptarse a la // // placa PLUMA P18F4550. A las funciones originales se han añadido las siguientes: // //-----------------------------------------------------------------------------------------// // void lcd_init(void); // Config. LCD 5x7,2Lines, // void cambio_linea(void); // Situa cursor en pos cero de línea 2 // void escribe_cadena(const rom char *pc);// Muestra una cadena en la pos. del cursor // void escribe_caracter(char car); // Muestra un caracter en la pos. del cursor // void escribe_entero(int valor_entero); // Muestra un número entero " " " // void borrar_lcd(void); // Borra el contenido del lcd // void gotoxy (uchar col , uchar fil); // Situa el cursor en las coordenadas fil, col // void crea_cara1(void); // Crea el caracter :-) // void crea_cara2(void); // Crea el caracter :-( //--------------------------------------------------------------------------------------// #ifndef __XLCD_H #define __XLCD_H // Interface de 8 o 4 bits // Para 8-bit descomentar el #define BIT8 // #define BIT8 */ // Con interface de 4-bit definir si los datos estan en el nibble alto o bajo // Si los datos estan en el nibble bajo comentar el #define UPPER #define UPPER // Definiciones para el puerto de DATOS donde está conectado el LCD #define DATA_PORT PORTD // LCD conectado en PORTD #define TRIS_DATA_PORT TRISD // Definiciones para las lineas de control del LCD #define RW_PIN PORTEbits.RE1 /* PORT for RW */ #define TRIS_RW DDREbits.RE1 /* TRIS for RW */ #define RS_PIN PORTEbits.RE0 /* PORT for RS */ #define TRIS_RS DDREbits.RE0 /* TRIS for RS */ #define E_PIN PORTEbits.RE2 /* PORT for E */ #define TRIS_E DDREbits.RE2 /* TRIS for E */ // Definiciones de comandos ON/OFF del LCD #define DON 0b00001111 /* Display on */ #define DOFF 0b00001011 /* Display off */ #define CURSOR_ON 0b00001111 /* Cursor on */ #define CURSOR_OFF 0b00001101 /* Cursor off */ #define BLINK_ON 0b00001111 /* Cursor Blink */ #define BLINK_OFF 0b00001110 /* Cursor No Blink */ // Definiciones de comandos de modo de desplazamiento del cursor #define SHIFT_CUR_LEFT 0b00010011 /* Cursor shifts to the left */ #define SHIFT_CUR_RIGHT 0b00010111 /* Cursor shifts to the right */ #define SHIFT_DISP_LEFT 0b00011011 /* Display shifts to the left */ #define SHIFT_DISP_RIGHT 0b00011111 /* Display shifts to the right */ // Definiciones de conmandos de configuracion #define FOUR_BIT 0b00101111 /* 4-bit Interface */ #define EIGHT_BIT 0b00111111 /* 8-bit Interface */ #define LINE_5X7 0b00110011 /* 5x7 characters, single line */ #define LINE_5X10 0b00110111 /* 5x10 characters */ #define LINES_5X7 0b00111011 /* 5x7 characters, multiple line */ #define PARAM_SCLASS auto #define MEM_MODEL far /* Change this to near for small memory model */ //---------------------------------------------------------------------// // FUNCIONES BASICAS DE CONTROL DEL LCD PROPORCIONADAS POR MICROCHIP // //---------------------------------------------------------------------// void OpenXLCD(unsigned char lcdtype); void SetCGRamAddr(unsigned char CGaddr); void SetDDRamAddr(unsigned char DDaddr); unsigned char BusyXLCD(void); unsigned char ReadAddrXLCD(void); char ReadDataXLCD(void); void WriteCmdXLCD(unsigned char cmd); void WriteDataXLCD(char data); #define putcXLCD WriteDataXLCD void putsXLCD(char *buffer); void putrsXLCD(const rom char *buffer); // Funciones de retardo obligatorias void DelayFor18TCY(void); void DelayPORXLCD(void); void DelayXLCD(void); // Funciones de usuario void lcd_init(void); // Config. LCD 5x7,2Lines, void borrar_lcd(void); // Borra el contenido del lcd void cambio_linea(void); // Situa cursor en pos cero de línea 2 void gotoxy(unsigned char col , unsigned char fil); // Situa cursor en x,y void escribe_texto(const rom char *pc); // Muestra una cadena en la función void escribe_cadena(const rom char *pc); // Muestra una cadena declarada en rom void escribe_caracter(char car); // Muestra un caracter en la pos. del cursor void escribe_entero(int valor_entero); // Muestra un número entero " " " void escribe_digito(unsigned char dato8bits);// Muestra un dígito 0-9 void lcd_putc(char c); void lcd_puts(char *ptr); void crea_cara1(void); void crea_cara2(void); ///////////////////////////////////////////////////////////// // FUNCIONES DE RETARDO OBLIGATORIAS PARA PROGRAMAR EL LCD // // Nota:Los retardos están calculados para Xtal=20Mhz // ///////////////////////////////////////////////////////////// void DelayFor18TCY(void) { Delay10TCYx(10); // Retardo de al menos 18 TCy } void DelayPORXLCD(void) { Delay1KTCYx(80); // Retardo de al menos 15ms } void DelayXLCD(void) { Delay1KTCYx(10); // Retardo de al menos 5ms } /******************************************************************** * Function Name: OpenXLCD * * Return Value: void * * Parameters: lcdtype: sets the type of LCD (lines) * * Description: This routine configures the LCD. Based on * * the Hitachi HD44780 LCD controller. The * * routine will configure the I/O pins of the * * microcontroller, setup the LCD for 4- or * * 8-bit mode and clear the display. The user * * must provide three delay routines: * * DelayFor18TCY() provides a 18 Tcy delay * * DelayPORXLCD() provides at least 15ms delay * * DelayXLCD() provides at least 5ms delay * ********************************************************************/ void OpenXLCD(unsigned char lcdtype) { // The data bits must be either a 8-bit port or the upper or // lower 4-bits of a port. These pins are made into inputs #ifdef BIT8 // 8-bit mode, use whole port DATA_PORT = 0; TRIS_DATA_PORT = 0xff; #else // 4-bit mode #ifdef UPPER // Upper 4-bits of the port DATA_PORT &= 0x0f; TRIS_DATA_PORT |= 0xf0; #else // Lower 4-bits of the port DATA_PORT &= 0xf0; TRIS_DATA_PORT |= 0x0f; #endif #endif TRIS_RW = 0; // All control signals made outputs TRIS_RS = 0; TRIS_E = 0; RW_PIN = 0; // R/W pin made low RS_PIN = 0; // Register select pin made low E_PIN = 0; // Clock pin made low // Delay for 15ms to allow for LCD Power on reset DelayPORXLCD(); // Setup interface to LCD #ifdef BIT8 // 8-bit mode interface TRIS_DATA_PORT = 0; // Data port output DATA_PORT = 0b00110000; // Function set cmd(8-bit interface) #else // 4-bit mode interface #ifdef UPPER // Upper nibble interface TRIS_DATA_PORT &= 0x0f; DATA_PORT &= 0x0f; DATA_PORT |= 0b00100000; // Function set cmd(4-bit interface) #else // Lower nibble interface TRIS_DATA_PORT &= 0xf0; DATA_PORT &= 0xf0; DATA_PORT |= 0b00000010; // Function set cmd(4-bit interface) #endif #endif E_PIN = 1; // Clock the cmd in DelayFor18TCY(); E_PIN = 0; // Delay for at least 4.1ms DelayXLCD(); // Setup interface to LCD #ifdef BIT8 // 8-bit interface DATA_PORT = 0b00110000; // Function set cmd(8-bit interface) #else // 4-bit interface #ifdef UPPER // Upper nibble interface DATA_PORT &= 0x0f; // Function set cmd(4-bit interface) DATA_PORT |= 0b00100000; #else // Lower nibble interface DATA_PORT &= 0xf0; // Function set cmd(4-bit interface) DATA_PORT |= 0b00000010; #endif #endif E_PIN = 1; // Clock the cmd in DelayFor18TCY(); E_PIN = 0; // Delay for at least 100us DelayXLCD(); // Setup interface to LCD #ifdef BIT8 // 8-bit interface DATA_PORT = 0b00110000; // Function set cmd(8-bit interface) #else // 4-bit interface #ifdef UPPER // Upper nibble interface DATA_PORT &= 0x0f; // Function set cmd(4-bit interface) DATA_PORT |= 0b00100000; #else // Lower nibble interface DATA_PORT &= 0xf0; // Function set cmd(4-bit interface) DATA_PORT |= 0b00000010; #endif #endif E_PIN = 1; // Clock cmd in DelayFor18TCY(); E_PIN = 0; #ifdef BIT8 // 8-bit interface TRIS_DATA_PORT = 0xff; // Make data port input #else // 4-bit interface #ifdef UPPER // Upper nibble interface TRIS_DATA_PORT |= 0xf0; // Make data nibble input #else // Lower nibble interface TRIS_DATA_PORT |= 0x0f; // Make data nibble input #endif #endif // Set data interface width, # lines, font while(BusyXLCD()); // Wait if LCD busy WriteCmdXLCD(lcdtype); // Function set cmd // Turn the display off then on while(BusyXLCD()); // Wait if LCD busy WriteCmdXLCD(DOFF&CURSOR_OFF&BLINK_OFF); // Display OFF/Blink OFF while(BusyXLCD()); // Wait if LCD busy WriteCmdXLCD(DON&CURSOR_ON&BLINK_ON); // Display ON/Blink ON // Clear display while(BusyXLCD()); // Wait if LCD busy WriteCmdXLCD(0x01); // Clear display // Set entry mode inc, no shift while(BusyXLCD()); // Wait if LCD busy WriteCmdXLCD(SHIFT_CUR_LEFT); // Entry Mode // Set DD Ram address to 0 while(BusyXLCD()); // Wait if LCD busy SetDDRamAddr(0); // Set Display data ram address to 0 return; } /******************************************************************** * Function Name: BusyXLCD * * Return Value: char: busy status of LCD controller * * Parameters: void * * Description: This routine reads the busy status of the * * Hitachi HD44780 LCD controller. * ********************************************************************/ unsigned char BusyXLCD(void) { RW_PIN = 1; // Set the control bits for read RS_PIN = 0; DelayFor18TCY(); E_PIN = 1; // Clock in the command DelayFor18TCY(); #ifdef BIT8 // 8-bit interface if(DATA_PORT&0x80) // Read bit 7 (busy bit) { // If high E_PIN = 0; // Reset clock line RW_PIN = 0; // Reset control line return 1; // Return TRUE } else // Bit 7 low { E_PIN = 0; // Reset clock line RW_PIN = 0; // Reset control line return 0; // Return FALSE } #else // 4-bit interface #ifdef UPPER // Upper nibble interface if(DATA_PORT&0x80) #else // Lower nibble interface if(DATA_PORT&0x08) #endif { E_PIN = 0; // Reset clock line DelayFor18TCY(); E_PIN = 1; // Clock out other nibble DelayFor18TCY(); E_PIN = 0; RW_PIN = 0; // Reset control line return 1; // Return TRUE } else // Busy bit is low { E_PIN = 0; // Reset clock line DelayFor18TCY(); E_PIN = 1; // Clock out other nibble DelayFor18TCY(); E_PIN = 0; RW_PIN = 0; // Reset control line return 0; // Return FALSE } #endif } /******************************************************************** * Function Name: putrsXLCD * Return Value: void * Parameters: buffer: pointer to string * Description: This routine writes a string of bytes to the * Hitachi HD44780 LCD controller. The user * must check to see if the LCD controller is * busy before calling this routine. The data * is written to the character generator RAM or * the display data RAM depending on what the * previous SetxxRamAddr routine was called. ********************************************************************/ void putrsXLCD(const rom char *buffer) { while(*buffer) // Write data to LCD up to null { while(BusyXLCD()); // Wait while LCD is busy WriteDataXLCD(*buffer); // Write character to LCD buffer++; // Increment buffer } return; } /******************************************************************** * Function Name: putsXLCD * Return Value: void * Parameters: buffer: pointer to string * Description: This routine writes a string of bytes to the * Hitachi HD44780 LCD controller. The user * must check to see if the LCD controller is * busy before calling this routine. The data * is written to the character generator RAM or * the display data RAM depending on what the * previous SetxxRamAddr routine was called. ********************************************************************/ void putsXLCD(char *buffer) { while(*buffer) // Write data to LCD up to null { while(BusyXLCD()); // Wait while LCD is busy WriteDataXLCD(*buffer); // Write character to LCD buffer++; // Increment buffer } return; } /********************************************************************* * Function Name: ReadAddrXLCD * * Return Value: char: address from LCD controller * * Parameters: void * * Description: This routine reads an address byte from the * * Hitachi HD44780 LCD controller. The user * * must check to see if the LCD controller is * * busy before calling this routine. The address* * is read from the character generator RAM or * * the display data RAM depending on what the * * previous SetxxRamAddr routine was called. * *********************************************************************/ unsigned char ReadAddrXLCD(void) { char data; // Holds the data retrieved from the LCD #ifdef BIT8 // 8-bit interface RW_PIN = 1; // Set control bits for the read RS_PIN = 0; DelayFor18TCY(); E_PIN = 1; // Clock data out of the LCD controller DelayFor18TCY(); data = DATA_PORT; // Save the data in the register E_PIN = 0; RW_PIN = 0; // Reset the control bits #else // 4-bit interface RW_PIN = 1; // Set control bits for the read RS_PIN = 0; DelayFor18TCY(); E_PIN = 1; // Clock data out of the LCD controller DelayFor18TCY(); #ifdef UPPER // Upper nibble interface data = DATA_PORT&0xf0; // Read the nibble into the upper nibble of data #else // Lower nibble interface data = (DATA_PORT<<4)&0xf0; // Read the nibble into the upper nibble of data #endif E_PIN = 0; // Reset the clock DelayFor18TCY(); E_PIN = 1; // Clock out the lower nibble DelayFor18TCY(); #ifdef UPPER // Upper nibble interface data |= (DATA_PORT>>4)&0x0f; // Read the nibble into the lower nibble of data #else // Lower nibble interface data |= DATA_PORT&0x0f; // Read the nibble into the lower nibble of data #endif E_PIN = 0; RW_PIN = 0; // Reset the control lines #endif return (data&0x7f); // Return the address, Mask off the busy bit } /******************************************************************** * Function Name: ReadDataXLCD * * Return Value: char: data byte from LCD controller * * Parameters: void * * Description: This routine reads a data byte from the * * Hitachi HD44780 LCD controller. The user * * must check to see if the LCD controller is * * busy before calling this routine. The data * * is read from the character generator RAM or * * the display data RAM depending on what the * * previous SetxxRamAddr routine was called. * ********************************************************************/ char ReadDataXLCD(void) { char data; #ifdef BIT8 // 8-bit interface RS_PIN = 1; // Set the control bits RW_PIN = 1; DelayFor18TCY(); E_PIN = 1; // Clock the data out of the LCD DelayFor18TCY(); data = DATA_PORT; // Read the data E_PIN = 0; RS_PIN = 0; // Reset the control bits RW_PIN = 0; #else // 4-bit interface RW_PIN = 1; RS_PIN = 1; DelayFor18TCY(); E_PIN = 1; // Clock the data out of the LCD DelayFor18TCY(); #ifdef UPPER // Upper nibble interface data = DATA_PORT&0xf0; // Read the upper nibble of data #else // Lower nibble interface data = (DATA_PORT<<4)&0xf0; // read the upper nibble of data #endif E_PIN = 0; // Reset the clock line DelayFor18TCY(); E_PIN = 1; // Clock the next nibble out of the LCD DelayFor18TCY(); #ifdef UPPER // Upper nibble interface data |= (DATA_PORT>>4)&0x0f; // Read the lower nibble of data #else // Lower nibble interface data |= DATA_PORT&0x0f; // Read the lower nibble of data #endif E_PIN = 0; RS_PIN = 0; // Reset the control bits RW_PIN = 0; #endif return(data); // Return the data byte } /******************************************************************** * Function Name: SetCGRamAddr * * Return Value: void * * Parameters: CGaddr: character generator ram address * * Description: This routine sets the character generator * * address of the Hitachi HD44780 LCD * * controller. The user must check to see if * * the LCD controller is busy before calling * * this routine. * ********************************************************************/ void SetCGRamAddr(unsigned char CGaddr) { #ifdef BIT8 // 8-bit interface TRIS_DATA_PORT = 0; // Make data port ouput DATA_PORT = CGaddr | 0b01000000; // Write cmd and address to port RW_PIN = 0; // Set control signals RS_PIN = 0; DelayFor18TCY(); E_PIN = 1; // Clock cmd and address in DelayFor18TCY(); E_PIN = 0; DelayFor18TCY(); TRIS_DATA_PORT = 0xff; // Make data port inputs #else // 4-bit interface #ifdef UPPER // Upper nibble interface TRIS_DATA_PORT &= 0x0f; // Make nibble input DATA_PORT &= 0x0f; // and write upper nibble DATA_PORT |= ((CGaddr | 0b01000000) & 0xf0); #else // Lower nibble interface TRIS_DATA_PORT &= 0xf0; // Make nibble input DATA_PORT &= 0xf0; // and write upper nibble DATA_PORT |= (((CGaddr |0b01000000)>>4) & 0x0f); #endif RW_PIN = 0; // Set control signals RS_PIN = 0; DelayFor18TCY(); E_PIN = 1; // Clock cmd and address in DelayFor18TCY(); E_PIN = 0; #ifdef UPPER // Upper nibble interface DATA_PORT &= 0x0f; // Write lower nibble DATA_PORT |= ((CGaddr<<4)&0xf0); #else // Lower nibble interface DATA_PORT &= 0xf0; // Write lower nibble DATA_PORT |= (CGaddr&0x0f); #endif DelayFor18TCY(); E_PIN = 1; // Clock cmd and address in DelayFor18TCY(); E_PIN = 0; #ifdef UPPER // Upper nibble interface TRIS_DATA_PORT |= 0xf0; // Make inputs #else // Lower nibble interface TRIS_DATA_PORT |= 0x0f; // Make inputs #endif #endif return; } /******************************************************************** * Function Name: SetDDRamAddr * * Return Value: void * * Parameters: CGaddr: display data address * * Description: This routine sets the display data address * * of the Hitachi HD44780 LCD controller. The * * user must check to see if the LCD controller* * is busy before calling this routine. * ********************************************************************/ void SetDDRamAddr(unsigned char DDaddr) { #ifdef BIT8 // 8-bit interface TRIS_DATA_PORT = 0; // Make port output DATA_PORT = DDaddr | 0b10000000; // Write cmd and address to port RW_PIN = 0; // Set the control bits RS_PIN = 0; DelayFor18TCY(); E_PIN = 1; // Clock the cmd and address in DelayFor18TCY(); E_PIN = 0; DelayFor18TCY(); TRIS_DATA_PORT = 0xff; // Make port input #else // 4-bit interface #ifdef UPPER // Upper nibble interface TRIS_DATA_PORT &= 0x0f; // Make port output DATA_PORT &= 0x0f; // and write upper nibble DATA_PORT |= ((DDaddr | 0b10000000) & 0xf0); #else // Lower nibble interface TRIS_DATA_PORT &= 0xf0; // Make port output DATA_PORT &= 0xf0; // and write upper nibble DATA_PORT |= (((DDaddr | 0b10000000)>>4) & 0x0f); #endif RW_PIN = 0; // Set control bits RS_PIN = 0; DelayFor18TCY(); E_PIN = 1; // Clock the cmd and address in DelayFor18TCY(); E_PIN = 0; #ifdef UPPER // Upper nibble interface DATA_PORT &= 0x0f; // Write lower nibble DATA_PORT |= ((DDaddr<<4)&0xf0); #else // Lower nibble interface DATA_PORT &= 0xf0; // Write lower nibble DATA_PORT |= (DDaddr&0x0f); #endif DelayFor18TCY(); E_PIN = 1; // Clock the cmd and address in DelayFor18TCY(); E_PIN = 0; #ifdef UPPER // Upper nibble interface TRIS_DATA_PORT |= 0xf0; // Make port input #else // Lower nibble interface TRIS_DATA_PORT |= 0x0f; // Make port input #endif #endif return; } /******************************************************************** * Function Name: WriteCmdXLCD * * Return Value: void * * Parameters: cmd: command to send to LCD * * Description: This routine writes a command to the Hitachi* * HD44780 LCD controller. The user must check * * to see if the LCD controller is busy before * * calling this routine. * ********************************************************************/ void WriteCmdXLCD(unsigned char cmd) { #ifdef BIT8 // 8-bit interface TRIS_DATA_PORT = 0; // Data port output DATA_PORT = cmd; // Write command to data port RW_PIN = 0; // Set the control signals RS_PIN = 0; // for sending a command DelayFor18TCY(); E_PIN = 1; // Clock the command in DelayFor18TCY(); E_PIN = 0; DelayFor18TCY(); TRIS_DATA_PORT = 0xff; // Data port input #else // 4-bit interface #ifdef UPPER // Upper nibble interface TRIS_DATA_PORT &= 0x0f; DATA_PORT &= 0x0f; DATA_PORT |= cmd&0xf0; #else // Lower nibble interface TRIS_DATA_PORT &= 0xf0; DATA_PORT &= 0xf0; DATA_PORT |= (cmd>>4)&0x0f; #endif RW_PIN = 0; // Set control signals for command RS_PIN = 0; DelayFor18TCY(); E_PIN = 1; // Clock command in DelayFor18TCY(); E_PIN = 0; #ifdef UPPER // Upper nibble interface DATA_PORT &= 0x0f; DATA_PORT |= (cmd<<4)&0xf0; #else // Lower nibble interface DATA_PORT &= 0xf0; DATA_PORT |= cmd&0x0f; #endif DelayFor18TCY(); E_PIN = 1; // Clock command in DelayFor18TCY(); E_PIN = 0; #ifdef UPPER // Make data nibble input TRIS_DATA_PORT |= 0xf0; #else TRIS_DATA_PORT |= 0x0f; #endif #endif return; } /******************************************************************** * Function Name: WriteDataXLCD * * Return Value: void * * Parameters: data: data byte to be written to LCD * * Description: This routine writes a data byte to the * * Hitachi HD44780 LCD controller. The user * * must check to see if the LCD controller is * * busy before calling this routine. The data * * is written to the character generator RAM or* * the display data RAM depending on what the * * previous SetxxRamAddr routine was called. * ********************************************************************/ void WriteDataXLCD(char data) { #ifdef BIT8 // 8-bit interface TRIS_DATA_PORT = 0; // Make port output DATA_PORT = data; // Write data to port RS_PIN = 1; // Set control bits RW_PIN = 0; DelayFor18TCY(); E_PIN = 1; // Clock data into LCD DelayFor18TCY(); E_PIN = 0; RS_PIN = 0; // Reset control bits TRIS_DATA_PORT = 0xff; // Make port input #else // 4-bit interface #ifdef UPPER // Upper nibble interface TRIS_DATA_PORT &= 0x0f; DATA_PORT &= 0x0f; DATA_PORT |= data&0xf0; #else // Lower nibble interface TRIS_DATA_PORT &= 0xf0; DATA_PORT &= 0xf0; DATA_PORT |= ((data>>4)&0x0f); #endif RS_PIN = 1; // Set control bits RW_PIN = 0; DelayFor18TCY(); E_PIN = 1; // Clock nibble into LCD DelayFor18TCY(); E_PIN = 0; #ifdef UPPER // Upper nibble interface DATA_PORT &= 0x0f; DATA_PORT |= ((data<<4)&0xf0); #else // Lower nibble interface DATA_PORT &= 0xf0; DATA_PORT |= (data&0x0f); #endif DelayFor18TCY(); E_PIN = 1; // Clock nibble into LCD DelayFor18TCY(); E_PIN = 0; #ifdef UPPER // Upper nibble interface TRIS_DATA_PORT |= 0xf0; #else // Lower nibble interface TRIS_DATA_PORT |= 0x0f; #endif #endif return; } //-------------------------------------------------// // OTRAS FUNCIONES DE USUSARIO PARA MANEJAR EL LCD // //-------------------------------------------------// void lcd_init(void) { OpenXLCD(FOUR_BIT & LINES_5X7); // Interface 4Bits con matriz 5x7 ptos. while(BusyXLCD()); // Esperar lcd=listo WriteCmdXLCD(0x01); // Clear display while(BusyXLCD()); // Esperar lcd=listo WriteCmdXLCD(DON & CURSOR_OFF & BLINK_OFF); // LCD=ON, CURSOR=OFF, PARPADEO=OFF while(BusyXLCD()); // Esperar lcd=listo WriteCmdXLCD(0x02); } void borrar_lcd(void) { while(BusyXLCD()); // Wait if LCD busy WriteCmdXLCD(0x01); // Clear display } void cambio_linea(void) { while(BusyXLCD()); WriteCmdXLCD(0xC0); // El comando 0xC0 situa el cursor al } // comienzo de la segunda linea del LCD. void gotoxy(unsigned char col , unsigned char fil) { if(fil == 0) { while(BusyXLCD()); WriteCmdXLCD(0x80 | col); // Cursor en linea 1 } else { while(BusyXLCD()); WriteCmdXLCD(0xC0 | col); // Cursor en linea 2 } } void lcd_putc(char c){ switch(c){ case '\f': WriteCmdXLCD(0x01); break; case '\n': WriteCmdXLCD(0xC0); break; default: WriteDataXLCD(c); break; } } void escribe_cadena(const rom char *pc) { while(BusyXLCD()); putrsXLCD(pc); } void escribe_caracter(char car) { while(BusyXLCD()); WriteDataXLCD(car); } void lcd_puts(char *ptr){ while(*ptr){ lcd_putc(*ptr++); } } void escribe_texto(const rom char *pc){ while(*pc){ lcd_putc(*pc++); } } //---------- FUNCIONES PARA MOSTRAR NÚMEROS EN EL LCD -------------// //------------------------------------------------------------------// void escribe_entero(int valor_entero) { char txt_int[16]; itoa(valor_entero,txt_int); // Convertir int a cadena putsXLCD(txt_int); // Mostrar cadena en lcd } void escribe_digito(unsigned char dato8bits) { char tabla[]={'0','1','2','3','4','5','6','7','8','9'}; escribe_caracter(tabla[dato8bits]); } //----- FUNCIONES PARA CREAR CARACTERES ESPECIALES EN EL LCD -------// //------------------------------------------------------------------// void crea_cara1(void) { // Tablas con los datos del carácter especial ":)" // PORTB es el puerto de datos del LCD, cambiar si es necesario // el caracter se crea en la posicion 0x00 unsigned char cara1[]={0x00,0x0A,0x00,0x04,0x11,0x0E,0x00,0x00}; unsigned char pos=0; for(pos=0;pos<8;pos++) { while(BusyXLCD()); WriteCmdXLCD(0x40 + pos); while(BusyXLCD()); WriteDataXLCD(cara1[pos]); } borrar_lcd(); while(BusyXLCD()); WriteCmdXLCD(0x02); } void crea_cara2(void) { // Tablas con los datos del carácter especial ":-(" // PORTB es el puerto de datos del LCD, cambiar si es necesario // el caracter se crea en la posicion 0x01 unsigned char cara2[]={0x00,0x0A,0x00,0x04,0x00,0x0E,0x11,0x00}; unsigned char pos=0; for(pos=0;pos<8;pos++) { while(BusyXLCD()); WriteCmdXLCD(0x48 + pos); while(BusyXLCD()); WriteDataXLCD(cara2[pos]); } borrar_lcd(); while(BusyXLCD()); WriteCmdXLCD(0x02); }