ESP32 S3 基于开发框架(Arduino)使用IIC总线

611 阅读2分钟

1 IIC总线-数据主机写向从机

1.1 主机

#include <SPI.h>
#include <Wire.h>
#include "USB.h"
#include <SlowSoftWire.h>

#define SSDA_PIN 42 // TMS
#define SSCL_PIN 41 // TDI 

SlowSoftWire i2c(SSDA_PIN, SSCL_PIN,true);  

void init_Blink(){
  pinMode(13, OUTPUT); 
  pinMode(SSDA_PIN, OUTPUT);
  pinMode(SSCL_PIN, OUTPUT);         
}

void drive_Blink()
{
    digitalWrite(13, HIGH);  
    delay(100);
    digitalWrite(13, LOW);
    delay(100);       
}

void setup() {
    USBSerial.begin(115200);   // communicate to PC.
    //init_Blink();
    Wire.begin();
}

void loop() { 

    //驱动板1:
    i2c.beginTransmission(8); 		// 向地址为8的从机传送数据
    // 16进制字符
    // 寄存器7-0:1000 0000  
    // i2c.write("8080808080");  	
    // 寄存器7-0:1100 0000 
    i2c.write("C0C0C0C0C0");
    i2c.endTransmission();    		// 结束传送
    delay(10); //ms单位
}

1.2 从机

#include <Wire.h>
#include "string.h"
// 主机: 高位在前 1F: 0001 1111
// 从机: FluxCom数组低位在前 1111 1000

// 主机1F: 0001 1111   ->  从机FluxCom数组: 1111 1000
// 主机2F: 0010 1111   ->  从机FluxCom数组: 1111 0100

//从机地址
int iic_addr=11;

int nReg=0;
static int FluxInt[5];
int nChar=0;
static char flex[10];
unsigned int char_to_hex(char *pUserInput, unsigned char *pKeyArray);

void setup() {
  Wire.begin(iic_addr);                // join I2C bus with address #8
  Wire.onReceive(receiveEvent); // register event
  Serial.begin(115200);         // start serial for output
  begin();
}

void loop() {
}

// 当主机发送的数据被收到时,将触发 receiveEvent() 事件
void receiveEvent(int howMany) {
  //Serial.println("howMany:");  // 串口输出该字符串
  //Serial.println(howMany);         		    // 串口输出该字符串
  // 循环读取收到的数据,最后一个数据单独读取
  while (0 < Wire.available()) { 
    char c = Wire.read(); 			     // 以字符形式接收数据
    flex[nChar++]=c;
  }
  nChar=0;
  drive_Fluxels();
}


unsigned int char_to_hex(char *pUserInput, unsigned char *pKeyArray)
{
	if (NULL == pUserInput || NULL == pKeyArray)
	{
			return 0;
	}
	
	unsigned int uiKeySize = strlen(pUserInput) / 2;
	int i = 0;
	char cTempor = 0;
	
	while(i < uiKeySize)
	{
		if (*pUserInput >= '0' && *pUserInput <= '9')
		{
			cTempor = *pUserInput - 48;
		}
		else if (*pUserInput >= 'a' && *pUserInput <= 'z') 
		{
			cTempor = 0xa + (*pUserInput - 'a');
		}
		else 
		{
			cTempor = 0xa + (*pUserInput - 'A');
		}
		
		pKeyArray[i] = cTempor;
		pUserInput++;
		
		if (*pUserInput >= '0' && *pUserInput <= '9')
		{
			cTempor = *pUserInput - 48;
		}
		else if (*pUserInput >= 'a' && *pUserInput <= 'z') 
		{
			cTempor = 0xa + (*pUserInput - 'a');
		}
		else 
		{
			cTempor = 0xa + (*pUserInput - 'A');
		}
		
		pKeyArray[i] = (pKeyArray[i] << 4) | cTempor;
		pUserInput++;
		i++;
	}
	
	return uiKeySize;	
}

3 IIC总线-数据主机向从机请求数据

3.1 从机响应数据

#include <Wire.h>

void setup() {
  Wire.begin(8);                 // join I2C bus with address #10
  Wire.onRequest(requestEvent);   // 注册一个IIC事件,用于响应主机的数据请求
  Serial.begin(115200);           // start serial for output
}

void loop() {
  delay(100);
}

//每当主机请求数据时,该函数便会执行
//在setup()中,该函数被注册为一个事件
void requestEvent() {
  Wire.write("101010"); // 用6B的信息回应主机的请求,hello后带一个空格
  Serial.println(101010);
}

3.2 主机请求数据

#include <SPI.h>
#include <Wire.h>
#include "USB.h"
#include <SlowSoftWire.h>

#define SSDA_PIN 42 // TMS
#define SSCL_PIN 41 // TDI 
uint8_t value8;
uint8_t value9;

SlowSoftWire i2c(SSDA_PIN, SSCL_PIN,true);  

void init_Blink(){
  pinMode(13, OUTPUT); 
  pinMode(SSDA_PIN, OUTPUT);
  pinMode(SSCL_PIN, OUTPUT);         
}

void drive_Blink()
{
    digitalWrite(13, HIGH);  
    delay(100);
    digitalWrite(13, LOW);
    delay(100);       
}

void setup() {
    init_Blink();
    USBSerial.begin(115200);   // communicate to PC.
    Wire.begin();
}


void loop() {
    
    //等待从机8发送数据
    i2c.requestFrom(8,6);    // 向8号机请求6B的数据
    while (i2c.available()) { 
        char c = i2c.read(); 		// 以字符形式接受并读取从机发来的一个字节的数据
        USBSerial.print(c);       // 串口输出该字符
    }
    USBSerial.println();				// 换行
    delay(1000);

    //等待从机9发送数据
    i2c.requestFrom(9,6);    // 向8号机请求6B的数据
    while (i2c.available()) { 
        char c = i2c.read(); 		// 以字符形式接受并读取从机发来的一个字节的数据
        USBSerial.print(c);       // 串口输出该字符
    }
    USBSerial.println();				// 换行
    delay(1000);


    // 等待从机10发送数据
    i2c.requestFrom(10,6);    // 向8号机请求6B的数据
    while (i2c.available()) { 
        char c = i2c.read(); 		// 以字符形式接受并读取从机发来的一个字节的数据
        USBSerial.print(c);       // 串口输出该字符
    }
    USBSerial.println();				// 换行
    delay(1000);
}