Problem with C code for pic (lcd characters wrong placement) - Page 1
Forum section: electronics / beginners

author: little_carlos
author_rank: Regular Contributor
timestamp: March 20, 2017, 12:46:58 am
content: HelloI made a termometer and im displaying values on the lcd, but the value of "i" is not displaying where it should be. Im putting in to be on "2,2,6" but instead is displaying on "2,2,10". ive double checked the whole code but i cant find what is wrong. Could you help me explain what is going on?here is the code:Code:sbit LCD_RS at RB5_bit;sbit LCD_EN at RB4_bit;sbit LCD_D7 at RB0_bit;sbit LCD_D6 at RB1_bit;sbit LCD_D5 at RB2_bit;sbit LCD_D4 at RB3_bit;sbit LCD_RS_Direction at TRISB5_bit;sbit LCD_EN_Direction at TRISB4_bit;sbit LCD_D7_Direction at TRISB0_bit;sbit LCD_D6_Direction at TRISB1_bit;sbit LCD_D5_Direction at TRISB2_bit;sbit LCD_D4_Direction at TRISB3_bit;signed int Radc = 0;float Tem = 0;char Text2;char Text;char txt1[] = "Temp: ";char txt2[] = "Act:";unsigned int i;int b;void main(){ANSEL = 0b00000001;ANSELH = 0;TRISA0_bit = 1;TRISA1_bit = 1;TRISA2_bit = 1;TRISA3_bit = 0;C1ON_bit = 0;C2ON_bit = 0;i = 30;ADC_Init();PORTA = 0;Lcd_Init();Lcd_Cmd(_LCD_CLEAR);Lcd_Cmd(_LCD_CURSOR_OFF);Lcd_Out(1,1, txt1);Lcd_Chr(1,14,223);Lcd_Chr(1,15,'C');Lcd_Chr(2,10,223);Lcd_Out(2,1,txt2);while(1){Radc = ADC_Get_Sample(0);if(RA1_bit == 1){ // Boton para incrementar la temperatura de activacionDelay_ms(10);i++;}if(RA2_bit == 1){ // Boton para decrementar la temperatura de activacionDelay_ms(10);i--;}if(Tem >= i ){RA3_bit = 1; // aqui se enciende la ventilacion cuando la temperatura de activacion es igual o mayor a la lectura de temperaturaLcd_Out(2,12,"ONN");}if(Tem <= i-10){ // Aqui es donde se modifica el parametro de apagado de la refrigeracion, el numero "-X" puede cambiarse para queRA3_bit = 0; // se apage la refrigeracion al llegar a esa temperatura, en este caso cuando la temperatura es 10 grados menor queLcd_Out(2,12,"OFF"); // la temperatura de acticación}Tem = (float)(Radc/2.05); // Conversion de lectura del ADC a valor de temperatura en grados centigradosIntToStr(i,Text2);FloatToStr(Tem,Text);Lcd_Out(2,6,Text2);Lcd_Out(1,6, Text);Delay_ms(300);}}here is a picture of simulation, it beheaves the same on physical

author: rs20
author_rank: Super Contributor
timestamp: March 20, 2017, 12:51:47 am
content: Where's your implementations of IntToStr and FloatToStr?And what on earth do you mean by "2,2,6" and "2,2,10"? Is this a three-dimensional display? Screen coordinates only need two numbers.

author: little_carlos
author_rank: Regular Contributor
timestamp: March 20, 2017, 01:00:00 am
content: I think you didnt read the whole code bro, saludos.

author: hexreader
author_rank: Frequent Contributor
timestamp: March 20, 2017, 02:31:36 am
content: Quote from: little_carlos on March 20, 2017, 12:46:58 amCheck help for IntToStr function. The returned string is 6 characters long followed by null terminatorYou code DOES display the six characters at 2,2,6 but the first 4 characters are spaces in your example.Change the appropriate line to something more like this to get what you want:Code:Lcd_Out(2,6,Text2 + 3); // only show last 3 characters of 6 character numberYou forgot to say which compiler you use and which PIC. Luckily my crystal ball tells me you are using mikroC pro for PIC compiler and PIC16F887

author: alsetalokin4017
author_rank: Super Contributor
timestamp: March 20, 2017, 03:06:29 am
content: I'd like to know what kind of thermometer sensor is precise to the hundred-thousandth of a degree C, and what kind of application needs that kind of precision and accuracy.

author: amyk
author_rank: Super Contributor
timestamp: March 20, 2017, 09:48:34 am
content: It reminds me of C<>F conversions which disregard precision.

author: little_carlos
author_rank: Regular Contributor
timestamp: July 02, 2017, 02:35:01 pm
content: Im making interdimentional portalsNah, just that im not quite aware of how to reduce the amount of digits shown.

author: testian
author_rank: Regular Contributor
timestamp: July 02, 2017, 10:26:28 pm
content: Check the documentation for the function.https://download.mikroe.com/documents/compilers/mikroc/pic/help/conversions_library.htm#inttostrYou number has just spaces in front.You can remove them with ltrim.https://download.mikroe.com/documents/compilers/mikroc/pic/help/conversions_library.htm#ltrimCode:Ltrim ( Text2 );Or try to write your own conversion function without leading spaces.http://www.geeksforgeeks.org/implement-itoa/