need help to display on LCD

kingrosekhan123

Apr 14, 2013
19
Joined
Apr 14, 2013
Messages
19
hello..
i am using PIC 18f458
please help me..i write code for capture to measure period of pulse.
but i dont know how to display it on LCD.
kindly help me in code..to display.
Code:
CCP1CON=Ox05; I /Capture mode on every rising edge
T3CON=0x00; //Timerl for capture
TlCON=0x00; //Timerl internal elk, 1:1 prescaler
TRISB=0; //make PORTE output port
TRISD=0; //make PORTD output port
TRISCbits.TRISC2=1; //make CCPl pin an input
CCPR1L=0; //CCPRlL = 0
CCPR1H=0; //CCPRlH = 0
while (1)
{
TMRlH=0;
TMRlL=0;
//clear Timerl
PIR1bits.CCP1IF=0; //clear CCPliF flag
while (PIRlbits. CCP1IF==0); I /wait for 1st rising edge
TlCONbits.TMRlON=1; //start Timerl
PIRlbits.CCP1IF=0; //clear CCPIF for next edge
while (PIR1bits. CCP1IF==0) ; I /wait for 2nd rising edge
T1CONbits.TMR1ON=0; //stop Timerl
PORTB=CCPR1L;
PORTD=CCPR1H;
}


i'm new to this fourm.
waiting for good responce.
Thanks..
 
Last edited:

BobK

Jan 5, 2010
7,682
Joined
Jan 5, 2010
Messages
7,682
Your C compiler library probably has code to drive LCD displays.

If not, Google will find many variants of it.

Bob
 

kingrosekhan123

Apr 14, 2013
19
Joined
Apr 14, 2013
Messages
19
i mean that...here is to calculate period of pulse..
but may changes will appear on lcd display.
for example ..if i change freq. then it must vary on LCD..there should be a formula that is associated with LCD.
i.e made changes in value on LCD at that spot..
 

kingrosekhan123

Apr 14, 2013
19
Joined
Apr 14, 2013
Messages
19
Code:
#include <htc.h>

#define RS RA0
#define EN RA1
#define _XTAL_FREQ 4000000

//prototypes
void display(char fig);
void init(void);
void main(void);
void clock(void);
void clear(void);

__CONFIG(MCLRDIS & WDTDIS & UNPROTECT & LVPDIS & INTCLK);

unsigned char d1,d2,d3,d4,d5,d6,d7;

void init(void){

	CMCON = 0b00000111;		//comparator off
	T1CON = 0b1110;        //TMR1 prescale 1:1, osc=on
	TRISB = 0b11000000;		 //RB 4 bits data output
	TRISA = 0b11110000;
	OPTION = 0b10100000;	//TMR1 pre=1:2
	
	//LCD init, LCD module is preset for 8 bits data so we need to set it for 4 bits mode.
	__delay_ms(100);
	PORTA=0;
	PORTB=2; clock();	//4 bits mode
	PORTB=2; clock();	//function set: 4 bit
	PORTB=0; clock();	//function set: 1 line
	PORTB=0; clock();	//display set:
	PORTB=12; clock();	//display set: curzor off
	PORTB=0; clock();	//Entry mode:
	PORTB=6; clock();	//Entry mode:increment address	
}

void main(void){
	unsigned long total;
	unsigned int freq2;
	unsigned char freq1,nz;	
	init();
for(;;){	
	freq1 = 0;
	freq2 = 0;
	TMR1L = 0;
	TMR1H = 0;
	TMR1IF = 0;
	nz=0;			//no zero flag
	TMR1ON = 1;		// open gate
	TMR0 = 0;		// start count
	T0IF=0;
	
	while (!TMR1IF){		//loop for 2 seconds
		if (T0IF){++freq2; T0IF=0;}
	}
	
	freq1=TMR0;		//stop count=copy reg
	TMR1ON = 0;		//close gate 

	total=(unsigned long)freq1 + (unsigned long)freq2 * 256; //sum up the 2 reg
	       //convert binary to 7 decimal digits
	d7=total/1000000+48;		//1MHz digit
	d6=(total/100000) %10+48;	//100KHz digit
	d5=(total/10000) %10+48;
	d4=(total/1000) %10+48;
	d3=(total/100) %10+48;
	d2=(total/10) %10+48;		//10Hz digit	
	d1=total %10+48;
	clear();		//clear LCD		
	display(32);	//blank
	display(32);	//blank
	display(32);	//blank
	display(32);	//blank
			//remove leading zeros from first 4 digits				
	if(d7 == 48){display(32); display(32);}
		else{display(d7); display(44); nz=1;}	 //44=,
	if(d6 == 48 && nz==0){display(32);}	
		else{display(d6); nz=1;}
	if(d5 == 48 && nz==0){display(32);}	
		else{display(d5); nz=1;}
	if(d4 == 48 && nz==0){display(32); display(32);}	
		else{display(d4); display(44);}		

	display(d3);
	display(d2);
	display(d1);
	display(32);	//blank
	display(72);	//H
	display(122);	//z
	}
}

void clock(void){
	EN=1;
	__delay_us(25);		//if you have a newish LCD you can try to reduce the clock pulse width to 10us.
	EN=0;
	__delay_us(25);
}

void clear(void){
	RS=0;
	PORTB=0; clock(); PORTB=1; clock(); 	//clear display
	PORTB=0; clock(); PORTB=2; clock();	//return home
	__delay_ms(2);					//2ms

}

void display(char fig){

	RS=1;
	PORTB=(fig >> 4); clock();			//send 4 MSB
	PORTB=fig & 0b00001111; clock();	//send 4 LSB
	RS=0;

}

finally i google for LCD code to measure frequency .
but i dont know how he initialize for LCD .how he make calculations???
for example
Code:
d7=total/1000000+48;
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
If the solution to a problem is trivially easy to find on Google, then we're not going to waste our time re-typing it for you.

If you've tried to find it and can't, then perhaps we can point you to a page or suggest a search that will find it.

If you've found stuff, but it's not useful or you can't understand it, then you need to tell us why, or explain what it is you don't understand.
 

(*steve*)

¡sǝpodᴉʇuɐ ǝɥʇ ɹɐǝɥd
Moderator
Jan 21, 2010
25,510
Joined
Jan 21, 2010
Messages
25,510
In terms of initialising your LCD, you'll need to google for something like "PIC initialise <LCD model>".

Have you told us what the model is? Often the controller IC is most important here.

Many are HD44780 compatible, certainly those in the 8x1, 16x1, 16x2, etc. sizes.
 

gorgon

Jun 6, 2011
603
Joined
Jun 6, 2011
Messages
603
When you calculate (or convert from binary to ASCII) you need to adjust the 'total' with the value from the previous digit. something like this:

dx=int(number/divx)
number = number - (dx*divx)
dx-1=int(number/divx-1)
number = number - (dn-1*divx-1)

And so on until the number is in the range 0-9.

To get this correct you need to validate/test that the startvalue of number(total) is less than divx*10 before you start, or you'll get dx >9

When you display this you only need to add 0x30 (48 decimal) to each digit before you write it to the LCD.
 
Last edited:

kingrosekhan123

Apr 14, 2013
19
Joined
Apr 14, 2013
Messages
19
if anyone have code for frequency counter using ccp,kindly share it to me .
that is easy to understand.
Thanks..


kingrosekhan123
 
Top