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*********************************//
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*********************************//