help me fix the code where it use the sub function to calculate the CRC of the hex…
- User
help me fix the code where it use the sub function to calculate the CRC of the hex 0b3f60 /******************************************************************************
Online C Compiler. Code, Compile, Run and Debug C program online.Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/ #include <stdio.h> #include <stdint.h> #include <stdlib.h> #include <string.h>
int main() { uint8_t Wdata_NoCRC = 0x0b3F60 CRC_rtn = crc8_sae(Wdata_NoCRC, 3); printf("%x",CRC_rtn); return 0; }
//crc calculator #define CRC8_POLY 0x1D uint8_t crc8_sae(uint8_t *data, uint8_t len){ uint8_t crc = 0xFF; uint8_t i, j; for (i = 0; i < len; i++) { crc ^=data[i]; for (j = 0; j < 8; j++) { if (crc & 0x80){ crc = (crc << 1)^ CRC8_POLY; }else{ crc <<= 1; } } } return crc; }
- Assistant
It looks like there are a few issues in your code:
- Declaration of
Wdata_NoCRCas auint8_ttype while it needs to hold more data. - Missing a semicolon at the end of the declaration of
Wdata_NoCRC. - The function
crc8_saeexpects a pointer to the data, so the data should be stored as an array ofuint8_t.
Here is the revised code:
#include <stdio.h> #include <stdint.h> #include <stdlib.h> #include <string.h> //crc calculator #define CRC8_POLY 0x1D uint8_t crc8_sae(uint8_t *data, uint8_t len) { uint8_t crc = 0xFF; uint8_t i, j; for (i = 0; i < len; i++) { crc ^= data[i]; for (j = 0; j < 8; j++) { if (crc & 0x80) { crc = (crc << 1) ^ CRC8_POLY; } else { crc <<= 1; } } } return crc; } int main() { uint8_t Wdata_NoCRC[] = {0x0B, 0x3F, 0x60}; // Store the data in an array uint8_t CRC_rtn = crc8_sae(Wdata_NoCRC, 3); printf("%x\n", CRC_rtn); // Add a newline for readability return 0; } - Declaration of
- User
strcmp(I2C_rxBuff_value, I2C_rxBuff_compare) where I2C_rxBuff_value is '0' and I2C_rxBuff_compare is'0'\0 what will the result be\
- Assistant
In C, the
strcmpfunction is used to compare two null-terminated strings (character arrays). Here's a brief explanation of what happens when you usestrcmp(I2C_rxBuff_value, I2C_rxBuff_compare)with the given values: - User
how to convert both to uint8 for comparing
- Assistant
If you want to compare the numeric values represented by the strings
I2C_rxBuff_valueandI2C_rxBuff_compare, you can convert these strings touint8_t(unsigned 8-bit integers) using the C standard library functionstrtoul. Here’s a step-by-step guide on how you can do that:- Convert each string to
uint8_t. - Compare the resulting
uint8_tvalues.
- Convert each string to
- User
void I2C_read(uint8_t I2C_device_addr, uint8_t I2C_reg_addr, char* I2C_rxBuff_compare){
rxBuff = (uint8_t*)calloc(reg_val_size, sizeof(uint8_t)); I2C_ReadRegs(I2C0, I2C_device_addr, I2C_reg_addr,reg_val_size, rxBuff);// read data and save it to rxBuff // Convert rxBuff hex values to the character representation for (int i = 0; i < reg_val_size; i++) { sprintf(I2C_rxBuff_compare + i * 2, "%02", rxBuff[i]); } //I2C_rxBuff_compare[reg_val_size*2] = '\0'; // Null-terminate the string free(rxBuff);}
the code here rxbuff after reading is 64 in dec, with reg_val_size = 1 what do I have for my I2C-rxbuff_compare. currently the the output is not correct
- Assistant