Skip to content

Commit b1bc30a

Browse files
committed
CH552 examples
Adding examples for the QT Py CH552 learn guide. examples for led blink, analog in, capacitive touch, i2c with the AHT20 and neopixel swirl
1 parent bc31f2d commit b1bc30a

File tree

5 files changed

+373
-0
lines changed

5 files changed

+373
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// SPDX-FileCopyrightText: 2024 ladyada for Adafruit Industries
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
#define NEOPIXEL_PIN 10
6+
#define A0 11
7+
#define A1 14
8+
#define A2 15
9+
#define MOSI A2
10+
#define MISO 16
11+
#define SCK 17
12+
#define RX 30
13+
#define TX 31
14+
#define A3 32
15+
#define SCL 33
16+
#define SDA 34
17+
18+
int led = MISO;
19+
20+
void setup() {
21+
pinMode(led, OUTPUT);
22+
}
23+
24+
void loop() {
25+
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
26+
delay(1000); // wait for a second
27+
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
28+
delay(1000); // wait for a second
29+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// SPDX-FileCopyrightText: 2024 ladyada for Adafruit Industries
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
#include <TouchKey.h>
6+
7+
uint8_t count = 0;
8+
uint8_t state = 0;
9+
10+
void setup() {
11+
while (!USBSerial()); // wait for serial port to connect. Needed for native USB port only
12+
delay(100);
13+
USBSerial_println("QT Py CH552 Cap Touch Test");
14+
USBSerial_println("Uses pin A0 (P1.1)");
15+
TouchKey_begin((1 << 1)); //Enable channel P1.1/A0
16+
}
17+
18+
void loop() {
19+
// put your main code here, to run repeatedly:
20+
TouchKey_Process();
21+
uint8_t touchResult = TouchKey_Get();
22+
if (touchResult) {
23+
if (state == 0) {
24+
count += 1;
25+
state = 1;
26+
USBSerial_print("TIN1.1 touched ");
27+
USBSerial_print(count);
28+
USBSerial_println(" times");
29+
}
30+
} else {
31+
state = 0;
32+
}
33+
delay(1000);
34+
35+
}
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
// SPDX-FileCopyrightText: 2024 ladyada for Adafruit Industries
2+
//
3+
// SPDX-License-Identifier: MIT
4+
// https://chat.openai.com/share/5dddee44-3196-4a6b-b445-58ac6ef18501
5+
6+
#include <SoftI2C.h>
7+
8+
extern uint8_t scl_pin;
9+
extern uint8_t sda_pin;
10+
11+
void Wire_begin(uint8_t scl, uint8_t sda);
12+
bool Wire_scan(uint8_t i2caddr);
13+
bool Wire_writeBytes(uint8_t i2caddr, uint8_t *data, uint8_t bytes);
14+
bool Wire_readBytes(uint8_t i2caddr, uint8_t *data, uint8_t bytes);
15+
bool Wire_readRegister(uint8_t i2caddr, uint8_t regaddr, uint8_t *data, uint8_t bytes);
16+
17+
bool readAHT20(float *temperature, float *humidity);
18+
#define AHTX0_I2CADDR_DEFAULT 0x38
19+
20+
void setup() {
21+
while (!USBSerial()); // wait for serial port to connect. Needed for native USB port only
22+
delay(100);
23+
24+
USBSerial_println("CH552 QT Py I2C sensor test");
25+
Wire_begin(33, 34); // set up I2C on CH552 QT Py
26+
27+
USBSerial_print("I2C Scan: ");
28+
for (uint8_t a=0; a<=0x7F; a++) {
29+
if (!Wire_scan(a)) continue;
30+
USBSerial_print("0x");
31+
USBSerial_print(a, HEX);
32+
USBSerial_print(", ");
33+
}
34+
USBSerial_println();
35+
36+
if (! Wire_scan(AHTX0_I2CADDR_DEFAULT)) {
37+
USBSerial_println("No AHT20 found!");
38+
while (1);
39+
}
40+
}
41+
42+
void loop() {
43+
delay(100);
44+
45+
float t, h;
46+
if (!readAHT20(&t, &h)) {
47+
USBSerial_println("Failed to read from AHT20");
48+
}
49+
USBSerial_print("Temp: ");
50+
USBSerial_print(t);
51+
USBSerial_print(" *C, Hum: ");
52+
USBSerial_print(h);
53+
USBSerial_println(" RH%");
54+
}
55+
56+
/*********************** AHT20 'driver */
57+
58+
#define AHTX0_CMD_TRIGGER 0xAC
59+
#define AHTX0_STATUS_BUSY 0x80
60+
61+
bool AHT20_getStatus(uint8_t *status) {
62+
return Wire_readBytes(AHTX0_I2CADDR_DEFAULT, status, 1);
63+
}
64+
65+
bool readAHT20(float *temperature, float *humidity) {
66+
uint8_t cmd[3] = {AHTX0_CMD_TRIGGER, 0x33, 0x00};
67+
uint8_t data[6], status;
68+
uint32_t rawHumidity, rawTemperature;
69+
70+
// Trigger AHT20 measurement
71+
if (!Wire_writeBytes(AHTX0_I2CADDR_DEFAULT, cmd, 3)) {
72+
return false;
73+
}
74+
75+
// Wait until the sensor is no longer busy
76+
do {
77+
if (!AHT20_getStatus(&status)) {
78+
return false;
79+
}
80+
delay(10); // Delay 10ms to wait for measurement
81+
} while (status & AHTX0_STATUS_BUSY);
82+
83+
// Read the measurement data
84+
if (!Wire_readBytes(AHTX0_I2CADDR_DEFAULT, data, 6)) {
85+
return false;
86+
}
87+
88+
// Parse humidity data
89+
rawHumidity = data[1];
90+
rawHumidity = (rawHumidity << 8) | data[2];
91+
rawHumidity = (rawHumidity << 4) | (data[3] >> 4);
92+
*humidity = ((float)rawHumidity * 100.0) / 0x100000;
93+
94+
// Parse temperature data
95+
rawTemperature = (data[3] & 0x0F);
96+
rawTemperature = (rawTemperature << 8) | data[4];
97+
rawTemperature = (rawTemperature << 8) | data[5];
98+
*temperature = ((float)rawTemperature * 200.0 / 0x100000) - 50.0;
99+
100+
return true;
101+
}
102+
103+
/**************************** Wire I2C interface */
104+
105+
void Wire_begin(uint8_t scl, uint8_t sda) {
106+
scl_pin = scl; //extern variable in SoftI2C.h
107+
sda_pin = sda;
108+
I2CInit();
109+
}
110+
111+
bool Wire_scan(uint8_t i2caddr) {
112+
return Wire_writeBytes(i2caddr, NULL, 0);
113+
}
114+
115+
bool Wire_readRegister(uint8_t i2caddr, uint8_t regaddr, uint8_t *data, uint8_t bytes) {
116+
if (!Wire_writeBytes(i2caddr, &regaddr, 1)) {
117+
return false;
118+
}
119+
120+
return Wire_readBytes(i2caddr, data, bytes);
121+
}
122+
123+
124+
bool Wire_writeBytes(uint8_t i2caddr, uint8_t *data, uint8_t bytes) {
125+
uint8_t ack_bit;
126+
127+
I2CStart();
128+
ack_bit = I2CSend(i2caddr << 1 | 0); // Shift address and append write bit
129+
if (ack_bit != 0) {
130+
I2CStop();
131+
return false;
132+
}
133+
134+
for (uint8_t i = 0; i < bytes; i++) {
135+
if (I2CSend(data[i]) != 0) {
136+
I2CStop();
137+
return false;
138+
}
139+
}
140+
I2CStop();
141+
return true;
142+
}
143+
144+
bool Wire_readBytes(uint8_t i2caddr, uint8_t *data, uint8_t bytes) {
145+
uint8_t ack_bit;
146+
147+
I2CStart();
148+
ack_bit = I2CSend(i2caddr << 1 | 1); // Shift address and append read bit
149+
if (ack_bit != 0) {
150+
I2CStop();
151+
return false;
152+
}
153+
154+
for (uint8_t i = 0; i < bytes; i++) {
155+
data[i] = I2CRead();
156+
if (i == bytes - 1) {
157+
I2CNak(); // NAK on last byte
158+
} else {
159+
I2CAck(); // ACK on other bytes
160+
}
161+
}
162+
163+
I2CStop();
164+
return true;
165+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
// SPDX-FileCopyrightText: 2024 ladyada for Adafruit Industries
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
#include <WS2812.h>
6+
7+
#define NEOPIXEL_PIN P1_0
8+
#define NUM_LEDS 1
9+
10+
#define COLOR_PER_LEDS 3
11+
#define NUM_BYTES (NUM_LEDS*COLOR_PER_LEDS)
12+
#if NUM_BYTES > 255
13+
#error "NUM_BYTES can not be larger than 255."
14+
#endif
15+
__xdata uint8_t ledData[NUM_BYTES];
16+
17+
/***********************************************************************/
18+
uint8_t neopixel_brightness = 255;
19+
uint32_t Wheel(byte WheelPos);
20+
void rainbowCycle(uint8_t wait);
21+
22+
#define NEOPIXEL_SHOW_FUNC CONCAT(neopixel_show_, NEOPIXEL_PIN)
23+
24+
void neopixel_begin() {
25+
pinMode(NEOPIXEL_PIN, OUTPUT); //Possible to use other pins.
26+
}
27+
28+
void neopixel_show() {
29+
NEOPIXEL_SHOW_FUNC(ledData, NUM_BYTES); //Possible to use other pins.
30+
}
31+
32+
void neopixel_setPixelColor(uint8_t i, uint32_t c) {
33+
uint16_t r, g, b;
34+
r = (((c >> 16) & 0xFF) * neopixel_brightness) >> 8;
35+
g = (((c >> 8) & 0xFF) * neopixel_brightness) >> 8;
36+
b = ((c & 0xFF) * neopixel_brightness) >> 8;
37+
38+
set_pixel_for_GRB_LED(ledData, i, r, g, b);
39+
}
40+
41+
void neopixel_setBrightness(uint8_t b) {
42+
neopixel_brightness = b;
43+
}
44+
/***********************************************************************/
45+
46+
47+
void setup() {
48+
neopixel_begin();
49+
neopixel_setBrightness(50);
50+
}
51+
52+
void loop() {
53+
rainbowCycle(5);
54+
}
55+
56+
57+
void rainbowCycle(uint8_t wait) {
58+
uint8_t i, j;
59+
60+
for (j=0; j<255; j++) {
61+
for (i=0; i < NUM_LEDS; i++) {
62+
neopixel_setPixelColor(i, Wheel(((i * 256 / NUM_LEDS) + j) & 255));
63+
}
64+
neopixel_show();
65+
delay(wait);
66+
}
67+
}
68+
69+
70+
// Input a value 0 to 255 to get a color value.
71+
// The colours are a transition r - g - b - back to r.
72+
uint32_t Wheel(byte WheelPos) {
73+
uint8_t r, g, b;
74+
uint32_t c;
75+
76+
if(WheelPos < 85) {
77+
r = WheelPos * 3;
78+
g = 255 - WheelPos * 3 ;
79+
b = 0;
80+
} else if(WheelPos < 170) {
81+
WheelPos -= 85;
82+
r = 255 - WheelPos * 3;
83+
g = 0;
84+
b = WheelPos * 3;
85+
} else {
86+
WheelPos -= 170;
87+
r = 0;
88+
g = WheelPos * 3;
89+
b = 255 - WheelPos * 3;
90+
}
91+
c = r;
92+
c <<= 8;
93+
c |= g;
94+
c <<= 8;
95+
c |= b;
96+
return c;
97+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// SPDX-FileCopyrightText: 2024 ladyada for Adafruit Industries
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
/*
6+
ReadAnalogVoltage
7+
8+
Reads an analog input on pin P1.1, converts it to voltage, and prints the result to the Serial Monitor.
9+
Graphical representation is available using Serial Plotter (Tools > Serial Plotter menu).
10+
Attach the center pin of a potentiometer to pin P1.1, and the outside pins to +5V and ground.
11+
12+
This example code is in the public domain.
13+
14+
http://www.arduino.cc/en/Tutorial/ReadAnalogVoltage
15+
*/
16+
17+
#include <Serial.h>
18+
19+
#define A0 11
20+
#define A1 14
21+
#define A2 15 // also MISO!
22+
#define A3 32
23+
24+
#define ANALOG_IN A3
25+
#define VREF 3.3
26+
27+
// the setup routine runs once when you press reset:
28+
void setup() {
29+
// No need to init USBSerial
30+
31+
// By default 8051 enable every pin's pull up resistor. Disable pull-up to get full input range.
32+
pinMode(ANALOG_IN, INPUT);
33+
}
34+
35+
// the loop routine runs over and over again forever:
36+
void loop() {
37+
// read the input on analog pin 0, P1.1:
38+
int sensorValue = analogRead(ANALOG_IN);
39+
// Convert the analog reading (which goes from 0 - 255) to VREF:
40+
float voltage = sensorValue * (VREF / 255.0);
41+
// print out the value you read:
42+
USBSerial_println(voltage);
43+
// or with precision:
44+
//USBSerial_println(voltage,1);
45+
46+
delay(10);
47+
}

0 commit comments

Comments
 (0)