InNeedOfHelp
Full Member level 3
- Joined
- Dec 5, 2012
- Messages
- 169
- Helped
- 1
- Reputation
- 2
- Reaction score
- 1
- Trophy points
- 1,298
- Activity points
- 2,205
Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
//turns watch dog timer off, turn low voltage programming off
#pragma config WDT=OFF, LVP=OFF, DEBUG=ON, MCLRE = OFF
#pragma config OSC=HS
#include <p18f4520.h>
#include <delays.h>
#define _XTAL_FREQ 10000000 // 10 MHz ---- check ur hardware and change according to it
//===============//
connect uC pin A2 to LCD RS, pin A1 to E, LCD R/W to ground --- change according to your hardware
#define RS LATAbits.LATA2 //Define LCD pinout RS
#define E LATAbits.LATA1 //Define LCD pinout E
#define lcdport LATB // LCD data pins connected to uC port B ----
void lcd_init();
void lcd_cmd(unsigned char);
void lcd_data(unsigned char);
void main()
{
lcd_init();
while(1)
{
lcd_cmd(0x80);
lcd_data("ding dong");
}
}
void lcd_init()
{
lcd_cmd(0x30); // Configure the LCD in 8-bit mode, 1 line and 5x7 font
lcd_cmd(0x0c); // display on and cursor off
lcd_cmd(0x01); // clear display screen
lcd_cmd(0x06); // increment cursor
lcd_cmd(0x80); // set cursor to 1st line
}
void lcd_cmd(unsigned char c)
{
lcdport = c;
RS = 0;
EN = 1;
delay_ms(15);
EN = 0;
}
void lcd_data(unsigned char z)
{
lcdport = z;
RS = 1;
EN = 1;
delay_ms(15);
EN = 0;
}
#define E LATAbits.LATA1
#define EN LATAbits.LATA1
//turns watch dog timer off, turn low voltage programming off
#pragma config WDT=OFF, LVP=OFF, DEBUG=ON, MCLRE = OFF
#pragma config OSC=HS
#define _XTAL_FREQ 20000000
#include <p18f4520.h>
#include <delays.h>
//LCD (PortD)
#define E LATDbits.LATD6
#define R_W LATDbits.LATD5
#define RS LATDbits.LATD4
#define LCDdata LATD
void command(unsigned char);
void write(unsigned char);
void Nybble(unsigned char);
void init(void);
void PutMessage(const rom char *);
void main()
{
ADCON1 = 0x0F; //make RA0 digital
//data direction registers all 0's mean that all pins are set to output
//all 1's means that all of the pins are set to operate as inputs
TRISA = 0x00;
TRISB = 0x00;
TRISD = 0x00;
init();
PutMessage(" Hello World!");
command(0xc0);
PutMessage(" WELCOME");
while(1);
}
//Write a string to the LCD
void PutMessage(const rom char *Message)
{
const rom char *Pos = Message;
while(*Pos!=0)
write(*Pos++);
}
/**********************************************************/
//4-bit methods for LCD
/**********************************************************/
void command(unsigned char i)
{
RS =0;
R_W =0; //R/W=LOW : Write
Nybble(i>>4); //Send upper 4 bits
Nybble(i); //Send lower 4 bits
Delay1KTCYx(2); //must wait at least 2mS (2*1000*4/1e6 = 8ms used)
}
void write(unsigned char i)
{
RS =1;
R_W =0; //R/W=LOW : Write
Nybble(i>>4); //Send upper 4 bits
Nybble(i); //Send lower 4 bits
Delay1KTCYx(2); //must wait 2mS
}
/**********************************************************/
void Nybble(unsigned char dat)
{
dat &= 0x0f; //clear top bits of dat
LCDdata &= 0xf0; //clear bottom bits of port (interested only in DB7-DB4)
LCDdata |= dat; //or the two and store at port
E = 1;
Delay1TCY(); //enable pulse width >= 300ns (used 4uS)
E = 0; //Clock enable: falling edge
}
/**********************************************************/
void init(void)
{
LCDdata=0x00;
Delay1KTCYx(40); //Wait >15 msec after power is applied (used 20mS)
Nybble(0x3); //command 0x30 = Wake up
Delay1KTCYx(15); //must wait 160us, busy flag not available (used 160uS)
Nybble(0x2); //command 0x30 = Wake up #2
Delay1KTCYx(15); //must wait 160us, busy flag not available (used 160uS)
command(0x2C); //Function set: 4-bit/2-line
command(0xC); //Set cursor
command(0x06); //Entry Mode set
command(0x01);
}
/**********************************************************/
//End methods for LCD
/**********************************************************/
/turns watch dog timer off, turn low voltage programming off
#pragma config WDT=OFF, LVP=OFF, DEBUG=ON, MCLRE = OFF
#pragma config OSC=HS
#define _XTAL_FREQ 32000
#include <p18f4520.h>
#include <delays.h>
//LCD (PortD)
#define E LATDbits.LATD6
#define R_W LATDbits.LATD5
#define RS LATDbits.LATD4
#define LCDdata LATB
void command(unsigned char);
void write(unsigned char);
void Nybble(unsigned char);
void init(void);
void PutMessage(const rom char *);
void main()
{
ADCON1 = 0x0F; //make RA0 digital
//data direction registers all 0's mean that all pins are set to output
//all 1's means that all of the pins are set to operate as inputs
TRISA = 0x00;
TRISB = 0x00;
TRISD = 0x00;
init();
PutMessage(" Hello World!");
command(0xc0);
PutMessage(" WELCOME");
while(1);
}
//Write a string to the LCD
void PutMessage(const rom char *Message)
{
const rom char *Pos = Message;
while(*Pos!=0)
write(*Pos++);
}
/**********************************************************/
//4-bit methods for LCD
/**********************************************************/
void command(unsigned char i)
{
RS =0;
R_W =0; //R/W=LOW : Write
Nybble(i>>4); //Send upper 4 bits
Nybble(i); //Send lower 4 bits
Delay1KTCYx(2); //must wait at least 2mS (2*1000*4/1e6 = 8ms used)
}
void write(unsigned char i)
{
RS =1;
R_W =0; //R/W=LOW : Write
Nybble(i>>4); //Send upper 4 bits
Nybble(i); //Send lower 4 bits
Delay1KTCYx(2); //must wait 2mS
}
/**********************************************************/
void Nybble(unsigned char dat)
{
dat &= 0x0f; //clear top bits of dat
LCDdata &= 0xf0; //clear bottom bits of port (interested only in DB7-DB4)
LCDdata |= dat; //or the two and store at port
E = 1;
Delay1TCY(); //enable pulse width >= 300ns (used 4uS)
E = 0; //Clock enable: falling edge
}
/**********************************************************/
void init(void)
{
LCDdata=0x00;
Delay1KTCYx(40); //Wait >15 msec after power is applied (used 20mS)
Nybble(0x3); //command 0x30 = Wake up
Delay1KTCYx(15); //must wait 160us, busy flag not available (used 160uS)
Nybble(0x2); //command 0x30 = Wake up #2
Delay1KTCYx(15); //must wait 160us, busy flag not available (used 160uS)
command(0x2C); //Function set: 4-bit/2-line
command(0xC); //Set cursor
command(0x06); //Entry Mode set
command(0x01);
}
/**********************************************************/
//End methods for LCD
/**********************************************************/