動作
Feature #46
已結束Feature #58: 光源模組連接ESP32
光源模組連接ESP32_韌體研發
是由 Chifu Chung 於 約 1 年 前加入. 於 12 個月 前更新.
開始日期:
2023-09-25
完成日期:
2023-10-31
完成百分比:
100%
預估工時:
檔案
clipboard-202310021404-gethv.png (540 KB) clipboard-202310021404-gethv.png | 宏益 廖, 2023-10-02 06:04 | ||
clipboard-202310021404-di9nw.png (771 KB) clipboard-202310021404-di9nw.png | 宏益 廖, 2023-10-02 06:04 |
是由 宏益 廖 於 約 1 年 前更新
- 檔案 clipboard-202310021404-gethv.png clipboard-202310021404-gethv.png 已新增
- 檔案 clipboard-202310021404-di9nw.png clipboard-202310021404-di9nw.png 已新增
*I2C相關知識¶
韌體開發前請先研讀I2C相關知識包含:需要幾條線、電路、接線、時序圖、START Condition、Stop Condition...
參考資料 or 自行Google:
https://wiki.csie.ncku.edu.tw/embedded/I2C
https://rexpighj123.pixnet.net/blog/post/219960237¶
============================================================================================¶
*TCA6507¶
------------------------------------------------------------------------------------------------------------------------------------------------¶
以下程式應用 Wire.h 完成 Datasheet 上要求的規則,達到傳輸資料的效果¶
============================================================================================¶
*程式¶
#include <Wire.h>
//ESP32 I2C 腳位 & TCA6507 Enable
#define SDA 21
#define SCL 22
#define EN 26
enum TCA_registers{
SELECT0 = 0x00,
SELECT1 = 0x01,
SELECT2 = 0x02,
FADE_ON_TIME = 0x03,
FULLY_ON_TIME = 0x04,
FADE_OFF_TIME = 0x05,
FIRST_FULLY_OFF_TIME = 0x06,
SECOND_FULLY_OFF_TIME = 0x06,
MAXIMUM_INTENSITY = 0x08,
ONE_SHOT = 0x09,
INITIALIZATION = 0x10,
};
int8_t TCA_writeBytes(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t *data){
uint8_t status = 0;
Wire.beginTransmission(devAddr);
Wire.write(regAddr);
for (uint8_t i = 0; i < length; i++) {
Wire.write((uint8_t) data[i]);
}
status = Wire.endTransmission();
return status == 0;
}
int8_t TCA_writeByte(uint8_t devAddr, uint8_t regAddr, uint8_t data){
return TCA_writeBytes(devAddr, regAddr, 1, &data);
}
int8_t TCA_read(uint8_t devAddr, uint8_t regAddr, uint8_t length, uint8_t *data, uint16_t timeout){
int8_t count = 0;
uint32_t t1 = millis();
for (uint8_t k = 0; k < length; k += std::min<int>(length, BUFFER_LENGTH)) {
Wire.beginTransmission(devAddr);
Wire.write(regAddr);
Wire.endTransmission();
Wire.requestFrom(devAddr, (uint8_t) std::min<int> (length - k, BUFFER_LENGTH));
for (; Wire.available() && (timeout == 0 || millis() - t1 < timeout); count++) {
data[count] = Wire.read();
}
}
if (timeout > 0 && millis() - t1 >= timeout && count < length) count = -1; // timeout
return count;
}
bool TCA_isConnected(){
uint8_t result = 0;
return ( TCA_read(device_address, SELECT0, 1, &result, 0) == 1);
}
void setup() {
Serial.begin(115200);
Wire.begin(SDA, SCL);
pinMode(EN, OUTPUT);
digitalWrite(EN, LOW);
delay(20);
digitalWrite(EN, HIGH);
delay(20);
if(TCA_isConnected()){
Serial.print("TCA6507 connected!!");
}
else{
Serial.print("TCA6507 doesn't connected.");
}
}
void loop() {
//on_off在後續LCD螢幕上可以用觸控來控制
//先試試用Serial port控制 或 讓on_off每隔1秒+1 讓燈閃爍
if(on_off % 2 == 1){
uint8_t selects[3] = {0x00, 0x00, 0x1E};
TCA_writeByte(device_address, SELECT2, selects[2]);
}
if(on_off % 2 == 0){
TCA_writeByte(device_address, SELECT2, 0x00);
}
}
動作