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.

ADC to LCD charater conversion concept

Status
Not open for further replies.

Sahil_khan

Newbie level 6
Joined
Jan 27, 2013
Messages
13
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Pakistan
Activity points
1,399
Dear Guys

I am new to PIC Microcontroller and just started working on an example i.e. to read ADC value and convert it to ASCI and then display on LCD. I am using mikroC compiler.

Can some body explain what does the following code do ?

unsigned char ch;
unsigned int adc_rd;
char *text;
long tlong;

tlong = (long)adc_rd * 5000; // covert adc reading to milivolts
tlong = tlong / 1023; // 0..1023 -> 0-5000mV

ch = tlong / 1000; // extract volts digit
LCD_Chr(2,9,48+ch); // write ASCII digit at 2nd row, 9th column
LCD_Chr_CP('.');

ch = (tlong / 100) % 10; // extract 0.1 volts digit
LCD_Chr_CP(48+ch); // write ASCII digit at cursor point

ch = (tlong / 10) % 10; // extract 0.01 volts digit
LCD_Chr_CP(48+ch); // write ASCII digit at cursor point

ch = tlong % 10; // extract 0.001 volts digit
LCD_Chr_CP(48+ch); // write ASCII digit at cursor point
LCD_Chr_CP('V');

specifically, I can not understand the highlighted BLUE text.

Thank you all.

Regards
Sahil Khan
 

I don't use PIC, but obviously it uses a 10 bit ADC (because of 1023). So you must first calculate mVolts per LSB, I suppose 5000 is the reference voltage used. So If the maximum ADC value is 1023 and the reference voltage is 5000mV, then 5000/1023 is the ADC resolution in mV. Multiplying this with the ADC result, you will get the value in mV of the ADC voltage (tlong variable in this sample program). I am not sure though, if this is the right division with 1023. Maybe 1024 is the right value, I am sure that the datasheet clarifies that in the ADC section.
 
If yo use mikroC then you can use this code.


Code C - [expand]
1
2
3
4
5
unsigned char str_tlong[17];
 
//Put the below code after the line tlong = tlong / 1023; // 0..1023 -> 0-5000mV
LongToStr(tlong, str_tlong);
LCD_Out(1,1,tlong);



The code you mentioned is extracting each digit of tlong and converting it to ascii characters and displaying it on LCD using LCD Character display function.
 
Thank you for your reply.

The problem is that, my reference voltage is 5V so based on that if I modify my code as follows;

tlong = (long)adc_rd*5/1023;
ch = tlong / 1; // extract volts digit

It gives me 5.005V instead of 5.000V (assuming adc_rd = 1023);

- - - Updated - - -

kindly Note the difference in the modified code i.e. 5000 is changed with 5 and 1000 is changed with 1 with reference to my initial post.
 

It is better if you use float instead of long like below


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
unsigned char str_adc_val[17];
float adc_val = 0.0, old_val = 0.0;
 
void main(){
 
 
    //TRISx setting here
    //ADCON1 setting here
 
    while(1){
 
        adc_val = ADC_Read(0);
        if(old_val != adc_val){
 
            old_val = adc_val;
            adc_val = adc_val * (5 / 1023);
            FloatToStr(adc_val, atr_adc_val);
            LCD_Out(1,1,str_adc_val);
        }
    }
}

 
It gives me 5.005V instead of 5.000V (assuming adc_rd = 1023);
Even if there is a cast problem from the code, this doesn't seem reasonable. You get this 5005 value from the ADC read, or you see this result on the LCD? Maybe there is a problem with the LCD code, that's why I'm asking.
 
No actually for debugging perpose I am assigning the value instead of measuring on the pin and then debugging in Proteus.
i.e. adc_rd = 1023;
 

@ Jayanth I am using PIC16F887 and I have configured all the required registers. Thank you very much.


Thank you all guys for your concern. I could not fix the problem yet but I hope it would be soon.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top