שימוש ב-CRC היא דרך פשוטה לביצוע בדיקה האם המידע השתנה או שהתקלקל. דוגמה זו מחשבת את ה-CRC על הערכים ב-EEPROM. ה-CRC הוא כמו חתימה, כל שינוי בערך המחושב של ה-CRC מצביע על שינוי בנתונים השמורים. מטרת הדוגמה היא להציג לכם איך משתמשים באובייקט EEPROM כמערך של נתונים.
ציוד נדרש
כרטיס פיתוח Arduino.מעגל
אין צורך בהכנת מעגל לדוגמה זו.שרטוט
אין צורך בשרטוט לדוגמה זו.קוד
- קוד: בחר הכל
/***
Written by Christopher Andrews.
CRC algorithm generated by pycrc, MIT licence ( https://github.com/tpircher/pycrc ).
A CRC is a simple way of checking whether data has changed or become corrupted.
This example calculates a CRC value directly on the EEPROM values.
The purpose of this example is to highlight how the EEPROM object can be used just like an array.
***/
#include <Arduino.h>
#include <EEPROM.h>
void setup() {
//Start serial
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
//Print length of data to run CRC on.
Serial.print("EEPROM length: ");
Serial.println(EEPROM.length());
//Print the result of calling eeprom_crc()
Serial.print("CRC32 of EEPROM data: 0x");
Serial.println(eeprom_crc(), HEX);
Serial.print("\n\nDone!");
}
void loop() {
/* Empty loop */
}
unsigned long eeprom_crc(void) {
const unsigned long crc_table[16] = {
0x00000000, 0x1db71064, 0x3b6e20c8, 0x26d930ac,
0x76dc4190, 0x6b6b51f4, 0x4db26158, 0x5005713c,
0xedb88320, 0xf00f9344, 0xd6d6a3e8, 0xcb61b38c,
0x9b64c2b0, 0x86d3d2d4, 0xa00ae278, 0xbdbdf21c
};
unsigned long crc = ~0L;
for (int index = 0 ; index < EEPROM.length() ; ++index) {
crc = crc_table[(crc ^ EEPROMindex]) & 0x0f] ^ (crc >> 4);
crc = crc_table[(crc ^ (EEPROMindex] >> 4)) & 0x0f] ^ (crc >> 4);
crc = ~crc;
}
return crc;
}
ראו גם:
()EEPROM.readפירוט ספריית EEPROM
דוגמת EEPROM Clear - ניקיון הבתים ב-EEPROM
דוגמת EEPROM Read - קריאת נתונים מ-EEPROM ושליחתם למחשב
דוגמת EEPROM Write - שמירת נתונים מכניסה אנלוגית ב-EEPROM
דוגמת EEPROM Iteration - הסבר על מעבר בין כתובות שונות ב-EEPROM
דוגמת EEPROM Put - שמירת נתונים ב-EEPROM עם התחשבות בסמנטיקה של המשתנים
דוגמת EEPROM Get - קריאת ערכים מ-EEPROM והדפסתם כ-float ל-Serial
דוגמת EEPROM Update - שמירת נתון מכניסה אנלוגית ב-EEPROM, כאשר הכתיבה מתבצעת רק כשיש שינוי כדי להאריך את אורך החיים של ה-EEPROM
פירוט שפת תכנות לסביבת Arduino
עמוד זה הוא תרגום של EEPROM CRC לפי רישיון Creative Commons Attribution-ShareAlike 3.0.