Skip to content

Commit a1b9bae

Browse files
committed
add I2C type UNIT
1 parent 5911719 commit a1b9bae

File tree

47 files changed

+7824
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+7824
-0
lines changed
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
Description: Control 4 relays and demonstrate the asynchronous control relay LED
3+
*/
4+
5+
#include <M5Core2.h>
6+
7+
/*-----------------------------------------------------------------------------*/
8+
// |RELAY control reg | 0x10
9+
// |-----------------------------------------------------------------------------
10+
// |Relay_ctrl_mode_reg[0] | R/W | System control
11+
// | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
12+
// | R | R | R | R | R | R | R | Sync Mode |
13+
// | -Sync Mode:0 LED&Relay Async
14+
// | -Sync Mode:1 LED&Relay Sync
15+
//---------------------------------------------------------------------------------
16+
// |Relay_ctrl_mode_reg[1] | R/W | Relay & LED control
17+
// | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
18+
// | LED1| LED2| LED3| LED4| RLY1| RLY2| RLY3| RLY4|
19+
//
20+
/*-------------------------------------------------------------------------------*/
21+
22+
void WriteRelayReg( int regAddr, int data )
23+
{
24+
Wire.beginTransmission(0x26);
25+
Wire.write(regAddr);
26+
Wire.write(data);
27+
Wire.endTransmission();
28+
Serial.printf("[ W ] %02X : %02X. \r\n", regAddr, data);
29+
}
30+
31+
int readRelayReg(int regAddr)
32+
{
33+
Wire.beginTransmission(0x26);
34+
Wire.write(regAddr);
35+
Wire.endTransmission();
36+
Wire.requestFrom(0x26, 1);
37+
int data = Wire.read() & 0x00ff;
38+
Serial.printf("[ R ] %02X : %02X. \r\n", regAddr, data);
39+
return data;
40+
}
41+
42+
void WriteRelayNumber( int number, int state )
43+
{
44+
int StateFromDevice = readRelayReg(0x11);
45+
if( state == 0 )
46+
{
47+
StateFromDevice &= ~( 0x01 << number );
48+
}
49+
else
50+
{
51+
StateFromDevice |= ( 0x01 << number );
52+
}
53+
WriteRelayReg(0x11,StateFromDevice);
54+
}
55+
56+
void setup() {
57+
// put your setup code here, to run once:
58+
M5.begin(true, true, true, true);
59+
M5.Lcd.setCursor(90, 0, 4);
60+
M5.Lcd.setTextColor(TFT_GREEN, TFT_BLACK);
61+
M5.Lcd.print("4-RELAY UNIT");
62+
M5.Lcd.setCursor(0, 220, 2);
63+
M5.Lcd.print("Independent Switch");
64+
M5.Lcd.setCursor(130, 220, 2);
65+
M5.Lcd.print("LED Sync/Async");
66+
M5.Lcd.setCursor(250, 220, 2);
67+
M5.Lcd.print("ALL Relay");
68+
M5.Lcd.setCursor(20, 50, 4);
69+
M5.Lcd.print("Relay State: ");
70+
M5.Lcd.setCursor(20, 80, 4);
71+
M5.Lcd.print("Sync Mode: ");
72+
readRelayReg(0x10);
73+
readRelayReg(0x11);
74+
WriteRelayReg(0x10,1);
75+
WriteRelayReg(0x11,0);
76+
//WriteRelayNumber(0,0);
77+
}
78+
79+
int count_i = 0;
80+
bool flag_led, flag_relay = false;
81+
82+
83+
void loop() {
84+
if(M5.BtnA.wasPressed()){
85+
M5.Lcd.fillRect(160, 50, 100, 20, TFT_BLACK);
86+
M5.Lcd.setCursor(160, 50, 4);
87+
M5.Lcd.printf("%d ON", count_i);
88+
WriteRelayReg(0x11,(0x01 << count_i));
89+
count_i++;
90+
if( count_i >= 4 ) count_i = 0;
91+
}
92+
if(M5.BtnB.wasPressed()){
93+
M5.Lcd.fillRect(160, 80, 100, 20, TFT_BLACK);
94+
if(!flag_led){
95+
M5.Lcd.setCursor(160, 80, 4);
96+
M5.Lcd.print("Async");
97+
WriteRelayReg(0x10, 0);
98+
}else {
99+
M5.Lcd.setCursor(160, 80, 4);
100+
M5.Lcd.print("Sync");
101+
WriteRelayReg(0x10, 1);
102+
}
103+
flag_led = !flag_led;
104+
}
105+
if(M5.BtnC.wasPressed()){
106+
M5.Lcd.fillRect(160, 50, 100, 20, TFT_BLACK);
107+
for(int i=0; i<4; i++){
108+
if(!flag_relay) {
109+
M5.Lcd.setCursor(160, 50, 4);
110+
M5.Lcd.print("ON");
111+
WriteRelayNumber(i, 1);
112+
}else {
113+
M5.Lcd.setCursor(160, 50, 4);
114+
M5.Lcd.print("OFF");
115+
WriteRelayNumber(i, 0);
116+
}
117+
}
118+
flag_relay = !flag_relay;
119+
}
120+
M5.update();
121+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
Description: Read ACCEL Unit three-axis acceleration
3+
Please install library before compiling:
4+
Arduino-ADXL345: https://github.com/jakalada/Arduino-ADXL345
5+
*/
6+
#include <M5Core2.h>
7+
#include <ADXL345.h>
8+
ADXL345 accel(ADXL345_ALT);
9+
10+
void setup() {
11+
// put your setup code here, to run once:
12+
M5.begin();
13+
Wire.begin();
14+
M5.Lcd.setCursor(140, 10, 4);
15+
M5.Lcd.println("ACC");
16+
17+
M5.Lcd.setCursor(40, 100); M5.Lcd.print(" x ");
18+
M5.Lcd.setCursor(140, 100); M5.Lcd.print(" y ");
19+
M5.Lcd.setCursor(240, 100); M5.Lcd.print(" z ");
20+
21+
byte deviceID = accel.readDeviceID();
22+
if (deviceID != 0) {
23+
Serial.print("0x");
24+
Serial.print(deviceID, HEX);
25+
Serial.println("");
26+
} else {
27+
Serial.println("read device id: failed");
28+
while(1) {
29+
delay(100);
30+
}
31+
}
32+
33+
// Data Rate
34+
// - ADXL345_RATE_3200HZ: 3200 Hz
35+
// - ADXL345_RATE_1600HZ: 1600 Hz
36+
// - ADXL345_RATE_800HZ: 800 Hz
37+
// - ADXL345_RATE_400HZ: 400 Hz
38+
// - ADXL345_RATE_200HZ: 200 Hz
39+
// - ADXL345_RATE_100HZ: 100 Hz
40+
// - ADXL345_RATE_50HZ: 50 Hz
41+
// - ADXL345_RATE_25HZ: 25 Hz
42+
// - ...
43+
if (!accel.writeRate(ADXL345_RATE_200HZ)) {
44+
Serial.println("write rate: failed");
45+
while(1) {
46+
delay(100);
47+
}
48+
}
49+
50+
// Data Range
51+
// - ADXL345_RANGE_2G: +-2 g
52+
// - ADXL345_RANGE_4G: +-4 g
53+
// - ADXL345_RANGE_8G: +-8 g
54+
// - ADXL345_RANGE_16G: +-16 g
55+
if (!accel.writeRange(ADXL345_RANGE_16G)) {
56+
Serial.println("write range: failed");
57+
while(1) {
58+
delay(100);
59+
}
60+
}
61+
62+
if (!accel.start()) {
63+
Serial.println("start: failed");
64+
while(1) {
65+
delay(100);
66+
}
67+
}
68+
}
69+
70+
void loop() {
71+
// put your main code here, to run repeatedly:
72+
if (accel.update()) {
73+
M5.Lcd.fillRect(0, 130, 360, 30, BLACK);
74+
M5.Lcd.setCursor(35, 130); M5.Lcd.print((int)(1000*accel.getX()));
75+
M5.Lcd.setCursor(135, 130); M5.Lcd.print((int)(1000*accel.getY()));
76+
M5.Lcd.setCursor(235, 130); M5.Lcd.print((int)(1000*accel.getZ()));
77+
//M5.Lcd.setCursor(300, 130); M5.Lcd.print("mg");
78+
} else {
79+
Serial.println("update failed");
80+
while(1) {
81+
delay(100);
82+
}
83+
}
84+
delay(100);
85+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
Description: Use ADC Unit to convert 0 ~ 12V analog voltage into 16-bit data and display it on the screen.
3+
*/
4+
#include <M5Core2.h>
5+
#include <Wire.h>
6+
#include "ADS1100.h"
7+
8+
ADS1100 ads;
9+
10+
void setup(void)
11+
{
12+
M5.begin(true, false, false);
13+
Serial.begin(115200);
14+
M5.Lcd.fillScreen(BLACK);
15+
M5.Lcd.setTextColor(ORANGE);
16+
17+
// The address can be changed making the option of connecting multiple devices
18+
ads.getAddr_ADS1100(ADS1100_DEFAULT_ADDRESS); // 0x48, 1001 000 (ADDR = GND)
19+
20+
// The ADC gain (PGA), Device operating mode, Data rate
21+
// can be changed via the following functions
22+
23+
ads.setGain(GAIN_ONE); // 1x gain(default)
24+
// ads.setGain(GAIN_TWO); // 2x gain
25+
// ads.setGain(GAIN_FOUR); // 4x gain
26+
// ads.setGain(GAIN_EIGHT); // 8x gain
27+
28+
ads.setMode(MODE_CONTIN); // Continuous conversion mode (default)
29+
// ads.setMode(MODE_SINGLE); // Single-conversion mode
30+
31+
ads.setRate(RATE_8); // 8SPS (default)
32+
// ads.setRate(RATE_16); // 16SPS
33+
// ads.setRate(RATE_32); // 32SPS
34+
// ads.setRate(RATE_128); // 128SPS
35+
36+
ads.setOSMode(OSMODE_SINGLE); // Set to start a single-conversion
37+
38+
ads.begin();
39+
}
40+
41+
void loop(void)
42+
{
43+
byte error;
44+
int8_t address;
45+
46+
address = ads.ads_i2cAddress;
47+
// The i2c_scanner uses the return value of
48+
// the Write.endTransmisstion to see if
49+
// a device did acknowledge to the address.
50+
Wire.beginTransmission(address);
51+
error = Wire.endTransmission();
52+
if (error == 0)
53+
{
54+
int16_t result;
55+
56+
Serial.println("Getting Differential Reading from ADS1100");
57+
Serial.println(" ");
58+
result = ads.Measure_Differential();
59+
Serial.print("Digital Value of Analog Input between Channel 0 and 1: ");
60+
Serial.println(result);
61+
M5.Lcd.fillScreen(BLACK);
62+
char data[20] = { 0 };
63+
sprintf(data, "%d", result);
64+
M5.Lcd.drawCentreString(data, 160, 100, 4);
65+
Serial.println(" ");
66+
Serial.println(" *************************** ");
67+
Serial.println(" ");
68+
}
69+
else
70+
{
71+
Serial.println("ADS1100 Disconnected!");
72+
Serial.println(" ");
73+
Serial.println(" ************ ");
74+
Serial.println(" ");
75+
M5.Lcd.setTextFont(4);
76+
M5.Lcd.setTextColor(TFT_WHITE, TFT_BLACK);
77+
M5.Lcd.drawString("No Found ADC sensor.",20, 100, 4);
78+
}
79+
80+
delay(1000);
81+
}

0 commit comments

Comments
 (0)