Shafaq Gul
Junior Member level 1
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;
}
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;
}