-1

I´m trying to make a simple caltulator using PIC18F46K22 microcontroler and UART. However it works only for adding two numbers, even though the strstr cor comparison and finding the "+" / "-".

Can someone please spot the problem?

Code:

`#include <xc.h>             //-- for XC8 compiler
#include <stdio.h>          //   for printf
#include <ctype.h>
#include <string.h>
#include <stdlib.h>

#define _XTAL_FREQ 32E6

typedef struct
{
    char data[32];
    char full;
    int  index;
} mailbox;


void main(void)
{ 
    
    while(1)
    {
        if(g_mail.full)
        {
            while(!TX1IF);
            char *ptr;
            int num1, num2;
            char operator;
            
  
            // Determine the operator using strstr
            if (strstr(g_mail.data, "+")) {
                operator = '+';
            } else if (strstr(g_mail.data, "-")) {
                operator = '-';
            }


            int result = 0;

            // Perform the calculation
            if (operator == '+') {
                result = num1 + num2;
            } else if (operator == '-') {
                result = num1 - num2;
            }

            // Prepare the result string
            char resultStr[16];
            sprintf(resultStr, "%d", result); // Convert result to a string

            // Send each character in the result string
            for (int i = 0; i < strlen(resultStr); i++) {
                while (!TXSTAbits.TRMT); // Wait for TX buffer to be ready
                TXREG = resultStr[i]; // Send character
            }

            g_mail.full = 0; // Reset mailbox flag
            memset(g_mail.data, 0, sizeof(g_mail.data)); // Clear the data array
        }
    }
}

Thanks everyone for your help and tips!

I have tried changing syntax and few different approches but nothing works. Chat also cannot find a problem and writes the same answe a little bit differently.

4
  • 1
    You haven't asked a question except "spot the problem" in some heavily abridged code. The problem could be in code not posted. Suggest you revise the question before asking for help... Commented Apr 27, 2024 at 1:13
  • You declare num1 and num2 but never initialize or assign to them. You need to parse the digit characters from your string, Commented Apr 27, 2024 at 1:16
  • As you nest everything in your while loop inside if(g_mail.full) { ... } I suggest you check for the opposite and skip the rest of the loop. E.g. if (!g_mail.full) continue; This will let you unindent your code a level. Commented Apr 27, 2024 at 4:06
  • If you can do a RPN input that will provide you the simplest, shortest code possible. If you absolutely must have infix notation, on a simple machine you will find either the standard Recursive Descent algorithm or Shunting Yard algorithm to be the only way to do it. Both are very easy to implement and can be very small code. Commented Apr 27, 2024 at 4:06

1 Answer 1

0

You're recognizing the + and - just fine; the problem seems to be that you're not recognizing the digits of your numbers. You could instead do something like:

if (sscanf(g_mail.data, "%d +%d", &num1, &num2) == 2) {
    result = num1 + num2;
} else if (sscanf(g_mail.data, "%d -%d", &num1, &num2) == 2) {
    result = num1 - num2;
} else {
    fprintf(stderr, "Calculation '%s' not recognized\n", g_mail.data);
    break;
}

This does bring in the whole stdio standard library which might be too much for a micrcontroller, in which case you'll need a light-weight numeric parser.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.