Question

I am trying to get the remainder when two integers are divided.And also trying to get the quotient.My variables are follows:

    const uint16_t key = 1000;
    uint8_t remainder;
    uint16_t temp;
     temp = somefunction();  //This returns a uint32_t

     while((UCSR0A&(1<<RXC0)) == 0);  //WAIT FOR CHAR
     //Wait for a char from serial
     remainder = temp % key;
     quotient = (temp/key);

      //Now I check to see if I got the correct remainder
      while((UCSR0A&(1<<UDRE0)) == 0); //wait until empty 
      UDR0 = remainder;
      //The remainder I get in minicom is something I am not expecting.
      //I checked the result of somefunction() and it is correct

Please help!

Était-ce utile?

La solution

Based on your comments:

The value which is being returned from somefunction() - 101010 - is beyond the range of the uint16_t variable temp which you are assigning it to. It is being truncated to 35474 (101010 mod 65536) when it is assigned to that variable, which would cause the results of the division and modulo to be 35 and 474, respectively.

You will need to change the type of temp to uint32_t, and change the type of remainder to uint32_t as well to avoid truncating the result.

Autres conseils

The value you write to UDR0 is being sent over serial as a character code, not as a human-readable number. If you want something you can read, you will need to do formatting yourself, or link the C library and use something like printf() or itoa().

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top