[PIC] 10 bit result of ADC is require to convert in decimal to display in LCD

Status
Not open for further replies.

ecaits

Member level 4
Joined
Jan 16, 2014
Messages
76
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Visit site
Activity points
579
I am using 16F877 pic controller.
I have prepared code for ADC.
Analog input is converted in to ADC result. Now I want to convert that hex number to decimal to display in lcd.

Plz support how can i convert hex to decimal for ADC 10 bit output???
 

Hi,

Your ADC result needs to be converted to ASCII format to be displayed on a lcd.

However, how you do that is depends on the compiler you are using, which is ...?
 

use following code to write on lcd or u can send it to UART
Code:
void show_decimal(unsigned int numb)
{
	unsigned int temp;	  
	temp = numb/10000000;			  //1
	data_write(temp + '0');	

	temp = (numb/1000000)%10;		//2
	data_write(temp + '0');	

	temp = ((numb/100000)%100)%10;	   //3
	data_write(temp + '0');	
	
	temp = (((numb/10000)%1000)%100)%10;
	data_write(temp + '0');	

	temp = ((((numb/1000)%10000)%1000)%100)%10;
	data_write(temp + '0');	

	temp = (((((numb/100)%100000)%10000)%1000)%100)%10;
	data_write(temp + '0');	

	temp = ((((((numb/10)%1000000)%100000)%10000)%1000)%100)%10;
	data_write(temp + '0');

	temp = (((((((numb/1)%10000000)%1000000)%100000)%10000)%1000)%100)%10;
	data_write(temp + '0');
}

first it gives MSB then towards LSB.
 

hello,


Code:
temp = numb/10000000;

temp = (numb/1000000)%10;

temp = ((numb/100000)%100)%10;

i suspect a probleme, even i didn't try your code... for the first 3 items..
because you will allways get 0 as result with using unsigned int for variable Temp.
(Divider is over range > 65535 !)


i propose this code , C18 MPLAB
use a long int as intermediate calculus , and a flag to push "0" or "space" to get a constant width of char
and another flag to print out on USART or LCD


Code:
#define OUT_RS232 Drapeaux.Lcd=0;
#define OUT_LCD Drapeaux.Lcd=1;


unsigned char Entree[17]="1234567890123456";
unsigned char *valtxt;
 struct chbits {   // 8 flags
                 unsigned FrameErr:1;
                 unsigned Over:1;
                 unsigned Elligible:1;
                 unsigned Togle:1;
                 unsigned Blc:1;
                 unsigned standby:1;
                 unsigned Fill:1;
                 unsigned Lcd:1;
               }Drapeaux ;


 union
{  unsigned char Fanion;
   struct chbits Drapeaux ;
} Flags;








void Write_Word_(unsigned int M,char Sign)
{
unsigned int i,k,l;
unsigned long M1;
 valtxt=&Entree[0];
 if (Sign>1) return;
 if (Sign==0)
  {       M1=M;
        ultoa(M1,&Entree[0]);
  }
  else itoa(M,&Entree[0]);
 if (Flags.Drapeaux.Fill)
 {
  k=strlen(Entree);
  for (i=0;i<k;i++) Entree[4+Sign-i]=Entree[k-i-1];
  for (i=0;i<(5+Sign-k);i++)
  {if (Flags.Drapeaux.Blc) Entree[i]='0'; else Entree[i]=' ';}
 }
 Entree[5+Sign]=0;
 #ifdef LCD2x16
 if (Flags.Drapeaux.Lcd==1) LCD_PutRamString(valtxt); else k=PutStr_RS(valtxt);
 #else
 k=PutStr_RS(valtxt);
 #endif
}
 
Last edited:

In this case, First i will get ADC output in 10bit and will stored in register ADRESH and ADRESL and this output is in hex form.
Now I need to convert this 10 bit hex code in one decimal number.
After getting one decimal number I have to use that division and modulo operation.

My question is, i will get direct one decimal number or i need to convert 10 bit hex code into one decimal number???
 

hello,


My question is, i will get direct one decimal number or i need to convert 10 bit hex code into one decimal number??

Inside every MCU , value are always in binary mode .. 0 or 1
so when you get the result of ADC
You get a binary value...from the 2 register of 8 bits ...
and combine them into a register of 16 bits
but only 10 bits are significatif, so
after a Right alignment , you can use this 16 bits register wich can contain a binary value from
0000000000000000 to 0000001111111111

after you can use or represente it for displaing
as an hexadecimal value with 4 digits
0000 to 03FF
or decimal value with 5 digits
0000 to 1023

on evoluate PIC18F in C18 langage it is easy to use differnet mode for displayng the value
example

Code:
M=read(ADC);
//we suppose result of reading ADC is in the integer value M = 512
 k=fprintf(_H_USART," M=%04u      M=%4X ", M,M);

will print on terminal RS232 (via UART)
M=0512 M=0200
 


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
unsigned char digits[5];
unsigned int adcval;
 
 
void main(){
 
 
    while(1){
 
        digits[0] = adcval / 1000 + 48;
        digits[1] = (adcval / 100) % 10 + 48;
        digits[2] = (adcval / 10) % 10 + 48;
        digits[3] = adcval % 10 + 48;
        digits[4] = '\0';
 
        lcd_string(digits);
 
    }
}

 
Reactions: ecaits

    ecaits

    Points: 2
    Helpful Answer Positive Rating
Here, do I need to convert 10 bit ADC result to Decimal value??? Or can i directly assign 10 bit ADC result to variable "adcval" and use above division-modulo operation???
 

by doing so you are doing same thing to convert it to decimal and then need to convert to ASCII
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…