Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

sprintf identifier undeclared

shivammun13

Newbie level 4
Joined
Apr 27, 2023
Messages
7
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
42
Hello, i have been trying to build a program which displays on an LCD display a counting which increments up by one digit and a pushbutton s1 is pressed and decrements by one digit when another button s2 is pressed. My goal is to make is start from an initial value of 10 and then decrements each time s2 is pressed. When it reaches zero it stops decrementing and displays a text. If s1 is pressed it should increment to the max value of 10.
Below is the code I have written in mikroC version 7.6. However, I cannot solve sprintf identifier problem, which you can see in the pics below. I am using the PIC16F877A mc and I have included the entire code.
Can someone help me in solving this? Thanks!


1682600362983.png


1682600451677.png


1682600576118.png


1682600656887.png
 
Please go to 'library manager' and check if you have ticked the 'LCD' option. By the way, I never used the sprintf function in microC. I am not sure if it is supported by microC. If you want to print any data to the LCD, you have to use Lcd_Out function as you did. For int to ASCII conversion, you have to add '0' to every bit.
 
Hi,

and for the future:
please post code as text, not as picture(screenshot). So members can use the text search function. Also it´s easy for members to test your code by "copy and paste".

Klaus
 
Please go to 'library manager' and check if you have ticked the 'LCD' option. By the way, I never used the sprintf function in microC. I am not sure if it is supported by microC. If you want to print any data to the LCD, you have to use Lcd_Out function as you did. For int to ASCII conversion, you have to add '0' to every bit.
i'm sorry but i didn't understand.
 
mikroC is different from industry standard C compiler in several regards. If you don't know about library manager, review manual or online help. Standard sprintf function is provided in mikroC by sprint library.
 
Hi,

and for the future:
please post code as text, not as picture(screenshot). So members can use the text search function. Also it´s easy for members to test your code by "copy and paste".

Klaus
I have updated my code so it does not use sprintf function. But when i compiled the code and tried simulating the code on proteus, it displays "P.SLOT LEFT'' but does not display the counting. Even though when i click the decrement button, on the 10th click, the the lcd displays "parking full". But it does not change futher, even when i press the increment button. Can someone solve this for me?
Here's the code:
Code:
// LCD module connections
sbit LCD_RS at RD0_bit;
sbit LCD_EN at RD1_bit;
sbit LCD_D4 at RD2_bit;
sbit LCD_D5 at RD3_bit;
sbit LCD_D6 at RD4_bit;
sbit LCD_D7 at RD5_bit;

sbit LCD_RS_Direction at TRISD0_bit;
sbit LCD_EN_Direction at TRISD1_bit;
sbit LCD_D4_Direction at TRISD2_bit;
sbit LCD_D5_Direction at TRISD3_bit;
sbit LCD_D6_Direction at TRISD4_bit;
sbit LCD_D7_Direction at TRISD5_bit;
// End LCD module connections

// Pushbutton connections
sbit Increment_Button at RB0_bit;
sbit Decrement_Button at RB1_bit;

// Global variables
unsigned short count = 10; // Start counter at 10
unsigned short max_count = 10; // Maximum counter value

// Function prototypes
void Update_Display();
void Delay_ms(unsigned int time);

void main() {
  // Configure ports
  TRISB = 0xFF; // Set PORTB as input

  // Initialize LCD
  Lcd_Init();
  Lcd_Cmd(_LCD_CLEAR);
  Lcd_Cmd(_LCD_CURSOR_OFF);

  // Display initial count
  Update_Display();

  // Main loop
  while(1) {
    // Check if increment button is pressed
    if(Increment_Button == 0) {
      while(Increment_Button == 0); // Wait for button release
      count++; // Increment count
      if(count > max_count) {
        count = max_count; // Limit count to maximum value
      }
      Update_Display(); // Update display
      Delay_ms(50); // Debounce delay
    }

    // Check if decrement button is pressed
    if(Decrement_Button == 0) {
      while(Decrement_Button == 0); // Wait for button release
      count--; // Decrement count
      if(count == 0) {
        Lcd_Cmd(_LCD_CLEAR); // Clear display
        Lcd_Out(1, 3, "PARKING FULL"); // Display "FULL" message
        while(1); // Stop program execution
      }
      Update_Display(); // Update display
      Delay_ms(50); // Debounce delay
    }
  }
}

// Update display with current count value
void Update_Display() {
  char str[4]; // String buffer for number conversion
  Lcd_Cmd(_LCD_CLEAR); // Clear display
  Lcd_Out(1, 1, "P.SLOT LEFT: "); // Display label
  IntToStr(count, str); // Convert count to string
  Lcd_Out(1, 13, str); // Display count value
}

// Delay function in milliseconds
void Delay_ms(unsigned int time) {
  unsigned int i, j;
  for(i = 0; i < time; i++) {
    for(j = 0; j < 165; j++);
  }
}
 
Last edited by a moderator:
As far as I can remember it was not possible to print the whole array in a single like just like this. I had print the characters one by one. Like this,
Lcd_Out(1,13,str[0])
Lcd_Out(1,14,str[1])
Lcd_Out(1,15,str[2]) etc....
Yes you're right. I also had to do it manually one by one myself. I did it like this:
If (count == 10) {
Lcd_Out(1, 1, "P. Slot left: 10");
}
If (count == 2) {
Lcd_Out(1, 14, "09");
}... Etc
 
Hi,

What's your display? How many lines and how many characters per line?
An the range of count is 0..10?

According Susan ... it seems the intToStr produces similar results than:
sprintf(buf, "%6d", count)
usually does. (Mind the "6")

Klaus
 
Last edited:

LaTeX Commands Quick-Menu:

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top