0

Page 14 says that the method should return the value 'h17'

typedef struct  {
  uint8_t data;
  uint8_t timestamp;
  unsigned char crc;
} tData;


int main(void)
{
  unsigned char* tempData = "snap";

    for(;;)
    {
      tData payload;
      payload.crc = crc8(tempData,sizeof(tempData));
      UDR0 = payload.crc;
    }

    return 0;
}
2

Because “h17” is an unprintable character, namely ASCII ETB. The 'h' in “h17” stands for “hexadecimal”, like the 0x usually used in C.

You should convert the value to an ASCII number if you want to print it, using for example Serial.print(payload.crc, 16); or – if you are not using Arduino core – (s)printf().


Edit: here is an implementation of my comment “sprintf() into a buffer and dump the buffer to UDR0 one byte at a time”:

#include <stdio.h>

void print_byte_in_hex(uint8_t byte)
{
    // Print the byte in ASCII into an internal buffer.
    char buffer[8];
    sprintf(buffer, "%02x\r\n", byte);

    // Send it one byte at a time through the USART.
    for (char * p = buffer; *p; p++) {
        loop_until_bit_is_set(UCSR0A, UDRE0);
        UDR0 = *p;
    }
}
  • I'm not using Arduino core in this instance. What is (s)? So something like: UDR0 = printf(payload.crc) – SensationSama Apr 6 '16 at 15:14
  • @SensationSama: I mean you either sprintf() into a buffer and dump the buffer to UDR0 one byte at a time, or you set up stdin to print through the serial port and then do something like printf("%02x\n", (unsigned int) payload.crc). – Edgar Bonet Apr 6 '16 at 15:21
  • Okay great. Can you spoon feed me the buffer process a bit? – SensationSama Apr 6 '16 at 15:28
  • @SensationSama: done in the edit, but not tested. – Edgar Bonet Apr 6 '16 at 15:51
  • Thanks, seems to work perfectly! One last follow-up question. If h17 is hexadecimal, and the decimal equivalent is 23, than why is my terminal printing 99? – SensationSama Apr 6 '16 at 18:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.