i want to make frequency counter using LCD

Thread Starter

ect_09

Joined May 6, 2012
180
Rich (BB code):
#include<P18f458.h>

#define mydata PORTB
#define rs PORTCbits.RC1
#define en PORTCbits.RC3
void lcdcmd(unsigned value);
void lcddata (unsigned value);
void delay(unsigned int t);
void lcdstring(unsigned char *string2lcd);
unsigned char lowbyte, highbyte;
unsigned int result;
unsigned char str_result[10];
unsigned char msg1[] = "MALIK";

void main()
{
	lcdcmd(0x38);
	delay(15);
	lcdcmd(0x00);
	delay(15);
	lcdcmd(0x0E);
	delay(15);
	lcdcmd(0x01);
	delay(15);
	lcdcmd(0x86);
	delay(15);

CCP1CON=0x05;
T3CON=0X00;
T1CON=0X00;
TRISB=0;
TRISCbits.TRISC2=1;
CCPR1L=0x00;
CCPR1H=0x00;
while(1)
{
TMR1H=0;
TMR1L=0;
PIR1bits.CCP1IF=0;
while(PIR1bits.CCP1IF==0)
T1CONbits.TMR1ON=1;
PIR1bits.CCP1IF=0;
T1CONbits.TMR1ON=0;
lowbyte=CCPR1H;
highbyte=CCPR1L;
result = highbyte;
result = result << 8;
result = result | lowbyte;
str_result[0] = result/10000 + 48;
str_result[1] = (result/1000)%10 + 48;
str_result[2] = (result/100)%10 + 48;
str_result[3] = (result/10)%10 + 48;
str_result[4] = (result/1)%10 + 48;
str_result[5] = '\0';

PORTB=str_result[0]; 

//send str_result to lcd print function.
lcdstring(&msg1);
lcdstring(&str_result[0]);
lcdstring(&str_result[1]);
lcdstring(&str_result[2]);
lcdstring(&str_result[3]);
lcdstring(&str_result[4]);
}
}
void lcdcmd(unsigned value)
	{
	mydata=value;
	rs=0;
	en=1;
	delay(1);
	en=0;
}
void lcddata(unsigned value)
	{
	mydata=value;
	rs=1;
	en=1;
	delay(1);
	en=0;

}
void delay(unsigned int t){
	
	unsigned int i,j;
	for(i=0;i<t;i++)
	for(j=0;j<135;j++);
}

void lcdstring(unsigned char *string2lcd){

	while(string2lcd){
		lcddata(*string2lcd++);
		delay(15);
	}



}
please check this code.i just tried to write it generally.
its not displaying on LCD .please sort out my mistakes and give me tips what should i do.

Thanks..........!!
 

spinnaker

Joined Oct 29, 2009
7,830
please check this code.i just tried to write it generally.
its not displaying on LCD .please sort out my mistakes and give me tips what should i do.

Thanks..........!!
That is your job not ours. We are here to help not do you job for you.


Where is your schematic ? Where are you initializing your latches that are connected to the LCD? It looks like you are using port registers for your LCD. You should be using latches. You read ports and set latches.


Why aren't you using one of the many code libraries already out there to send data to the LCD? Just search this forum, there are many examples of writing to an LCD.

Forget about all of the other code that does not write to the LCD. In fact create a new program that does nothing but write to the LCD. Again search this forum. I believe The_RB and a few others have posted code examples a couple of times.
 

ErnieM

Joined Apr 24, 2011
8,377
First off you need a long delay (see your spec sheet) after you power on. However, first thing you do is start banging commands to the LCD. Add a delay first thing. The one I use wants 15 mS.

Next, do the simplest thing possible. Do things in steps. Don't sample frequency, convert to ASCII, display on LCD, and when you get nothing start and wonder.

First display something on the LCD in a simple way, like call lcddata() character by character. When that works THEN try the lcdstring() function.

Then try converting a number (not a reading, a number you typed in) to ASCII and display that.

Once you get the basic stuff all working, THEN go on to reading frequency, etc. Break things into steps.

Does the code compile clean? Do you have an in circuit debugger?
 

spinnaker

Joined Oct 29, 2009
7,830
First off you need a long delay (see your spec sheet) after you power on. However, first thing you do is start banging commands to the LCD. Add a delay first thing. The one I use wants 15 mS.

Next, do the simplest thing possible. Do things in steps. Don't sample frequency, convert to ASCII, display on LCD, and when you get nothing start and wonder.

First display something on the LCD in a simple way, like call lcddata() character by character. When that works THEN try the lcdstring() function.

Then try converting a number (not a reading, a number you typed in) to ASCII and display that.

Once you get the basic stuff all working, THEN go on to reading frequency, etc. Break things into steps.
Apparently we have another forum member that refuses to take advice. I suggested simple, you elaborated on it which is good.
 

t06afre

Joined May 11, 2009
5,934
As suggested. It is a good idea. To split your problems into several isolated problems. First try to make a good LCD routine. Then that is working. Do something else. And also the debugger is your friend. Which compiler do you use? It could be that you can use more of builtin the compiler functions than you do now
 

Thread Starter

ect_09

Joined May 6, 2012
180
Rich (BB code):
#include<p18f452.h>

#define mydata PORTB
#define rs PORTCbits.RC2
#define en PORTCbits.RC3

void lcdcmd(unsigned value);
void lcddata (unsigned value);
void delay(unsigned int t);
void lcdstring(unsigned char *string2lcd);
unsigned char msg1[] = "frequency";


void main (){

	TRISB=0x00;
	TRISCbits.TRISC0=1;
	TRISCbits.TRISC1=1;
	TRISCbits.TRISC2=0;
	TRISCbits.TRISC3=0;
	
	lcdcmd(0x38);
	delay(15);
	lcdcmd(0x00);
	delay(15);
	lcdcmd(0x0E);
	delay(15);
	lcdcmd(0x01);
	delay(15);
	lcdcmd(0x86);
	delay(15);
	
	while(1){
	

		lcdstring(&msg1);
	


	}
}

void lcdcmd(unsigned value){
	mydata=value;
	rs=0;
	en=1;
	delay(1);
	en=0;
}
void lcddata (unsigned value){
	mydata=value;
	rs=1;
	en=1;
	delay(1);
	en=0;

}
void delay(unsigned int t){
	
	unsigned int i,j;
	for(i=0;i<t;i++)
	for(j=0;j<135;j++);
}

void lcdstring(unsigned char *string2lcd){

	while(string2lcd){
		lcddata(*string2lcd++);
		delay(15);
	}
}
this is code for LCD
i also attached image.
kindly check this.is it ok??

kindly advice me for this.if its not good according to you people.
 

Attachments

kubeek

Joined Sep 20, 2005
5,794
Did you even read what they wrote? You need to wait at least x miliseconds (depends on your lcd) before you issue the first command. You are clearly not doing that.

Second thing, that delay(15) function delays by 15 potatoes? Why dont you use some library that actually does proper delays so that you know how long it will be?
 

Thread Starter

ect_09

Joined May 6, 2012
180
i didnt use library and i dont know about library that how to use.
if these libraries helpful then kindly guide me sir..

Thanks.........!
 

t06afre

Joined May 11, 2009
5,934
What compiler do you use? What LCD do you use?
Somehow I think the LCD works. At least in the Proteus simulator. Since it displays the word frequency. And that is kind of a start. But as long as the OP holds back on vital information. It is hard to help. I asked the compiler question some postings ago.
 

Thread Starter

ect_09

Joined May 6, 2012
180
i am using MPLAB-C18-Full-v3_10.

that is,which i install after installing MPLAB 8.60...

and 16x2 LCD i used..its just simulation on proteous .
 

t06afre

Joined May 11, 2009
5,934
I see some problems here. First you do not set the configuration words in your code. They may be set in MPLAB. But the best thing is to set them in the code. Have you taken care of this? Looking at your code. You have none functions that take care of the frequency counting. I can nor will not just write these code segments for you. But you can take a look at the first post here http://forum.allaboutcircuits.com/showthread.php?t=44852 and the "Compiled Tips ‘N Tricks Guide" I also highly recommend taking a look at the other links.
 

ErnieM

Joined Apr 24, 2011
8,377
"proteous" seems to be telling you your code is in the proper order... so something in the real world hardware isn't working. Possible candidates are:

- No power on wait before accessing LCD (as you have been told)
- The configuration bits are incorrect. This can't be checked as you did not put them in your code.
- some other thing, wiring error, bad part, a short... all the usual suspects.
 

ErnieM

Joined Apr 24, 2011
8,377
I believe C18 has a library for LCD. Search all the C18 folders for XLCD. There may be a sample program there.

I don't like the library as it stands. I use it, but I have heavily modified it in some areas.

The way it handles and defines delays are the main problem.
 

ErnieM

Joined Apr 24, 2011
8,377
can anyone tell me how to use libraries in MPLAB.??
if possible give link for tutorial.
1. Find the dot H file for the library. "#include library_name_here.h" for each and every library you use in your dot C file.

2. In some cases you may need to add a search path to the project, on the menu Project | Build Options Project. On the "Directories" tab select "Library Search Path" hit "New" and drill towards your library folder and hit OK.

3. Some code (such as XLCD) is not a true library in the sense it is pre-compiled. For these you add the dot C files into your project (under Source Files) so they all get built together.
 

Thread Starter

ect_09

Joined May 6, 2012
180
sir i try to made a little library just as a sample..
i attached file.
kindly check this..

am i going right......??

i feel happy because it reduce my time..
 

Attachments

Top