Upload files to "src/i2c/esp32c3_master_i2c"
This commit is contained in:
parent
9a7677a857
commit
36185974eb
@ -1,77 +1,77 @@
|
|||||||
#include <Wire.h>
|
#include <Wire.h>
|
||||||
#include <Adafruit_GFX.h>
|
#include <Adafruit_GFX.h>
|
||||||
#include <Adafruit_SSD1306.h>
|
#include <Adafruit_SSD1306.h>
|
||||||
|
|
||||||
#define SCREEN_WIDTH 128
|
#define SCREEN_WIDTH 128
|
||||||
#define SCREEN_HEIGHT 32
|
#define SCREEN_HEIGHT 32
|
||||||
#define OLED_RESET -1 // No reset pin
|
#define OLED_RESET -1 // No reset pin
|
||||||
#define OLED_ADDR 0x3C
|
#define OLED_ADDR 0x3C
|
||||||
|
|
||||||
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
|
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
|
||||||
|
|
||||||
// NEW Datatype For State
|
// NEW Datatype For State
|
||||||
enum _state { NOT_READY, READY, SENDING };
|
enum _state { NOT_READY, READY, SENDING };
|
||||||
|
|
||||||
// Variables
|
// Variables
|
||||||
_state state = NOT_READY;
|
_state state = NOT_READY;
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
|
|
||||||
// Serial
|
// Serial
|
||||||
Serial.begin(115200);
|
Serial.begin(115200);
|
||||||
|
|
||||||
// Initialize I2C bus (ESP32-C3 default pins)
|
// Initialize I2C bus (ESP32-C3 default pins)
|
||||||
Wire.begin();
|
Wire.begin();
|
||||||
|
|
||||||
// Scan I2C devices
|
// Scan I2C devices
|
||||||
Serial.println("Scanning I2C devices...");
|
Serial.println("Scanning I2C devices...");
|
||||||
|
|
||||||
// Scan I2C
|
// Scan I2C
|
||||||
scanI2C();
|
scanI2C();
|
||||||
|
|
||||||
// Initialize OLED
|
// Initialize OLED
|
||||||
if(!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR)) {
|
if(!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR)) {
|
||||||
Serial.println("SSD1306 allocation failed");
|
Serial.println("SSD1306 allocation failed");
|
||||||
while(1);
|
while(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Print On Display
|
// Print On Display
|
||||||
oled_write("Hello, ESP32-C3!");
|
oled_write("Hello, ESP32-C3!");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
|
|
||||||
// Send Tex Message To Slave
|
// Send Tex Message To Slave
|
||||||
i2c_send_string(0x55, Question("String Message"));
|
i2c_send_string(0x55, Question("String Message"));
|
||||||
|
|
||||||
// Request Data
|
// Request Data
|
||||||
Wire.requestFrom(0x55, 1);
|
Wire.requestFrom(0x55, 1);
|
||||||
|
|
||||||
// Read Data Available
|
// Read Data Available
|
||||||
if(Wire.available()) {
|
if(Wire.available()) {
|
||||||
if(char(Wire.read()) == 'N') {
|
if(char(Wire.read()) == 'N') {
|
||||||
Serial.println("Requested Data : Not Ready");
|
Serial.println("Requested Data : Not Ready");
|
||||||
state = NOT_READY;
|
state = NOT_READY;
|
||||||
} else {
|
} else {
|
||||||
Serial.println("Requested Data : Ready");
|
Serial.println("Requested Data : Ready");
|
||||||
state = READY;
|
state = READY;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Request Data
|
// Request Data
|
||||||
Wire.requestFrom(0x55, 6);
|
Wire.requestFrom(0x55, 6);
|
||||||
|
|
||||||
// Prepare 6 Bytes Of Data
|
// Prepare 6 Bytes Of Data
|
||||||
uint8_t data[6];
|
uint8_t data[6];
|
||||||
|
|
||||||
// Read Data Available
|
// Read Data Available
|
||||||
if(Wire.available()) {
|
if(Wire.available()) {
|
||||||
for(int i=0; i<6; i++) {
|
for(int i=0; i<6; i++) {
|
||||||
data[i] = Wire.read();
|
data[i] = Wire.read();
|
||||||
Serial.print(char(data[i]));
|
Serial.print(char(data[i]));
|
||||||
}
|
}
|
||||||
Serial.println();
|
Serial.println();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
51
src/i2c/esp32c3_master_i2c/functions.ino
Normal file
51
src/i2c/esp32c3_master_i2c/functions.ino
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
// Question
|
||||||
|
String Question(String Message) {
|
||||||
|
|
||||||
|
Serial.print(Message + " : ");
|
||||||
|
while(!Serial.available());
|
||||||
|
|
||||||
|
Message = Serial.readStringUntil('\n');
|
||||||
|
while(Serial.available()) Serial.read();
|
||||||
|
Message.trim();
|
||||||
|
Serial.println(Message);
|
||||||
|
return Message;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// OLED Display
|
||||||
|
void oled_write(String message) {
|
||||||
|
display.clearDisplay();
|
||||||
|
display.setTextSize(1);
|
||||||
|
display.setTextColor(SSD1306_WHITE);
|
||||||
|
display.setCursor(0, 0);
|
||||||
|
display.println(message);
|
||||||
|
display.display();
|
||||||
|
}
|
||||||
|
|
||||||
|
// I2C Scanner
|
||||||
|
void scanI2C() {
|
||||||
|
|
||||||
|
byte error, address;
|
||||||
|
int nDevices = 0;
|
||||||
|
|
||||||
|
for(address = 1; address < 127; 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.println(address, HEX);
|
||||||
|
nDevices++;
|
||||||
|
} else if (error==4) {
|
||||||
|
Serial.print("Unknown error at address 0x");
|
||||||
|
if (address<16) Serial.print("0");
|
||||||
|
Serial.println(address, HEX);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (nDevices == 0)
|
||||||
|
Serial.println("No I2C devices found");
|
||||||
|
else
|
||||||
|
Serial.println("Scan complete");
|
||||||
|
|
||||||
|
}
|
22
src/i2c/esp32c3_master_i2c/i2c_functions.ino
Normal file
22
src/i2c/esp32c3_master_i2c/i2c_functions.ino
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
|
||||||
|
// This Function If Called Sends A Text Message Via I2C
|
||||||
|
void i2c_send_string(uint8_t Slave, String message) {
|
||||||
|
|
||||||
|
// Sending Data
|
||||||
|
Wire.beginTransmission(Slave);
|
||||||
|
|
||||||
|
// First Data Byte Is Length
|
||||||
|
Wire.write(message.length());
|
||||||
|
|
||||||
|
// Second Send Data One Byte At A Time
|
||||||
|
for(int i=0; i<message.length(); i++) {
|
||||||
|
|
||||||
|
// Send String Bits
|
||||||
|
Wire.write(message[i]);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Finally Closing Transmission
|
||||||
|
Wire.endTransmission();
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user