/*
LCD_2x16_.c
Programador: Moyano Jonathan.
Año: 2012.
Descripción: Librería de control para un LCD de 2 líneas * 16 caracteres.
*/
// Declaramos las funciones.
// Convierte un dato de 8 bits en booleano y le envía el dato al LCD.
void lcd_send_nibble(int8 nibble);
// Le envía un byte al LCD, puede ser dato o comando.
void lcd_send_byte(int8 address, int8 n);
// Inicializa al LCD.
void lcd_init(void);
// Situa al cursor en la posición (X,Y), dentro de la pantalla del LCD.
void lcd_gotoxy(int8 x, int8 y);
// Escribe un caracter en el LCD.
void lcd_putc(char c);
// Desplaza el cursor hacia la derecha.
void lcd_cursor_shift_right(void);
// Desplaza el cursor hacia la izquierda.
void lcd_cursor_shift_left(void);
// Borra el LCD.
void lcd_clear(void);
// Borra el LCD con un retardo de 2 segundos.
void lcd_time_clear(void);
////////////////////////////////////////////////////////////////////////////////
// Definimos pines del LCD.
// Definición de pines para la plantilla del PIC18F4550.
#define LCD_DB4 PIN_D3
#define LCD_DB5 PIN_D2
#define LCD_DB6 PIN_D1
#define LCD_DB7 PIN_D0
#define LCD_RS PIN_C6
#define LCD_E PIN_C7
// Definimos la dirección de cada línea del LCD.
#define LCD_LINE_1_ADDRESS 0x00
#define LCD_LINE_2_ADDRESS 0x40
// Constantes del LCD.
#define lcd_tipo 2 // 0=5x7, 1=5x10, 2=2 Líneas o más de caracteres.
int8 lcd_line;
int8 const LCD_INIT_STRING[4] =
{
0x20 | (lcd_tipo << 2), // Modo: 4-bits, 2+ líneas, 5x8 puntos.
0xc, // Encendido del display.
1, // Limpia display.
6 // Incrementa cursor.
};
void lcd_send_nibble(int8 nibble) {
output_bit(LCD_DB4, !!(nibble & 1));
output_bit(LCD_DB5, !!(nibble & 2));
output_bit(LCD_DB6, !!(nibble & 4));
output_bit(LCD_DB7, !!(nibble & 8));
delay_cycles(1);
output_high(LCD_E);
delay_us(2);
output_low(LCD_E);
}
void lcd_send_byte(int8 address, int8 n){
output_low(LCD_RS);
delay_us(60);
if(address)
output_high(LCD_RS);
else
output_low(LCD_RS);
delay_cycles(1);
output_low(LCD_E);
lcd_send_nibble(n >> 4);
lcd_send_nibble(n & 0xf);
}
void lcd_init(void)
{
int8 i;
lcd_line = 1;
output_low(LCD_RS);
output_low(LCD_E);
delay_ms(35);
for(i=0 ;i < 3; i++)
{
lcd_send_nibble(0x03);
delay_ms(5);
}
lcd_send_nibble(0x02);
for(i=0; i < sizeof(LCD_INIT_STRING); i++)
{
lcd_send_byte(0, LCD_INIT_STRING[i]);
delay_ms(5);
}
}
void lcd_gotoxy(int8 x, int8 y){
int8 address;
switch(y)
{
case 1:
address = LCD_LINE_1_ADDRESS;
break;
case 2:
address = LCD_LINE_2_ADDRESS;
break;
default:
address = LCD_LINE_1_ADDRESS;
break;
}
address += x-1;
lcd_send_byte(0, 0x80 | address);
}
void lcd_putc(char c)
{
switch(c)
{
case '\f':
lcd_send_byte(0,1);
delay_ms(2);
break;
case '\n' : lcd_gotoxy(1, ++lcd_line);
break;
case '\b' : lcd_send_byte(0,0x10);
break;
case '\t' : lcd_send_byte(0,0x14);
break;
case '\r' : lcd_send_byte(0,0x18);
break;
case '\v' : lcd_send_byte(0,0x1C);
break;
default : lcd_send_byte(1,c);
break;
}
}
void lcd_cursor_shift_right(){
lcd_send_byte(0,0x10|0x08|0x04);
}
void lcd_cursor_shift_left(void){
lcd_send_byte(0,0x10|0x08|0x00);
}
void lcd_clear(void)
{
lcd_send_byte(0,0x01);
delay_ms(250);
}
void lcd_time_clear(void)
{
delay_ms(2000);
lcd_send_byte(0,0x01);
}