Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

READING A PUSHBUTTON & display on LCD

Status
Not open for further replies.
Uhmmmm Hello AtomSoft...could you tell me why you suggest i use int instead of unsigned int....??? the distances are all going to be positive...

This is from the original code:
unsigned int math; // used for voltage calculations


heh just dont forget in that junebug page read the comments well...

// load ADC value in 16bit math var

so..
int math
 
Uhmmmm Hello AtomSoft...could you tell me why you suggest i use int instead of unsigned int....??? the distances are all going to be positive...

This is from the original code:
unsigned int math; // used for voltage calculations

I never checked original code. Also i never said dont use unsigned int as long as its a int :)

Unsigned int is best in this case
 
O Ok...Cool. Hello AtomSoft...I wrote a test today & came home early...hehe I am attempting ADC 1st time today lol....well it looks easy to set up and stuff:)...hopefully it will work when i put it in the circuit:rolleyes: (o else lol someone will be getting bothered :rolleyes::rolleyes::rolleyes:)
 
This compiled successfully but im not sure if im doing the part highlighted in red correctly.....I want to display the unsigned int value stored in cm to the LCD. cm=math from the calc_distance function. i commented home_screen for now. Want to get cm value displaying and will integrate it later.

Code:
#include <pic.h>
#include "pic.h"
#include "delay.h"
#include "math.h" 
#include <stdio.h>
#include <stdlib.h>	//

void userMenu(char pos); //  
void FloatToStr(float , char[]); 
void DelayMs(unsigned char);
void lcd_cmd(unsigned char);
void lcd_data(unsigned char); 
void lcd_clear(void);
void lcd_puts(const char[]);
void lcd_goto_L1(void);
void lcd_goto_L2(void);
void lcd_cursor(unsigned char);
void lcd_init(void);
void init(void);
char WaitForInput(void);
unsigned char user_input(void);
void home_screen(void);
void EnterHeight(void);
void EnterScreen(void);
void ShowDigits(unsigned char val);
void calc_distance(void);
void main(void);

unsigned int temp;
unsigned char cm2LCD;


      

#define LCD_RS RC0		//LCD RS pin
#define LCD_EN RC1		//LCD EN pin
#define LCD_STROBE()	LCD_EN = 1; asm("nop"); asm("nop"); LCD_EN = 0

unsigned char cm10;		//
unsigned char cm;		//
unsigned int math;		// used for voltage calculations
unsigned char NumDec;
unsigned char NumSep[2];
   
unsigned char i,j,k;
//char temp[8];
//[/b]
unsigned char height=50;
unsigned char range;
unsigned char area;
unsigned char SensorPos=10;
//[/b] New Vars
char input_sw;

  
char mnuPOS;

unsigned char MyVal;
unsigned char MyValLCD[2];
unsigned char MyMaxVal;
unsigned char MyMinVal;
unsigned long bres;		// for bresenham 2-second timer system

unsigned char ;

#define HOME_SW RC2				//HOME switch	
#define INCREASE_SW RC3			//INCREASE switch
#define DECREASE_SW RC4			//DECREASE switch
#define ENTERSETTINGS_SW RA4	//ENTERSETTINGS switch



///////////////////////CONVERT FLOAT TO STRING///////////////////
// This function was taken from the CAVR library. It was modified slightly
// to suit our design.
void FloatToStr(float n, char str[])
{
float scale;
unsigned char d,f;
f=0;i=0;
if (n<0.0) {n=-n; str[f]='-'; f++;};
n=n+0.005;
scale=1.0;
while (n>=scale) {scale=scale*10.0; ++i;};
if (i==0) {str[f]='0'; f++;}
else
while (i--)
	  {
      scale=floor(0.5+scale/10.0);
      d=(unsigned char) (n/scale);
      str[f]=d+'0';
      n=n-scale*d;
	  f++;
      };

str[f]='.';
f++;
for (j=0;j<=1;j++) //2 decimal points
      {
      n=n*10.0;
      d=(unsigned char) n;
      str[f]=d+'0';
      n=n-d;
	  f++;
	  };
str[f]='\0';
}
///////////////////END CONVERT FLOAT TO STRING///////////////////

/////////////////////////////DELAY///////////////////////////////
void DelayMs(unsigned char cnt)
{
#if	XTAL_FREQ <= 2MHZ
	do {
		DelayUs(996);
	} while(--cnt);
#endif

#if    XTAL_FREQ > 2MHZ	
	unsigned char	p;
	do {
		p = 4;
		do { 
			DelayUs(250);
		} while(--p);
	} while(--cnt);
#endif
}

void DelayS(unsigned char cnt)
{
	for (j=0; j<(cnt*10); j++)
		DelayMs(100);
}
///////////////////////////DELAY END/////////////////////////////

//////////////////////////////LCD SETUP//////////////////////////
/* send a command to the LCD */
void lcd_cmd(unsigned char c)
{
	DelayMs(2); //wait for LCD to be ready shorter delay 
	LCD_RS = 0;	 //write instruction
	PORTB = (c & 0xF0); //load upper nibble on LCD data lines
	LCD_STROBE(); //send instruction to LCD
	PORTB = ((c << 4) & 0xF0); //load upper nibble on LCD data lines
	LCD_STROBE(); //send instruction to LCD   	
}

/* send data to the LCD */
void lcd_data(unsigned char c)
{
	DelayMs(2); //wait for LCD to be ready shorter delay 
	PORTB = 0x00;
	LCD_RS = 1; //write data
    PORTB |= (c & 0xF0); //load upper nibble on LCD data lines     
	LCD_STROBE(); //send instruction to LCD
	PORTB &= 0x00; //load upper nibble on LCD data lines
	PORTB |= ( (c << 4) & 0xF0); 
	LCD_STROBE(); //send instruction to LCD
}

/*Clear the LCD*/
void lcd_clear(void)
{
	lcd_cmd(0x01); //command to clear LCD
}

/*write a string of chars to the LCD*/
void lcd_puts(const char s[])
{
	j = -1;
	while(s[++j]!=('\0')) // send characters until null character reached
		lcd_data(s[j]);
}

/*go to beginning of line 1*/
void lcd_goto_L1(void)
{
	lcd_cmd(0b10000000); // command to go to line 1
}

/*go to beginning of line 2*/
void lcd_goto_L2(void)
{
	lcd_cmd(0b11000000); // command to go to line 2
}

/*move cursor "x" positions to the right*/
void lcd_cursor(unsigned char x)
{
	lcd_cmd(((x)&0x7F)|0x80); 
}

/*initialise the LCD - put into 4 bit mode*/
void lcd_init(void)
{
	LCD_RS = 0;	
	LCD_EN = 0;
	DelayMs(20); //wait for LCD startup
	lcd_cmd(0x02);
 
	lcd_cmd(0x28);	// 4-bit mode
	lcd_cmd(0x08);	// display off
	lcd_cmd(0x01);	// clear display
	lcd_cmd(0x0C);	// disp. on, cursor off, cursor blink off
	lcd_cmd(0x06);	// entry mode
	lcd_cmd(0x80);  // initialise DDRAM address to zero
}
//////////////////////////LCD SETUP END//////////////////////////
    

void init(void)
{   
	
	// setup the PIC 16f690
	OSCCON = 0x72;      	// internal osc, 8MHz
	PORTA = 0;
	TRISA = 0b10010010; 	// RA7 high imp, RA3 is serial out, RA4 button input 


	
 	PORTB = 0;          	// PORTB not used
    WPUB = 1;				// PORTB pullups ON		
    RABPU = 0;


    /* Init ADC */
    ADCON0 = 0b10000101;	// bit 7 right justify,analogue channel select bits bits5-2  0001=AN1,ADC ON, RA1 is ADC input
	ADCON1 = 0b00100000;	//bits6-4  fosc/32
    ADON=1;	                // turn on the A2D conversion module

	
	ANSEL=0x02;            //set RA1 as analog input for GP2 sensor
	ANSELH=0x00;

    T1CON = 0b00010001;     // TMR1 is ON, 1:2 prescale, =1MHz
	T2CON = 0b00000101;     // TMR2 is ON, 1:4 prescale, =1MHz



	MyVal = 0; //initializn these variables here
	MyMinVal = 0;
	MyMaxVal = 99;
  
	TRISB=0x00; 
	TRISC=0xFC;
    
	lcd_init(); //call LCD initialisation
}

char WaitForInput(void){
char done;
char temp;
done = 0;

while(!done){
    if(!ENTERSETTINGS_SW){
        temp = 1;
        done = 0xff;
    }

    if(!HOME_SW){
        temp = 2;
        done = 0xff;
    }

    if(!INCREASE_SW){
        temp = 3;
        done = 0xff;
    }

    if(!DECREASE_SW){
        temp = 4;
        done = 0xff;
    }
}//end of while
    DelayMs(150);    //debounce
    return temp;
}//
void userMenu(char pos){
	lcd_clear();
	lcd_goto_L1();

	switch(pos){
		case 0:
			lcd_puts("    HEIGHT    ");
			break;		
		case 1:
			lcd_puts("    RANGE     ");
			break;		
		case 2:
			lcd_puts(" SURFACE AREA ");
			break;		
	}

	lcd_goto_L2();
	lcd_puts("Press Up/Down"); //home screen message (line 2)
}
// New Menu System
void EnterHeight(void){
	lcd_clear();
	lcd_goto_L1();
	lcd_puts(" ENTER HEIGHT ");
	lcd_goto_L2();
	lcd_puts("Press Up/Down"); //home screen message (line 2)
}

void EnterScreen(void){
	lcd_clear();
	lcd_goto_L1();
	lcd_puts(" [cm] ");
}

void ShowDigits(unsigned char val){

    MyValLCD[0] = val /10;    //returns the quotient (if temp = 35 the result is 3)
    MyValLCD[1] = val % 10; 	//Returns remainder   (if temp = 35 the result is 5)

	MyValLCD[0] += 0x30;	//to ASCII
	MyValLCD[1] += 0x30;	//to ASCII

	EnterScreen();
	lcd_goto_L2();
	lcd_data(MyValLCD[0]);	//to LCD
	lcd_data(MyValLCD[1]);  //to LCD
}

  
void calc_distance(void)
{
	// from the transeiver datasheet the analog voltage is
	// the inverse of distance, so distance can be calculated
	// d = (1 / volts) then just scaled to suit the transeiver

	// load ADC value in 16bit math var
	math = ADRESH;
	math = (math * 256);
	math += ADRESL;

	// now invert it; (1 / volts) use (6050 / volts) for scaling
	math = (6050 / math);
	if(math >= 2) math -=2;		// fix linear error (-2)
	if(math > 99) math = 99;	// max limit at 99cm

	// convert from 0-99 to 2 decimal digits, 0-99cm
	cm10=0;
	while(math >= 10)
	{
		cm10++;
		math -= 10;
	}
	cm = math;
}
 //
unsigned char user_input(void)		//This will return what we want
{
	char done = 0;

	MyVal = 0;			//Start on 0
	while(done == 0){
   	  input_sw = WaitForInput();
		
      switch(input_sw){
		case 1:
			done = 0xff; 			//This tells us the user finished entering
			lcd_goto_L1();
			lcd_puts("     Done!      "); //home screen message (line 1)
			break;
      	case 3:
			if(MyVal < MyMaxVal)
      			MyVal++;
			EnterScreen();
			ShowDigits(MyVal);
			break;
      	case 4:
			if(MyVal > MyMinVal)
      			MyVal--;
			EnterScreen();
			ShowDigits(MyVal);
        	break;
		default: 
			break;
      }
	  
	}
	DelayS(1);
	return MyVal;
}

void home_screen(void){
	mnuPOS = 0;
	lcd_clear();
	lcd_goto_L1();
	lcd_puts("INFRARED LIQUID"); //home screen message (line 1)
	lcd_goto_L2();
	lcd_puts("LEVEL DETECTOR"); //home screen message (line 2)
  
	input_sw = 0;			//Reset the value

	while(input_sw != 1)  	//Wait until enter is pressed
		input_sw = WaitForInput();

	userMenu(0);
	DelayMs(2);				//shorter delay 
    height = user_input();	//The HEIGHT var will have the myVal

	userMenu(1);
	DelayMs(2);				//shorter delay 
    range = user_input();	//The HEIGHT var will have the myVal

	userMenu(2);
	DelayMs(2);				//shorter delay 
    area = user_input();	//The HEIGHT var will have the myVal
/*
"enter height"
call enter/settings (which is now user input function in new code)
height=MyVal

"enter range"
call user_input
range=MyVal

"enter surface area"
call user_input
area=MyVal
*/
}
  
//*********************************************************
/*  Junebug_Serial4.c    RomanBlack.com 19th July 2009.

    uses "zero error 1 second timer"
	system to generate a 2 second interval, then every
	2 seconds it reads an analog voltage from a
	Sharp GP2 distance sensor and converts it to decimal
	distance, then sends that data to a LCD

	Code for MikroC, config handled by MikroC compiler;
	_INTI02_OSC_1H
	_WDT_OFF_2H
	-MCLRE_ON_3H
	_LVP_OFF_4L
	_PWRT_ON_2L
*/
//*********************************************************

void main(void)
{  
    init();	// initialise I/O ports, LCD
    while(1){
 	//	home_screen();

 	// wait for 2 seconds, uses TMR1 free running at 1Mhz
   	while(!TMR1IF)  // wait for TMR1 overflow
	TMR1IF = 0; 	// clear overflow flag

	bres += 65536;			// add 65536uS to bres value
	if(bres >= 2000000)		// if reached 2 seconds!
	 {
    	bres -= 2000000;	// subtract 2 seconds, keep error
		  	
		// read the ADC voltage RA1 (Sharp GP2 sensor)
		GODONE=1;	// initiate conversion on the channel 0
		while(GODONE) continue;  // Wait convertion done
		calc_distance();		// convert ADC value to distance
		[COLOR="Red"][B]cm2LCD=cm+0x30;    //converting cm(from calc_distance procedure) to ascii 
		lcd_data(cm2LCD);[/B][/COLOR]

      				         
       }  
            
 
   } 
}
 
Last edited:
actually i tried something like this 1st:

Code:
tempvar=calc_distance();   // tempvar assigned cm value from calc_distance procedure 

cm2LCD=tempvar+0x30; 
 lcd_data(cm2LCD);

but i was messing up the var types
 
Last edited:
You have to remember this. Can CM be higher than 09 ? if so then you should spilt the variable CM into 2 like
Code:
cmHigh = ( ( cm >> 4 ) & 0x0F ) + 0x30;
cmLow = ( cm & 0x0F ) + 0x30;

lcd_data(cmHigh);
lcd_data(cmLow);
This will give you 2 seperate ASCII values for the LCD.
 
Last edited:
the sensor has a range from 10cm to 80 cm So yes CM can be higher than 09.


I ran my code that i posted and the LCD displays random numbers when i power up.

EG. 12768126498124987491874
12094809128409283498239

But when i put a page in front the sensor and i hold it steadily then the sensor displays

EG. 888888888888888888888888
888888888888888888888888

and if i move it away and hold it steady it displays

4444444444444444444444444444
4444444444444444444444444444

if its not steady then the numbers are just random......im guessing im not formatting it correctly or something......


im going to try what you suggested now..........


its improving.................

when i power up its random numbers

now when i put a page & hold it steady....

EG.

010101010101010
010101010101010

i keep moving it away and...

020202020202020
020202020202020

030303030303030
030303030303030

till.....

090909090909090
090909090909090

THEN
000000000000000
000000000000000

and it repeats the sequence

010101010101010
010101010101010...............

and so on......

BTW

(it does not go to 111111111111111
111111111111111)

and 1212121212121212
1212121212121212 .....etc)


i just measured manually with a ruler to verify the values....its accurate

meaning....
the sensor has a min range of 10 cm......
so when the page is 10cm away it displays....

000000000000000
000000000000000

and if it 5cm away from the min range(ie. 15cm)

then it display:

050505050505050
050505050505050

and i measured this with a ruler and it gives 15cm
 
Last edited:
Its not giving you a long value lol its looping
:eek::eek::eek::eek::rolleyes::rolleyes::rolleyes::p:p:p:):):)

clearing the LCD gets rid of the lol "LooooOOooG values":D;)

Thanks Atomsoft.........

*****************************************************
Shady's back....back again...hehe

AtomSoft im trying to integrate the user input with the sensor code.....
(both work separately)

When i uncomment home and run the code....it allows the user to enter values but when i press enter ...and am done! it returns to the home screen..."IR LLD'
which is what i want....

BUT i want it to display the distance measurements after that and not stick on the home screen.....

do i need a lcd_clear() somewhere again ???to clear "IR LLD" and show the distance....


or do i need to prevent the used input function from coming back to the home screen when entering inputs is "done!"

:confused:???

Code:
#include <pic.h>
#include "pic.h"
#include "delay.h"
#include "math.h" 
#include <stdio.h>
#include <stdlib.h>	//

void userMenu(char pos); //  
void FloatToStr(float , char[]); 
void DelayMs(unsigned char);
void lcd_cmd(unsigned char);
void lcd_data(unsigned char); 
void lcd_clear(void);
void lcd_puts(const char[]);
void lcd_goto_L1(void);
void lcd_goto_L2(void);
void lcd_cursor(unsigned char);
void lcd_init(void);
void init(void);
char WaitForInput(void);
unsigned char user_input(void);
void home_screen(void);
void EnterHeight(void);
void EnterScreen(void);
void ShowDigits(unsigned char val);
void calc_distance(void);
void main(void);


unsigned char cm2LCD;

unsigned char cmHigh, cmLow;

      

#define LCD_RS RC0		//LCD RS pin
#define LCD_EN RC1		//LCD EN pin
#define LCD_STROBE()	LCD_EN = 1; asm("nop"); asm("nop"); LCD_EN = 0

unsigned char cm10;		//
unsigned char cm;		//
unsigned int math;		// used for voltage calculations

unsigned int LiquidLevel;
unsigned char NumDec;
unsigned char NumSep[2];
   
unsigned char i,j,k;
//char temp[8];
//[/b]
unsigned char height=50;
unsigned char range;
unsigned char area;
unsigned char SensorPos=10;
//[/b] New Vars
char input_sw;

  
char mnuPOS;

unsigned char MyVal;
unsigned char MyValLCD[2];
unsigned char MyMaxVal;
unsigned char MyMinVal;
unsigned long bres;		// for bresenham 2-second timer system

unsigned char ;

#define HOME_SW RC2				//HOME switch	
#define INCREASE_SW RC3			//INCREASE switch
#define DECREASE_SW RC4			//DECREASE switch
#define ENTERSETTINGS_SW RA4	//ENTERSETTINGS switch



///////////////////////CONVERT FLOAT TO STRING///////////////////
// This function was taken from the CAVR library. It was modified slightly
// to suit our design.
void FloatToStr(float n, char str[])
{
float scale;
unsigned char d,f;
f=0;i=0;
if (n<0.0) {n=-n; str[f]='-'; f++;}; 
n=n+0.005;
scale=1.0;
while (n>=scale) {scale=scale*10.0; ++i;};
if (i==0) {str[f]='0'; f++;}
else
while (i--)
	  {
      scale=floor(0.5+scale/10.0);
      d=(unsigned char) (n/scale);
      str[f]=d+'0';
      n=n-scale*d;
	  f++;
      };

str[f]='.';
f++;
for (j=0;j<=1;j++) //2 decimal points
      {
      n=n*10.0;
      d=(unsigned char) n;
      str[f]=d+'0';
      n=n-d;
	  f++;
	  };
str[f]='\0';
}
///////////////////END CONVERT FLOAT TO STRING///////////////////

/////////////////////////////DELAY///////////////////////////////
void DelayMs(unsigned char cnt)
{
#if	XTAL_FREQ <= 2MHZ
	do {
		DelayUs(996);
	} while(--cnt);
#endif

#if    XTAL_FREQ > 2MHZ	
	unsigned char	p;
	do {
		p = 4;
		do { 
			DelayUs(250);
		} while(--p);  
	} while(--cnt);
#endif
}

void DelayS(unsigned char cnt)
{
	for (j=0; j<(cnt*10); j++)
		DelayMs(100);
}
///////////////////////////DELAY END/////////////////////////////

//////////////////////////////LCD SETUP//////////////////////////
/* send a command to the LCD */
void lcd_cmd(unsigned char c)
{
	DelayMs(2); //wait for LCD to be ready shorter delay 
	LCD_RS = 0;	 //write instruction
	PORTB = (c & 0xF0); //load upper nibble on LCD data lines
	LCD_STROBE(); //send instruction to LCD
	PORTB = ((c << 4) & 0xF0); //load upper nibble on LCD data lines
	LCD_STROBE(); //send instruction to LCD   	
}

/* send data to the LCD */
void lcd_data(unsigned char c)
{
	DelayMs(2); //wait for LCD to be ready shorter delay 
	PORTB = 0x00;
	LCD_RS = 1; //write data
    PORTB |= (c & 0xF0); //load upper nibble on LCD data lines     
	LCD_STROBE(); //send instruction to LCD
	PORTB &= 0x00; //load upper nibble on LCD data lines
	PORTB |= ( (c << 4) & 0xF0); 
	LCD_STROBE(); //send instruction to LCD
}

/*Clear the LCD*/
void lcd_clear(void)
{
	lcd_cmd(0x01); //command to clear LCD
}

/*write a string of chars to the LCD*/
void lcd_puts(const char s[])
{
	j = -1;
	while(s[++j]!=('\0')) // send characters until null character reached
		lcd_data(s[j]);
}

/*go to beginning of line 1*/
void lcd_goto_L1(void)
{
	lcd_cmd(0b10000000); // command to go to line 1
}

/*go to beginning of line 2*/
void lcd_goto_L2(void)
{
	lcd_cmd(0b11000000); // command to go to line 2
}

/*move cursor "x" positions to the right*/
void lcd_cursor(unsigned char x)
{
	lcd_cmd(((x)&0x7F)|0x80); 
}

/*initialise the LCD - put into 4 bit mode*/
void lcd_init(void)
{
	LCD_RS = 0;	
	LCD_EN = 0;
	DelayMs(20); //wait for LCD startup
	lcd_cmd(0x02);
 
	lcd_cmd(0x28);	// 4-bit mode
	lcd_cmd(0x08);	// display off
	lcd_cmd(0x01);	// clear display
	lcd_cmd(0x0C);	// disp. on, cursor off, cursor blink off
	lcd_cmd(0x06);	// entry mode
	lcd_cmd(0x80);  // initialise DDRAM address to zero
}
//////////////////////////LCD SETUP END//////////////////////////
    

void init(void)
{   
	
	// setup the PIC 16f690
	OSCCON = 0x72;      	// internal osc, 8MHz


	PORTA = 0;
	TRISA = 0b10010010; 	// RA7 high imp, RA3 is serial out, RA4 button input 


	
 	PORTB = 0;          	// PORTB not used
    WPUB = 1;				// PORTB pullups ON		
    RABPU = 0;


    /* Init ADC */
    ADCON0 = 0b10000101;	// bit 7 right justify,analogue channel select bits bits5-2  0001=AN1,ADC ON, RA1 is ADC input
	ADCON1 = 0b00100000;	//bits6-4  fosc/32
    ADON=1;	                // turn on the A2D conversion module

	
	ANSEL=0x02;            //set RA1 as analog input for GP2 sensor
	ANSELH=0x00;

    T1CON = 0b00010001;     // TMR1 is ON, 1:2 prescale, =1MHz
	T2CON = 0b00000101;     // TMR2 is ON, 1:4 prescale, =1MHz



	MyVal = 0; //initializn these variables here
	MyMinVal = 0;
	MyMaxVal = 99;
  
	TRISB=0x00; 
	TRISC=0xFC;
    
	lcd_init(); //call LCD initialisation

}

char WaitForInput(void){
char done;
char temp;
done = 0;

while(!done){
    if(!ENTERSETTINGS_SW){
        temp = 1;
        done = 0xff;
    }

    if(!HOME_SW){
        temp = 2;
        done = 0xff;
    }

    if(!INCREASE_SW){
        temp = 3;
        done = 0xff;
    }

    if(!DECREASE_SW){
        temp = 4;
        done = 0xff;
    }
}//end of while
    DelayMs(150);    //debounce
    return temp;
}//
void userMenu(char pos){
	lcd_clear();
	lcd_goto_L1();

	switch(pos){
		case 0:
			lcd_puts("    HEIGHT    ");
			break;		
		case 1:
			lcd_puts("    RANGE     ");
			break;		
		case 2:
			lcd_puts(" SURFACE AREA ");
			break;		
	}

	lcd_goto_L2();
	lcd_puts("Press Up/Down"); //home screen message (line 2)
}
// New Menu System
void EnterHeight(void){
	lcd_clear();
	lcd_goto_L1();
	lcd_puts(" ENTER HEIGHT ");
	lcd_goto_L2();
	lcd_puts("Press Up/Down"); //home screen message (line 2)
}

void EnterScreen(void){
	lcd_clear();
	lcd_goto_L1();
	lcd_puts(" [cm] ");
}

void ShowDigits(unsigned char val){

    MyValLCD[0] = val /10;    //returns the quotient (if temp = 35 the result is 3)
    MyValLCD[1] = val % 10; 	//Returns remainder   (if temp = 35 the result is 5)

	MyValLCD[0] += 0x30;	//to ASCII
	MyValLCD[1] += 0x30;	//to ASCII

	EnterScreen();
	lcd_goto_L2();
	lcd_data(MyValLCD[0]);	//to LCD
	lcd_data(MyValLCD[1]);  //to LCD
}

  
void calc_distance(void)
{
	// from the transeiver datasheet the analog voltage is
	// the inverse of distance, so distance can be calculated
	// d = (1 / volts) then just scaled to suit the transeiver

	// load ADC value in 16bit math var
	math = ADRESH;
	math = (math * 256);
	math += ADRESL;

	// now invert it; (1 / volts) use (6050 / volts) for scaling
	math = (6050 / math);
	if(math >= 2) math -=2;		// fix linear error (-2)
	if(math > 99) math = 99;	// max limit at 99cm

    //LiquidLevel=height-;	

	// convert from 0-99 to 2 decimal digits, 0-99cm
	cm10=0;
	while(math >= 10)
	{
		cm10++;
		math -= 10;
	}
	cm = math;
}
 //
unsigned char user_input(void)		//This will return what we want
{
	char done = 0;
 
	MyVal = 0;			//Start on 0
	while(done == 0){
   	  input_sw = WaitForInput();
		
      switch(input_sw){
		case 1:
			done = 0xff; 			//This tells us the user finished entering
			lcd_goto_L1();
			lcd_puts("     Done!      "); //home screen message (line 1)
			break;
      	case 3:
			if(MyVal < MyMaxVal)
      			MyVal++;
			EnterScreen();
			ShowDigits(MyVal);
			break;
      	case 4:
			if(MyVal > MyMinVal)
      			MyVal--;
			EnterScreen();
			ShowDigits(MyVal);
        	break;
		default: 
			break;
      }
	  
	}
	DelayS(1);
	return MyVal;
}

void home_screen(void){
	mnuPOS = 0;
	lcd_clear();
	lcd_goto_L1();
	lcd_puts("INFRARED LIQUID"); //home screen message (line 1)
	lcd_goto_L2();
	lcd_puts("LEVEL DETECTOR"); //home screen message (line 2)
  
	input_sw = 0;			//Reset the value

	while(input_sw != 1)  	//Wait until enter is pressed
		input_sw = WaitForInput();

	userMenu(0);
	DelayMs(2);				//shorter delay 
    height = user_input();	//The HEIGHT var will have the myVal

	userMenu(1);
	DelayMs(2);				//shorter delay 
    range = user_input();	//The HEIGHT var will have the myVal

	userMenu(2);
	DelayMs(2);				//shorter delay 
    area = user_input();	//The HEIGHT var will have the myVal
/*
"enter height"
call enter/settings (which is now user input function in new code)
height=MyVal

"enter range"
call user_input
range=MyVal

"enter surface area"
call user_input
area=MyVal
*/
}
  
//*********************************************************
/*  Junebug_Serial4.c    RomanBlack.com 19th July 2009.

    uses "zero error 1 second timer"
	system to generate a 2 second interval, then every
	2 seconds it reads an analog voltage from a
	Sharp GP2 distance sensor and converts it to decimal
	distance, then sends that data to a LCD

	Code for MikroC, config handled by MikroC compiler;
	_INTI02_OSC_1H
	_WDT_OFF_2H
	-MCLRE_ON_3H
	_LVP_OFF_4L 
	_PWRT_ON_2L
*/
//*********************************************************

void main(void)
{  
    init();	// initialise I/O ports, LCD
    while(1){
 	[B][COLOR="Red"]home_screen();[/COLOR][/B]

 	// wait for 2 seconds, uses TMR1 free running at 1Mhz
   	while(!TMR1IF)  // wait for TMR1 overflow
	TMR1IF = 0; 	// clear overflow flag

	bres += 65536;			// add 65536uS to bres value
	if(bres >= 2000000)		// if reached 2 seconds!
	 {
    	bres -= 2000000;	// subtract 2 seconds, keep error
		  	
		// read the ADC voltage RA1 (Sharp GP2 sensor)
		GODONE=1;	// initiate conversion on the channel 0
		while(GODONE) continue;  // Wait convertion done
		calc_distance();		// convert ADC value to distance
	//	LiquidLevel=height-;
		lcd_clear();                // 
		lcd_data(cm10 + '0');
		lcd_data(cm + '0');
	    lcd_goto_L2(); 
	    lcd_puts("[cm] ");

 
    
       }  
             
 
   } 
}
 
Last edited:
Try this:
Code:
#include <pic.h>
#include "pic.h"
#include "delay.h"
#include "math.h" 
#include <stdio.h>
#include <stdlib.h>	//

void userMenu(char pos); //  
void FloatToStr(float , char[]); 
void DelayMs(unsigned char);
void lcd_cmd(unsigned char);
void lcd_data(unsigned char); 
void lcd_clear(void);
void lcd_puts(const char[]);
void lcd_goto_L1(void);
void lcd_goto_L2(void);
void lcd_cursor(unsigned char);
void lcd_init(void);
void init(void);
char WaitForInput(void);
unsigned char user_input(void);
void home_screen(void);
void EnterHeight(void);
void EnterScreen(void);
void ShowDigits(unsigned char val);
void calc_distance(void);
void main(void);


unsigned char cm2LCD;

unsigned char cmHigh, cmLow;

      

#define LCD_RS RC0		//LCD RS pin
#define LCD_EN RC1		//LCD EN pin
#define LCD_STROBE()	LCD_EN = 1; asm("nop"); asm("nop"); LCD_EN = 0

unsigned char cm10;		//
unsigned char cm;		//
unsigned int math;		// used for voltage calculations

unsigned int LiquidLevel;
unsigned char NumDec;
unsigned char NumSep[2];
   
unsigned char i,j,k;
//char temp[8];
//[/b]
unsigned char height=50;
unsigned char range;
unsigned char area;
unsigned char SensorPos=10;
//[/b] New Vars
char input_sw;

  
char mnuPOS;

unsigned char MyVal;
unsigned char MyValLCD[2];
unsigned char MyMaxVal;
unsigned char MyMinVal;
unsigned long bres;		// for bresenham 2-second timer system

unsigned char ;

#define HOME_SW RC2				//HOME switch	
#define INCREASE_SW RC3			//INCREASE switch
#define DECREASE_SW RC4			//DECREASE switch
#define ENTERSETTINGS_SW RA4	//ENTERSETTINGS switch



///////////////////////CONVERT FLOAT TO STRING///////////////////
// This function was taken from the CAVR library. It was modified slightly
// to suit our design.
void FloatToStr(float n, char str[])
{
float scale;
unsigned char d,f;
f=0;i=0;
if (n<0.0) {n=-n; str[f]='-'; f++;}; 
n=n+0.005;
scale=1.0;
while (n>=scale) {scale=scale*10.0; ++i;};
if (i==0) {str[f]='0'; f++;}
else
while (i--)
	  {
      scale=floor(0.5+scale/10.0);
      d=(unsigned char) (n/scale);
      str[f]=d+'0';
      n=n-scale*d;
	  f++;
      };

str[f]='.';
f++;
for (j=0;j<=1;j++) //2 decimal points
      {
      n=n*10.0;
      d=(unsigned char) n;
      str[f]=d+'0';
      n=n-d;
	  f++;
	  };
str[f]='\0';
}
///////////////////END CONVERT FLOAT TO STRING///////////////////

/////////////////////////////DELAY///////////////////////////////
void DelayMs(unsigned char cnt)
{
#if	XTAL_FREQ <= 2MHZ
	do {
		DelayUs(996);
	} while(--cnt);
#endif

#if    XTAL_FREQ > 2MHZ	
	unsigned char	p;
	do {
		p = 4;
		do { 
			DelayUs(250);
		} while(--p);  
	} while(--cnt);
#endif
}

void DelayS(unsigned char cnt)
{
	for (j=0; j<(cnt*10); j++)
		DelayMs(100);
}
///////////////////////////DELAY END/////////////////////////////

//////////////////////////////LCD SETUP//////////////////////////
/* send a command to the LCD */
void lcd_cmd(unsigned char c)
{
	DelayMs(2); //wait for LCD to be ready shorter delay 
	LCD_RS = 0;	 //write instruction
	PORTB = (c & 0xF0); //load upper nibble on LCD data lines
	LCD_STROBE(); //send instruction to LCD
	PORTB = ((c << 4) & 0xF0); //load upper nibble on LCD data lines
	LCD_STROBE(); //send instruction to LCD   	
}

/* send data to the LCD */
void lcd_data(unsigned char c)
{
	DelayMs(2); //wait for LCD to be ready shorter delay 
	PORTB = 0x00;
	LCD_RS = 1; //write data
    PORTB |= (c & 0xF0); //load upper nibble on LCD data lines     
	LCD_STROBE(); //send instruction to LCD
	PORTB &= 0x00; //load upper nibble on LCD data lines
	PORTB |= ( (c << 4) & 0xF0); 
	LCD_STROBE(); //send instruction to LCD
}

/*Clear the LCD*/
void lcd_clear(void)
{
	lcd_cmd(0x01); //command to clear LCD
}

/*write a string of chars to the LCD*/
void lcd_puts(const char s[])
{
	j = -1;
	while(s[++j]!=('\0')) // send characters until null character reached
		lcd_data(s[j]);
}

/*go to beginning of line 1*/
void lcd_goto_L1(void)
{
	lcd_cmd(0b10000000); // command to go to line 1
}

/*go to beginning of line 2*/
void lcd_goto_L2(void)
{
	lcd_cmd(0b11000000); // command to go to line 2
}

/*move cursor "x" positions to the right*/
void lcd_cursor(unsigned char x)
{
	lcd_cmd(((x)&0x7F)|0x80); 
}

/*initialise the LCD - put into 4 bit mode*/
void lcd_init(void)
{
	LCD_RS = 0;	
	LCD_EN = 0;
	DelayMs(20); //wait for LCD startup
	lcd_cmd(0x02);
 
	lcd_cmd(0x28);	// 4-bit mode
	lcd_cmd(0x08);	// display off
	lcd_cmd(0x01);	// clear display
	lcd_cmd(0x0C);	// disp. on, cursor off, cursor blink off
	lcd_cmd(0x06);	// entry mode
	lcd_cmd(0x80);  // initialise DDRAM address to zero
}
//////////////////////////LCD SETUP END//////////////////////////
    

void init(void)
{   
	
	// setup the PIC 16f690
	OSCCON = 0x72;      	// internal osc, 8MHz


	PORTA = 0;
	TRISA = 0b10010010; 	// RA7 high imp, RA3 is serial out, RA4 button input 


	
 	PORTB = 0;          	// PORTB not used
    WPUB = 1;				// PORTB pullups ON		
    RABPU = 0;


    /* Init ADC */
    ADCON0 = 0b10000101;	// bit 7 right justify,analogue channel select bits bits5-2  0001=AN1,ADC ON, RA1 is ADC input
	ADCON1 = 0b00100000;	//bits6-4  fosc/32
    ADON=1;	                // turn on the A2D conversion module

	
	ANSEL=0x02;            //set RA1 as analog input for GP2 sensor
	ANSELH=0x00;

    T1CON = 0b00010001;     // TMR1 is ON, 1:2 prescale, =1MHz
	T2CON = 0b00000101;     // TMR2 is ON, 1:4 prescale, =1MHz



	MyVal = 0; //initializn these variables here
	MyMinVal = 0;
	MyMaxVal = 99;
  
	TRISB=0x00; 
	TRISC=0xFC;
    
	lcd_init(); //call LCD initialisation

}

char WaitForInput(void){
char done;
char temp;
done = 0;

while(!done){
    if(!ENTERSETTINGS_SW){
        temp = 1;
        done = 0xff;
    }

    if(!HOME_SW){
        temp = 2;
        done = 0xff;
    }

    if(!INCREASE_SW){
        temp = 3;
        done = 0xff;
    }

    if(!DECREASE_SW){
        temp = 4;
        done = 0xff;
    }
}//end of while
    DelayMs(150);    //debounce
    return temp;
}//
void userMenu(char pos){
	lcd_clear();
	lcd_goto_L1();

	switch(pos){
		case 0:
			lcd_puts("    HEIGHT    ");
			break;		
		case 1:
			lcd_puts("    RANGE     ");
			break;		
		case 2:
			lcd_puts(" SURFACE AREA ");
			break;							//[b]
		case 3:
			lcd_puts("   Measured   ");
			lcd_goto_L2();
			lcd_data(cm10 + '0');
			lcd_data(cm + '0');
			lcd_puts(" [cm] ");
			break;		//[/b]
	}

	if(pos == 3) return;
	lcd_goto_L2();
	lcd_puts("Press Up/Down"); //home screen message (line 2)
}
// New Menu System
void EnterHeight(void){
	lcd_clear();
	lcd_goto_L1();
	lcd_puts(" ENTER HEIGHT ");
	lcd_goto_L2();
	lcd_puts("Press Up/Down"); //home screen message (line 2)
}

void EnterScreen(void){
	lcd_clear();
	lcd_goto_L1();
	lcd_puts(" [cm] ");
}

void ShowDigits(unsigned char val){

    MyValLCD[0] = val /10;    //returns the quotient (if temp = 35 the result is 3)
    MyValLCD[1] = val % 10; 	//Returns remainder   (if temp = 35 the result is 5)

	MyValLCD[0] += 0x30;	//to ASCII
	MyValLCD[1] += 0x30;	//to ASCII

	EnterScreen();
	lcd_goto_L2();
	lcd_data(MyValLCD[0]);	//to LCD
	lcd_data(MyValLCD[1]);  //to LCD
}

  
void calc_distance(void)
{
	// from the transeiver datasheet the analog voltage is
	// the inverse of distance, so distance can be calculated
	// d = (1 / volts) then just scaled to suit the transeiver

	// load ADC value in 16bit math var
	math = ADRESH;
	math = (math * 256);
	math += ADRESL;

	// now invert it; (1 / volts) use (6050 / volts) for scaling
	math = (6050 / math);
	if(math >= 2) math -=2;		// fix linear error (-2)
	if(math > 99) math = 99;	// max limit at 99cm

    //LiquidLevel=height-;	

	// convert from 0-99 to 2 decimal digits, 0-99cm
	cm10=0;
	while(math >= 10)
	{
		cm10++;
		math -= 10;
	}
	cm = math;
}
 //
unsigned char user_input(void)		//This will return what we want
{
	char done = 0;
 
	MyVal = 0;			//Start on 0
	while(done == 0){
   	  input_sw = WaitForInput();
		
      switch(input_sw){
		case 1:
			done = 0xff; 			//This tells us the user finished entering
			lcd_goto_L1();
			lcd_puts("     Done!      "); //home screen message (line 1)
			break;
      	case 3:
			if(MyVal < MyMaxVal)
      			MyVal++;
			EnterScreen();
			ShowDigits(MyVal);
			break;
      	case 4:
			if(MyVal > MyMinVal)
      			MyVal--;
			EnterScreen();
			ShowDigits(MyVal);
        	break;
		default: 
			break;
      }
	  
	}
	DelayS(1);
	return MyVal;
}

void home_screen(void){
	mnuPOS = 0;
	lcd_clear();
	lcd_goto_L1();
	lcd_puts("INFRARED LIQUID"); //home screen message (line 1)
	lcd_goto_L2();
	lcd_puts("LEVEL DETECTOR"); //home screen message (line 2)
  
	input_sw = 0;			//Reset the value

	while(input_sw != 1)  	//Wait until enter is pressed
		input_sw = WaitForInput();

	userMenu(0);
	DelayMs(2);				//shorter delay 
    height = user_input();	//The HEIGHT var will have the myVal

	userMenu(1);
	DelayMs(2);				//shorter delay 
    range = user_input();	//The HEIGHT var will have the myVal

	userMenu(2);
	DelayMs(2);				//shorter delay 
    area = user_input();	//The HEIGHT var will have the myVal
//[b]
	userMenu(3);
	DelayMs(2);				//shorter delay 
	input_sw = 0;			//Reset the value

	while(input_sw != 1)  	//Wait until enter is pressed
		input_sw = WaitForInput();
//Waits for user to press ENTER to show home screen[/b]
/*
"enter height"
call enter/settings (which is now user input function in new code)
height=MyVal

"enter range"
call user_input
range=MyVal

"enter surface area"
call user_input
area=MyVal
*/
}
  
//*********************************************************
/*  Junebug_Serial4.c    RomanBlack.com 19th July 2009.

    uses "zero error 1 second timer"
	system to generate a 2 second interval, then every
	2 seconds it reads an analog voltage from a
	Sharp GP2 distance sensor and converts it to decimal
	distance, then sends that data to a LCD

	Code for MikroC, config handled by MikroC compiler;
	_INTI02_OSC_1H
	_WDT_OFF_2H
	-MCLRE_ON_3H
	_LVP_OFF_4L 
	_PWRT_ON_2L
*/
//*********************************************************

void main(void)
{  
    init();	// initialise I/O ports, LCD
    while(1){
 	home_screen();

 	// wait for 2 seconds, uses TMR1 free running at 1Mhz
   	while(!TMR1IF)  // wait for TMR1 overflow
	TMR1IF = 0; 	// clear overflow flag

	bres += 65536;			// add 65536uS to bres value
	if(bres >= 2000000)		// if reached 2 seconds!
	 {
    	bres -= 2000000;	// subtract 2 seconds, keep error
		  	
		// read the ADC voltage RA1 (Sharp GP2 sensor)
		GODONE=1;	// initiate conversion on the channel 0
		while(GODONE) continue;  // Wait convertion done
		calc_distance();		// convert ADC value to distance
	//	LiquidLevel=height-;
		lcd_clear();                // Using the new menu you can erase or[b]
		lcd_data(cm10 + '0');
		lcd_data(cm + '0');
	    lcd_goto_L2(); 
	    lcd_puts("[cm] ");			//comment this out if you want[/b]

 
    
       }  
             
 
   } 
}
 
Wow Proteus is so NEAT!....hehe is that shareware LOL i doubt it:D................Id like to get that software package...............


:)rolleyes: I will "search" for it hehehe-cough cough RapidShare cough:D)


That clip is so cool....
 
uhmm i tried the code you posted...it shows the user menu stuff....

height, range, surface area..........and then:



Measured

00 [cm]

the values are not changing
 
Last edited:
Well the integration works now...I repositioned some stuff.
i moved the code in bold black to the bold red section without using a break.... the user interface works and after that the sensor works continuously

if i use a break then the sensor captures a single digit and the lcd displays just 1 digit not continuous values...

but i need continuous readings and i ALSO need to get to home screen if the user presses the home button....what should i do to get that???



Code:
#include <pic.h>
#include "pic.h"
#include "delay.h"
#include "math.h" 
#include <stdio.h>
#include <stdlib.h>	//

void userMenu(char pos); //  
void FloatToStr(float , char[]); 
void DelayMs(unsigned char);
void lcd_cmd(unsigned char);
void lcd_data(unsigned char); 
void lcd_clear(void);
void lcd_puts(const char[]);
void lcd_goto_L1(void);
void lcd_goto_L2(void);
void lcd_cursor(unsigned char);
void lcd_init(void);
void init(void);
char WaitForInput(void);
unsigned char user_input(void);
void home_screen(void);
void EnterHeight(void);
void EnterScreen(void);
void ShowDigits(unsigned char val);
void calc_distance(void);
void main(void);


unsigned char cm2LCD;

unsigned char cmHigh, cmLow;

      

#define LCD_RS RC0		//LCD RS pin
#define LCD_EN RC1		//LCD EN pin
#define LCD_STROBE()	LCD_EN = 1; asm("nop"); asm("nop"); LCD_EN = 0

unsigned char cm10;		//
unsigned char cm;		//
unsigned int math;		// used for voltage calculations

unsigned int LiquidLevel;
unsigned char NumDec;
unsigned char NumSep[2];
   
unsigned char i,j,k;
//char temp[8];
//[/b]
unsigned char height=50;
unsigned char range;
unsigned char area;
unsigned char SensorPos=10;
//[/b] New Vars
char input_sw;

  
char mnuPOS;

unsigned char MyVal;
unsigned char MyValLCD[2];
unsigned char MyMaxVal;
unsigned char MyMinVal;
unsigned long bres;		// for bresenham 2-second timer system

unsigned char ;

#define HOME_SW RC2				//HOME switch	
#define INCREASE_SW RC3			//INCREASE switch
#define DECREASE_SW RC4			//DECREASE switch
#define ENTERSETTINGS_SW RA4	//ENTERSETTINGS switch



///////////////////////CONVERT FLOAT TO STRING///////////////////
// This function was taken from the CAVR library. It was modified slightly
// to suit our design.
void FloatToStr(float n, char str[])
{
float scale;
unsigned char d,f;
f=0;i=0;
if (n<0.0) {n=-n; str[f]='-'; f++;}; 
n=n+0.005;
scale=1.0;
while (n>=scale) {scale=scale*10.0; ++i;};
if (i==0) {str[f]='0'; f++;}
else
while (i--)
	  {
      scale=floor(0.5+scale/10.0);
      d=(unsigned char) (n/scale);
      str[f]=d+'0';
      n=n-scale*d;
	  f++;
      };

str[f]='.';
f++;
for (j=0;j<=1;j++) //2 decimal points
      {
      n=n*10.0;
      d=(unsigned char) n;
      str[f]=d+'0';
      n=n-d;
	  f++;
	  };
str[f]='\0';
}
///////////////////END CONVERT FLOAT TO STRING///////////////////

/////////////////////////////DELAY///////////////////////////////
void DelayMs(unsigned char cnt)
{
#if	XTAL_FREQ <= 2MHZ
	do {
		DelayUs(996);
	} while(--cnt);
#endif

#if    XTAL_FREQ > 2MHZ	
	unsigned char	p;
	do {
		p = 4;
		do { 
			DelayUs(250);
		} while(--p);  
	} while(--cnt);
#endif
}

void DelayS(unsigned char cnt)
{
	for (j=0; j<(cnt*10); j++)
		DelayMs(100);
}
///////////////////////////DELAY END/////////////////////////////

//////////////////////////////LCD SETUP//////////////////////////
/* send a command to the LCD */
void lcd_cmd(unsigned char c)
{
	DelayMs(2); //wait for LCD to be ready shorter delay 
	LCD_RS = 0;	 //write instruction
	PORTB = (c & 0xF0); //load upper nibble on LCD data lines
	LCD_STROBE(); //send instruction to LCD
	PORTB = ((c << 4) & 0xF0); //load upper nibble on LCD data lines
	LCD_STROBE(); //send instruction to LCD   	
}

/* send data to the LCD */
void lcd_data(unsigned char c)
{
	DelayMs(2); //wait for LCD to be ready shorter delay 
	PORTB = 0x00;
	LCD_RS = 1; //write data
    PORTB |= (c & 0xF0); //load upper nibble on LCD data lines     
	LCD_STROBE(); //send instruction to LCD
	PORTB &= 0x00; //load upper nibble on LCD data lines
	PORTB |= ( (c << 4) & 0xF0); 
	LCD_STROBE(); //send instruction to LCD
}

/*Clear the LCD*/
void lcd_clear(void)
{
	lcd_cmd(0x01); //command to clear LCD
}

/*write a string of chars to the LCD*/
void lcd_puts(const char s[])
{
	j = -1;
	while(s[++j]!=('\0')) // send characters until null character reached
		lcd_data(s[j]);
}

/*go to beginning of line 1*/
void lcd_goto_L1(void)
{
	lcd_cmd(0b10000000); // command to go to line 1
}

/*go to beginning of line 2*/
void lcd_goto_L2(void)
{
	lcd_cmd(0b11000000); // command to go to line 2
}

/*move cursor "x" positions to the right*/
void lcd_cursor(unsigned char x)
{
	lcd_cmd(((x)&0x7F)|0x80); 
}

/*initialise the LCD - put into 4 bit mode*/
void lcd_init(void)
{
	LCD_RS = 0;	
	LCD_EN = 0;
	DelayMs(20); //wait for LCD startup
	lcd_cmd(0x02);
 
	lcd_cmd(0x28);	// 4-bit mode
	lcd_cmd(0x08);	// display off
	lcd_cmd(0x01);	// clear display
	lcd_cmd(0x0C);	// disp. on, cursor off, cursor blink off
	lcd_cmd(0x06);	// entry mode
	lcd_cmd(0x80);  // initialise DDRAM address to zero
}
//////////////////////////LCD SETUP END//////////////////////////
    

void init(void)
{   
	
	// setup the PIC 16f690
	OSCCON = 0x72;      	// internal osc, 8MHz


	PORTA = 0;
	TRISA = 0b10010010; 	// RA7 high imp, RA3 is serial out, RA4 button input 


	
 	PORTB = 0;          	// PORTB not used
    WPUB = 1;				// PORTB pullups ON		
    RABPU = 0;


    /* Init ADC */
    ADCON0 = 0b10000101;	// bit 7 right justify,analogue channel select bits bits5-2  0001=AN1,ADC ON, RA1 is ADC input
	ADCON1 = 0b00100000;	//bits6-4  fosc/32
    ADON=1;	                // turn on the A2D conversion module

	
	ANSEL=0x02;            //set RA1 as analog input for GP2 sensor
	ANSELH=0x00;

    T1CON = 0b00010001;     // TMR1 is ON, 1:2 prescale, =1MHz
	T2CON = 0b00000101;     // TMR2 is ON, 1:4 prescale, =1MHz



	MyVal = 0; //initializn these variables here
	MyMinVal = 0;
	MyMaxVal = 99;
  
	TRISB=0x00; 
	TRISC=0xFC;
    
	lcd_init(); //call LCD initialisation

}

char WaitForInput(void){
char done;
char temp;
done = 0;

while(!done){
    if(!ENTERSETTINGS_SW){
        temp = 1;
        done = 0xff;
    }

    if(!HOME_SW){
        temp = 2;
        done = 0xff;
    }

    if(!INCREASE_SW){
        temp = 3;
        done = 0xff;
    }

    if(!DECREASE_SW){
        temp = 4;
        done = 0xff;
    }
}//end of while
    DelayMs(150);    //debounce
    return temp;
}//
void userMenu(char pos){
	lcd_clear();
	lcd_goto_L1();

	switch(pos){
		case 0:
			lcd_puts("    HEIGHT    ");
			break;		
		case 1:
			lcd_puts("    RANGE     ");
			break;		
		case 2:
			lcd_puts(" SURFACE AREA ");
			break;							//
		case 3: 
			
			[B][COLOR="Red"] while(1){
 

 	// wait for 2 seconds, uses TMR1 free running at 1Mhz
   	while(!TMR1IF)  // wait for TMR1 overflow
	TMR1IF = 0; 	// clear overflow flag

	bres += 65536;			// add 65536uS to bres value
	if(bres >= 2000000)		// if reached 2 seconds!
	 {
    	bres -= 2000000;	// subtract 2 seconds, keep error
		  	
		// read the ADC voltage RA1 (Sharp GP2 sensor)
		GODONE=1;	// initiate conversion on the channel 0
		while(GODONE) continue;  // Wait convertion done
		calc_distance();		// convert ADC value to distance
	//	LiquidLevel=height-;
		lcd_clear();                // Using the new menu you can erase or
		lcd_data(cm10 + '0');
		lcd_data(cm + '0');
	   // lcd_goto_L2(); 
	    lcd_puts(" [cm] ");			//comment this out if you want[/COLOR][/B]
		
		[COLOR="DarkOrchid"][B]/*	lcd_clear();
 			lcd_puts("   Measured   ");
			lcd_goto_L2();
			lcd_data(cm10 + '0');
			lcd_data(cm + '0');
			lcd_puts(" [cm] ");
			//break;		//*/[/B][/COLOR]
      
       }  
             
 
   } 


		
	}

	if(pos == 3) return;
	lcd_goto_L2();
	lcd_puts("Press Up/Down"); //home screen message (line 2)
}
// New Menu System
void EnterHeight(void){
	lcd_clear();
	lcd_goto_L1();
	lcd_puts(" ENTER HEIGHT ");
	lcd_goto_L2();
	lcd_puts("Press Up/Down"); //home screen message (line 2)
}

void EnterScreen(void){
	lcd_clear();
	lcd_goto_L1();
	lcd_puts(" [cm] ");
}

void ShowDigits(unsigned char val){

    MyValLCD[0] = val /10;    //returns the quotient (if temp = 35 the result is 3)
    MyValLCD[1] = val % 10; 	//Returns remainder   (if temp = 35 the result is 5)

	MyValLCD[0] += 0x30;	//to ASCII
	MyValLCD[1] += 0x30;	//to ASCII

	EnterScreen();
	lcd_goto_L2();
	lcd_data(MyValLCD[0]);	//to LCD
	lcd_data(MyValLCD[1]);  //to LCD
}

  
void calc_distance(void)
{
	// from the transeiver datasheet the analog voltage is
	// the inverse of distance, so distance can be calculated
	// d = (1 / volts) then just scaled to suit the transeiver

	// load ADC value in 16bit math var
	math = ADRESH;
	math = (math * 256);
	math += ADRESL;

	// now invert it; (1 / volts) use (6050 / volts) for scaling
	math = (6050 / math);
	if(math >= 2) math -=2;		// fix linear error (-2)
	if(math > 99) math = 99;	// max limit at 99cm

    //LiquidLevel=height-;	

	// convert from 0-99 to 2 decimal digits, 0-99cm
	cm10=0;
	while(math >= 10)
	{
		cm10++;
		math -= 10;
	}
	cm = math;
}
 //
unsigned char user_input(void)		//This will return what we want
{
	char done = 0;
 
	MyVal = 0;			//Start on 0
	while(done == 0){
   	  input_sw = WaitForInput();
		
      switch(input_sw){
		case 1:
			done = 0xff; 			//This tells us the user finished entering
			lcd_goto_L1();
			lcd_puts("     Done!      "); //home screen message (line 1)
			break;
      	case 3:
			if(MyVal < MyMaxVal)
      			MyVal++;
			EnterScreen();
			ShowDigits(MyVal);
			break;
      	case 4:
			if(MyVal > MyMinVal)
      			MyVal--;
			EnterScreen();
			ShowDigits(MyVal);
        	break;
		default: 
			break;
      }
	  
	}
	DelayS(1);
	return MyVal;
}

void home_screen(void){
	mnuPOS = 0;
	lcd_clear();
	lcd_goto_L1();
	lcd_puts("INFRARED LIQUID"); //home screen message (line 1)
	lcd_goto_L2();
	lcd_puts("LEVEL DETECTOR"); //home screen message (line 2)
  
	input_sw = 0;			//Reset the value

	while(input_sw != 1)  	//Wait until enter is pressed
		input_sw = WaitForInput();

	userMenu(0);
	DelayMs(2);				//shorter delay 
    height = user_input();	//The HEIGHT var will have the myVal

	userMenu(1);
	DelayMs(2);				//shorter delay 
    range = user_input();	//The HEIGHT var will have the myVal

	userMenu(2);
	DelayMs(2);				//shorter delay 
    area = user_input();	//The HEIGHT var will have the myVal
//
	userMenu(3);
	DelayMs(2);				//shorter delay 
	input_sw = 0;			//Reset the value

	while(input_sw != 1)  	//Wait until enter is pressed
		input_sw = WaitForInput();
//Waits for user to press ENTER to show home screen
/*
"enter height"
call enter/settings (which is now user input function in new code)
height=MyVal

"enter range"
call user_input
range=MyVal

"enter surface area"
call user_input
area=MyVal
*/
}
  
//*********************************************************
/*  Junebug_Serial4.c    RomanBlack.com 19th July 2009.

    uses "zero error 1 second timer"
	system to generate a 2 second interval, then every
	2 seconds it reads an analog voltage from a
	Sharp GP2 distance sensor and converts it to decimal
	distance, then sends that data to a LCD

	Code for MikroC, config handled by MikroC compiler;
	_INTI02_OSC_1H
	_WDT_OFF_2H
	-MCLRE_ON_3H
	_LVP_OFF_4L 
	_PWRT_ON_2L
*/
//*********************************************************

void main(void)
{  
    init();	// initialise I/O ports, LCD
    while(1){
 	home_screen();

 [B][COLOR="Black"]/*// wait for 2 seconds, uses TMR1 free running at 1Mhz
   	while(!TMR1IF)  // wait for TMR1 overflow
	TMR1IF = 0; 	// clear overflow flag

	bres += 65536;			// add 65536uS to bres value
	if(bres >= 2000000)		// if reached 2 seconds!
	 {
    	bres -= 2000000;	// subtract 2 seconds, keep error
		  	
		// read the ADC voltage RA1 (Sharp GP2 sensor)
		GODONE=1;	// initiate conversion on the channel 0
		while(GODONE) continue;  // Wait convertion done
		calc_distance();		// convert ADC value to distance
	//	LiquidLevel=height-;
	//	lcd_clear();                // Using the new menu you can erase or
	//	lcd_data(cm10 + '0');
	//	lcd_data(cm + '0');
	 //   lcd_goto_L2(); 
	   // lcd_puts("[cm] ");			//comment this out if you want
*/
 [/COLOR][/B]
      
       
             
 
   } 
}


**********************************************************

By the way if i replace my

Code:
lcd_clear();                // Using the new menu you can erase or
		lcd_data(cm10 + '0');
		lcd_data(cm + '0');
	   // lcd_goto_L2(); 
	    lcd_puts(" [cm] ");			//comment this out if you want[/COLOR][/B]

with your:

Code:
[COLOR="DarkOrchid"][B]/*	lcd_clear();
 			lcd_puts("   Measured   ");
			lcd_goto_L2();
			lcd_data(cm10 + '0');
			lcd_data(cm + '0');
			lcd_puts(" [cm] ");
			//break;		//*/[/B][/COLOR]


it gives continuous readings in with the repositioning of other code but...

it displays:


Measured (and this is dark...visible)


35cm (extremely light in colour can hardly see it)
 
Last edited:
You mean the Measured text is visible and the other 35cm is barely visible? Post the full code your using at the moment. Also check your PM in about 5 minutes
 
Last edited:
Yeah thats what i mean. This is the code im using....

Code:
#include <pic.h>
#include "pic.h"
#include "delay.h"
#include "math.h" 
#include <stdio.h>
#include <stdlib.h>	//

void userMenu(char pos); //  
void FloatToStr(float , char[]); 
void DelayMs(unsigned char);
void lcd_cmd(unsigned char);
void lcd_data(unsigned char); 
void lcd_clear(void);
void lcd_puts(const char[]);
void lcd_goto_L1(void);
void lcd_goto_L2(void);
void lcd_cursor(unsigned char);
void lcd_init(void);
void init(void);
char WaitForInput(void);
unsigned char user_input(void);
void home_screen(void);
void EnterHeight(void);
void EnterScreen(void);
void ShowDigits(unsigned char val);
void calc_distance(void);
void main(void);


unsigned char cm2LCD;

unsigned char cmHigh, cmLow;

      

#define LCD_RS RC0		//LCD RS pin
#define LCD_EN RC1		//LCD EN pin
#define LCD_STROBE()	LCD_EN = 1; asm("nop"); asm("nop"); LCD_EN = 0

unsigned char cm10;		//
unsigned char cm;		//
unsigned int math;		// used for voltage calculations

unsigned int LiquidLevel;
unsigned char NumDec;
unsigned char NumSep[2];
   
unsigned char i,j,k;
//char temp[8];
//[/b]
unsigned char height=50;
unsigned char range;
unsigned char area;
unsigned char SensorPos=10;
//[/b] New Vars
char input_sw;

  
char mnuPOS;

unsigned char MyVal;
unsigned char MyValLCD[2];
unsigned char MyMaxVal;
unsigned char MyMinVal;
unsigned long bres;		// for bresenham 2-second timer system

unsigned char ;

#define HOME_SW RC2				//HOME switch	
#define INCREASE_SW RC3			//INCREASE switch
#define DECREASE_SW RC4			//DECREASE switch
#define ENTERSETTINGS_SW RA4	//ENTERSETTINGS switch



///////////////////////CONVERT FLOAT TO STRING///////////////////
// This function was taken from the CAVR library. It was modified slightly
// to suit our design.
void FloatToStr(float n, char str[])
{
float scale;
unsigned char d,f;
f=0;i=0;
if (n<0.0) {n=-n; str[f]='-'; f++;}; 
n=n+0.005;
scale=1.0;
while (n>=scale) {scale=scale*10.0; ++i;};
if (i==0) {str[f]='0'; f++;}
else
while (i--)
	  {
      scale=floor(0.5+scale/10.0);
      d=(unsigned char) (n/scale);
      str[f]=d+'0';
      n=n-scale*d;
	  f++;
      };

str[f]='.';
f++;
for (j=0;j<=1;j++) //2 decimal points
      {
      n=n*10.0;
      d=(unsigned char) n;
      str[f]=d+'0';
      n=n-d;
	  f++;
	  };
str[f]='\0';
}
///////////////////END CONVERT FLOAT TO STRING///////////////////

/////////////////////////////DELAY///////////////////////////////
void DelayMs(unsigned char cnt)
{
#if	XTAL_FREQ <= 2MHZ
	do {
		DelayUs(996);
	} while(--cnt);
#endif

#if    XTAL_FREQ > 2MHZ	
	unsigned char	p;
	do {
		p = 4;
		do { 
			DelayUs(250);
		} while(--p);  
	} while(--cnt);
#endif
}

void DelayS(unsigned char cnt)
{
	for (j=0; j<(cnt*10); j++)
		DelayMs(100);
}
///////////////////////////DELAY END/////////////////////////////

//////////////////////////////LCD SETUP//////////////////////////
/* send a command to the LCD */
void lcd_cmd(unsigned char c)
{
	DelayMs(2); //wait for LCD to be ready shorter delay 
	LCD_RS = 0;	 //write instruction
	PORTB = (c & 0xF0); //load upper nibble on LCD data lines
	LCD_STROBE(); //send instruction to LCD
	PORTB = ((c << 4) & 0xF0); //load upper nibble on LCD data lines
	LCD_STROBE(); //send instruction to LCD   	
}

/* send data to the LCD */
void lcd_data(unsigned char c)
{
	DelayMs(2); //wait for LCD to be ready shorter delay 
	PORTB = 0x00;
	LCD_RS = 1; //write data
    PORTB |= (c & 0xF0); //load upper nibble on LCD data lines     
	LCD_STROBE(); //send instruction to LCD
	PORTB &= 0x00; //load upper nibble on LCD data lines
	PORTB |= ( (c << 4) & 0xF0); 
	LCD_STROBE(); //send instruction to LCD
}

/*Clear the LCD*/
void lcd_clear(void)
{
	lcd_cmd(0x01); //command to clear LCD
}

/*write a string of chars to the LCD*/
void lcd_puts(const char s[])
{
	j = -1;
	while(s[++j]!=('\0')) // send characters until null character reached
		lcd_data(s[j]);
}

/*go to beginning of line 1*/
void lcd_goto_L1(void)
{
	lcd_cmd(0b10000000); // command to go to line 1
}

/*go to beginning of line 2*/
void lcd_goto_L2(void)
{
	lcd_cmd(0b11000000); // command to go to line 2
}

/*move cursor "x" positions to the right*/
void lcd_cursor(unsigned char x)
{
	lcd_cmd(((x)&0x7F)|0x80); 
}

/*initialise the LCD - put into 4 bit mode*/
void lcd_init(void)
{
	LCD_RS = 0;	
	LCD_EN = 0;
	DelayMs(20); //wait for LCD startup
	lcd_cmd(0x02);
 
	lcd_cmd(0x28);	// 4-bit mode
	lcd_cmd(0x08);	// display off
	lcd_cmd(0x01);	// clear display
	lcd_cmd(0x0C);	// disp. on, cursor off, cursor blink off
	lcd_cmd(0x06);	// entry mode
	lcd_cmd(0x80);  // initialise DDRAM address to zero
}
//////////////////////////LCD SETUP END//////////////////////////
    

void init(void)
{   
	
	// setup the PIC 16f690
	OSCCON = 0x72;      	// internal osc, 8MHz


	PORTA = 0;
	TRISA = 0b10010010; 	// RA7 high imp, RA3 is serial out, RA4 button input 


	
 	PORTB = 0;          	// PORTB not used
    WPUB = 1;				// PORTB pullups ON		
    RABPU = 0;


    /* Init ADC */
    ADCON0 = 0b10000101;	// bit 7 right justify,analogue channel select bits bits5-2  0001=AN1,ADC ON, RA1 is ADC input
	ADCON1 = 0b00100000;	//bits6-4  fosc/32
    ADON=1;	                // turn on the A2D conversion module

	
	ANSEL=0x02;            //set RA1 as analog input for GP2 sensor
	ANSELH=0x00;

    T1CON = 0b00010001;     // TMR1 is ON, 1:2 prescale, =1MHz
	T2CON = 0b00000101;     // TMR2 is ON, 1:4 prescale, =1MHz



	MyVal = 0; //initializn these variables here
	MyMinVal = 0;
	MyMaxVal = 99;
  
	TRISB=0x00; 
	TRISC=0xFC;
    
	lcd_init(); //call LCD initialisation

}

char WaitForInput(void){
char done;
char temp;
done = 0;

while(!done){
    if(!ENTERSETTINGS_SW){
        temp = 1;
        done = 0xff;
    }

    if(!HOME_SW){
        temp = 2;
        done = 0xff;
    }

    if(!INCREASE_SW){
        temp = 3;
        done = 0xff;
    }

    if(!DECREASE_SW){
        temp = 4;
        done = 0xff;
    }
}//end of while
    DelayMs(150);    //debounce
    return temp;
}//
void userMenu(char pos){
	lcd_clear();
	lcd_goto_L1();

	switch(pos){
		case 0:
			lcd_puts("    HEIGHT    ");
			break;		
		case 1:
			lcd_puts("    RANGE     ");
			break;		
		case 2:
			lcd_puts(" SURFACE AREA ");
			break;							//
		case 3: 
			
			 while(1){
 

 	// wait for 2 seconds, uses TMR1 free running at 1Mhz
   	while(!TMR1IF)  // wait for TMR1 overflow
	TMR1IF = 0; 	// clear overflow flag

	bres += 65536;			// add 65536uS to bres value
	if(bres >= 2000000)		// if reached 2 seconds!
	 {
    	bres -= 2000000;	// subtract 2 seconds, keep error
		  	
		// read the ADC voltage RA1 (Sharp GP2 sensor)
		GODONE=1;	// initiate conversion on the channel 0
		while(GODONE) continue;  // Wait convertion done
		calc_distance();		// convert ADC value to distance
	//	LiquidLevel=height-;
		lcd_clear();                // Using the new menu you can erase or
		lcd_data(cm10 + '0');
		lcd_data(cm + '0');
	   // lcd_goto_L2(); 
	    lcd_puts(" [cm] ");			//comment this out if you want
		
		/*	lcd_clear();
 			lcd_puts("   Measured   ");
			lcd_goto_L2();
			lcd_data(cm10 + '0');
			lcd_data(cm + '0');
			lcd_puts(" [cm] ");
			//break;		//*/
      
       }  
             
 
   } 


		
	}

	if(pos == 3) return;
	lcd_goto_L2();
	lcd_puts("Press Up/Down"); //home screen message (line 2)
}
// New Menu System
void EnterHeight(void){
	lcd_clear();
	lcd_goto_L1();
	lcd_puts(" ENTER HEIGHT ");
	lcd_goto_L2();
	lcd_puts("Press Up/Down"); //home screen message (line 2)
}

void EnterScreen(void){
	lcd_clear();
	lcd_goto_L1();
	lcd_puts(" [cm] ");
}

void ShowDigits(unsigned char val){

    MyValLCD[0] = val /10;    //returns the quotient (if temp = 35 the result is 3)
    MyValLCD[1] = val % 10; 	//Returns remainder   (if temp = 35 the result is 5)

	MyValLCD[0] += 0x30;	//to ASCII
	MyValLCD[1] += 0x30;	//to ASCII

	EnterScreen();
	lcd_goto_L2();
	lcd_data(MyValLCD[0]);	//to LCD
	lcd_data(MyValLCD[1]);  //to LCD
}

  
void calc_distance(void)
{
	// from the transeiver datasheet the analog voltage is
	// the inverse of distance, so distance can be calculated
	// d = (1 / volts) then just scaled to suit the transeiver

	// load ADC value in 16bit math var
	math = ADRESH;
	math = (math * 256);
	math += ADRESL;

	// now invert it; (1 / volts) use (6050 / volts) for scaling
	math = (6050 / math);
	if(math >= 2) math -=2;		// fix linear error (-2)
	if(math > 99) math = 99;	// max limit at 99cm

    //LiquidLevel=height-;	

	// convert from 0-99 to 2 decimal digits, 0-99cm
	cm10=0;
	while(math >= 10)
	{
		cm10++;
		math -= 10;
	}
	cm = math;
}
 //
unsigned char user_input(void)		//This will return what we want
{
	char done = 0;
 
	MyVal = 0;			//Start on 0
	while(done == 0){
   	  input_sw = WaitForInput();
		
      switch(input_sw){
		case 1:
			done = 0xff; 			//This tells us the user finished entering
			lcd_goto_L1();
			lcd_puts("     Done!      "); //home screen message (line 1)
			break;
      	case 3:
			if(MyVal < MyMaxVal)
      			MyVal++;
			EnterScreen();
			ShowDigits(MyVal);
			break;
      	case 4:
			if(MyVal > MyMinVal)
      			MyVal--;
			EnterScreen();
			ShowDigits(MyVal);
        	break;
		default: 
			break;
      }
	  
	}
	DelayS(1);
	return MyVal;
}

void home_screen(void){
	mnuPOS = 0;
	lcd_clear();
	lcd_goto_L1();
	lcd_puts("INFRARED LIQUID"); //home screen message (line 1)
	lcd_goto_L2();
	lcd_puts("LEVEL DETECTOR"); //home screen message (line 2)
  
	input_sw = 0;			//Reset the value

	while(input_sw != 1)  	//Wait until enter is pressed
		input_sw = WaitForInput();

	userMenu(0);
	DelayMs(2);				//shorter delay 
    height = user_input();	//The HEIGHT var will have the myVal

	userMenu(1);
	DelayMs(2);				//shorter delay 
    range = user_input();	//The HEIGHT var will have the myVal

	userMenu(2);
	DelayMs(2);				//shorter delay 
    area = user_input();	//The HEIGHT var will have the myVal
//
	userMenu(3);
	DelayMs(2);				//shorter delay 
	input_sw = 0;			//Reset the value

	while(input_sw != 1)  	//Wait until enter is pressed
		input_sw = WaitForInput();
//Waits for user to press ENTER to show home screen
/*
"enter height"
call enter/settings (which is now user input function in new code)
height=MyVal

"enter range"
call user_input
range=MyVal

"enter surface area"
call user_input
area=MyVal
*/
}
  
//*********************************************************
/*  Junebug_Serial4.c    RomanBlack.com 19th July 2009.

    uses "zero error 1 second timer"
	system to generate a 2 second interval, then every
	2 seconds it reads an analog voltage from a
	Sharp GP2 distance sensor and converts it to decimal
	distance, then sends that data to a LCD

	Code for MikroC, config handled by MikroC compiler;
	_INTI02_OSC_1H
	_WDT_OFF_2H
	-MCLRE_ON_3H
	_LVP_OFF_4L 
	_PWRT_ON_2L
*/
//*********************************************************

void main(void)
{  
    init();	// initialise I/O ports, LCD
    while(1){
 	home_screen();

 /*// wait for 2 seconds, uses TMR1 free running at 1Mhz
   	while(!TMR1IF)  // wait for TMR1 overflow
	TMR1IF = 0; 	// clear overflow flag

	bres += 65536;			// add 65536uS to bres value
	if(bres >= 2000000)		// if reached 2 seconds!
	 {
    	bres -= 2000000;	// subtract 2 seconds, keep error
		  	
		// read the ADC voltage RA1 (Sharp GP2 sensor)
		GODONE=1;	// initiate conversion on the channel 0
		while(GODONE) continue;  // Wait convertion done
		calc_distance();		// convert ADC value to distance
	//	LiquidLevel=height-;
	//	lcd_clear();                // Using the new menu you can erase or
	//	lcd_data(cm10 + '0');
	//	lcd_data(cm + '0');
	 //   lcd_goto_L2(); 
	   // lcd_puts("[cm] ");			//comment this out if you want
*/
 
      
        
             
 
   } 
}
 
Last edited:
Try this:

Code:
#include <pic.h>
#include "pic.h"
#include "delay.h"
#include "math.h" 
#include <stdio.h>
#include <stdlib.h>	//

void userMenu(char pos); //  
void FloatToStr(float , char[]); 
void DelayMs(unsigned char);
void lcd_cmd(unsigned char);
void lcd_data(unsigned char); 
void lcd_clear(void);
void lcd_puts(const char[]);
void lcd_goto_L1(void);
void lcd_goto_L2(void);
void lcd_cursor(unsigned char);
void lcd_init(void);
void init(void);
char WaitForInput(char expire);  //[b]Edited this with new expire input[/b]
unsigned char user_input(void);
void home_screen(void);
void EnterHeight(void);
void EnterScreen(void);
void ShowDigits(unsigned char val);
void calc_distance(void);
void main(void);


unsigned char cm2LCD;

unsigned char cmHigh, cmLow;

      

#define LCD_RS RC0		//LCD RS pin
#define LCD_EN RC1		//LCD EN pin
#define LCD_STROBE()	LCD_EN = 1; asm("nop"); asm("nop"); LCD_EN = 0

unsigned char cm10;		//
unsigned char cm;		//
unsigned int math;		// used for voltage calculations

unsigned int LiquidLevel;
unsigned char NumDec;
unsigned char NumSep[2];
   
unsigned char i,j,k;
//char temp[8];
//[/b]
unsigned char height=50;
unsigned char range;
unsigned char area;
unsigned char SensorPos=10;
//[/b] New Vars
char input_sw;

  
char mnuPOS;

unsigned char MyVal;
unsigned char MyValLCD[2];
unsigned char MyMaxVal;
unsigned char MyMinVal;
unsigned long bres;		// for bresenham 2-second timer system

unsigned char ;

#define HOME_SW RC2				//HOME switch	
#define INCREASE_SW RC3			//INCREASE switch
#define DECREASE_SW RC4			//DECREASE switch
#define ENTERSETTINGS_SW RA4	//ENTERSETTINGS switch



///////////////////////CONVERT FLOAT TO STRING///////////////////
// This function was taken from the CAVR library. It was modified slightly
// to suit our design.
void FloatToStr(float n, char str[])
{
float scale;
unsigned char d,f;
f=0;i=0;
if (n<0.0) {n=-n; str[f]='-'; f++;}; 
n=n+0.005;
scale=1.0;
while (n>=scale) {scale=scale*10.0; ++i;};
if (i==0) {str[f]='0'; f++;}
else
while (i--)
	  {
      scale=floor(0.5+scale/10.0);
      d=(unsigned char) (n/scale);
      str[f]=d+'0';
      n=n-scale*d;
	  f++;
      };

str[f]='.';
f++;
for (j=0;j<=1;j++) //2 decimal points
      {
      n=n*10.0;
      d=(unsigned char) n;
      str[f]=d+'0';
      n=n-d;
	  f++;
	  };
str[f]='\0';
}
///////////////////END CONVERT FLOAT TO STRING///////////////////

/////////////////////////////DELAY///////////////////////////////
void DelayMs(unsigned char cnt)
{
#if	XTAL_FREQ <= 2MHZ
	do {
		DelayUs(996);
	} while(--cnt);
#endif

#if    XTAL_FREQ > 2MHZ	
	unsigned char	p;
	do {
		p = 4;
		do { 
			DelayUs(250);
		} while(--p);  
	} while(--cnt);
#endif
}

void DelayS(unsigned char cnt)
{
	for (j=0; j<(cnt*10); j++)
		DelayMs(100);
}
///////////////////////////DELAY END/////////////////////////////

//////////////////////////////LCD SETUP//////////////////////////
/* send a command to the LCD */
void lcd_cmd(unsigned char c)
{
	DelayMs(2); //wait for LCD to be ready shorter delay 
	LCD_RS = 0;	 //write instruction
	PORTB = (c & 0xF0); //load upper nibble on LCD data lines
	LCD_STROBE(); //send instruction to LCD
	PORTB = ((c << 4) & 0xF0); //load upper nibble on LCD data lines
	LCD_STROBE(); //send instruction to LCD   	
}

/* send data to the LCD */
void lcd_data(unsigned char c)
{
	DelayMs(2); //wait for LCD to be ready shorter delay 
	PORTB = 0x00;
	LCD_RS = 1; //write data
    PORTB |= (c & 0xF0); //load upper nibble on LCD data lines     
	LCD_STROBE(); //send instruction to LCD
	PORTB &= 0x00; //load upper nibble on LCD data lines
	PORTB |= ( (c << 4) & 0xF0); 
	LCD_STROBE(); //send instruction to LCD
}

/*Clear the LCD*/
void lcd_clear(void)
{
	lcd_cmd(0x01); //command to clear LCD
}

/*write a string of chars to the LCD*/
void lcd_puts(const char s[])
{
	j = -1;
	while(s[++j]!=('\0')) // send characters until null character reached
		lcd_data(s[j]);
}

/*go to beginning of line 1*/
void lcd_goto_L1(void)
{
	lcd_cmd(0b10000000); // command to go to line 1
}

/*go to beginning of line 2*/
void lcd_goto_L2(void)
{
	lcd_cmd(0b11000000); // command to go to line 2
}

/*move cursor "x" positions to the right*/
void lcd_cursor(unsigned char x)
{
	lcd_cmd(((x)&0x7F)|0x80); 
}

/*initialise the LCD - put into 4 bit mode*/
void lcd_init(void)
{
	LCD_RS = 0;	
	LCD_EN = 0;
	DelayMs(20); //wait for LCD startup
	lcd_cmd(0x02);
 
	lcd_cmd(0x28);	// 4-bit mode
	lcd_cmd(0x08);	// display off
	lcd_cmd(0x01);	// clear display
	lcd_cmd(0x0C);	// disp. on, cursor off, cursor blink off
	lcd_cmd(0x06);	// entry mode
	lcd_cmd(0x80);  // initialise DDRAM address to zero
}
//////////////////////////LCD SETUP END//////////////////////////
    

void init(void)
{   
	
	// setup the PIC 16f690
	OSCCON = 0x72;      	// internal osc, 8MHz


	PORTA = 0;
	TRISA = 0b10010010; 	// RA7 high imp, RA3 is serial out, RA4 button input 


	
 	PORTB = 0;          	// PORTB not used
    WPUB = 1;				// PORTB pullups ON		
    RABPU = 0;


    /* Init ADC */
    ADCON0 = 0b10000101;	// bit 7 right justify,analogue channel select bits bits5-2  0001=AN1,ADC ON, RA1 is ADC input
	ADCON1 = 0b00100000;	//bits6-4  fosc/32
    ADON=1;	                // turn on the A2D conversion module

	
	ANSEL=0x02;            //set RA1 as analog input for GP2 sensor
	ANSELH=0x00;

    T1CON = 0b00010001;     // TMR1 is ON, 1:2 prescale, =1MHz
	T2CON = 0b00000101;     // TMR2 is ON, 1:4 prescale, =1MHz



	MyVal = 0; //initializn these variables here
	MyMinVal = 0;
	MyMaxVal = 99;
  
	TRISB=0x00; 
	TRISC=0xFC;
    
	lcd_init(); //call LCD initialisation

}
//[b] EDITED this a bit. now if expire input is set it will leave loop with a 0
//instead of rechecking this will be useful for display your measured data and 
//waiting for user to exit.[/b]
char WaitForInput(char expire){
char done;
char temp;
done = 0;
temp = 0;
while(!done){
    if(!ENTERSETTINGS_SW){
		while(ENTERSETTINGS_SW);
        temp = 1;
        done = 0xff;
    }

    if(!HOME_SW){
		while(HOME_SW);
        temp = 2;
        done = 0xff;
    }

    if(!INCREASE_SW){
		while(INCREASE_SW);
        temp = 3;
        done = 0xff;
    }

    if(!DECREASE_SW){
		while(DECREASE_SW);
        temp = 4;
        done = 0xff;
    }
	if(expire == 1) break;
}//end of while
    DelayMs(150);    //debounce
    return temp;
}//
void userMenu(char pos){
	lcd_clear();
	lcd_goto_L1();

	switch(pos){
		case 0:
			lcd_puts("    HEIGHT    ");
			break;		
		case 1:
			lcd_puts("    RANGE     ");
			break;		
		case 2:
			lcd_puts(" SURFACE AREA ");
			break;							//
		case 3: 
			lcd_puts("   MEASURED   ");
			 while(WaitForInput(1) != 1){		//[b]Wait for user to press enter to leave loop[/b]

 				// wait for 2 seconds, uses TMR1 free running at 1Mhz
   				while(!TMR1IF)  		// wait for TMR1 overflow
				TMR1IF = 0; 			// clear overflow flag

				bres += 65536;			// add 65536uS to bres value
				if(bres >= 2000000)		// if reached 2 seconds!
	 			{
    				bres -= 2000000;	// subtract 2 seconds, keep error
		  	
					// read the ADC voltage RA1 (Sharp GP2 sensor)
					GODONE=1;					// initiate conversion on the channel 0
					while(GODONE) continue;  	// Wait convertion done
					calc_distance();			// convert ADC value to distance

					lcd_goto_L2();				//[b]Only change line 2[/b]
					lcd_data(cm10 + '0');
					lcd_data(cm + '0');
	    			lcd_puts(" [cm] ");			//comment this out if you want
			   } 
		}
		break;
	}

	while(WaitForInput(1) == 1);
	if(pos == 3) return;
	lcd_goto_L2();
	lcd_puts("Press Up/Down"); //home screen message (line 2)
}
// New Menu System
void EnterHeight(void){
	lcd_clear();
	lcd_goto_L1();
	lcd_puts(" ENTER HEIGHT ");
	lcd_goto_L2();
	lcd_puts("Press Up/Down"); //home screen message (line 2)
}

void EnterScreen(void){
	lcd_clear();
	lcd_goto_L1();
	lcd_puts(" [cm] ");
}

void ShowDigits(unsigned char val){

    MyValLCD[0] = val /10;    //returns the quotient (if temp = 35 the result is 3)
    MyValLCD[1] = val % 10; 	//Returns remainder   (if temp = 35 the result is 5)

	MyValLCD[0] += 0x30;	//to ASCII
	MyValLCD[1] += 0x30;	//to ASCII

	EnterScreen();
	lcd_goto_L2();
	lcd_data(MyValLCD[0]);	//to LCD
	lcd_data(MyValLCD[1]);  //to LCD
}

  
void calc_distance(void)
{
	// from the transeiver datasheet the analog voltage is
	// the inverse of distance, so distance can be calculated
	// d = (1 / volts) then just scaled to suit the transeiver

	// load ADC value in 16bit math var
	math = ADRESH;
	math = (math * 256);
	math += ADRESL;

	// now invert it; (1 / volts) use (6050 / volts) for scaling
	math = (6050 / math);
	if(math >= 2) math -=2;		// fix linear error (-2)
	if(math > 99) math = 99;	// max limit at 99cm

    //LiquidLevel=height-;	

	// convert from 0-99 to 2 decimal digits, 0-99cm
	cm10=0;
	while(math >= 10)
	{
		cm10++;
		math -= 10;
	}
	cm = math;
}
 //
unsigned char user_input(void)		//This will return what we want
{
	char done = 0;
 
	MyVal = 0;			//Start on 0
	while(done == 0){
   	  input_sw = WaitForInput(0);
		
      switch(input_sw){
		case 1:
			done = 0xff; 			//This tells us the user finished entering
			lcd_goto_L1();
			lcd_puts("      OK       "); //home screen message (line 1)
			break;
      	case 3:
			if(MyVal < MyMaxVal)
      			MyVal++;
			EnterScreen();
			ShowDigits(MyVal);
			break;
      	case 4:
			if(MyVal > MyMinVal)
      			MyVal--;
			EnterScreen();
			ShowDigits(MyVal);
        	break;
		default: 
			break;
      }
	  
	}
	DelayMs(250);
	DelayMs(250);
	return MyVal;
}

void home_screen(void){
	mnuPOS = 0;
	lcd_clear();
	lcd_goto_L1();
	lcd_puts("INFRARED LIQUID"); //home screen message (line 1)
	lcd_goto_L2();
	lcd_puts("LEVEL DETECTOR"); //home screen message (line 2)
  
	input_sw = 0;			//Reset the value

	while(input_sw != 1)  	//Wait until enter is pressed
		input_sw = WaitForInput(0);

	userMenu(0);
	DelayMs(2);				//shorter delay 
    height = user_input();	//The HEIGHT var will have the myVal

	userMenu(1);
	DelayMs(2);				//shorter delay 
    range = user_input();	//The HEIGHT var will have the myVal

	userMenu(2);
	DelayMs(2);				//shorter delay 
    area = user_input();	//The HEIGHT var will have the myVal
//
	userMenu(3);
	DelayMs(2);				//shorter delay 
	input_sw = 0;			//Reset the value

	while(input_sw != 1)  	//Wait until enter is pressed
		input_sw = WaitForInput(0);
//Waits for user to press ENTER to show home screen
/*
"enter height"
call enter/settings (which is now user input function in new code)
height=MyVal

"enter range"
call user_input
range=MyVal

"enter surface area"
call user_input
area=MyVal
*/
}
  
//*********************************************************
/*  Junebug_Serial4.c    RomanBlack.com 19th July 2009.

    uses "zero error 1 second timer"
	system to generate a 2 second interval, then every
	2 seconds it reads an analog voltage from a
	Sharp GP2 distance sensor and converts it to decimal
	distance, then sends that data to a LCD

	Code for MikroC, config handled by MikroC compiler;
	_INTI02_OSC_1H
	_WDT_OFF_2H
	-MCLRE_ON_3H
	_LVP_OFF_4L 
	_PWRT_ON_2L
*/
//*********************************************************

void main(void)
{  
    init();	// initialise I/O ports, LCD
    while(1){
 	home_screen();

 /*// wait for 2 seconds, uses TMR1 free running at 1Mhz
   	while(!TMR1IF)  // wait for TMR1 overflow
	TMR1IF = 0; 	// clear overflow flag

	bres += 65536;			// add 65536uS to bres value
	if(bres >= 2000000)		// if reached 2 seconds!
	 {
    	bres -= 2000000;	// subtract 2 seconds, keep error
		  	
		// read the ADC voltage RA1 (Sharp GP2 sensor)
		GODONE=1;	// initiate conversion on the channel 0
		while(GODONE) continue;  // Wait convertion done
		calc_distance();		// convert ADC value to distance
	//	LiquidLevel=height-;
	//	lcd_clear();                // Using the new menu you can erase or
	//	lcd_data(cm10 + '0');
	//	lcd_data(cm + '0');
	 //   lcd_goto_L2(); 
	   // lcd_puts("[cm] ");			//comment this out if you want
*/
 
      
        
             
 
   } 
}

**broken link removed**
 
Last edited:
Its displaying the distances BUT its changing extremely slowly(b4 it was fast) AND the home button doesnt go to the home screen


***************************************************************
Actually while taking readings.....the measuring can be EXITED to the HOME screen when i press ENTER/SETTINGS not the HOME button....

so it is EXITING just not with the right button
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top