Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 #define _XTAL_FREQ 8000000 #include"newxc8_header.h" #include"Lcd_hd44780_pic16.h" #define RS RB5 #define EN RB4 #define D4 RB0 #define D5 RB1 #define D6 RB2 #define D7 RB3 void main (void) { TRISB=0x00; //Initialize the LCD Module LCDInit(LS_NONE); //Clear the display LCDClear(); LCDGotoXY(0x00); //Write a string LCDWriteString("Hello World !"); while(1) { //Do nothing, just loop indefinitely } }
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 [/ PIC16F877A Configuration Bit Settings // 'C' source line config statements // CONFIG #pragma config FOSC = HS // Oscillator Selection bits (HS oscillator) #pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled) #pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled) #pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled) #pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming) #pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off) #pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control) #pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off) // #pragma config statements should precede project file includes. // Use project enums instead of #define for ON and OFF. #include <xc.h>]
/*
* File: lcd_hd44780_pic16.h
* Author: Avinash
*
* Created on 6 June, 2013, 11:51 AM
*/
#ifndef LCD_HD44780_PIC16_H
#define LCD_HD44780_PIC16_H
#include <xc.h>
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
/************************************************
LCD CONNECTIONS
*************************************************/
//LCD Data Port
//Port PD0-PD3 are connected to D4-D7
#define LCD_DATA D //Port PD0-PD3 are connected to D4-D7
#define LCD_DATA_POS 0
//Register Select (RS)
//RS is connected to Port D bit 4
#define LCD_RS_PORT D
#define LCD_RS_POS 4
//Read/Write (RW)
//RW is connected to Port D bit 5
#define LCD_RW_PORT D
#define LCD_RW_POS 5
//Enable signal (E)
//E is connected to Port D bit 6
#define LCD_E_PORT D
#define LCD_E_POS 6
/***********************************************
LCD Type Selection
Uncomment Just one of them
************************************************/
//#define LCD_TYPE_202 //For 20 Chars by 2 lines
//#define LCD_TYPE_204 //For 20 Chars by 4 lines
#define LCD_TYPE_162 //For 16 Chars by 2 lines
//#define LCD_TYPE_164 //For 16 Chars by 4 lines
//************************************************
//************************************************
#define LS_BLINK 0B00000001
#define LS_ULINE 0B00000010
#define LS_NONE 0B00000000
/***************************************************
F U N C T I O N S
****************************************************/
void LCDInit(uint8_t style);
void LCDWriteString(const char *msg);
void LCDWriteInt(int val,int8_t field_length);
void LCDGotoXY(uint8_t x,uint8_t y);
//Low level
void LCDByte(uint8_t,uint8_t);
#define LCDCmd(c) (LCDByte(c,0))
#define LCDData(d) (LCDByte(d,1))
void LCDBusyLoop();
/***************************************************
F U N C T I O N S E N D
****************************************************/
/***************************************************
M A C R O S
***************************************************/
#define LCDClear() LCDCmd(0b00000001)
#define LCDHome() LCDCmd(0b00000010)
#define LCDWriteStringXY(x,y,msg) {\
LCDGotoXY(x,y);\
LCDWriteString(msg);\
}
#define LCDWriteIntXY(x,y,val,fl) {\
LCDGotoXY(x,y);\
LCDWriteInt(val,fl);\
}
/***************************************************/
/*_________________________________________________________________________________________*/
#ifdef __cplusplus
}
#endif
#endif /* LCD_HD44780_PIC16_H */
/******************************************************************************
eXtreme Electronics xAPI(TM)
----------------------------
xAPI is a Powerful but easy to use C library to program the xBoard(TM)
series of PIC development board.
The APIs are highly documented and easy to use even by a beginner.
Log
08 June 2013
*Negative Integer printing corrected and tested.
*Cursor style LS_NONE corrected and tested.
*Custom chars tested.
For More Info Log On to
[url]www.eXtremeElectronics.co.in[/url]
Copyright 2008-2013 eXtreme Electronics India
LCD Core - ver 1.0 (2013)
This module is used for interfacing with Standard Alpha Numeric LCD Modules.
For More information please see supplied tutorials and videos.
NOTICE
NO PART OF THIS WORK CAN BE COPIED, DISTRIBUTED OR PUBLISHED WITHOUT A
WRITTEN PERMISSION FROM EXTREME ELECTRONICS INDIA. THE LIBRARY, NOR ANY PART
OF IT CAN BE USED IN COMMERCIAL APPLICATIONS. IT IS INTENDED TO BE USED FOR
HOBBY, LEARNING AND EDUCATIONAL PURPOSE ONLY. IF YOU WANT TO USE THEM IN
COMMERCIAL APPLICATION PLEASE WRITE TO THE AUTHOR.
WRITTEN BY:
AVINASH GUPTA
[email]me@avinashgupta.com[/email]
*******************************************************************************/
#include <xc.h>
#include <stdint.h>
#include "lcd_hd44780_pic16.h"
#include "myutils.h"
#include "custom_char.h"
#define LCD_DATA_PORT PORT(LCD_DATA)
#define LCD_DATA_TRIS TRIS(LCD_DATA)
#define LCD_E PORTBIT(LCD_E_PORT,LCD_E_POS)
#define LCD_E_TRIS TRISBIT(LCD_E_PORT,LCD_E_POS)
#define LCD_RS PORTBIT(LCD_RS_PORT,LCD_RS_POS)
#define LCD_RS_TRIS TRISBIT(LCD_RS_PORT,LCD_RS_POS)
#define LCD_RW PORTBIT(LCD_RW_PORT,LCD_RW_POS)
#define LCD_RW_TRIS TRISBIT(LCD_RW_PORT,LCD_RW_POS)
#define SET_E() (LCD_E=1)
#define SET_RS() (LCD_RS=1)
#define SET_RW() (LCD_RW=1)
#define CLEAR_E() (LCD_E=0)
#define CLEAR_RS() (LCD_RS=0)
#define CLEAR_RW() (LCD_RW=0)
#ifdef LCD_TYPE_162
#define LCD_TYPE_204
#endif
#ifdef LCD_TYPE_202
#define LCD_TYPE_204
#endif
void LCDByte(uint8_t c,uint8_t isdata)
{
//Sends a byte to the LCD in 4bit mode
//cmd=0 for data
//cmd=1 for command
//NOTE: THIS FUNCTION RETURS ONLY WHEN LCD HAS PROCESSED THE COMMAND
uint8_t hn,ln; //Nibbles
uint8_t temp;
hn=c>>4;
ln=(c & 0x0F);
if(isdata==0)
CLEAR_RS();
else
SET_RS();
__delay_us(0.5); //tAS
SET_E();
//Send high nibble
temp=(LCD_DATA_PORT & (~(0X0F<<LCD_DATA_POS)))|((hn<<LCD_DATA_POS));
LCD_DATA_PORT=temp;
__delay_us(1); //tEH
//Now data lines are stable pull E low for transmission
CLEAR_E();
__delay_us(1);
//Send the lower nibble
SET_E();
temp=(LCD_DATA_PORT & (~(0X0F<<LCD_DATA_POS)))|((ln<<LCD_DATA_POS));
LCD_DATA_PORT=temp;
__delay_us(1); //tEH
//SEND
CLEAR_E();
__delay_us(1); //tEL
LCDBusyLoop();
}
void LCDBusyLoop()
{
//This function waits till lcd is BUSY
uint8_t busy,status=0x00,temp;
//Change Port to input type because we are reading data
LCD_DATA_TRIS|=(0x0f<<LCD_DATA_POS);
//change LCD mode
SET_RW(); //Read mode
CLEAR_RS(); //Read status
//Let the RW/RS lines stabilize
__delay_us(0.5); //tAS
do
{
SET_E();
//Wait tDA for data to become available
__delay_us(0.5);
status=(LCD_DATA_PORT>>LCD_DATA_POS);
status=status<<4;
__delay_us(0.5);
//Pull E low
CLEAR_E();
__delay_us(1); //tEL
SET_E();
__delay_us(0.5);
temp=(LCD_DATA_PORT>>LCD_DATA_POS);
temp&=0x0F;
status=status|temp;
busy=status & 0b10000000;
__delay_us(0.5);
CLEAR_E();
__delay_us(1); //tEL
}while(busy);
CLEAR_RW(); //write mode
//Change Port to output
LCD_DATA_TRIS&=(~(0x0F<<LCD_DATA_POS));
}
void LCDInit(uint8_t style)
{
/*****************************************************************
This function Initializes the lcd module
must be called before calling lcd related functions
Arguments:
style = LS_BLINK,LS_ULINE(can be "OR"ed for combination)
LS_BLINK : The cursor is blinking type
LS_ULINE : Cursor is "underline" type else "block" type
LS_NONE : No visible cursor
*****************************************************************/
//After power on Wait for LCD to Initialize
__delay_ms(30);
//Set IO Ports
LCD_DATA_TRIS&=(~(0x0F<<LCD_DATA_POS)); //Output
LCD_E_TRIS=0; //Output
LCD_RS_TRIS=0; //Output
LCD_RW_TRIS=0; //Output
LCD_DATA_PORT&=(~(0x0F<<LCD_DATA_POS));//Clear data port
CLEAR_E();
CLEAR_RW();
CLEAR_RS();
//Set 4-bit mode
__delay_us(0.5); //tAS
SET_E();
LCD_DATA_PORT|=((0b00000010)<<LCD_DATA_POS); //[B] To transfer 0b00100000 i was using LCD_DATA_PORT|=0b00100000
__delay_us(1);
CLEAR_E();
__delay_us(1);
//Wait for LCD to execute the Functionset Command
LCDBusyLoop(); //[B] Forgot this delay
//Now the LCD is in 4-bit mode
LCDCmd(0b00101000); //function set 4-bit,2 line 5x7 dot format
LCDCmd(0b00001100|style); //Display On
/* Custom Char */
LCDCmd(0b01000000);
uint8_t __i;
for(__i=0;__i<sizeof(__cgram);__i++)
LCDData(__cgram[__i]);
}
void LCDWriteString(const char *msg)
{
/*****************************************************************
This function Writes a given string to lcd at the current cursor
location.
Arguments:
msg: a null terminated C style string to print
Their are 8 custom char in the LCD they can be defined using
"LCD Custom Character Builder" PC Software.
You can print custom character using the % symbol. For example
to print custom char number 0 (which is a degree symbol), you
need to write
LCDWriteString("Temp is 30%0C");
^^
|----> %0 will be replaced by
custom char 0.
So it will be printed like.
Temp is 30°C
In the same way you can insert any symbols numbered 0-7
*****************************************************************/
while(*msg!='\0')
{
//Custom Char Support
if(*msg=='%')
{
msg++;
int8_t cc=*msg-'0';
if(cc>=0 && cc<=7)
{
LCDData(cc);
}
else
{
LCDData('%');
LCDData(*msg);
}
}
else
{
LCDData(*msg);
}
msg++;
}
}
void LCDWriteInt(int val,int8_t field_length)
{
/***************************************************************
This function writes a integer type value to LCD module
Arguments:
1)int val : Value to print
2)unsigned int field_length :total length of field in which the value is printed
must be between 1-5 if it is -1 the field length is no of digits in the val
****************************************************************/
char str[5]={0,0,0,0,0};
int i=4,j=0;
//Handle negative integers
if(val<0)
{
LCDData('-'); //Write Negative sign
val=val*-1; //convert to positive
}
while(val)
{
str[i]=val%10;
val=val/10;
i--;
}
if(field_length==-1)
while(str[j]==0) j++;
else
j=5-field_length;
for(i=j;i<5;i++)
{
LCDData(48+str[i]);
}
}
/********************************************************************
Position the cursor to specific part of the screen
********************************************************************/
void LCDGotoXY(uint8_t x,uint8_t y)
{
if(x>=20) return;
#ifdef LCD_TYPE_204
switch(y)
{
case 0:
break;
case 1:
x|=0b01000000;
break;
case 2:
x+=0x14;
break;
case 3:
x+=0x54;
break;
}
#endif
#ifdef LCD_TYPE_164
switch(y)
{
case 0:
break;
case 1:
x|=0b01000000;
break;
case 2:
x+=0x10;
break;
case 3:
x+=0x50;
break;
}
#endif
x|=0b10000000;
LCDCmd(x);
}
#define _XTAL_FREQ 4000000
#include"newxc8_header.h"
#include"Lcd_hd44780_pic16.h"
#define RW RA2
#define RS RA3
#define E RA1
#define D4 RD0
#define D5 RD1
#define D6 RD2
#define D7 RD3
void main (void)
{
TRISA=0x00;
TRISD=0x00;
//Initialize the LCD Module
LCDInit(LS_NONE);
//Clear the display
LCDClear();
//Write a string
LCDWriteString("Hello World !");
while(1)
{
//Do nothing, just loop indefinitely
}
}
I would say don't necessarily need read access, LCD in 8 bit mode neither needs it, by the way. But you're required to use maximal delay for all commands which slows down the interface. Polling for ready makes sense in an effective LCD driver implementation.a LCD used in 4 bit data mode, don't need R/W command.
LCD R/W pin must be connected to Gnd.
Sorry for all that. Its just that the code tag icon doesnt appear on my console when I am using the "Quick reply" . Henceforth, I will be using "Go advanced" where the code tag icon is available.
Code Verilog - [expand] 1 2 3 [code] ...your code.. [/code]
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?