Upload files to "/"
This commit is contained in:
parent
7e5082f72d
commit
9e91df209a
27
i2c_256_master_t4.ino
Normal file
27
i2c_256_master_t4.ino
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
#include <Arduino.h>
|
||||||
|
#include <i2c_device.h>
|
||||||
|
|
||||||
|
#define SLAVE_ADDR 0x08
|
||||||
|
#define BUFFER_SIZE 256
|
||||||
|
|
||||||
|
i2c_device i2c = i2c_device(Wire, SLAVE_ADDR); // Use default Wire (I2C1)
|
||||||
|
|
||||||
|
uint8_t dataBuffer[BUFFER_SIZE];
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
Wire.begin();
|
||||||
|
Wire.setClock(1000000); // Set I2C clock speed to 1 MHz
|
||||||
|
|
||||||
|
// Fill buffer with test data
|
||||||
|
for (int i = 0; i < BUFFER_SIZE; i++) {
|
||||||
|
dataBuffer[i] = i;
|
||||||
|
}
|
||||||
|
|
||||||
|
delay(1000); // Allow slave to initialize
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
i2c.write(dataBuffer, BUFFER_SIZE); // Send full 256 bytes in one transaction
|
||||||
|
|
||||||
|
delay(10); // Small delay before next transmission
|
||||||
|
}
|
26
i2c_256_slave_t4.ino
Normal file
26
i2c_256_slave_t4.ino
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
#include <Teensy4I2C.h>
|
||||||
|
|
||||||
|
#define SLAVE_ADDR 0x08
|
||||||
|
#define BUFFER_SIZE 256
|
||||||
|
|
||||||
|
uint8_t receivedData[BUFFER_SIZE];
|
||||||
|
|
||||||
|
void receiveEvent(size_t count) {
|
||||||
|
I2C.read(receivedData, count);
|
||||||
|
}
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
I2C.begin(SLAVE_ADDR);
|
||||||
|
I2C.onReceive(receiveEvent);
|
||||||
|
Serial.begin(115200);
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
delay(500);
|
||||||
|
Serial.print("Received Data: ");
|
||||||
|
for (int i = 0; i < BUFFER_SIZE; i++) {
|
||||||
|
Serial.print(receivedData[i], HEX);
|
||||||
|
Serial.print(" ");
|
||||||
|
}
|
||||||
|
Serial.println();
|
||||||
|
}
|
27
i2c_master.ino
Normal file
27
i2c_master.ino
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
#include <Wire.h>
|
||||||
|
#define SLAVE_ADDRESS 0x20
|
||||||
|
|
||||||
|
void setup()
|
||||||
|
{
|
||||||
|
Wire.setClock(1000000);
|
||||||
|
Serial.begin(9600);
|
||||||
|
Wire.begin();
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop()
|
||||||
|
{
|
||||||
|
Wire.beginTransmission(SLAVE_ADDRESS);
|
||||||
|
Wire.write(0x09);
|
||||||
|
byte busStatus = Wire.endTransmission();
|
||||||
|
if(busStatus !=0)
|
||||||
|
{
|
||||||
|
Serial.println("I2C Bus error!");
|
||||||
|
while(true);
|
||||||
|
}
|
||||||
|
Serial.println("Data Transmission is ok!");
|
||||||
|
//--------------------------------------
|
||||||
|
Wire.requestFrom(SLAVE_ADDRESS, 1);
|
||||||
|
byte m = Wire.read();
|
||||||
|
Serial.println(m, HEX); //should show: 7
|
||||||
|
delay(1000);
|
||||||
|
}
|
34
i2c_slave.ino
Normal file
34
i2c_slave.ino
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
#include <Wire.h>
|
||||||
|
#define SLAVE_ADDRESS 0x20
|
||||||
|
byte reTxData;
|
||||||
|
volatile bool flag = false;
|
||||||
|
|
||||||
|
void setup()
|
||||||
|
{
|
||||||
|
Wire.setClock(1000000);
|
||||||
|
Serial.begin(9600);
|
||||||
|
Wire.onReceive(receiveEvent);
|
||||||
|
Wire.onRequest(sendEvent);
|
||||||
|
Wire.begin(SLAVE_ADDRESS);
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop()
|
||||||
|
{
|
||||||
|
if (flag == true)
|
||||||
|
{
|
||||||
|
Serial.print("Data received from Master: ");
|
||||||
|
Serial.println(reTxData);
|
||||||
|
flag = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void receiveEvent(int numBytes)
|
||||||
|
{
|
||||||
|
reTxData = Wire.read();
|
||||||
|
flag = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void sendEvent()
|
||||||
|
{
|
||||||
|
Wire.write(reTxData);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user