Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
Resource icon

4-bit LCD in C18 Compiler 2012-08-21

Hi, here is a code in C18 for using an LCD in 4-bit mode to display the string "Hello ETO!". It uses a 4MHz oscillator for a clock frequency of 1us. The simulation file is in Proteus. To run it you can download a demo version from Labcenter Electronics.

http://www.labcenter.com/download/prodemo_download.cfm

Code:
//Nibble mode LCD Version 1.0 XTAL=4.0 MHz, for a clock freqyebc
//Author: Wond3rboy at www.electro-tech-online.com
/*Connections:
LCD D4-D7 = PD0-PD3
LCD EN = PE0
LCD RS = PE1
LCD RW = PE2*/

#include<p18cxxx.h>
#include<delays.h>

#pragma config OSC=HS,WDT=OFF,DEBUG=ON,LVP=OFF,IESO=OFF,WRTD=OFF

#define LCD_en_dir TRISEbits.TRISE0
#define LCD_rs_dir TRISEbits.TRISE1
#define LCD_rw_dir TRISEbits.TRISE2
#define LCD_en LATEbits.LATE0
#define LCD_rs LATEbits.LATE1
#define LCD_rw LATEbits.LATE2
#define LCD_port_dir TRISD
#define LCD_port LATD

void LCD_reset(void);
void LCD_cmd(unsigned char);
void LCD_data(unsigned char);

void main(void)
{

//Port Directions
LCD_en_dir=0;
LCD_rs_dir=0;
LCD_rw_dir=0;
LCD_port_dir=0x0f0;//Last four bits connected to LCD Data pins

//Make all pins low initially
LCD_en=0;
LCD_rs=0;
LCD_rw=0;
LCD_port=0x00;

//LCD Initialization
LCD_reset();
LCD_cmd(0x28);//;4-bit, 2 line, 5x7 dots
Delay100TCYx(6);//Wait for some time to let the LCD process it
LCD_cmd(0x0F);//Display ON cursor OFF
Delay100TCYx(6);//Wait for some time to let the LCD process it
LCD_cmd(0x06);//Automatic Increment
Delay100TCYx(6);//Wait for some time to let the LCD process it
LCD_cmd(0x80);//Bring cursor to line 1
Delay100TCYx(6);//Wait for some time to let the LCD process it

//Writes "Hello ETO!" on the LCD
LCD_data('H');
Delay100TCYx(6);//Wait for some time to let the LCD process it
LCD_data('E');
Delay100TCYx(6);//Wait for some time to let the LCD process it
LCD_data('L');
Delay100TCYx(6);//Wait for some time to let the LCD process it
LCD_data('L');
Delay100TCYx(6);//Wait for some time to let the LCD process it
LCD_data('O');
Delay100TCYx(6);//Wait for some time to let the LCD process it

LCD_data(' ');
Delay100TCYx(6);//Wait for some time to let the LCD process it

LCD_data('E');
Delay100TCYx(6);//Wait for some time to let the LCD process it
LCD_data('T');
Delay100TCYx(6);//Wait for some time to let the LCD process it
LCD_data('O');
Delay100TCYx(6);//Wait for some time to let the LCD process it
LCD_data('!');
Delay100TCYx(6);//Wait for some time to let the LCD process it

while(1);
}

void LCD_reset(void)//LCD Reset Function
{
Delay10KTCYx(2);//Waits for 20ms
LCD_port=0x03;
LCD_en=1;
LCD_en=0;

Delay10KTCYx(1);//Waits for 10ms
LCD_port=0x03;
LCD_en=1;
LCD_en=0;

Delay1KTCYx(1);//Waits for 1ms
LCD_port=0x03;
LCD_en=1;
LCD_en=0;

Delay1KTCYx(1);//Waits for 1ms
LCD_port=0x02;
LCD_en=1;
LCD_en=0;
Delay1KTCYx(1);//Waits for 1ms
}

void LCD_cmd(unsigned char cmd)
{
unsigned char temp1;
temp1=cmd;

//sends the upper four bits
temp1=temp1>>4;
LCD_port=temp1;
LCD_en=1;
LCD_en=0;

//sends the lower 4 bits
temp1=cmd;
LCD_port=temp1;
LCD_en=1;
LCD_en=0;
}

void LCD_data(unsigned char data)//Routine for writing the data to the LCD
{
unsigned char temp1;
temp1=data;
LCD_rs=1;
//sends the upper four bits
temp1=temp1>>4;
LCD_port=temp1;
LCD_en=1;
LCD_en=0;

//sends the lower 4 bits
temp1=data;
LCD_port=temp1;
LCD_en=1;
LCD_en=0;
}
  • nibble_lcd.zip
    0 bytes · Views: 84
Author
Wond3rboy
Views
8,231
First release
Last update
Rating
0.00 star(s) 0 ratings

More resources from Wond3rboy

Latest threads

New Articles From Microcontroller Tips

Back
Top