最新消息:

Arduino库教程-EEPROM-EEPROM Crc

Arduino 少儿编程 2081浏览 0评论
Arduino库教程

EEPROM CRC(EEPROM校检)

  • 在Arduino和genuino板上的微控制器有512字节的EEPROM存储器:当开发板关闭时(就像一个小型硬盘驱动器)开始记忆(即是保存这些数值)。

  • 校验是一种简单的方法,用来检查数据是否已更改或已损坏。这个例子直接在EEPROM的值上计算一个CRC值。这个CRC就像是一个签名,并任何在计算好的CRC上的改变都意味着存储的数据发生变化。。这个例子的目的是为了突出了EEPROM的对象怎样像数组那样用。

硬件要求

  • Arduino 或者 Genuino 开发板
    电路

这个例子的电路没有额外的连接
Arduino库教程-EEPROM-EEPROM Crc
图由 Fritzing 软件绘制

原理图

Arduino库教程-EEPROM-EEPROM Crc
图由 Fritzing 软件绘制

样例代码

/***
    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("nnDone!");
}

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 ^ EEPROM[index]) & 0x0f] ^ (crc >> 4);
    crc = crc_table[(crc ^ (EEPROM[index] >> 4)) & 0x0f] ^ (crc >> 4);
    crc = ~crc;
  }
  return crc;
}

[Get Code]
更多

  • EEPROM.read()
  • EEPROM library reference
  • EEPROM Clear: 清理EEPROM里面的数据。
  • EEPROM Read: 读取EEPROM,并且发送它的值到电脑。
  • EEPROM Write: 保存模拟输入引脚的值到EEPROM。
  • EEPROM Crc: 将EEPROM内容里的CRC当作数组分析。
  • EEPROM Get: 从EEPROM获得一个值,并作为float格式串行打印。
  • EEPROM Iteration: 明白怎样到达EEPROM存储本地。
  • EEPROM Put: 用变量来把一些数值放到EEPROM里。
  • EEPROM Update: 保存从A0读取的数值到EEPROM里,仅在不同的时候写入,以延长EEPROM寿命。

您必须 登录 才能发表评论!