Peripheral Configuration
Instructions in this category switch peripheral modules on or off, and configure the on-board PWM, the external PCA9685 PWM modules, the real-time clock and the Playtune audio engine.
0xC0 Module Configuration (MKPZ)
Enable or disable several modules at once. OP-1 is a 16-bit switch map that replaces the whole peripheral switch bitmap (same bit assignment as register R34). Every bit of the map takes effect: a module whose bit is 0 is switched off.
| bit | Module |
|---|---|
| 0 | ADS1115 AD converter (0xC1) |
| 1 | BME280 environment sensor (0xC3) |
| 2 | DS1307 real-time clock (0xCA) |
| 3 | TCS34725 color sensor (0xC4) |
| 4 | PCA9685 PWM generator (0xC5) |
| 6 | WS2812B full-color LEDs (0xCB) |
| 8 | OLED display |
| 11 | Melody generator (0xE8) |
000 C0 0000 0105 ADS1115 + DS1307 + OLED on, all others off
0105 corresponds to the binary value 0000 0001 0000 0101: bit 0, bit 2 and bit 8 are set, exactly the three modules named in the comment above.
| Operands | Value/Range | Remark |
|---|---|---|
| OP-0 | 0000 | Ignored |
| OP-1 | 0000~FFFF | Switch map, one bit per module (table above) |
Module Switches: Common Format
The six single-module switch instructions 0xC1, 0xC3, 0xC4, 0xC5, 0xCA and 0xCB share the same five-byte layout XX 00 ss 00 00, where ss = 01 enables and any other value disables the module.
| Operands | Value/Range | Remark |
|---|---|---|
| OP-00 | 00 | Ignored |
| OP-01 | 01 | Enable the module |
| other | Disable the module (00 recommended) | |
| OP-1 | 0000 | Ignored |
- Instruction Form
- Mnemonic Form
000 C1 0001 0000 enable the ADS1115 module
001 C1 0000 0000 disable the ADS1115 module
000 C1 0001 0000 enable the ADS1115 module
001 C1 0000 0000 disable the ADS1115 module
Since the 2026-09 firmware, the power-on self-test automatically enables every module it detects, so example programs generally no longer need explicit enable instructions. These switches are still useful for deliberately disabling a module, or for re-enabling one after it has been switched off.
0xC1 ADS1115 Switch
Enable or disable the ADS1115 analog-to-digital converter module.
Once enabled, the conversion results are maintained in R40~R4F — see Sensors.
0xC3 BME280 Switch
Enable or disable the BME280 environment sensor (temperature, humidity and pressure).
The sensor readings are maintained in R2E~R30.
0xC4 TCS34725 Switch
Enable or disable the TCS34725 color detection sensor.
0xC5 PCA9685 Switch
Enable or disable the PCA9685 external PWM generator module.
The external PWM instructions 0xE1~0xEF below require this module to be enabled.
0xCA DS1307 Switch
Enable or disable the DS1307 real-time clock module. Once enabled, the time data is kept up to date; a new time can be written with 0xE3.
0xCB WS2812B Switch (MKCD)
Enable or disable the WS2812B full-color LED module. The LED instructions are described in LED & Music.
0xCC On-board PWM Duty Cycle (PWM)
Set the duty cycle of the on-board PWM channels: CH1=PB4, CH2=PB3, CH3=PB0, CH4=PB1.
000 CC 0001 01F4 single channel: CH1 duty = 500
001 CC FF05 01F4 bit mask: CH1 and CH3 duty = 500
In the second line, 05 selects the channels by bit (b0101 = CH1 and CH3).
| Operands | Value/Range | Remark |
|---|---|---|
| OP-00 | FF | Bit-mask mode: OP-01 selects the channels by bit |
| other | Single-channel mode (00 recommended) | |
| OP-01 | 00~03 | Channel number (single-channel mode) |
00~0F | Channel bit mask, bit0=CH1 … bit3=CH4 (bit-mask mode) | |
| OP-1 | 0000~03E7 | Duty cycle; written only when the value is below 1000 |
0xD0 Playtune Switch
Activate or restore the Playtune audio engine. Value 01 switches the audio system to the three-channel MIDI mode required by the MIDI player 0xFE; value 00 restores the on-board PWM.
000 D0 0000 0001 activate Playtune (MIDI mode)
| Operands | Value/Range | Remark |
|---|---|---|
| OP-00 | 00 | Ignored |
| OP-01 | 01 | Activate Playtune (MIDI three-channel mode) |
00 | Restore on-board PWM | |
| OP-1 | 0000 | Ignored |
16-bit Whole Operands
From 0xE1 onwards, most operands in this chapter are interpreted as 16-bit wholes: OP-0 and OP-1 are each taken as a single 16-bit value (one four-digit hex number) rather than two independent 8-bit halves. For example, in E1 0003 01FF the value 0003 is the channel mask and 01FF is the duty cycle — neither is split into bytes. See also the operand notation in Brief.
0xE1 External PWM: Channels by Mask, Immediate Duty
Set the duty cycle of the 16 channels of PCA9685 module 0 in one instruction. Every channel whose mask bit is 1 receives the duty value.
000 E1 0003 01FF channels 0 and 1, duty = 01FF (about 12.5%)
| Operands | Value/Range | Remark |
|---|---|---|
| OP-0 | 0000~FFFF | 16-channel bit mask (bit0 = channel 0 … bit15 = channel 15) |
| OP-1 | 0000~0FFF | Duty cycle, 12-bit resolution |
0xE2 External PWM: Channels by Mask, Duty from Register
Same channel selection as 0xE1, but the duty cycle is taken from a register.
- Instruction Form
- Mnemonic Form
000 10 0020 01FF R20 = 01FF (duty)
001 E2 0003 0020 channels 0 and 1, duty from R20
000 FZ 0020 01FF R20 = 01FF (duty)
001 E2 0003 0020 channels 0 and 1, duty from R20
| Operands | Value/Range | Remark |
|---|---|---|
| OP-0 | 0000~FFFF | 16-channel bit mask |
| OP-1 | 00~FF | Register number; duty cycle = R[reg] |
0xE3 Write RTC Time
Write a new time to the DS1307 real-time clock. Seven consecutive registers starting at OP-01 hold year, month, day, weekday, hour, minute and second. The write runs in the background.
- Instruction Form
- Mnemonic Form
000 10 0030 001A R30 = 26 (year)
001 10 0031 0009 R31 = 9 (month)
002 10 0032 0019 R32 = 25 (day)
003 10 0033 0005 R33 = 5 (weekday)
004 10 0034 000A R34 = 10 (hour)
005 10 0035 001E R35 = 30 (minute)
006 10 0036 0000 R36 = 0 (second)
007 E3 0030 0000 write R30~R36 to the RTC
000 FZ 0030 001A R30 = 26 (year)
001 FZ 0031 0009 R31 = 9 (month)
002 FZ 0032 0019 R32 = 25 (day)
003 FZ 0033 0005 R33 = 5 (weekday)
004 FZ 0034 000A R34 = 10 (hour)
005 FZ 0035 001E R35 = 30 (minute)
006 FZ 0036 0000 R36 = 0 (second)
007 E3 0030 0000 write R30~R36 to the RTC
| Operands | Value/Range | Remark |
|---|---|---|
| OP-00 | 00 | Ignored |
| OP-01 | 00~FF | Starting register number; R[n]~R[n+6] = year, month, day, weekday, hour, minute, second |
| OP-1 | 0000 | Ignored |
The Three-Module Mask Mechanism
The external PWM port supports up to three PCA9685 modules — module 0, 1 and 2, 16 channels each (Port46, see Brief). For 0xEB0xEE, OP-0 is the number n of the first of three consecutive mask registers:
| Register | Content |
|---|---|
R[n] | 16-channel bit mask of module 0 |
R[n+1] | 16-channel bit mask of module 1 |
R[n+2] | 16-channel bit mask of module 2 |
0xEB and 0xEC update the masked channels of all three modules in one instruction (up to 48 channels); 0xED and 0xEE update only module 1 and module 2 respectively, using the corresponding mask register R[n+1] / R[n+2].
0xEB External PWM: Three Modules, Immediate Duty
Apply an immediate duty cycle to the channels of all three modules selected by the mask registers.
- Instruction Form
- Mnemonic Form
000 10 0040 0003 R40 = module 0 mask (channels 0 and 1)
001 10 0041 0001 R41 = module 1 mask (channel 0)
002 10 0042 0000 R42 = module 2 mask (none)
003 EB 0040 01FF apply duty 01FF to all masked channels
000 FZ 0040 0003 R40 = module 0 mask (channels 0 and 1)
001 FZ 0041 0001 R41 = module 1 mask (channel 0)
002 FZ 0042 0000 R42 = module 2 mask (none)
003 EB 0040 01FF apply duty 01FF to all masked channels
| Operands | Value/Range | Remark |
|---|---|---|
| OP-0 | 0000~00FF | Mask register number n (R[n]~R[n+2] = module 0/1/2 masks) |
| OP-1 | 0000~0FFF | Duty cycle, 12-bit resolution |
0xEC External PWM from Register (WPWM)
Register version of 0xEB: the duty cycle is taken from a register. One instruction refreshes up to 48 channels across all three modules.
- Instruction Form
- Mnemonic Form
000 10 0040 0003 R40 = module 0 mask
001 10 0041 0001 R41 = module 1 mask
002 10 0042 0000 R42 = module 2 mask
003 10 0050 01FF R50 = 01FF (duty)
004 EC 0040 0050 refresh all masked channels, duty from R50
000 FZ 0040 0003 R40 = module 0 mask
001 FZ 0041 0001 R41 = module 1 mask
002 FZ 0042 0000 R42 = module 2 mask
003 FZ 0050 01FF R50 = 01FF (duty)
004 WPWM 0040 0050 refresh all masked channels, duty from R50
| Operands | Value/Range | Remark |
|---|---|---|
| OP-0 | 0000~00FF | Mask register number n (R[n]~R[n+2] = module 0/1/2 masks) |
| OP-1 | 00~FF | Duty register number; duty cycle = R[reg] |
0xED External PWM: Module 1 from Register
Same as 0xEC, but only module 1 is updated (mask register R[n+1]).
000 10 0041 00F0 R41 = module 1 mask (channels 4~7)
001 10 0050 0800 R50 = 0800 (duty)
002 ED 0040 0050 update module 1 only, duty from R50
| Operands | Value/Range | Remark |
|---|---|---|
| OP-0 | 0000~00FF | Mask register number n; mask read from R[n+1] |
| OP-1 | 00~FF | Duty register number; duty cycle = R[reg] |
0xEE External PWM: Module 2 from Register
Same as 0xEC, but only module 2 is updated (mask register R[n+2]).
000 10 0042 0001 R42 = module 2 mask (channel 0)
001 10 0050 0400 R50 = 0400 (duty)
002 EE 0040 0050 update module 2 only, duty from R50
| Operands | Value/Range | Remark |
|---|---|---|
| OP-0 | 0000~00FF | Mask register number n; mask read from R[n+2] |
| OP-1 | 00~FF | Duty register number; duty cycle = R[reg] |
0xEF Set PWM Frequency (WPL)
Set the PWM frequency of one PCA9685 module (0, 1 or 2), in Hz.
000 EF 0000 03E8 module 0: PWM frequency 1000 Hz
| Operands | Value/Range | Remark |
|---|---|---|
| OP-0 | 0000~0002 | Module number |
| OP-1 | 0000~FFFF | Frequency in Hz (the PCA9685 supports roughly 24~1526 Hz) |
Setting the frequency suspends the channel refresh: after 0xEF runs, the channels keep their last output until the next WPWM 0xEC is executed.