Displaying on 2*16 LCD floating numbers

Thread Starter

AustinCann

Joined Jan 30, 2011
12
Dear all
I am finding transformer turn ratio using pic 16f877a and 2*16 LCD. I acquired the signal for primary voltage upto 3 decimal places as well as signal for secondary upto 3 decimal places.
The problem comes when I divide the numbers and display on the LCD. The best I achieved is the ratio result to the nearest integer which is unfortunately no good. Please find below my code. Cant find the way out really. Any ideas?
Kind Regards


unsigned char ch,ch2,ch3;
unsigned int adc_rd1,adc_rd2;

char *text;
long tlong,tlong1,tlong2,tlong3,tlong4,rate;





void main() {

TRISA = 0xFF; // designate PORTA as input
TRISB = 0X00;


//INTCON = 0; // disable all interrupts
Lcd_Config(&PORTB, 4, 5, 6, 3, 2, 1, 0); // Lcd_Init_EP5, see Autocomplete
LCD_Cmd(LCD_CURSOR_OFF); // send command to LCD (cursor off)
LCD_Cmd(LCD_CLEAR); // send command to LCD (clear LCD)



text = "TTR Finder"; // assign text to string
LCD_Out(1,1,text); // print string a on LCD, 1st row, 1st column
text = "Initializing...."; // assign text to string
LCD_Out(2,1,text); // print string a on LCD, 2nd row, 1st column
delay_ms(3000);
text = "Reading TTR";
LCD_Out(1,1,text);
text = "Please wait!" ;
LCD_Out(2,1,text);
delay_ms(2000);
LCD_Cmd(LCD_CLEAR);
while (1) {

text = "Vs:"; // assign text to string



LCD_Out(1,1,text); // print string a on LCD, 2nd row, 1st column

ADCON1 = 0x83; // configure VDD as Vref, and analog channels
adc_rd1 = ADC_Read(2); // get ADC value from 2nd channel

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

ch = tlong1 / 1000; // extract volts digit
LCD_Chr(1,4,48+ch); // write ASCII digit at 1st row, 4th column
LCD_Chr_CP('.');

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

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

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

Delay_ms(1);

text = "Vp:"; // assign text to string



LCD_Out(2,1,text); // print string a on LCD, 2nd row, 1st column

ADCON1 = 0x83; // configure VDD as Vref, and analog channels
adc_rd2 = ADC_Read(3); // get ADC value from 2nd channel

tlong2 = (long)adc_rd2 * 5000; // covert adc reading to milivolts
tlong3 = tlong2 / 1023; // 0..1023 -> 0-5000mV

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

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

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

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

Delay_ms(3000);
LCD_Cmd(LCD_CLEAR);
delay_ms(100);
text="TTR=";
LCD_out(1,1,text);
delay_ms(100);
rate=(long)(tlong3/tlong1)*1000;





ch3 = rate/1000; // extract volts digit
LCD_Chr(1,6,48+ch3); // write ASCII digit at 2nd row, 4th column
LCD_Chr_CP('.');


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

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

ch3 = rate % 10; // extract 0.001 volts digit
LCD_Chr_CP(48+ch3); // write ASCII digit at cursor point


Delay_ms(5000);
LCD_Cmd(LCD_CLEAR);
delay_ms(100);

}
}
 
Top