Author Topic: Problem with C code for pic (lcd characters wrong placement)  (Read 2360 times)

0 Members and 1 Guest are viewing this topic.

Offline little_carlosTopic starter

  • Regular Contributor
  • *
  • Posts: 133
Problem with C code for pic (lcd characters wrong placement)
« on: March 20, 2017, 12:46:58 am »
Hello
I 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: [Select]
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[15];
char Text[15];           
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 activacion
Delay_ms(10);
i++;
}

if(RA2_bit == 1){       // Boton para decrementar la temperatura de activacion
Delay_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 temperatura
Lcd_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 que
RA3_bit = 0;               // se apage la refrigeracion al llegar a esa temperatura, en este caso cuando la temperatura es 10 grados menor que
Lcd_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 centigrados
  IntToStr(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


 

Offline rs20

  • Super Contributor
  • ***
  • Posts: 2318
  • Country: au
Re: Problem with C code for pic (lcd characters wrong placement)
« Reply #1 on: March 20, 2017, 12:51:47 am »
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.
 

Offline little_carlosTopic starter

  • Regular Contributor
  • *
  • Posts: 133
Re: Problem with C code for pic (lcd characters wrong placement)
« Reply #2 on: March 20, 2017, 01:00:00 am »
I think you didnt read the whole code bro, saludos.
 
The following users thanked this post: MarkF

Offline hexreader

  • Frequent Contributor
  • **
  • Posts: 262
  • Country: england
Re: Problem with C code for pic (lcd characters wrong placement)
« Reply #3 on: March 20, 2017, 02:31:36 am »
Im putting in to be on "2,2,6" but instead is displaying on "2,2,10".
Check help for IntToStr function. The returned string is 6 characters long followed by null terminator

You 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: [Select]
  Lcd_Out(2,6,Text2 + 3);       // only show last 3 characters of 6 character number

You 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
« Last Edit: March 20, 2017, 03:02:00 am by hexreader »
 

Offline alsetalokin4017

  • Super Contributor
  • ***
  • Posts: 2055
  • Country: us
Re: Problem with C code for pic (lcd characters wrong placement)
« Reply #4 on: March 20, 2017, 03:06:29 am »
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.
 :wtf:
The easiest person to fool is yourself. -- Richard Feynman
 

Offline amyk

  • Super Contributor
  • ***
  • Posts: 8276
Re: Problem with C code for pic (lcd characters wrong placement)
« Reply #5 on: March 20, 2017, 09:48:34 am »
It reminds me of C<>F conversions which disregard precision.
 

Offline little_carlosTopic starter

  • Regular Contributor
  • *
  • Posts: 133
Re: Problem with C code for pic (lcd characters wrong placement)
« Reply #6 on: July 02, 2017, 02:35:01 pm »
Im making interdimentional portals :P
Nah, just that im not quite aware of how to reduce the amount of digits shown.
 

Offline testian

  • Regular Contributor
  • *
  • Posts: 55
Re: Problem with C code for pic (lcd characters wrong placement)
« Reply #7 on: July 02, 2017, 10:26:28 pm »
Check the documentation for the function. https://download.mikroe.com/documents/compilers/mikroc/pic/help/conversions_library.htm#inttostr
You number has just spaces in front.

You can remove them with ltrim. https://download.mikroe.com/documents/compilers/mikroc/pic/help/conversions_library.htm#ltrim
Code: [Select]
Ltrim ( Text2 );   
Or try to write your own conversion function without leading spaces. http://www.geeksforgeeks.org/implement-itoa/
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf