arduino: read unique ID

来源:互联网 发布:淘宝网店虚拟充值网店 编辑:程序博客网 时间:2024/05/22 16:38

在实际应用中,我们可能需要唯一的设备ID用于标识设备或者进行功能加密等操作。
Arduino中并未直接提供此类接口,那么是否可以实现此类功能呢?答案是肯定的。

在AVR的LIBC库中提供了以下定义(boot.h):

Read the Signature Row byte at address. For some MCU types, this function can
also retrieve the factory-stored oscillator calibration bytes.
Parameter address can be 0-0x1f as documented by the datasheet.
Note The values are MCU type dependent.

Datasheet中关于如何在软件中读取Signature的说明:

不同型号MCU的Device ID信息:

根据如上信息,写了个测试程序:
读取手头Arduino板的信息:

  • Arduino UNO R3 (1)

  • Arduino UNO R3 (2)

  • Arduino NANO

对于我们获取的Device ID以及文档中的说明,可知手头的两片Arduino UNO R3以及Arduino NANO 均采用ATMEGA328P.
那个RC Oscillator Calibration Byte没搞明白,先不理会啦。

可以明显看到这些数据分成几组,但是除了(Device ID)以及(Calibration Byte)没有从datasheet中找到其它部分对应的描述。
网上一些帖子说从第十四个字节(从0开始),后连续10个字节亦即MCU的唯一编码。

对程序稍作修改:

    #include "avr/boot.h"    void setup() {      Serial.begin(9600);      Serial.println("Arduino MCU Signature Reader");      Serial.println("By JoyTag, support@joytag.com\n");    }    void loop() {      // 28.3 Signature Bytes      Serial.print("Device ID:\t");      Serial.print(boot_signature_byte_get(0), HEX);      Serial.print("\t");      Serial.print(boot_signature_byte_get(2), HEX);      Serial.print("\t");      Serial.print(boot_signature_byte_get(4), HEX);      // 28.4 Calibration Byte      Serial.print("\nCalibration Byte:\t");      Serial.println(boot_signature_byte_get(1), HEX);      //23.12.2.14 #define boot_signature_byte_get( addr )      Serial.println("\nRow Bytes:");      for (int i = 0; i <= 0x1F; i++)      {        Serial.print(boot_signature_byte_get(i), HEX);        Serial.print(", ");      }      Serial.println("");      Serial.println("\nUID Bytes:");      for (int i = 14; i < 14 + 10; i++)      {        Serial.print(boot_signature_byte_get(i), HEX);        Serial.print(", ");      }      Serial.println("");      while (1);    }

看来,利用这个方法读取唯一标识还是可行的。

参考资料

  • http://atmel.force.com/support/articles/en_US/FAQ/How-to-read-signature-byte
  • http://www.atmel.com/webdoc/AVRLibcReferenceManual/group__avr__boot_1gaf375d2543ba38dc56697b4f4bc37a717.html
  • http://www.amobbs.com/forum.php?mod=viewthread&tid=5485868&highlight=AVR%2B%E5%BA%8F%E5%88%97%E5%8F%B7
  • http://www.atmel.com/images/Atmel-8271-8-bit-AVR-Microcontroller-ATmega48A-48PA-88A-88PA-168A-168PA-328-328P_datasheet_Complete.pdf

补充:
带水印的截图都是本人以前亲自做的,懒得把多个设备拿出来重新跑,直接用老图啦。
文章所述功能属于隐藏技能哦,我周围很多做产品的都不知道这个事那,免费大放送啦。