Operácie

Zbernica i2c: nástroje II.: Rozdiel medzi revíziami

Zo stránky SensorWiki

Balogh (diskusia | príspevky)
Bez shrnutí editace
Balogh (diskusia | príspevky)
Bez shrnutí editace
Riadok 3: Riadok 3:


Príklady sú doplnkom cvičenia s obvodom AT24C02 - pamäť EEPROM.
Príklady sú doplnkom cvičenia s obvodom AT24C02 - pamäť EEPROM.
* https://github.com/RobTillaart/Arduino/tree/master/sketches/MultiSpeedI2CScanner  Check this!
* i2cdetect je na linuxe/RPi a toto je Arduino verzia, vieme to spravit aj bez kniznice asi.





Verzia z 13:42, 12. máj 2023

Niekoľko užitočných programov k zbernici i2c. Nech poslúžia na štúdium a tvorbu vlastných pokusov.

Príklady sú doplnkom cvičenia s obvodom AT24C02 - pamäť EEPROM.


Základný zápis a čítanie podľa návodu

#include <Wire.h>
#define deviceAddress B01010000
void setup() 
{

  Serial.begin(9600);
    Wire.begin();

/* Zapiseme do EEPROM niekolko znakov */

  for (int i = 0; i < 10; i++) 
  {
    eeprom_i2c_write(deviceAddress, i, 'A'+i);
    delay(100);   /* lebo zapis chvilu trva a nekontrolujeme ACK */
  }

  Serial.println("Writen to memory!");
}

void loop() 
{
  byte myData;
  for (int memAddress = 0; memAddress < 20; memAddress++) 
  {
    myData = eeprom_i2c_read(deviceAddress, memAddress);

    Serial.print(memAddress);
    Serial.print(" - ");
    Serial.print((char)myData);
    Serial.print("\n");
    delay(100);
  }
    delay(2000);

}


void eeprom_i2c_write(byte address, byte from_addr, byte data) {
  Wire.beginTransmission(address);
  Wire.write(from_addr);
  Wire.write(data);
  Wire.endTransmission();
}

byte eeprom_i2c_read(int address, int from_addr) {

  byte result=0xFF;   // ak nic neprijmeme, tak vrati FF
  Wire.beginTransmission(address);
  Wire.write(from_addr);
  Wire.endTransmission();

  Wire.requestFrom(address, 1);
  if(Wire.available())
    result = Wire.read();
  Wire.endTransmission();
 
  return result;
}


Výpis celého obsahu pamäte

#include <Wire.h>
#define M24C02 (0xA0>>1)
void setup()
{
  Wire.begin();        // join i2c bus (address optional for master)
  Serial.begin(9600);  // start serial for output
  Serial.println("Vypis pamate M24C02:");
  Serial.println("--------------------");

  Wire.beginTransmission(M24C02);      // part address is 0xA0 
  Wire.write(0x00); //
  Wire.endTransmission();

  delay(1);

  for (int i=0;i<=7; i++)
  {
    Serial.print(i,HEX);
    Serial.print(": ");

   // tuto moze byt max. 32, aj ked dam viac, nevrati viac nez 32
   // adresa 0xA0 je pre Arduino len 50 (lebo 7 bitov?)
   Wire.requestFrom(M24C02, 255);    // request 6 bytes from slave device #2
   while(Wire.available())    // slave may send less than requested
   { 
    char c = Wire.read(); // receive a byte as character
    Serial.print(c);         // print the character
   }
   Serial.println(" --------------");
}

}

void loop()
{

}

Preskenovanie zbernice a nájdenie obvodov

#include <Wire.h>


void setup()
{
  Wire.begin();

  Serial.begin(9600);
  Serial.println("\nI2C Scanner");
}


void loop()
{
  byte error, address;
  int nDevices;

  Serial.println("Scanning...");

  nDevices = 0;
  for(address = 1; address < 127; address++ ) 
  {
    // The i2c_scanner uses the return value of
    // the Write.endTransmisstion to see if
    // a device did acknowledge to the address.
    Wire.beginTransmission(address);
    error = Wire.endTransmission();

    if (error == 0)
    {
      Serial.print("I2C device found at address 0x");
      if (address<16) 
        Serial.print("0");
      Serial.print(address,HEX);
      Serial.println("  !");

      nDevices++;
    }
    else if (error==4) 
    {
      Serial.print("Unknow error at address 0x");
      if (address<16) 
        Serial.print("0");
      Serial.println(address,HEX);
    }    
  }
  if (nDevices == 0)
    Serial.println("No I2C devices found\n");
  else
    Serial.println("done\n");

  delay(5000);           // wait 5 seconds for next scan
}