[SOLVED] Integer to string convertor for mikroC

Status
Not open for further replies.

shubham kumar

Member level 3
Joined
Sep 11, 2014
Messages
59
Helped
1
Reputation
2
Reaction score
1
Trophy points
8
Location
bangalore
Activity points
511
Hi
I am using MikroC and PIC18F452.
I am trying to make integer to string function by myself but the output is not correct.
When I am running the same code in debug mode.. the values in there are correct but when I display it on LCD they are different.

Code:
// LCD module connections
sbit LCD_RS at LATD6_bit;
sbit LCD_EN at LATD7_bit;
sbit LCD_D4 at LATB4_bit;
sbit LCD_D5 at LATB5_bit;
sbit LCD_D6 at LATB6_bit;
sbit LCD_D7 at LATB7_bit;

sbit LCD_RS_Direction at TRISD6_bit;
sbit LCD_EN_Direction at TRISD7_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
// End LCD module connections

#include <stdio.h>
#include<stdio.h>
#include<string.h>
#include<math.h>

int displ(int, char *);

int main()
 {
char arr[16];
int i=-398;
  LCD_init();
  Lcd_Cmd(_LCD_CLEAR);
  
displ(i,arr);
/*for(i=0;arr[i]!='\0';i++)
printf("%d",arr[i]) ;*/

 LCD_out(1,1,arr);
return 0;
}

int displ( int i,char *n)
{
char test_arr[16];
char j=0,l;
int sign,k=0,p=0;   // k,p are looping variables
if(i<0)
{
sign=i;
i=-i;
}

while(i>0)                     // this will store the integer digits in reverse 
{
n[j++]= i%10;
i=i/10;
}

if(sign<0)                                // till here total number will be reversed
{
n[j++]=  '-';
}
n[j]= '\0';              


for(p=0,k=j-1;k>=0;p++,k--)               // above number is again reversed and stored using pointer 
                                                     // address
{
test_arr[p]=  n[k];
}

for(k=0;test_arr[k]!='\0';k++)                     // converting a integer to its character value
{                                                           // 48 in character refers to 0
test_arr[k]=test_arr[k]+48;
n[k]=  test_arr[k];

  if((sign<0) && (k==0))  {                        // if number is negative then make it to '-' sign
  test_arr[0]=test_arr[0]-48;
  n[0]=  test_arr[0];
               }
}

n[k] = '\0';            // not necessary -- if want to check till n[k]!='\0'

/*for(k=0;k<j;k++)
 {
    printf("%d",test_arr[k]);
 }*/
 
 LCD_out(2,1,test_arr);
 return 0;
}

The values of variables in the debug mode are perfectly fine. but not working when trying to display on LCD
 

Are you terminating the test_arr with a null character before sending it to LCD_Out() ?
 

Yup, I am terminating it with null character

Code:
sbit LCD_RS at LATD6_bit;
sbit LCD_EN at LATD7_bit;
sbit LCD_D4 at LATB4_bit;
sbit LCD_D5 at LATB5_bit;
sbit LCD_D6 at LATB6_bit;
sbit LCD_D7 at LATB7_bit;

sbit LCD_RS_Direction at TRISD6_bit;
sbit LCD_EN_Direction at TRISD7_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
// End LCD module connections

#include <stdio.h>
#include<stdio.h>
#include<string.h>
#include<math.h>

void LC_cmd(unsigned char);
void LC_out(unsigned char ,unsigned char ,  char *);
void LC_dat(unsigned char);
void LC_ini(void);

int displ(int, char *);

int main()
 {
char arr[16];
int i=-43;
  LCD_init();
   Lcd_Cmd(_LCD_CLEAR);
displ(i,arr);

 LCD_out(1,1,arr);
return 0;
}

int displ( int i,char *n)
{
char test_arr[16];
char j=0,l;
int sign,k=0,p=0,t;
if(i<0)
{
sign=i;
i=-i;
}

while(i>0)
{
t= i%10;
t=t+48;
n[j++] = (char) t;
i=i/10;
}

if(sign<0)
{
n[j++]=  '-';
}
n[j]= '\0';

for(p=0,k=j-1;k>=0;p++,k--)
{
test_arr[p]=  n[k];
}
 test_arr[p]='\0';

for(k=0;test_arr[k]!='\0';k++)
{
test_arr[k]=test_arr[k];
n[k]=  test_arr[k];

/*if((sign<0) && (k==0))  {
  test_arr[0]=test_arr[0]-48;
  n[0]=  test_arr[0];
               }*/
}

n[k] = '\0';
 LCD_out(2,1,test_arr);
 return 0;
}


I reached till here.
Here everything is working good apart from the thing that it is showing minus (-) sign before positive and negative numbers.
When I check the array values in debug mode there is no problem (I mean it is showing correct values.

- - - Updated - - -

I found the mistake. Thanks.
for variable sign, I didn't initialize it with 0, therefore it was taking previous value and giving - sign.

Thanks a lot.
 
Last edited:

@ milan.rajik

Hi.

My Integer to string program is working efficiently and LCD out program is working perfectly and independently.
When I tries to combine both , then it is not working.

While running, in one program, for "inttostr" I used inbuilt function for LCD,
in another program, for LCD I used inbuilt integer to string function.

Now I combined both my programs, it is not giving correct result.
Where can be the problem...
 

sorry I forgot to post the code

Code:
#define WAIT {LCD_EN=1; Delay_us(100); LCD_EN =0; Delay_us(100);}
sbit LCD_RS at LATD6_bit;
sbit LCD_EN at LATD7_bit;
sbit LCD_D4 at LATB4_bit;
sbit LCD_D5 at LATB5_bit;
sbit LCD_D6 at LATB6_bit;
sbit LCD_D7 at LATB7_bit;

sbit LCD_RS_Direction at TRISD6_bit;
sbit LCD_EN_Direction at TRISD7_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;


void LC_cmd(unsigned char);
void LC_out(unsigned char ,unsigned char ,  char *);
void LC_dat(unsigned char);
void LC_ini(void);
int displ(int, char *);

void main(){
int i=5,p=-19138 ;
char ldg[]="MIkrOC",j,txt[16],txt1[16];
    TRISD=0X00;
    PORTD=0X00;
    TRISB=0X00;
    PORTD=0X00;
    LC_ini();


        Displ(p,txt1);
        LC_out(1,1,txt1);
        LC_out(2,8,"miKRoc");
}

void LC_ini(void){

     LC_cmd(0x33);    // Reset LCD
     LC_cmd(0x32);
     LC_cmd(0X28);    // 4 bit mode
     LC_cmd(0X0E);    // display on sursor blinking
     LC_cmd(0X01);    // clear LCd
     LC_cmd(0X80);    // row1 initial position 6
}

void LC_cmd(unsigned char k){

    PORTB= (k & 0xF0);      // sending higher nibble
    LCD_RS=0;          // command
    WAIT

    PORTB= ((k & 0x0F)<<4);    //sending lower nibble
    LCD_RS=0;          // command
    WAIT
}

void LC_dat(unsigned char k){

    PORTB= (k & 0xF0);        // sending higher nibble
    LCD_RS=1;
    WAIT

    PORTB= ((k & 0x0F)<<4);       // sending lower nibble
    LCD_RS=1;
    WAIT
}

  void LC_out(unsigned char row,unsigned char col, char *str )
  {
    switch(row) {

         case 1: LC_cmd(0x80 + col-1); break;
         case 2: LC_cmd(0xC0 + col-1); break;
         default: LC_cmd(0x80 + col-1);break;
                }
     while(*str)
       LC_dat(*str++);
}


    int displ( int i,char *n)
{
char test_arr[16];
char j=0,l;
int sign=0,k=0,p=0,ttt=0;
if(i<0)
{
sign=i;
i=-i;
}

while(i>0)
{
ttt= i%10;
ttt=ttt+48;
n[j++] = (char) ttt;
i=i/10;
}

if(sign<0)
{
n[j++]=  '-';
}
n[j]= '\0';

for(p=0,k=j-1;k>=0;p++,k--)
{
test_arr[p]=  n[k];
}
 test_arr[p]='\0';

for(k=0;test_arr[k]!='\0';k++)
{
test_arr[k]=test_arr[k];
n[k]=  test_arr[k];
}

n[k] = '\0';
 LC_out(2,1,test_arr);
 return 0;
}
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…