Tuesday, December 17, 2024

Arduino - Interfase con LCD usando modo de 4 bits

La clave en la funcion de Lectura es separar la activacion de RS y RW del estrobo (Enable).
Vease la funcion "send" cuando se usa "r" para read.


Lo que hace el programa:


// C++ code
// integrate 4 functions into 1
// No funciona en el simulador
// pero si funciona en un display real
//
unsigned int send(unsigned char byte,char type) {
  unsigned char valor;
  unsigned char LSN = (byte & 0x0f);
  unsigned char MSN = (byte & 0xf0);

  switch(type) {
  case '4': PORTD = (LSN << 4) | 0x04;
            delay(5);
            PORTD = (LSN << 4);
            break;
  case 'c': PORTD = MSN | 0x04;
            delay(5);
            PORTD = MSN;
            PORTD = (LSN << 4) | 0x04;
            delay(5);
            PORTD = (LSN << 4);
            break;
  case 'd': PORTD = MSN | 0x05;
            delay(5);
            PORTD = MSN | 0x01;
            PORTD = (LSN << 4) | 0x05;
            delay(5);
            PORTD = (LSN << 4) | 0x01;
            break;
  case 'r': DDRD  = 0B00001111;
            delay(10);
            PORTD = PORTD | 0x07;
            delay(10);
            PORTD = PORTD | 0x04;
            MSN = PIND & 0xf0;
            delay(10);
            PORTD = PORTD & 0xfb;
            //
            delay(50);
            PORTD = PORTD | 0x07;
            delay(10);
            PORTD = PORTD | 0x04;
            LSN = PIND & 0xf0;
            delay(10);
            PORTD = PORTD & 0xfb;
            valor = MSN + (LSN >> 4);
            delay(10);
            DDRD = 0xFF;
            delay(50);
            break;
  }

return valor;
}

void setup() {
unsigned char value;

DDRB = 0x00;//Todo entradas
DDRD = 0xFF;//Todo Salidas
delay(150);//Retardo mayor de 15ms
send(0x3,'4');  
delay(50);//Retardo mayor de 4.1ms
send(0x3,'4');
delay(10);//Retardo mayor de 100us
send(0x3,'4');  
send(0x2,'4');  
send(0x2,'4');  
 
//C for LCD 2 lineas y caracter 5x10 puntos
send(0xc,'4');  
 
//send cmd 01
send(0x01,'c');

//send cmd 0c
send(0x0c,'c');

//send cmd 06
send(0x06,'c');

//send cmd 0f
send(0x0f,'c');

//send cmd 01
send(0x01,'c');

//send data
send(0x30,'d');

int i;  
  for(i = 0; i<9 ; i++){
    send(0x31 + i,'d');
  }

char string1[] = "ABCDEF";
  for(i = 0; i < strlen(string1); i++) {
    send(string1[i],'d');
  }

//set cursor to line #2  
send(0xc0,'c');
 
char string2[] = "Jorge";
  for(i = 0; i < strlen(string2); i++) {
    send(string2[i],'d');
  }

//set cursor to line #2  
send(0xc0,'c');
delay(500);
value = send(0x0,'r');
delay(500);
//set cursor to line #1    
send(0x80,'c');

//poner ahi lo que se copio' de la linea 1
send(value,'d');

}

void loop()
{
}


No comments: