GP2Y0E03烧写熔丝位失败-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 685|回复: 0

[未解决] GP2Y0E03烧写熔丝位失败

[复制链接]
发表于 2021-10-20 19:25 | 显示全部楼层 |阅读模式
想用arduino uno烧写GP2Y0E03,改写IIC地址,但是一直不成功
  1. /*
  2.     ----------------------------------------------
  3.     Title:   E_FUSE.ino
  4.     Author:  Martin Pålsson
  5.     Description:
  6.     E-Fuse programmer for SHARP GP2Y0E02B, GP2Y0E03
  7.     -----------------------------------------------
  8. */
  9.    
  10. #include <Wire.h>

  11. /* Default address of sensor */
  12. #define ADDRESS (0x80 >> 1) // 7 MSB, default address of sensor

  13. /* Desired address of the sensor */
  14. #define SETADDR 0x00        // 4 MSB, will bitshift when used.

  15. /*  Prototypes  */
  16. int ScanBus();
  17. uint8_t readSequence1(uint8_t devAddr, uint8_t regAddr);
  18. void writeSequence(uint8_t devAddr, uint8_t regAddr, uint8_t data);
  19. void EFuseSlaveID(uint8_t newID);

  20. /* Global variables */
  21. //uint8_t checkSum = 0; //  Not currently used
  22. void setup()
  23. {
  24.   pinMode(4, OUTPUT);       //  Connect to Vpp pad on sensor
  25.   pinMode(13, OUTPUT);      //  Onboard LED
  26.   digitalWrite(13, HIGH);   //  To indicate that serial communication has been established. No led - Connection to PC established.
  27.   Wire.begin();

  28.   Serial.begin(9600);
  29.   
  30.   while (!Serial);             // Due: wait for serial monitor
  31.   digitalWrite(13, LOW);       // To acknowledge that serial communication has been established.
  32.   
  33.   Serial.println("\nGP2Y0E02B E-Fuse Programmer Version 1.0");
  34. }
  35. void loop()
  36. {
  37.   Serial.println("Scanning I2C bus...");
  38.   if(ScanBus())
  39.   {
  40.     EFuseSlaveID(byte(SETADDR));
  41.   }
  42.   else
  43.   {
  44.     Serial.println("No devices found on bus");
  45.   }
  46.   /* Nothing else to do */
  47.   while(1);;
  48. }
  49. void EFuseSlaveID(uint8_t newID)
  50. {
  51.     /* ----- Stage 1 ----- */
  52.   Serial.println("Stage 1 started.");
  53.   
  54.   writeSequence(byte(ADDRESS), byte(0xEC), byte(0xFF));
  55.   Serial.println("Data = 0xFF is set in Address = 0xEC");
  56.   
  57.   digitalWrite(4, HIGH);
  58.   Serial.println("3.3V is applied in the Vpp terminal");
  59.   
  60.   /* ----- Stage 2 ------ */
  61.   Serial.println("Stage 2 started.");
  62.   writeSequence(byte(ADDRESS), byte(0xC8), byte(0x00));
  63.   Serial.println("Data = 0x00 is set in Address = 0xC8");
  64.   
  65.   /* ----- Stage 3 ------ */
  66.   Serial.println("Stage 3 started.");
  67.   writeSequence(byte(ADDRESS), byte(0xC9), byte(0x45));
  68.   Serial.println("Data = 0x45 is set in Address = 0xC9");

  69.   /* ----- Stage 4 ------ */
  70.   /* THIS IS WHERE THE ADDRESS WILL BE SET! */
  71.   Serial.println("Stage 4 started.");
  72.   writeSequence(byte(ADDRESS), byte(0xCD), byte(newID >> 4));
  73.   Serial.println("Data = SETADDR >> 4 is set in Address = 0xCD");


  74.   /* ----- Stage 5 ------ */
  75.   Serial.println("Stage 5 started.");
  76.   writeSequence(byte(ADDRESS), byte(0xCA), byte(0x01));
  77.   Serial.println("Data = 0x01 is set in Address = 0xCA");
  78.   Serial.println("Wait for 500 us");
  79.   delayMicroseconds(500);

  80.   /* ----- Stage 6 ------ */
  81.   Serial.println("Stage 6 started.");
  82.   writeSequence(byte(ADDRESS), byte(0xCA), byte(0x00));
  83.   Serial.println("Data = 0x00 is set in Address = 0xCA");
  84.   digitalWrite(4, LOW);
  85.   Serial.println("Vpp terminal grounded.");

  86.   /* ----- Stage 7 ------ */
  87.   Serial.println("Stage 7 started.");
  88.   writeSequence(byte(ADDRESS), byte(0xEF), byte(0x00));
  89.   Serial.println("Data = 0x00 is set in Address = 0xEF");
  90.   
  91.   writeSequence(byte(ADDRESS), byte(0xC8), byte(0x40));
  92.   Serial.println("Data = 0x40 is set in Address = 0xC8");
  93.   
  94.   writeSequence(byte(ADDRESS), byte(0xC8), byte(0x00));
  95.   Serial.println("Data = 0x00 is set in Address = 0xC8");

  96.   /* ----- Stage 8 ------ */
  97.   Serial.println("Stage 8 started.");
  98.   writeSequence(byte(ADDRESS), byte(0xEE), byte(0x06));
  99.   Serial.println("Data = 0x06 is set in Address = 0xEE");

  100.   /* ----- Stage 9 ------ */
  101.   Serial.println("Stage 9 started.");

  102.   writeSequence(byte(ADDRESS), byte(0xEF), byte(0x00));
  103.   Serial.println("Data = 0x00 is set in Address = 0xEF");
  104.   
  105.   writeSequence(byte(ADDRESS), byte(0xEC), byte(0xFF));
  106.   Serial.println("Data = 0xFF is set in Address = 0xEC");
  107.   
  108.   writeSequence(byte(ADDRESS), byte(0xEF), byte(0x03));
  109.   Serial.println("Data = 0x03 is set in Address = 0xEF");

  110.   Serial.println("Read out the data in Address = 0x27.");
  111.   Serial.print("Data: 0B");
  112.   Serial.println(readSequence1(byte(ADDRESS), byte(0x27)), BIN);

  113.   writeSequence(byte(ADDRESS), byte(0xEF), byte(0x00));
  114.   Serial.println("Data = 0x00 is set in Address = 0xEF");
  115.   
  116.   writeSequence(byte(ADDRESS), byte(0xEC), byte(0x7F));
  117.   Serial.println("Data = 0x7F is set in Address = 0xEC");

  118. }
  119. void writeSequence(uint8_t devAddr, uint8_t regAddr, uint8_t data)
  120. {
  121.   Wire.beginTransmission(devAddr);
  122.   Wire.write(byte(regAddr));
  123.   Wire.write(data);
  124.   Wire.endTransmission();
  125. }

  126. uint8_t readSequence1(uint8_t devAddr, uint8_t regAddr)
  127. {
  128.   Wire.beginTransmission(devAddr);
  129.   Wire.write(byte(regAddr));
  130.   Wire.endTransmission();

  131.   Wire.requestFrom(devAddr, 1);

  132.   return Wire.read();
  133. }

  134. int ScanBus()
  135. {
  136.   byte error, address;
  137.   int nDevices;

  138.   //Serial.println("I2C Scanner starting...");

  139.   nDevices = 0;
  140.   for(address = 0; address < 127; address++ )
  141.   {
  142.     // The i2c_scanner uses the return value of
  143.     // the Write.endTransmisstion to see if
  144.     // a device did acknowledge to the address.
  145.     Wire.beginTransmission(address);
  146.     error = Wire.endTransmission();

  147.     if (error == 0)
  148.     {
  149.       Serial.print("I2C device found at address 0x");
  150.       if (address<16)
  151.         Serial.print("0");
  152.       Serial.print(address << 1,HEX);
  153.       Serial.println("  !");

  154.       nDevices++;
  155.     }
  156.     else if (error==4)
  157.     {
  158.       Serial.print("Unknown error at address 0x");
  159.       if (address<16)
  160.         Serial.print("0");
  161.       Serial.println(address,HEX);
  162.     }   
  163.   }
  164.   if (nDevices == 0)
  165.   {
  166.     Serial.println("No I2C devices found\n");
  167.     return 0;
  168.   }
  169.   else
  170.   {
  171.     Serial.print(nDevices);
  172.     Serial.println(" devices found.");
  173.     Serial.println("I2C Scanner done.\n");
  174.     return nDevices;
  175.   }
  176. }
复制代码


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-11-28 21:54 , Processed in 0.124461 second(s), 15 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表