PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

51 行
1.4KB

  1. /***
  2. Written by Christopher Andrews.
  3. CRC algorithm generated by pycrc, MIT licence ( https://github.com/tpircher/pycrc ).
  4. A CRC is a simple way of checking whether data has changed or become corrupted.
  5. This example calculates a CRC value directly on the EEPROM values.
  6. The purpose of this example is to highlight how the EEPROM object can be used just like an array.
  7. ***/
  8. #include <Arduino.h>
  9. #include <EEPROM.h>
  10. void setup(){
  11. //Start serial
  12. Serial.begin(9600);
  13. while (!Serial) {
  14. ; // wait for serial port to connect. Needed for Leonardo only
  15. }
  16. //Print length of data to run CRC on.
  17. Serial.print( "EEPROM length: " );
  18. Serial.println( EEPROM.length() );
  19. //Print the result of calling eeprom_crc()
  20. Serial.print( "CRC32 of EEPROM data: 0x" );
  21. Serial.println( eeprom_crc(), HEX );
  22. Serial.print( "\n\nDone!" );
  23. }
  24. void loop(){ /* Empty loop */ }
  25. unsigned long eeprom_crc( void ){
  26. const unsigned long crc_table[16] = {
  27. 0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
  28. 0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
  29. 0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
  30. 0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
  31. };
  32. unsigned long crc = ~0L;
  33. for( unsigned int index = 0 ; index < EEPROM.length() ; ++index ){
  34. crc = crc_table[( crc ^ EEPROM[index] ) & 0x0f] ^ (crc >> 4);
  35. crc = crc_table[( crc ^ ( EEPROM[index] >> 4 )) & 0x0f] ^ (crc >> 4);
  36. crc = ~crc;
  37. }
  38. return crc;
  39. }