LCD Interface PGM

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

/* * * * * * * * * * * * * *

LCD interface example Uses routines from delay.c This code will interface to a standard LCD controller like the Hitachi HD44780. It uses it in 4 bit mode, with the hardware connected as follows (the standard 14 pin LCD connector is used): PORTD bits 0-3 are connected to the LCD data bits 4-7 (high nibble) PORTA bit 3 is connected to the LCD RS input (register select) PORTA bit 1 is connected to the LCD EN bit (enable) To use these routines, set up the port I/O (TRISA, TRISD) then call lcd_init(), then other routines as required.

#include //#include #include

<pic.h> "lcd.h" "delay.h"

#defineLCD_RS RD2 //#define LCD_RW RA2 #define LCD_EN RD3 #define LCD_DATA4 #define LCD_DATA5 #define LCD_DATA6 #define LCD_DATA7 //#define LCD_DATA RD4 RD5 RD6 RD7 PORTD

#defineLCD_STROBE()

((LCD_EN = 1),(LCD_EN=0))

/* write one character to the LCD */

/* write a byte to the LCD in 4 bit mode */ void lcd_write(unsigned char c) { unsigned char f; f = c; DelayUs(40); if(c & 0x10) LCD_DATA4 = 1;

else LCD_DATA4 = 0; if(c & 0x20) LCD_DATA5 = 1; else LCD_DATA5 = 0; if(c & 0x40) LCD_DATA6 = 1; else LCD_DATA6 = 0; if(c & 0x80) LCD_DATA7 = 1; else LCD_DATA7 = 0; //LCD_DATA = ( c & 0xF0 ); LCD_STROBE(); if(c & 0x01) LCD_DATA4 = 1; else LCD_DATA4 = 0; if(c & 0x02) LCD_DATA5 = 1; else LCD_DATA5 = 0; if(c & 0x04) LCD_DATA6 = 1; else LCD_DATA6 = 0; if(c & 0x08) LCD_DATA7 = 1; else LCD_DATA7 = 0; //LCD_DATA = ( ( f << 4 ) & 0xF0 ); LCD_STROBE(); }

void lcd_putch(char c) { LCD_RS = 1; // write characters lcd_write( c );

} /* * */

Clear and home the LCD

void lcd_clear(void) { LCD_RS = 0; lcd_write(0x1); DelayMs(2); } /* write a string of chars to the LCD */ void lcd_puts(const char * s) { LCD_RS = 1; // write characters while(*s) lcd_write(*s++); }

/* * Go to the specified position */ void lcd_1st_line(unsigned char pos) { LCD_RS = 0; lcd_write(0x80+pos); } void lcd_2nd_line(unsigned char pos) { LCD_RS = 0; lcd_write(0xC0+pos); }

/* initialise the LCD - put into 4 bit mode */ void lcd_init() { char init_value;

ADCON1 = 0x06; init_value = 0x30; LCD_RS = 0; LCD_EN = 0; LCD_RW = 0;

// Disable analog pins on PORTA

//

DelayMs(15); // wait 15mSec after power applied, LCD_DATA4 = 1; LCD_DATA5 = 1; LCD_DATA6 = 0; LCD_DATA7 = 0; LCD_STROBE(); DelayMs(5); LCD_STROBE(); DelayUs(200); LCD_STROBE(); DelayUs(200); LCD_DATA4 = 0; LCD_DATA5 = 1; LCD_DATA6 = 0; LCD_DATA7 = 0; LCD_STROBE(); lcd_write(0x28); // Set interface length lcd_write(0x0E); // Display On, Cursor On, Cursor Blink lcd_clear(); // Clear screen lcd_write(0x06); // Set entry Mode }

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy