Paso 3: Cómo leer desde la pantalla
readDDRAM.c
Esta pantalla no se parece mucho, pero echa un vistazo a lo que realmente está sucediendo.
Primero escribimos un texto a la pantalla, entonces restablecer la posición inicial de la AC, dirección contraria y leer los caracteres escritos.
A continuación, escribimos los caracteres que se leen hacia atrás a la pantalla.
Veamos el código:
#include #include "hd44780_4bit_lib.h" #include /* * This code example show how to read text from DDRAM, * in this example we will write some text to the display * then read the text and then write the read text * back to the display. */void main() { WDTCTL = WDTPW | WDTHOLD; // Disable watchdog timer uint8_t rtn = 0; uint8_t readStr[80]; uint8_t *pint; unsigned int cnt = 0; unsigned int charCnt = 0; // according to spec give the display 10ms to come up to voltage __delay_cycles(10000 * CPUSPEED); // set up data length, number of lines and font // notice we have setup the display for 2 lines // and using the 5 x 8 character size hd44780_init(N_2LINE, FONT_8); // turn display on and clear the display // and set cursor to home position 0x00 // hd44780_send_command(HD44780_CMD_DISPLAY_ON); // hd44780_send_command(HD44780_CMD_RETURN_HOME); // hd44780_send_command(HD44780_CMD_CLEAR_DISPLAY); hd44780_setCursorPosition(0, 0); hd44780_write_string("this"); hd44780_setCursorPosition(0, 0); pint = readStr; // reads characters from display while(1) { rtn = hd44780_readByte(); if (rtn == ' ') { break; } *pint = rtn; pint++; charCnt++; } // writes read characters back to display for(cnt = 0; cnt < charCnt; cnt++) { hd44780_write_special_char(readStr[cnt]); } hd44780_send_command(HD44780_CMD_RETURN_HOME); }
Romper el código y ver lo que está sucediendo:
uint8_t rtn = 0;uint8_t readStr[80]; uint8_t *pint; unsigned int cnt = 0; unsigned int charCnt = 0;// according to spec give the display 10ms to come up to voltage __delay_cycles(10000 * CPUSPEED);// set up data length, number of lines and font // notice we have setup the display for 2 lines // and using the 5 x 8 character size hd44780_init(N_2LINE, FONT_8);
En primer lugar off configuramos algunas variables que podemos usar durante la lectura y la escritura de los caracteres extraídos de la pantalla.
Aviso que creamos la pantalla de 2 líneas y caracteres de 5 x 8 puntos patrón.
hd44780_setCursorPosition(0, 0); hd44780_write_string("this"); hd44780_setCursorPosition(0, 0); pint = readStr; // reads characters from display while(1) { rtn = hd44780_readByte(); if (rtn == ' ') { break; } *pint = rtn; pint++; charCnt++; }
Ahora fijar la posición del cursor en la pantalla para la fila 0, columna 0 y escribir el texto en la pantalla.
Configuramos un puntero a la matriz readStr. Ahora nos recorrer la DDRAM, asiendo un personaje en un momento y poner el carácter en la matriz.
Si la siguiente recibe el carácter ' ', entonces asumimos que hay no hay mas personajes y salir del bucle.
Esto es sólo un ejemplo, si tiene previsto una serie de palabras y su lógica tendrá que ser un poco más complejo, como el bucle a través de toda la línea, una solución fácil.
// writes read characters back to display for(cnt = 0; cnt < charCnt; cnt++) { hd44780_write_special_char(readStr[cnt]); } hd44780_send_command(HD44780_CMD_RETURN_HOME);
Desde la posición del cursor está siendo rastreada por la AC, contador de la dirección, el cursor ya está sentado en una ubicación después del texto leído. Todo lo que tenemos que hacer es leer los caracteres de la matriz y los muestra uno por uno de nuevo a la pantalla.