display of temperature on 2x16 lcd in 4 bit mode using pic18f4455

Status
Not open for further replies.

Shafaq Gul

Junior Member level 1
Joined
Dec 24, 2013
Messages
18
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Visit site
Activity points
122
Hello!
I am using temperature sensor LM35 and 20MHz frequency of pic18f4455 when i simulate my code in Proteus it doesn't work would someone check my code as i have spend couple of days on it.(C18 maplab)




#include<p18f4455.h>
#include<adc.h>
#include<math.h>
#include<stdlib.h>
#include<delays.h>


int L_Byte,H_Byte,Bin_Temp;
int i,u,num1,m,z;
char num2[5]; //create a buffer for the ascii string
float t,val;

void dis_cmd(unsigned char);
void dis_data(unsigned char);
void lcdcmd(unsigned char);
void lcddata(unsigned char);
//LCD Control pins
#define rs LATBbits.LATB0
#define rw LATBbits.LATB1
#define en LATBbits.LATB2

//LCD Data pins
#define lcdport LATB
int j=0;
void main()
{

dis_cmd(0x02); // To initialize LCD in 4-bit mode.
dis_cmd(0x28); // To initialize LCD in 2 lines, 5x7 dots and 4bit mode.
dis_cmd(0x0C);
dis_cmd(0x06);
dis_cmd(0x80); // Move cursor to line 1 position 1

ADCON0bits.CHS0=0;
ADCON0bits.CHS1=0;
ADCON0bits.CHS2=0;
ADCON0bits.CHS3=0;
ADCON0bits.ADON=1;
ADCON1=0b00000000;
ADCON2=0b00001010;//left justified

TRISB=0; // Configure Port B as output port
//LATB=0;
// lcd_ini(); // LCD initialization


while(1)
{
ADCON0bits.GO_DONE=1;
while(ADCON0bits.GO_DONE);
val=ADRESH;
//H_Byte=ADRESH;
//val= ReadADC ();
t=(val*0.48876);
//L_Byte>>=2;
//L_Byte&=0x3F;
//H_Byte<<6;
//H_Byte&=0xC0;
//Bin_Temp=L_Byte|H_Byte;
//PORTD=Bin_Temp;
//value=ADCread(0);
//t=round(val*0.48876);
num1=t;

for(i=0;i<500;i++);

itoa(num1,num2);

j=0;
while(num2[j]!='\0')
{
dis_data(num2[j]);
for( u=0;u<500;u++);
j++;
}
}


}



void dis_cmd(unsigned char cmd_value)
{
unsigned char cmd_value1;
cmd_value1 = (cmd_value & 0xF0); // Mask lower nibble because RB4-RB7 pins are being used
lcdcmd(cmd_value1); // Send to LCD
cmd_value1 = ((cmd_value<<4) & 0xF0); // Shift 4-bit and mask
lcdcmd(cmd_value1); // Send to LCD
}


void dis_data(unsigned char data_value)
{
unsigned char data_value1;
data_value1=(data_value&0xF0);
lcddata(data_value1);
data_value1=((data_value<<4)&0xF0);
lcddata(data_value1);
}

void lcdcmd(unsigned char cmdout)
{
lcdport=cmdout; //Send command to lcdport=PORTB
rs=0;
rw=0;
en=1;
for(m=0;m<5000;m++);
en=0;
}

void lcddata(unsigned char dataout)
{
lcdport=dataout; //Send data to lcdport=PORTB
rs=1;
rw=0;
en=1;
for(z=0;z<5000;z++);
en=0;
}
 

add this before the while loop and comment your initialization commands.
HTML:
   disp_cmd(0x02);      // To initialize LCD in 4-bit mode.
   dis_cmd(0x20);      // To initialize LCD in 1 lines, 5x7 dots and 4bit mode.
   dis_cmd(0x0C);
   dis_cmd(0x01);
   dis_cmd(0x06);
   dis_cmd(0x80);
 

hello,

if you are working with MPLAB C18 you can use the XLCD library

extract of init

Code:
void XLCDInit(void)
{

//PORT initialization  
//4bit mode
//Upper 4-bits of the DATAPORT output
XLCD_DATAPORT_TRIS  &= 0x0f;
XLCD_DATAPORT &= 0x0f;
//control port initialization
XLCD_RSPIN_TRIS =0;                         //make control ports output
XLCD_ENPIN_TRIS =0;
XLCD_RSPIN  =0;                             //clear control ports
XLCD_ENPIN  =0;

//initialization by instruction
//  envoi 3 fois de suite 0x03H sur le quarter MSB du PORTB
XLCDDelay15ms(); 
// Upper nibble interface
XLCD_DATAPORT   &= 0x0f;        // 1ere init
XLCD_DATAPORT   |= 0b00110000;
Pulse_E() ;
XLCDDelay4ms();

// Upper nibble interface
XLCD_DATAPORT   &= 0x0f;        // 2em ini
XLCD_DATAPORT   |= 0b00110000;
Pulse_E() ;
XLCDDelay4ms(); 
   
// Upper nibble interface
XLCD_DATAPORT   &= 0x0f;        // 3em init
XLCD_DATAPORT   |= 0b00110000;
Pulse_E() ;
XLCDDelay4ms();    
    
//Force le mode 4 Bits    0x02H sur quartet MSB du PortB  
//Function SET Commande 0 0 1 DL N F X X  avec RS=0 RW=0
// Upper nibble interface
XLCD_DATAPORT   &= 0x0f;        // Clear upper port
XLCD_DATAPORT   |= 0b00100000;
Pulse_E() ;
   
//Function SET Commande 0 0 1 DL N F X X  avec RS=0 RW=0
//Definit la taille de l'interface (DL=0 pour mode 4 bits, DL=1 pour mode 8 bits),
// le nombre de lignes (NL=0 pour 1 ligne, N=1 pour 2 ou 4 lignes),
// et la taille des fontes (F=0 pour des cars 5x7, F=1 pour des cars 5x10). 
XLCDCommand(0b00101000);    // 0x28     2Line 5x8
//Display on/off control  0 0 0 0 1 D C B 
// affichage (D), curseur (C), clignotement du curseur (B).
XLCDCommand(0b00001000);    //display off
XLCDCommand(0b00000001);    //display clear
//Entry mode setting
//Entry mode command " 0 0 0 0 0 1 ID S "
//ID =0 no cursor increment during read and write
//ID =1 cursor increment during read and write
//S =0 no display during read and write
//S =1 display shift 
       
 XLCDCommand(0b00000110);    //if cursor inc and no display shift
//Display on off ,Blink ,cursor command set 
//"0 0 0 0 1 D C B "
//D=1 dislay on, C=1 cursor on, B=1 blink on
XLCDCommand(0b00001111);    //display on cursor on blink on
// end of initialization
  return;
}
 
Greetings,
What are the errors? Post your errors or error window.
I can see you have used for loop for delay, it might work in the simulator but for real circuit there are probable chances that it may not work(you have to find correct delay for your commands and data). If you want to be assure that your program works on real circuit use busy pin of LCD.
 
Last edited:
Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…