fork download
  1. #include <stdio.h>
  2. #include <stdint.h>
  3.  
  4. typedef struct {
  5. uint8_t dst;
  6. uint8_t l;
  7. uint8_t crc_m;
  8. uint8_t h;
  9. uint8_t crc_l;
  10. } cct_frame_t;
  11.  
  12. static cct_frame_t cct_frame;
  13.  
  14.  
  15.  
  16. uint16_t calculate_crc_loop_CCITT_A( int l, uint8_t *p, uint16_t seed )
  17. {
  18. int i, j;
  19. uint16_t crc = seed;
  20. for ( i = 0; i < l; ++i )
  21. {
  22. crc ^= ( p[ i ] << 8 );
  23. for ( j = 0; j < 8; ++j )
  24. {
  25. if ( crc & 0x8000 ){
  26. crc = ( crc << 1 ) ^ 0x1021; // 0001.0000 0010.0001 = x^12 + x^5 + 1 ( + x^16 )
  27. }
  28. else{
  29. crc <<= 1;
  30. }
  31. }
  32. }
  33. return crc;
  34. }
  35.  
  36. int main(void) {
  37. uint16_t crc = 0;
  38.  
  39. cct_frame.dst = 0;
  40. cct_frame.l = 0;
  41. cct_frame.h = 4;
  42. crc = calculate_crc_loop_CCITT_A(1, &cct_frame.dst, crc);
  43. crc = calculate_crc_loop_CCITT_A(1, &cct_frame.l, crc);
  44. crc = calculate_crc_loop_CCITT_A(1, &cct_frame.h, crc);
  45. cct_frame.crc_m = 0xFF & (crc >> 8);
  46. cct_frame.crc_l = 0xFF & crc;
  47.  
  48. printf ("%x %x %x %x %x\n %x", cct_frame.dst, cct_frame.l, cct_frame.crc_m, cct_frame.h, cct_frame.crc_l, crc);
  49.  
  50. // your code goes here
  51. return 0;
  52. }
  53.  
Success #stdin #stdout 0s 5504KB
stdin
Standard input is empty
stdout
0 0 40 4 84
 4084