Skip to main content

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.

bitModule
0ADS1115 AD converter (0xC1)
1BME280 environment sensor (0xC3)
2DS1307 real-time clock (0xCA)
3TCS34725 color sensor (0xC4)
4PCA9685 PWM generator (0xC5)
6WS2812B full-color LEDs (0xCB)
8OLED display
11Melody 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.

info
OperandsValue/RangeRemark
OP-00000Ignored
OP-10000~FFFFSwitch 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.

info
OperandsValue/RangeRemark
OP-0000Ignored
OP-0101Enable the module
otherDisable the module (00 recommended)
OP-10000Ignored
000  C1  0001  0000    enable the ADS1115 module
001 C1 0000 0000 disable the ADS1115 module
tip

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.

tip

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).

tip

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.

tip

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).

info
OperandsValue/RangeRemark
OP-00FFBit-mask mode: OP-01 selects the channels by bit
otherSingle-channel mode (00 recommended)
OP-0100~03Channel number (single-channel mode)
00~0FChannel bit mask, bit0=CH1 … bit3=CH4 (bit-mask mode)
OP-10000~03E7Duty cycle; written only when the value is below 1000
Advanced

While a melody (0xE8) or a MIDI playback (0xFE) occupies the audio pin, the affected PWM channel is not reconfigured.

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)
info
OperandsValue/RangeRemark
OP-0000Ignored
OP-0101Activate Playtune (MIDI three-channel mode)
00Restore on-board PWM
OP-10000Ignored

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%)
info
OperandsValue/RangeRemark
OP-00000~FFFF16-channel bit mask (bit0 = channel 0 … bit15 = channel 15)
OP-10000~0FFFDuty cycle, 12-bit resolution
tip

0xE1 addresses module 0 only. To update several PCA9685 modules at once, use 0xEB or 0xEC.

0xE2 External PWM: Channels by Mask, Duty from Register​

Same channel selection as 0xE1, but the duty cycle is taken from a register.

000  10  0020  01FF    R20 = 01FF (duty)
001 E2 0003 0020 channels 0 and 1, duty from R20
info
OperandsValue/RangeRemark
OP-00000~FFFF16-channel bit mask
OP-100~FFRegister 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.

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
info
OperandsValue/RangeRemark
OP-0000Ignored
OP-0100~FFStarting register number; R[n]~R[n+6] = year, month, day, weekday, hour, minute, second
OP-10000Ignored

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:

RegisterContent
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.

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
info
OperandsValue/RangeRemark
OP-00000~00FFMask register number n (R[n]~R[n+2] = module 0/1/2 masks)
OP-10000~0FFFDuty 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.

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
info
OperandsValue/RangeRemark
OP-00000~00FFMask register number n (R[n]~R[n+2] = module 0/1/2 masks)
OP-100~FFDuty 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
info
OperandsValue/RangeRemark
OP-00000~00FFMask register number n; mask read from R[n+1]
OP-100~FFDuty 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
info
OperandsValue/RangeRemark
OP-00000~00FFMask register number n; mask read from R[n+2]
OP-100~FFDuty 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
info
OperandsValue/RangeRemark
OP-00000~0002Module number
OP-10000~FFFFFrequency in Hz (the PCA9685 supports roughly 24~1526 Hz)
caution

Setting the frequency suspends the channel refresh: after 0xEF runs, the channels keep their last output until the next WPWM 0xEC is executed.