pic18f14k50: pantalla lcd 20x4 (hd44780)

6

Click here to load reader

Upload: dragoon-micromar

Post on 22-Mar-2016

481 views

Category:

Documents


61 download

DESCRIPTION

Como trabajar con una pantalla LCD de 20x4 con pic18f14k50

TRANSCRIPT

Page 1: PIC18F14K50: Pantalla LCD 20x4 (HD44780)

Por: Omar Gurrola 2/26/13 http://www.proprojects.wordpress.com

PIC18F14K50: Pantalla LCD 20x4 (HD44780)

Esta sección es muy similar a la anterior, con su diferencia de que esta vez será con un LCD de 20 caracteres por 4 líneas. La lógica o protocolo es igual que si se trabaja con un LCD de 16x2. Los únicos cambios a realizar son:

1. Número de líneas y renglones en la cabecera <HD44780.h> 2. El texto que vamos a enviar, al igual que los datos del gotoxy, ya podemos trabajar hasta 20 en X y 4 en Y.

HD44780.h

/** * Driver library for HD44780-compatible LCD displays. * https://github.com/armandas/Yet-Another-LCD-Library * * Author: Armandas Jarusauskas (http://projects.armandas.lt) * * Contributors: * Sylvain Prat (https://github.com/sprat) * * This work is licensed under a * Creative Commons Attribution 3.0 Unported License. * http://creativecommons.org/licenses/by/3.0/ * */ /** Mod. By: Omar Gurrola www.proprojects.wordpress.com 2/26/13 - Added #if DATA_SHIFT > 0 to shift (To remove warning) - Added LCD_RW_MODE if you use RW pin otherwise connecto to 0 (GND) - Added StartUpCode in lcd_init() - Added LCD_START_DELAY() to stablize when power on. - Changed algorith for lcd_gotoxy(), because don't work right for 20x4 LCD **/ /******************************* CONFIG ***************************************/ #include "wait.h" #include "pic18f14k50_io.h" // number of lines on the LCD #define LCD_LINES 4 #define LCD_CHARACTERS 20 // If you will use RW pin otherwise comment it //#define LCD_RW_MODE // If you will control backlight with a pin //#define LCD_HAS_BACKLIGHT // data mask // this must match the data pins on the port // this program uses 4 lower bits of PORT #define DATA_MASK 0b00001111 // if you don't use the lower four pins // for your data port, your data will have // to be shifted.

Page 2: PIC18F14K50: Pantalla LCD 20x4 (HD44780)

Por: Omar Gurrola 2/26/13 http://www.proprojects.wordpress.com // number of left shifts to do: #define DATA_SHIFT 0 // Lines directions for gotoxy function #define LCD_LINEDEF_DIR 0x00 // If other than 1-4 is selected #define LCD_LINE1_DIR 0x00 #define LCD_LINE2_DIR 0x40 #define LCD_LINE3_DIR 0x14 #define LCD_LINE4_DIR 0x54 // Define StartUp Code Code if you need // Like AD disable, etc. #define StartUpCode() OpenOutPC() // pins #define LCD_DATA LATC #define LCD_DATA_DDR TRISC #define LCD_EN LATCbits.LATC4 #define LCD_EN_DDR TRISCbits.TRISC4 #define LCD_RS LATCbits.LATC5 #define LCD_RS_DDR TRISCbits.TRISC5 #ifdef LCD_RW_MODE #define LCD_RW TRISGbits.TRISG3 #define LCD_RW_DDR LATGbits.LATG3 #endif #ifdef LCD_HAS_BACKLIGHT #define LCD_BL_DDR TRISGbits.TRISG4 #define LCD_BL LATGbits.LATG4 #endif // timings: depend on the instruction clock speed #define LCD_ADDRESS_SETUP_DELAY() Nop() // > 40 ns #define LCD_EXECUTION_DELAY() Wait50us() // > 50 us #define LCD_ENABLE_PULSE_DELAY() Wait250us() // > 250 ns #define LCD_HOME_CLEAR_DELAY() Waitmsx(2) // > 1.6 ms #define LCD_START_DELAY() Waitmsx(10) // ~ 10 ms /******************************* END OF CONFIG ********************************/ // commands #define CLEAR_DISPLAY 0x01 #define RETURN_HOME 0x02 #define ENTRY_MODE 0x04 #define DISPLAY_CONTROL 0x08 #define CURSOR_DISPLAY_SHIFT 0x10 #define FUNCTION_SET 0x20 #define SET_CGRAM_ADDR 0x40 #define SET_DDRAM_ADDR 0x80 // ENTRY_MODE flags #define CURSOR_INCREMENT 0x02 #define ENABLE_SHIFTING 0x01 // DISPLAY_CONTROL flags #define DISPLAY_ON 0x04 #define CURSOR_ON 0x02 #define BLINKING_ON 0x01 // CURSOR_DISPLAY_SHIFT flags #define DISPLAY_SHIFT 0x08 #define SHIFT_RIGHT 0x04 // FUNCTION_SET flags #define DATA_LENGTH 0x10 #define DISPLAY_LINES 0x08 #define CHAR_FONT 0x04 // function prototypes void lcd_flags_set(unsigned char, unsigned char, unsigned char); void lcd_initialize(void); void lcd_command(unsigned char); void lcd_data(unsigned char); void lcd_write(char *); void lcd_write_pgm(const rom char *); void lcd_goto(unsigned char, unsigned char); void lcd_add_character(unsigned char, unsigned char *); // inline functions #define lcd_clear() lcd_command(CLEAR_DISPLAY); LCD_HOME_CLEAR_DELAY() #define lcd_return_home() lcd_command(RETURN_HOME); LCD_HOME_CLEAR_DELAY()

Page 3: PIC18F14K50: Pantalla LCD 20x4 (HD44780)

Por: Omar Gurrola 2/26/13 http://www.proprojects.wordpress.com #define lcd_display_on() lcd_flags_set(DISPLAY_CONTROL, DISPLAY_ON, 1) #define lcd_display_off() lcd_flags_set(DISPLAY_CONTROL, DISPLAY_ON, 0) #define lcd_cursor_on() lcd_flags_set(DISPLAY_CONTROL, CURSOR_ON, 1) #define lcd_cursor_off() lcd_flags_set(DISPLAY_CONTROL, CURSOR_ON, 0) #define lcd_blinking_on() lcd_flags_set(DISPLAY_CONTROL, BLINKING_ON, 1) #define lcd_blinking_off() lcd_flags_set(DISPLAY_CONTROL, BLINKING_ON, 0) #ifdef LCD_HAS_BACKLIGHT #define lcd_backlight_on() LCD_BL = 1 #define lcd_backlight_off() LCD_BL = 0 #endif

main.c

/* * Copyright (c) 2011-2013, http://www.proprojects.wordpress.com * All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * * 1.- Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * 2.- Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY * WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /********************************************************************************** * Author: Omar Gurrola * Site: http://www.proprojects.wordpress.com * Processor: PIC18 * Compiler: C18 v3.45 * File Name: main.c * Description: Main program * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ * Rev. Date Comment * 1.0 04/01/12 Initial version * 1.1 01/29/13 Remove ISR prototipes, they are declared in hid_bl.h *********************************************************************************/ /** INCLUDES *******************************************************/ #include <p18f14k50.h> #include "pic18f14k50_cbits.h" #include "pic18f14k50_io.h" #include "stdvars.h" #include "wait.h" #include "HD44780.h" /** PROTOTYPES *****************************************************/ /** VARIABLES ******************************************************/ /** DECLARATIONS ***************************************************/ #pragma code // Forces the code below this line to be put into the code section (Memory Adress >= 0x'REMDIR'02A) /** Interrupt Service Routines (ISR)********************************/ #pragma interrupt HighPriorityISR void HighPriorityISR (void){ //Check which interrupt flag caused the interrupt. //Service the interrupt //Clear the interrupt flag //Etc. } //This return will be a "retfie fast", since this is in a #pragma interrupt section #pragma interruptlow LowPriorityISR void LowPriorityISR (void){ //Check which interrupt flag caused the interrupt. //Service the interrupt //Clear the interrupt flag //Etc. } //This return will be a "retfie", since this is in a #pragma interruptlow section void main(void){ OSCCONbits.IRCF = 0b110; // Poscaler selected to 8 MHz

Page 4: PIC18F14K50: Pantalla LCD 20x4 (HD44780)

Por: Omar Gurrola 2/26/13 http://www.proprojects.wordpress.com

OSCTUNEbits.SPLLEN = 1; // PLLx4 Enable System FQ = 32 MHz lcd_initialize(); lcd_goto(1,5); lcd_write_pgm("Omar Gurrola"); lcd_goto(2,5); lcd_write_pgm("Pro Projects"); lcd_goto(3,4); lcd_write_pgm("LCD 20x04 TEST"); lcd_goto(4,1); lcd_write_pgm("Using PIC18F14K50 uC"); while(true){ ; } // end while } // end main()

Page 5: PIC18F14K50: Pantalla LCD 20x4 (HD44780)

Por: Omar Gurrola 2/26/13 http://www.proprojects.wordpress.com Diagrama esquemático:

Circuito armado:

Page 6: PIC18F14K50: Pantalla LCD 20x4 (HD44780)

Por: Omar Gurrola 2/26/13 http://www.proprojects.wordpress.com

Referencias

Microchip, “PIC18F/LF1XK50 Data Sheet”, 2010,

http://ww1.microchip.com/downloads/en/DeviceDoc/41350E.pdf

Hitachi, “HD44780U (LCD-II) Data Sheet”

http://www.sparkfun.com/datasheets/LCD/HD44780.pdf

Armandas, “HD44780 LCD driver library for PIC18 microcontrollers”

http://projects.armandas.lt/lcd-library.html

assadmahmood, “AVR studio 4 forum - 20x4 LCD problem”

http://www.avrfreaks.net/index.php?name=PNphpBB2&file=printview&t=127798&start=20%20%28Direcciones

%20Y%20para%2020x4%29