shubham kumar
Member level 3
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.
The values of variables in the debug mode are perfectly fine. but not working when trying to display on LCD
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