Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

pic18 adc output to lcd

Status
Not open for further replies.

balaji3724

Junior Member level 3
Joined
Jan 25, 2011
Messages
26
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,580
hi friends
i am doing a simple project in pic18 , doing a adc and display the output in lcd, i am using one chennal for analog input i got the 10 bit output clearly but i don't know how to show the 10 bit value in lcd please explain the logic only please :sad:
 

hi friends
i am doing a simple project in pic18 , doing a adc and display the output in lcd, i am using one chennal for analog input i got the 10 bit output clearly but i don't know how to show the 10 bit value in lcd please explain the logic only please :sad:





Here is the code for PIC18 using ADC for displaying current and Voltage in the LCD


#include <p18xxxx.h>
#include <delays.h>
#include "lcd.h"
#include <string.h>


#define LCD_RS PORTAbits.RA3
#define LCD_EN PORTAbits.RA1
#define LCD_WR PORTAbits.RA2

#define set_dd_line1_pos1 0x80
#define set_dd_line2_pos1 0x80

char value1 [ ] = "0V";
char value2 [ ] = "5V";


void lcd_write_cmd(unsigned char cmd)
{
unsigned char temp2;
LCD_RS = 0;
Delay10TCYx(4);

temp2 = cmd;
temp2 = temp2 >> 4;
PORTD = temp2 & 0x0F;

Delay1KTCYx(1);
lcd_strobe();
Delay1KTCYx(1);

temp2 = cmd;
PORTD = temp2 & 0x0F;

Delay1KTCYx(1);
lcd_strobe();
Delay1KTCYx(1);

}



void lcd_clear(void)
{
lcd_write_cmd(0x01);
Delay1KTCYx(2);

}



void lcd_write_data(char data)
{
char temp1;

LCD_RS = 1;
Delay10TCYx(4);

temp1 = data;
temp1 = temp1 >> 4;
PORTD = temp1 & 0x0F;

Delay1KTCYx(1);
lcd_strobe();
Delay1KTCYx(1);

temp1 = data;
PORTD = temp1 & 0x0F;

Delay1KTCYx(1);
lcd_strobe();
Delay1KTCYx(1);

}



void lcd_goto(unsigned char LCD_POS)
{
lcd_write_cmd(LCD_POS);
Delay1KTCYx(2);

}



void lcd_strobe(void)
{
LCD_EN = 1;
Delay10TCYx(4);

LCD_EN = 0;
Delay10TCYx(4);
}

void lcd_init(void)
{
LCD_RS = 0;
LCD_WR = 0;

Delay1KTCYx(250);
Delay1KTCYx(250);
Delay1KTCYx(250);
Delay1KTCYx(250);

PORTD = 0x03;

lcd_strobe();
Delay1KTCYx(250);

PORTD = 0x03;
lcd_strobe();
Delay1KTCYx(250);

PORTD = 0x03;
lcd_strobe();
Delay1KTCYx(250);

PORTD = 0x02;
lcd_strobe();
Delay1KTCYx(250);
Delay1KTCYx(250);


lcd_write_cmd(0x28)

lcd_write_cmd(0x01);
Delay1KTCYx(20);
Delay1KTCYx(20);
lcd_write_cmd(0x0F);

Delay1KTCYx(20);

lcd_write_cmd(0x06);


Delay1KTCYx(20);
}

main(void)
{

char outchar;
unsigned char Sizevalue1, Sizevalue2, Sizevalue3;
unsigned char index;
#define constant 0x01
#define constant2 0x05

TRISA = 0x00;
TRISD = 0x00;

PORTD = 0x00;


ADCON0 = 0b11000001;

ADCON1 = 0b00001110;

for(;;)
{
ADCON0bits.GO = 1;
(PIR1bits.ADIF == 0);
PORTD = ADRESH;

if(ADRESH < constant)
{
Sizevalue1 = strlen(value1); // get size of array
LCD_EN = 0;
LCD_RS = 0;
LCD_WR = 0;
lcd_init();
LCD_RS = 1;

lcd_goto(set_dd_line1_pos1);
for (index = 0; index < Sizevalue1; index++)
{
outchar = value1[index];
lcd_write_data(outchar);
}
Delay1KTCYx(250);
Delay1KTCYx(250);
Delay1KTCYx(250);
lcd_goto(set_dd_line1_pos1);
PIR1bits.ADIF = 0;
lcd_clear();
}


if(ADRESH > constant2)
{
Sizevalue2 = strlen(value2);
LCD_EN = 0;
LCD_RS = 0;
LCD_WR = 0;
lcd_init();
LCD_RS = 1;

lcd_goto(set_dd_line1_pos1);
for (index = 0; index < Sizevalue1; index++)
{
outchar = value2[index];
lcd_write_data(outchar);
}
Delay1KTCYx(250);
Delay1KTCYx(250);
Delay1KTCYx(250);
lcd_goto(set_dd_line1_pos1);
PIR1bits.ADIF = 0;
lcd_clear();
}
}
}
 
hi friends
i am doing a simple project in pic18 , doing a adc and display the output in lcd, i am using one chennal for analog input i got the 10 bit output clearly but i don't know how to show the 10 bit value in lcd please explain the logic only please :sad:

Nothing to worry about !
You only need to write values in ADRESH and ADRESL (Where ADC results are stored by PIC). Don't worry about 10bit; ADRESH has zeros at unused higher-6-bits.

Than you,
 
thanks your responce
i am using c

---------- Post added at 07:59 ---------- Previous post was at 07:56 ----------

ok
but ADRESH and ADRESL values how to display in lcd this are 10 bit value know so.....
 

Hi,

ADRESH and ADRESL are 8bit values, same as other registers. Concatenating both them form a 16bit value. But ADC result is 10bit. So upper 6bits in ADRESH are not used, they are all zero. When you read ADRESH you see 8bits but actually first 2bits were written by ADC.

How you display 8bit value on LCD ? did you convert them to decimal value ?

If so, you have to convert this 16bit-binary to decimal and display decimal value on LCD.

Thank you,
 
k thanks
can u explain the pic16 i2c ,i know the procedure about i2c but i need some sample pgm using c (mplab hi-tech complier)
 

k i know the this page but there is no clear material, i mean this things are used only experience guys

---------- Post added at 14:36 ---------- Previous post was at 14:36 ----------

hi
using c

---------- Post added at 14:38 ---------- Previous post was at 14:36 ----------

hi
as well as i am using hi tech complier c so this codes are not this complier
 

but ADRESH and ADRESL values how to display in lcd this are 10 bit value know so.....

---------- Post added at 12:44 ---------- Previous post was at 12:43 ----------

what language u use?

**broken link removed**
Online Movies free
 

hi
this rtc using pic16 i2c code in lcd the output is empty so please see my code if any mistake please inform me




#include<htc.h>

#include<stdio.h>
//#include<main.h>
//#include<intrins.h>
#pragma config WDT=OFF
#define ACK 1
#define NO_ACK 0
#define SLAVE 0xD0
#define WRITE 0x00
#define READ 0x01
#define ERR_ACK 0x01
#define DATA PORTB
//LCD Control Lines
#define RS RB0
#define lcd_RW RB1
#define lcd_e RB2


//Display messages
unsigned char msg[] = ("REAL TIME CLOCK ");
unsigned char msg1[] = ("*** DEMO ***");
unsigned char msg2[] = {"DATE: / / "};
unsigned char msg3[] = {"TIME: : : "};


unsigned int i;
unsigned char j,msb,lsb;
//void i2c_init();
void i2c_waitForIdle();
//void stop1();
//void start1();
//LCD Functions
void lcd_init(void);
void lcd_cmd(unsigned char);
void lcd_display(unsigned char);
void delay(void);
void disp_date_time(void);
void lcd_display1(unsigned char);
void lcd_display2(unsigned char);
void lcd_display3(unsigned char);
void lcd_display4(unsigned char);
void lcd_display5(unsigned char);
void lcd_display6(unsigned char);
void lcd_display7(unsigned char);
void lcd_display8(unsigned char);
void lcd_display9(unsigned char);
void lcd_display10(unsigned char);
void lcd_display11(unsigned char);
void lcd_display12(unsigned char);

void InitSerial(void);
void DelayMs(unsigned int a);

void hex_decimal(unsigned char);
void bcd_hex(unsigned char);





//---------------------------------------
// DS1307 driver
//---------------------------------------

unsigned char RTC_ARR[7]; // Buffer for second,minute,.....,year
unsigned char p;

void lcd_cmd(unsigned char cmnd)
{
DATA = 0xf0&cmnd; //Masking lower 4 Bits
RS = 0; lcd_RW = 0;
lcd_e = 1;
DelayMs(35);
lcd_e = 0;

DATA = cmnd*16; //Masking lower 4 Bits
RS = 0; lcd_RW = 0;
lcd_e = 1;
DelayMs(35);
lcd_e = 0;

}

//----------------------------------
// LCD Data Function
//----------------------------------
void lcd_display(unsigned char dat)
{
DATA = 0xf0&dat; //Masking lower 4 Bits
RS = 1; lcd_RW = 0;
lcd_e = 1;
DelayMs(15);
lcd_e = 0;

DATA = dat*16; //Masking lower 4 Bits
RS = 1; lcd_RW = 0;
lcd_e = 1;
DelayMs(15);
lcd_e = 0;


}

//----------------------------------
// LCD Initialization
//----------------------------------
void lcd_init(void)
{
unsigned char i;

lcd_cmd(0x28);
DelayMs(15);
lcd_cmd(0x0c);
DelayMs(15);
lcd_cmd(0x06);
DelayMs(15);
lcd_cmd(0x01);
DelayMs(15);

//-------------------------------------------
// First Line Message Display
//-------------------------------------------
lcd_cmd(0x80); //First Line Initialization
DelayMs(35);
i=0;
while(msg!='\0')
{
lcd_display(msg);
i++;
}
DelayMs(50);

//-------------------------------------------
// Second Line Message Display
//-------------------------------------------
lcd_cmd(0xc0); //Second Line Initialization
DelayMs(35);
i=0;
while(msg1!='\0')
{
lcd_display(msg1);
i++;
}
DelayMs(50);
}

//LCD Delay function
void delay(void)
{
unsigned int a;
for(a=0;a<=1000;a++);
}


//DIsplay DATE and Time
//Display Message in First line
void disp_date_time()
{
lcd_cmd(0x80);
delay();
delay();
for(i=0;msg2 != '\0';i++)
lcd_display(msg2);
delay();

//Display Message in First line
lcd_cmd(0xC0);
delay();
delay();
for(i=0;msg3 != '\0';i++)
lcd_display(msg3);
delay();
}




// start I2C
//void Start(void)
//void st1()
//{
// i2c_waitForIdle();
// SSPCON2=0x01;
//}
void start()
{
//i2c_waitForIdle();
//SSPCON2=0x1b;
SEN=1;
while(SSPIF==1);

delay();
}

// stop I2C
//void Stop(void)
//void sp1()
//{
//i2c_waitForIdle();
//SSPCON2=0x04;
//}

void stop()
{
//i2c_waitForIdle();
PEN=1;
delay();
}











// Write I2C
//void WriteI2C(unsigned char Data)
unsigned char WriteI2C( unsigned char i2cWriteData )
{
//i2c_waitForIdle();
while(SSPIF==1);
SSPBUF = i2cWriteData;

delay();
//if(ACKSTAT)
{
while(BF);
SSPIF=0;
while(ACKSTAT);
}
return ( ! ACKSTAT ); // function returns '1' if transmission is acknowledged
}



// Read I2C
//unsigned char ReadI2C(bit ACK_Bit)
int ReadI2C( unsigned char ack )
{
unsigned char i2cReadData;

//i2c_waitForIdle();

RCEN=1;

//i2c_waitForIdle();

i2cReadData = SSPBUF;
while(RCEN==0);
PORTD=i2cReadData;
//delay();
//i2c_waitForIdle();

if ( ack )
{
ACKDT=0;
}
else
{
ACKDT=1;
}
ACKEN=1; // send acknowledge sequence

return( i2cReadData );
}



// Read RTC (all real time)
void ReadRTC(unsigned char * buff)
{
start();
WriteI2C(0xD0);
WriteI2C(0x00);
stop();

start();
WriteI2C(0xD1);
stop();
*(buff+0)=ReadI2C(ACK); // Second
*(buff+1)=ReadI2C(ACK); // Minute
*(buff+2)=ReadI2C(ACK); // hour
*(buff+3)=ReadI2C(ACK); // Day
*(buff+4)=ReadI2C(ACK); // date
*(buff+5)=ReadI2C(ACK); // month
*(buff+6)=ReadI2C(NO_ACK); // year
stop();
}

// Write RTC
void WriteRTC(unsigned char *buff)
{
start();
WriteI2C(0xD0);
WriteI2C(0x00);
WriteI2C(*(buff+0));
WriteI2C(*(buff+1));
WriteI2C(*(buff+2));
WriteI2C(*(buff+3));
WriteI2C(*(buff+4));
WriteI2C(*(buff+5));
WriteI2C(*(buff+6));
stop();
}




// Delay mS function
void DelayMs(unsigned int count)
{ // mSec Delay 11.0592 Mhz
unsigned int i;
while(count)
{
i = 115;
while(i>0) i--;
count--;
}
}

//SECONDS DISPLAY
void lcd_display1(unsigned char dat)
{
lcd_cmd(0xcd);
delay();
lcd_display(dat);
delay();
}

void lcd_display2(unsigned char dat)
{
lcd_cmd(0xcc);
delay();
lcd_display(dat);
}

//Minutes DISPLAY
void lcd_display3(unsigned char dat)
{
lcd_cmd(0xca);
delay();
lcd_display(dat);
}

void lcd_display4(unsigned char dat)
{
lcd_cmd(0xc9);
delay();
lcd_display(dat);
}


//Hours DISPLAY
void lcd_display5(unsigned char dat)
{
lcd_cmd(0xc7);
DelayMs(2);
lcd_display(dat);

}

void lcd_display6(unsigned char dat)
{
lcd_cmd(0xc6);
delay();
lcd_display(dat);
delay();
}


//DATE DISPLAY
void lcd_display7(unsigned char dat)
{
lcd_cmd(0x87);
delay();
lcd_display(dat);
}

void lcd_display8(unsigned char dat)
{
lcd_cmd(0x86);
delay();
lcd_display(dat);

}

//Month DISPLAY
void lcd_display9(unsigned char dat)
{
lcd_cmd(0x8a);
delay();
lcd_display(dat);

}

void lcd_display10(unsigned char dat)
{
lcd_cmd(0x89);
delay();
lcd_display(dat);

}

//YEAR DISPLAY
void lcd_display11(unsigned char dat)
{
lcd_cmd(0x8d);
delay();
delay();
lcd_display(dat);
delay();
}

void lcd_display12(unsigned char dat)
{
lcd_cmd(0x8c);
delay();
delay();
lcd_display(dat);
delay();
}



//unsigned char bcd_hex(unsigned char hex)
void bcd_hex(unsigned char hex)
{
unsigned char bcd;
bcd = hex;
bcd = bcd & 0x0F;
bcd = bcd | 0x30;
lsb = bcd;
bcd = hex;
bcd>>=4;
bcd = bcd & 0x0F;
bcd = bcd | 0x30;
msb=bcd;
}





//---------------------------------------
// Main program
//---------------------------------------
void main(void)
{
// ptr=day;
TRISB=0;
//void i2c_init();
//TRISC=0x18;
TRISC = 0b00011000; // set SCL and SDA pins as inputs
//SSPADD=0x18;

SSPCON = 0x38; // set I2C master mode
SSPCON2 = 0x00;

//file://SSPADD = 0x6; // 400kHz bus with 10MHz xtal - use 0x0C with 20MHz xtal
SSPADD = 0x09; // 100k at 4Mhz clock

CKE=0; // use I2C levels worked also with '0'
SMP=1; // disable slew rate control worked also with '0'

//PSPIF=0; // clear SSPIF interrupt flag
//BCLIF=0;
lcd_init();
delay();
disp_date_time();

// Setup time and enable oscillator
ReadRTC(&RTC_ARR[0]);
RTC_ARR[0] = RTC_ARR[0] & 0x7F; // enable oscillator (bit 7=0)
RTC_ARR[1] = 0x45; // minute = 59
RTC_ARR[2] = 0x11; // hour = 05 ,24-hour mode(bit 6=0)
RTC_ARR[3] = 0x03; // Day = 1 or sunday
RTC_ARR[4] = 0x24; // Date = 30
RTC_ARR[5] = 0x11; // month = August
RTC_ARR[6] = 0x08; // year = 05 or 2005
WriteRTC(&RTC_ARR[0]); // Set RTC

while(1)
{
ReadRTC(&RTC_ARR[0]);
bcd_hex(RTC_ARR[0]);//seconds
lcd_display1(lsb);
lcd_display2(msb);
bcd_hex(RTC_ARR[1]);//minutes
lcd_display3(lsb);
lcd_display4(msb);
bcd_hex(RTC_ARR[2]);//Hours
lcd_display5(lsb);
lcd_display6(msb);
bcd_hex(RTC_ARR[4]);//Date
lcd_display7(lsb);
lcd_display8(msb);
bcd_hex(RTC_ARR[5]);//Month
lcd_display9(lsb);
lcd_display10(msb);
bcd_hex(RTC_ARR[6]);//Year
lcd_display11(lsb);
lcd_display12(msb);

}
}
 

hai
using c, i have only doubt in conversion
 

Hi balaji,

Could you elaborate more.
It's easy if you debug the code and come up with any error. What you mean by convention ? (from where to where; about AD convention or I2C for RTC)

Thank you,
 

Hi balaji,

Could you elaborate more.
It's easy if you debug the code and come up with any error. What you mean by convention ? (from where to where; about AD convention or I2C for RTC)

Thank you,
sorry sir converstion
 

If you need to format an integer or float value to send to a display, you can use the sprintf function that is defined in stdio.h.
Example:

Code:
#include <stdio.h>

void display_value(void)
  {
  unsigned int value = 22;
  char buffer[16];  /* size of line on display */
  
  sprintf(buffer, "Value is %d", value);  /* Same formatting options as printf */
  display_line(buffer);                   /* Your call to display function */
  }
 

Hi balaji,

Do you mean how to convert 10bit ADC result value for LCD display ?
If so, you need to convert this 10bit binary value to Decimal-digits (like binary 11 11111111 to decimal 1023) Then convert them to ASCII codes of digits, then write on LCD.

Thank you,
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top