CH552G - Wunderkind?
Indulging my whimsical penchant for quirky tech I recently took delivery of the enigmatic CH552G. It turns out to have a lot of stuff locked up in its monolithic shell! From the datasheet comes the following:
Core: Enhanced E8051 core compatible with MCS51 command set, 79% of its commands are single-byte single-cycle commands, and the average command speed is 8 ~ 15 times faster than that of the standard MCS51, with special XRAM data fast copy command, and double DPTR pointers.
ROM: Non-volatile memory ROM that can be programmed for many times, with the capacity of 16KB, can albe used for program storage. Or it can be divided into a 14KB program storage area and a 2KB BootLoader/ISP program area.
DataFlash: 128-byte non-volatile data memory that can be erased for multiple times and supports rewrite data in unit of byte.
RAM: 256-byte internal iRAM, which can be used for fast temporary storage of data and stack. 1KB on-chip xRAM, which can be used for temporary storage of large amount of data and direct memory access (DMA).
USB: Built-in USB controller and USB transceiver, support USB-Device mode, support USB type-C master-slave detection, support USB 2.0 full-speed (12Mbps) and low-speed (1.5Mbps) traffic.
Support data packet of up to 64 bytes, built-in FIFO, and support DMA.
Timer: 3 sets of timers (T0/T1/T2), which are standard MCS51 timers.
Capture: Timer T2 is extended to support 2-channel signal capture.
PWM: 2 PWM outputs, PWM1 and PWM2, are 2-channel 8-bit PWM output.
UART: 2 sets of UARTs. Both support higher communication baud rate. UART0 is a standard MCS51 serial port.
SPI: The SPI controller has built-in FIFO, and the clock frequency can reach half of the system dominant frequency Fsys. It supports simplex multiplex of serial data input and output, and Master/Slave mode.
ADC: 4-channel 8-bit A/D converter. It supports voltage comparison.
Touch-key: 6-channel capacitance detection. It supports up to 15 touch keys, and supports independent timing interrupt.
GPIO: Up to 17 GPIO pins (including XI/XO and RST as well as USB signal pins).
Interrupt: It supports 14 sets of interrupt signal sources, including 6 sets of interrupts compatible with the standard MCS51 (INT0, T0, INT1, T1, UART0, T2), and 8 sets of extended interrupts (SPI0, TKEY, USB, ADC, UART1, PWMX, GPIO, WDOG). And GPIO interrupt can be selected from 7 pins.
Watch-Dog: 8-bit presettable watchdog timer WDOG, support timing interrupt.
Reset: 4 kinds of reset signal sources. Built-in power on reset, software reset, watchdog overflow reset and optional pin external input reset.
Clock: Built-in 24MHz clock source, which can support external crystals by multiplexing GPIO pins.
Power: Built-in 5V to 3.3V low dropout voltage regulator. It supports 5V or 3.3V or even 2.8V supply voltage. Support low-power sleep mode and external wake-up of USB, UART0, UART1, SPI0 and part of GPIOs.
Built-in unique ID.
The highlighted dot points above are particular features that I would like to explore with this little chip. The first of these (touch channels) is the subject of this blog and video.
In the diagram below any pins labelled TIN are touch channel pins.
I wanted to blink lights (of course!) when a touch was detected, but it becomes difficult if a light is blinking (and thus employing a pause or delay in processing) to detect a touch.
If we had, for instance, a blink of 1 second on, 1 second off the microcontroller is effectively "frozen" waiting for the delay to finish. A touch detection could be missed while the chip waits for this delay to be over.
This is a fairly normal programming dilemma for single core microcontrollers. As there is no multi-threading etc to split the program, I used a standard technique to simulate multi-tasking.
Effectively we let the clock keep running and the loop stays in motion. There are no delays used in the program to "pause" the instructions. Instead, we use timing markers and we do comparisons each time the loop is executed.
If a previously defined timer length is tripped, then something happens. For instance, if we are "waiting" one second for an LED to turn off, then a timer comparison between when the LED went on with the current time (an "if" statement with a subtraction) would trip that event.
There are plenty of good explanations for this method already online, and you could also step through the code provided below (and watch the video) to see how it is applied in this case.
/* * Touch PIN code for CH552G * OneCircuit Sat 24 Sep 2022 16:52:26 AEST */ #include <TouchKey.h> // timing variables: how long since last touch, since // last fast touch and since fast slow touch long touchtime = 0; long fast = 0; long slow = 0; long currenttime = 0; int turnofftime = 15000; // no touch time, turn off LEDs // were any wires touched? boolean notouch = false; // timing and status for LEDs int slowtime = 500; // milliseconds int fasttime = 100; boolean slowstatus = false; // lit or not? boolean faststatus = false; boolean usingfast = false; // flashing fast or slow? boolean usingslow = false; // the led pins #define LEDslow 30 #define LEDfast 11 void setup() { // output and write low pinMode(LEDslow, OUTPUT); pinMode(LEDfast, OUTPUT); digitalWrite(LEDfast, LOW); digitalWrite(LEDslow, LOW); // setup Touch key for TIN2 and TIN3 delay(50); TouchKey_begin( (1 << 2) | (1 << 3) ); // Enable TIN2(P1.4), TIN3(P1.5) delay(50); } // function that checks if wire has been touched // then returns that wire uint8_t checktouch() { uint8_t anytouch = 0; uint8_t stoptouch = 1; TouchKey_Process(); anytouch = TouchKey_Get(); // if you hold the wire, nothing happens until // you let go, even dousing the lights if (anytouch != 0) { digitalWrite(LEDfast, LOW); digitalWrite(LEDslow, LOW); while (stoptouch != 0) { TouchKey_Process(); stoptouch = TouchKey_Get(); } } return anytouch; } void loop() { // check if wires are touched and check // current time notouch = checktouch(); currenttime = millis(); // we've been touched, so turn on the appropriate LED if (notouch != 0) { touchtime = millis(); // start the timing count // it was a fast touch if ((notouch) & (1 << 2)) { usingfast = true; usingslow = false; digitalWrite(LEDfast, HIGH); digitalWrite(LEDslow, LOW); fast = millis(); slowstatus = false; faststatus = true; notouch == 0; } // it was a slow touch if ((notouch) & (1 << 3)) { usingslow = true; usingfast = false; digitalWrite(LEDslow, HIGH); digitalWrite(LEDfast, LOW); slow = millis(); faststatus = false; slowstatus = true; notouch = 0; } } // no touch so just check blinking time etc // are we past the turnoff time? if (currenttime - touchtime < turnofftime) { // fast led timing on and off if ((usingfast) && (currenttime - fast > fasttime)) { if (faststatus) { digitalWrite(LEDfast, LOW); fast = millis(); faststatus = false; } else { digitalWrite(LEDfast, HIGH); fast = millis(); faststatus = true; } } // slow led timing on and off if ((usingslow) && (currenttime - slow > slowtime)) { if (slowstatus) { digitalWrite(LEDslow, LOW); slow = millis(); slowstatus = false; } else { digitalWrite(LEDslow, HIGH); slow = millis(); slowstatus = true; } } } // past turn off time, so turn off everything else { digitalWrite(LEDfast, LOW); digitalWrite(LEDslow, LOW); slowstatus = false; faststatus = false; } }
In the end the program worked out really well and due to the "fake multitasking" the pins were very responsive to touch.
I would be keen to implement interrupts as well and streamline the code a little bit.
I really enjoyed working with this little wonder, and soon I'll take a look at the "Built in unique ID" which seems interesting as well!
No comments:
Post a Comment