-1
#include <xc.h>
#include "LCD.h"
#include <stdio.h>
#include <stdlib.h>
int main()
{
    unsigned int a; int x = 0; char s[10000];
    TRISD = 0x00; TRISB = 0x03;
    Lcd_Start();
    while(1)
    {
        Lcd_Clear();Lcd_Set_Cursor(1,1);
        int x = 0;
        if (PORTBbits.RB0==1)
        {
            Lcd_Clear();
            x += 1;
            Lcd_Print_String("Current Number");
            Lcd_Set_Cursor(2,1);
           Lcd_Print_String(x)
        }
        else if(PORTBbits.RB1==1)
        {
            Lcd_Clear();
            x += -1;
            Lcd_Print_String("Current Number");
            Lcd_Set_Cursor(2,1);
            Lcd_Print_String(x);
        }
    }
    return 0;
}

This is Lcd_Print_String:

void Lcd_Print_Char(char data)  //Send 8-bits through 4-bit mode
{
    char Lower_Nibble,Upper_Nibble;
    Lower_Nibble = data&0x0F;
    Upper_Nibble = data&0xF0;
    RS = 1;             // => RS = 1
    Lcd_SetBit(Upper_Nibble>>4);             //Send upper half by shifting by 4
    EN = 1;
    for (int i=2130483; i<=0; i--)  NOP(); 
    EN = 0;
    Lcd_SetBit(Lower_Nibble); //Send Lower half
    EN = 1;
    for (int i=2130483; i<=0; i--)  NOP();
    EN = 0;
}

void Lcd_Print_String(char *a)
{
    int i;
    for (i=0;a[i]!='\0';i++)
        Lcd_Print_Char(a[i]);
}

I want to display the value of x on the screen, my Lcd_pring_String only takes string type. Is there a way convert x to a string so I can display on LCD?

I am using PIC16f877A, LCD(lm016) and two switches to take signal for +1, -1.

Mike
  • 4,041
  • 6
  • 20
  • 37

1 Answers1

1

You probably want to use sprintf which is declared in <stdio.h>:

char buffer[20];             // char buffer
sprintf(buffer, "%d", x);    // "print" x into the char buffer
Lcd_Print_String(buffer);    // send buffer contents to LCD

or instead of sprintf use snprintf if available:

snprintf(buffer, sizeof buffer, "%d", x);

I'd make a new function Lcd_Print_Number out of this.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • what does sprintf exactly do?? i'm not printing the answer on regular console –  Sep 04 '18 at 09:29
  • @User1984 click on the [`sprintf`](http://www.cplusplus.com/reference/cstdio/sprintf/) in the question to get the documentation. `sprintf` is like `printf` but instead of printing on the console it "prints" into a memory buffer. Then you send the contents of that buffer to the LCD display using the existing `Lcd_Print_String` function. – Jabberwocky Sep 04 '18 at 09:31