PIC18F14K50: Pantalla LCD 20x4 (HD44780)

Page 1

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.


Por: Omar Gurrola

2/26/13

// 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 #define LCD_DATA_DDR

LATC TRISC

#define LCD_EN #define LCD_EN_DDR

LATCbits.LATC4 TRISCbits.TRISC4

#define LCD_RS #define LCD_RS_DDR

LATCbits.LATC5 TRISCbits.TRISC5

#ifdef LCD_RW_MODE #define LCD_RW #define LCD_RW_DDR #endif

TRISGbits.TRISG3 LATGbits.LATG3

#ifdef LCD_HAS_BACKLIGHT #define LCD_BL_DDR #define LCD_BL #endif

TRISGbits.TRISG4 LATGbits.LATG4

// 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 #define RETURN_HOME #define ENTRY_MODE #define DISPLAY_CONTROL #define CURSOR_DISPLAY_SHIFT #define FUNCTION_SET #define SET_CGRAM_ADDR #define SET_DDRAM_ADDR

0x01 0x02 0x04 0x08 0x10 0x20 0x40 0x80

// ENTRY_MODE flags #define CURSOR_INCREMENT #define ENABLE_SHIFTING

0x02 0x01

// DISPLAY_CONTROL flags #define DISPLAY_ON #define CURSOR_ON #define BLINKING_ON

0x04 0x02 0x01

// CURSOR_DISPLAY_SHIFT flags #define DISPLAY_SHIFT #define SHIFT_RIGHT

0x08 0x04

// FUNCTION_SET flags #define DATA_LENGTH #define DISPLAY_LINES #define CHAR_FONT

0x10 0x08 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() #define lcd_return_home()

lcd_command(CLEAR_DISPLAY); LCD_HOME_CLEAR_DELAY() lcd_command(RETURN_HOME); LCD_HOME_CLEAR_DELAY()

http://www.proprojects.wordpress.com


Por: Omar Gurrola #define #define #define #define #define #define

lcd_display_on() lcd_display_off() lcd_cursor_on() lcd_cursor_off() lcd_blinking_on() lcd_blinking_off()

2/26/13 lcd_flags_set(DISPLAY_CONTROL, lcd_flags_set(DISPLAY_CONTROL, lcd_flags_set(DISPLAY_CONTROL, lcd_flags_set(DISPLAY_CONTROL, lcd_flags_set(DISPLAY_CONTROL, lcd_flags_set(DISPLAY_CONTROL,

http://www.proprojects.wordpress.com

DISPLAY_ON, 1) DISPLAY_ON, 0) CURSOR_ON, 1) CURSOR_ON, 0) BLINKING_ON, 1) 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


Por: Omar Gurrola OSCTUNEbits.SPLLEN = 1;

2/26/13 // 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()

http://www.proprojects.wordpress.com


Por: Omar Gurrola Diagrama esquemรกtico:

Circuito armado:

2/26/13

http://www.proprojects.wordpress.com


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


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.