Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Need Help doing C Language Programming for PIC18F4520 MPLAB lCD 3

Status
Not open for further replies.

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
Hi guys, my final year project require me to program a tiny thermal sensor on the board . But i have totally no idea how to begin with. i am not planning to ask for the whole code, i only need a small tutorial on how to display word on the LCD and if there is any ways to light up the LEDs.

IMG_0085[1].JPG
 

what compiler are you using?

you can find more threads on this forum about how to displaying words in lcd.
 

you can find this link as useful.
**broken link removed**

Can you post the schematic?
 
IMAG0002.jpg
thanks for the link

- - - Updated - - -

4520.PNG

this is the PIC18F4520 im using.

tc74.PNG

This is the thermal sensor for my project.

lcd.PNG

and this is the lcd im using..
 

Im using PIC18F4520 to display other strings like "ding dong" . But i dunno how to start so. Thanks for the link anyway :)
 

i don't have the c18 compiler, so i didn't compile the program. try to compile and let me know if there are any errors. And double-check the hardware connections with the pins used in the software.
HTML:
//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;
}

i can help you as much if you are using ccs c compiler.

Best wishes :)
 
Last edited:

CCS C Compiler ? what is that ? got one error after building it,

error.PNG
here is the errors
errors.PNG
 

It is another compiler.

and do the changes as mentioned above by jayanth.

change this
HTML:
#define  E	 LATAbits.LATA1
to
HTML:
#define EN LATAbits.LATA1
compile now.
 

Im using MPLAB IDE to compile my program. i tried changing the above mentioned by jayanth. but it is still the same..
New error.PNG
 

Hi i found this code from you link



//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
/**********************************************************/

i build successfully but the LCD didnt appear anything..
 

compare the code with your hardware.
1. what crystal have you connected in your hardware. frequency mentioned in software is 20Mhz. change the value according to your hardware. for eg. in your hardware if you have connected 12Mhz crystal, change in software as 12000000
2. In software, enable pins connected to pin 6 of port D. Change according to the hardware. for eg,in your hardware, if you have connected enable pin of LCD to uC pin 1 of port A ,
#define E LATAbits.LATA1
3. similar to point 2, change according to RS pin.
4. to which port you have connected your data pins to uC. if you have connected to portB change as , #define LCDdata LATB
5. check the connection in R/W pin of lcd. It should be connected to ground. If it is connected to uC pin. change the #define like mentioned in point 2.

Now compile it will work :)
 

32.768kHz , do i write 32768 or just write 32000.
i have change all the LCDs Ports and Pins
 

are you using external crstal?

just write as #define _XTAL_FREQ 32768
 

Yup i wrote that. but still can't work , nothing appear on LCD
 

can u post the code please. what are the changes have you did so that i can correct..
 

here is the code
/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
/**********************************************************/

Thank you
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top