Display ADRESH on LCD

Status
Not open for further replies.

prashant_kd

New Member
Hello guys,
I am designing a overhead tank water level display using PIC16F873A. For LCD, i have modified Hitec 4-bit interface lib to 8-bit interface. For reading Water level , i am supplying 5V to one of the two probes immersed in tank and reading V change from other via Voltage divider ckt. Now my problem is that i have to calibrate ADC with different water levels and print equivalent Characters. For that i first have to know ADRESH ( 8-bit resolution is sufficient) value at different levels. So how can i convert ADRESH 8-bit value to ASCII ( Character string ) so as to display descimal value of that data . Although only ADC function is revelent to the topic i am still posting te whole code so others can use it too.,hope u ppl wont mind. Thnx

/*********************************************************************************************
****PROJECT : WATER LEVEL DISPLAY**************************************************************
****AUTHOR: PRASHANT_kd**********************************************************************
****DATE: ***************************************************************************
****DEVICE: PIC16F873A************************************************************************/

#include<htc.h>

__CONFIG(LVPDIS & UNPROTECT & WDTDIS & XT);// Low voltage Programming Disable & Watch Dog Timer OFF & External OScillator

#ifndef _XTAL_FREQ
#define _XTAL_FREQ 4000000 // oscillator freq 4MHZ
#endif

#define Data_Port PORTB
#define RS_PIN RC0
#define E_PIN RC1
// RW is grounded
#define HIGH 1
#define LOW 0

void ADC_read(void); // prototype
void lcd_puts(const char * s); // prototype
void lcd_write(unsigned int data); // prototype
void LCD_cmd(unsigned int data); // prototype
void LCD_init(void); // prototype
void lcd_val(const char * s); // prototype
void lcd_goto(unsigned char pos); // prototype

//********************************LCD FUNCTIONS*********************************************//

void lcd_write(unsigned int data)
{
Data_Port = data;// Write data to portB
E_PIN = HIGH; //
_delay(10); //STROBE
E_PIN = LOW; //
_delay(10); //
}

void LCD_cmd(unsigned int data)
{
RS_PIN = LOW;
Data_Port = data;
E_PIN = HIGH;
_delay(10);
E_PIN = LOW;
}

void lcd_val(const char * s)
{
RS_PIN = 1; // write characters
while(*s)
lcd_write(*s++);
}

void lcd_goto(unsigned char pos)
{
RS_PIN = 0; // write command
lcd_write(0x80+pos);
}

//********************************************************************************************//
void LCD_init(void)
{
__delay_ms(100);
LCD_cmd(0X01); // clear display
__delay_ms(20);
LCD_cmd(0X02); // cursor to home
__delay_ms(20);
LCD_cmd(0X07); // show shift and auto increament cursor
__delay_ms(20);
LCD_cmd(0X0c); // Display on
__delay_ms(20);
LCD_cmd(0X1C); // Display shift, Right shift
__delay_ms(20);
LCD_cmd(0X3C); // 8 bit interface, 5X10 dot matrix
__delay_ms(20);
}

//*******************************************************************************************//
//*******************************ADC FUNCTIONS***********************************************//

void ADC_read(void)
{
//const char * Val_x;

ADCON0 = 0b11000101 ; // start conversion
__delay_ms(20);

if( (ADCON0 & 0b11000001) == 0b11000001 ) // when conversion completes
{
// *Val_x = ADRESH;
lcd_goto(0);
lcd_write(ADRESH++);

}
}



//********************************************************************************************//
//****************************MAIN FUNCTION***************************************************//
void main(void)
{
TRISA = 0XFF; // pin 0 AN0, 2 -Vref , 3 +Vref set as i/p
TRISB = 0X00; // output port
TRISC = 0X00; // output port

PORTA = 0X00; // initialize port as low
PORTB = 0X00; // initialize port as low
PORTC = 0X1C; // initialize port as low

LCD_init();

ADCON1 = 0b01001111;// Fosc/4 , AN0 = Analog , Vref = Vdd
ADCON0 = 0b11000001;// Fosc/4, AN0 , ADON = 1.


while(1)
{
ADC_read();
}
}

//*****************I need help with ADC_read() Function*******************************************//
//******Also please comment if there is any way to improvise the code*********************************//
 
LCD_write is missing RS = 1

But you may want to add the code:
Code:
void LCD_WriteUChar(unsigned char x){
   LCD_write(x/100 + '0');
   LCD_write(((x/10)%10) + '0');
   LCD_write((x%10) + '0');
}
 
Nop, i made following changes but no success:

void ADC_read(void)
{
unsigned char * Val_x;

ADCON0 = 0b11000101 ; // start conversion
__delay_ms(20);

if( (ADCON0 & 0b11000001) == 0b11000001 ) // when conversion completes
{
Val_x[0] = (ADRESH & 0b00000001);
Val_x[1] = (ADRESH & 0b00000010);
Val_x[2] = (ADRESH & 0b00000100);
Val_x[3] = (ADRESH & 0b00001000);
Val_x[4] = (ADRESH & 0b00010000);
Val_x[5] = (ADRESH & 0b00100000);
Val_x[6] = (ADRESH & 0b01000000);
Val_x[7] = (ADRESH & 0b10000000);

lcd_goto(1);
lcd_write(Val_x);

}
}
 
lcd_write is to write a single character to the LCD. use lcd_val to write a string. Not that it matters, I doubt the values you've written into Val_x are what you're after. Feel free to put RS = 1; as the first line in the lcd_write function.
 
to dougy83....

Rs is mentioned here:

void lcd_val(const char * s)
{
RS_PIN = 1; // write characters
while(*s)
lcd_write(*s++);
}
 
my problem is that value in ADRESH is on 8-bit. I just wanna convert that into ASCII string so that my lcd_val() function can put it on LCD as it is. I need to know different descimal values at different water levels.
 

I can see rs=1 is in lcd_val. Hopefully you can see that it's not present in lcd_write, which is what I said. So, if, as you've done above, you call lcd_goto(1); followed by lcd_write(xyz); lcd_write will send a command and not display a character.

Also, in the code above you call lcd_write with a pointer to an array. I think you meant to call lcd_val with the array.
 
my problem is that value in ADRESH is on 8-bit. I just wanna convert that into ASCII string so that my lcd_val() function can put it on LCD as it is. I need to know different descimal values at different water levels.
Did you have a look at my first post? It shows how to convert a char (value 0-255) into 3 ASCII characters.
 
correct me if i am wrong.. but writing rs=1 to lcd_write will create error in lcd_goto() as i have called lcd_write function there. thus the rs will go high and instead going to any position it will take it as char.

about calling lcd_write() func.. you r right . i wanted to call lcd_val(*Val_x).

as for your code, i am still trying that. Thnx for sharing.
 
whats wrong with this function:


void ADC_read(void)
{
unsigned char * Adc_x;
unsigned int Adc_val;
ADCON0 = 0b11000101 ; // start conversion
__delay_ms(20);

if( (ADCON0 & 0b11000001) == 0b11000001 ) // when conversion completes
{
Adc_val = ADRESH;

Adc_x[0] = (Adc_val/100);
lcd_write(Adc_x[0]);
Adc_x[1] = ((Adc_val/10)%10);
lcd_write(Adc_x[1]);
Adc_x[2] = (Adc_val %10);
lcd_write(Adc_x[2]);

}
}
 
whats wrong with this function:
You haven't said what the error message is, so I can't tell you specifically what's wrong with your program. I can tell you that you haven't allocated memory for the Adc_x array - you're just assigning values to random spots in memory. You don't even need the array. I posted the code to do it above.

Code:
void ADC_read(void)
	{
		unsigned int Adc_val;
	      ADCON0 = 0b11000101 ;						 // start conversion
		__delay_ms(20);
		
		if( (ADCON0 & 0b11000001) == 0b11000001 )   // when conversion completes 
			{
			    Adc_val = ADRESH;

			    lcd_write((Adc_val/100)+'0');
			    lcd_write(((Adc_val/10)%10)+'0');
			    lcd_write((Adc_val %10)+'0');
			}
	}
 
Hey dougy, thanx a lot. The code is working now. However can u plz tell me the '0' part used in lcd_write. I really cant make any sense out of it. I am posting the whole working code again for reference of others. Thanks again.

/**********************************************************************************************************************
****PROJECT : WATER LEVEL DISPLAY**************************************************************************************
****AUTHOR: PRASHANT KARADE********************************************************************************************
****DATE:24-January-2010 ********************************************************************************************
****DEVICE: PIC16F873A*************************************************************************************************
****This project sense the analog value at pin AN0 and displays equivalent decimal value on LCD display****************
**********************************************************************************************************************/
#include<htc.h>

__CONFIG(LVPDIS & UNPROTECT & WDTDIS & XT);// Low voltage Programming Disable & Watch Dog Timer OFF & External OScillator

#ifndef _XTAL_FREQ
#define _XTAL_FREQ 4000000 // oscillator freq 4MHZ
#endif

#define Data_Port PORTB
#define RS_PIN RC0
#define E_PIN RC1
// RW is grounded
#define HIGH 1
#define LOW 0

void ADC_read(void); // prototype
void lcd_write(unsigned char data); // prototype
void LCD_cmd(unsigned int data); // prototype
void LCD_init(void); // prototype
void lcd_str(const char * s); // prototype
void lcd_goto(unsigned int pos); // prototype

//********************************LCD FUNCTIONS***********************************************************//

void lcd_write(unsigned char data)
{
RS_PIN = HIGH;
Data_Port = data;// Write data to portB
E_PIN = HIGH; //
_delay(20); //STROBE
E_PIN = LOW; //
_delay(40); //
}

void LCD_cmd(unsigned int data)
{
RS_PIN = LOW;
Data_Port = data;
E_PIN = HIGH;
__delay_ms(10);
E_PIN = LOW;
__delay_ms(10);
}

void lcd_str(const char * s)
{
RS_PIN = 1; // write characters
while(*s)
lcd_write(*s++);
}

void lcd_goto(unsigned int pos)
{
LCD_cmd(128+pos);
}

//*****************************************************************************************************//
void LCD_init(void)
{
__delay_ms(100);
LCD_cmd(0X01); // clear display
__delay_ms(20);
LCD_cmd(0X02); // cursor to home
__delay_ms(20);
LCD_cmd(0X07); // show shift and auto increament cursor
__delay_ms(20);
LCD_cmd(0X0c); // Display on
__delay_ms(20);
LCD_cmd(0X1C); // Display shift, Right shift
__delay_ms(20);
LCD_cmd(0X3C); // 8 bit interface, 5X10 dot matrix
__delay_ms(20);
}

//**********************************************************************************************//
//*******************************ADC FUNCTIONS*************************************************//
void ADC_read(void)
{
unsigned int Adc_val;
ADCON0 = 0b11000101 ; // start conversion
__delay_ms(20);

if( (ADCON0 & 0b11000001) == 0b11000001 ) // when conversion completes
{
Adc_val = ADRESH;
lcd_goto(0X40);
lcd_write((Adc_val/100)+'0');
lcd_write(((Adc_val/10)%10)+'0');
lcd_write((Adc_val %10)+'0');
}
}
//**********************************************************************************************//
//****************************MAIN FUNCTION*****************************************************//
void main(void)
{
TRISA = 0XFF; // pin 0 AN0, 2 -Vref , 3 +Vref set as i/p
TRISB = 0X00; // output port
TRISC = 0X00; // output port

PORTA = 0X00; // initialize port as low
PORTB = 0X00; // initialize port as low
PORTC = 0X1C; // initialize port as low

LCD_init();

ADCON1 = 0b01001111;// Fosc/4 , AN0 = Analog , Vref = Vdd
ADCON0 = 0b11000001;// Fosc/4, AN0 , ADON = 1.

lcd_goto(0X00);
lcd_str("Water Level");

while(1)
{
ADC_read();
}
}
 
the '0' part:

If you were to try lcd_write(5), it wouldn't look real good. The ASCII character 5 is some control character that won't display properly as the ASCII value for '5' is 53. So if you tried lcd_write('5') or lcd_write(53) or lcd_write(5 + '0') then it would display as a readable '5' character.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…