Port Match is a versatile feature included on most Silicon Labs 8-bit MCUs (C8051 and EFM8). This feature allows an interrupt to be generated any time a GPIO pin input level does not match an expected value (which is why it's sometimes referred to as Port Mismatch). On parts with sleep mode, this function can also wake the device, allowing for low-power applications that are sensitive to GPIO changes.
General Operation:
Port Match is enabled for a particular pin by setting its respective bit in its PnMASK register. For example, to enable port match on P0.0, you would write the following instruction:
P0MASK |= 0x01;
Additionally, Port Match will not work if the pin is in analog mode, so its respective PnMDIN bit must be set as well to enable the pin's input drivers.
P0MDIN |= 0x01;
Once enabled, an interrupt is generated whenever the pin's respective Pn bit and PnMAT bit do not match. For example, if P0 reads 0x01 and P0MAT reads 0x00, an interrupt would be generated due to P0.0 not matching.
Thus, to set P0.0 to interrupt if P0.0 reads 0, the following instruction would be used:
P0MAT |= 0x01;
Interrupt Operation:
In order to generate an interrupt, the Port Match interrupt and global interrupts should be enabled.
Note: an interrupt will be continually generated as long as there is a port mismatch. If a mismatch event still exists after the ISR exits, the ISR will be immediately called again.
In general, the most useful way to use Port Match is to use a particular trick so that interrupts are only generated on pin edges, rather than continually on one pin level. This can easily be accomplished by assigning the PnMAT register to its relative Pn register value. This removes the mismatch event. For example, an ISR that interrupts only on the edges (both rising and falling) of any enabled Port Match pins would look like the following:
Since the value of P0 may change during the ISR, it is recommended to sample it at the beginning of the ISR, then use the sample to determine the behavior of the rest of the ISR, like so:
If multiple pins are enabled for Port Match, it is still relatively simple to determine which pin or pins caused the port match interrupt. Similarly, it is easy to tell whether a rising or falling edge on this pin was the cause.
To determine the pin, merely XOR the PnMAT register with the Pn value.
To determine the edge, look at this pin's bit in its respective match register. If the match register reads 0 for this bit, then a mismatch will occur when the pin level is 1. This means that the pin transitioned from 0 to 1, or that this was a rising edge. If the match bit is 1, a falling edge occurred on this pin:
bit edge;
...
edge = (P0MAT & P0_matchPin) == 0;
In this case, edge being 1 would indicate a rising edge, 0 for a falling edge.
Wake-up Source:
On devices with a sleep-mode function (C8051F9xx, EFM8SBxx), Port Match events can also wake the device up from sleep mode. In this case, it is not necessary to generate an interrupt, so Port Match interrupts and global interrupts may be left disabled. Only the appropriate PnMAT and PnMASK bits must be set, and Port Match should be enabled as a wakeup source.
An example project for the EFM8BB1 demonstrating Port Match on all available pins and determining which pin/edge caused the event has been attached to this article.
This solution is implemented in firmware that runs on Silicon Labs C8051F340 or C8051T622 microcontrollers. Attached is the Wake-On-Lan firmware source code.
The documentation for the microcontrollers themselves, as well as development kits, can be found here:
In the Simplicity Studio Kit Manager, I see a "Firmware Version". What is this Kit Firmware?
Answer
On every EFM8 and EFM32 kit, there is a Segger J-Link debugger. The Kit Firmware is the debugger firmware, which is updated to address debugger-related bug fixes.
If your kit has old kit/debugger firmware, you will be notified when opening Kit Manager in Simplicity Studio. Upgrading to the newest kit/debugger firmware is always recommended.
A comparator input is a pin configured in analog mode. In analog mode, the pin's weak pull is disabled, and its leakage current is specified in the Port I/O DC Electrical Characteristics section of the datasheet, and not in the Comparator Electrical Characteristics section.
For example, the C8051T60x leakage current is max +/- 1 µA (Table 8.3 of the revision 1.2 datasheet).
8-bit Knowledge Base
Using Port Match
Wake-On-LAN Solution
Generate hex file with Raisonance 8051 tools
Firmware Version in Kit Manager
Comparator Input Leakage
STARTUP.A51是什么文件,如何使用?
Keil C51中如何在特定地址定义变量、常量?
Keil C51 编译的时候 Large Mode 、Compact Mode 与 Small Mode 区别有哪些?
C8051Fxxx / EFM8 可以使用哪些编译器?
如何消除Keil C51 的 uncalled functions警告?
由于C51内存比较小,所以Keil C51对于形参、局部变量按照函数的调用关系进行时分复用。这种时分复用不像一般的系统中那样由栈指针管理,而是给变量分配固定地址。所以有时候如果编译器无法知道调用关系就必须为函数分配独立的内存,比如函数定义了但是没有地方显式的调用,但是有可能通过函数指针调用。比如编译器警告function_a未调用,则解决办法可以在函数中加入
详细情况请在Keil帮助文档 C:\Keil\C51\Hlp\c51tools.chm 搜索 overlay。