The sixth chapter is about reusing peripheral driver code. The content of this article focuses on section 6.5, which covers the keyboard and digital tube interface.
6.5 Keyboard and Digital Tube Interface
> > > 6.5.1 ZLG72128 Interface
When expanding a matrix to a certain number, the progressive scan method becomes time-consuming, and if it's necessary to "simultaneously" operate two or more keys, it can be quite complicated. The ZLG72128 is a self-designed digital tube display driver and keyboard scanning management chip by ZLG. It can directly drive 12-bit common cathode digital tubes (or 96 independent LEDs) and can also scan up to 32 buttons, with 8 of them functioning as function keys similar to Ctrl, Shift, and Alt on a computer keyboard.
In addition, the ZLG72128 includes a combo counter that allows a key to be pressed continuously without being released. This chip is an industrial-grade component with strong anti-interference capabilities and has been widely used in industrial measurement and control systems.
1. Features
Directly drives 12-bit, 1-inch or smaller common cathode digital tubes or 96 independent LEDs;
Manages up to 32 buttons, 8 of which can be used as function keys to automatically eliminate jitter;
Power circuit easily drives large digital tubes over 1 inch;
Powerful functions such as bit flicker, bit blanking, segment lighting, segment extinction, function keys, and combo key counting;
Decoding display function for 10 digits and 21 letters, allowing direct writing of display data to the display buffer;
Software configuration supports 0~12 digital tube display modes;
Uses an I2C serial bus interface with the MCU;
Operating voltage range: 3.0 ~ 5.5V;
Operating temperature range: -40 to +85°C;
Package: TSSOP28.
2. Typical Application Circuit
Figure 6.12 shows the pin layout diagram of the ZLG72128. The corresponding pin function description is shown in Table 6.34.
Table 6.34 Pin Function Table
Figure 6.12 ZLG72128 Pin Layout Diagram
As shown in Figure 6.13, the ZLG72128 can manage up to 32 buttons (4 rows and 8 columns). The row lines are connected to COM8 ~ COM11 pins, and the column lines are connected to COM0 ~ COM7. Specifically, the first 3 lines of buttons (a total of 24 buttons) are normal buttons. When a button is pressed, the user is notified via the INT pin. No notification is given when the button is released. The last row of buttons (a total of 8 buttons) is a function key that represents the state of eight key values with one 8-bit data, where F0 ~ F7 correspond to bit0 to bit7 respectively. When pressed, the corresponding bit is 0. When the corresponding bit is released, the corresponding bit is 1. As long as the 8-bit data value of the 8 buttons changes, the user will be notified through the INT pin. Therefore, for the function button, whether the button is pressed or released can be notified.
Note that a resistor is required between the keyboard circuit and the ZLG72128 chip pin, typically 1KΩ. In most applications, you may not need so many keys, so you can either cut the keyboard in rows or columns. It should be noted that this button circuit is not applicable to the case where 3 or more keys are pressed.
Figure 6.13 Button Circuit
As shown in Figure 6.14, the button circuit is used when two or more function keys are used together with the normal keys. A diode is added between the function keys and the normal keys. Note that the diode should try to select a conduction voltage drop that is small.
Figure 6.14 Multiple Function Key Multiplexing Button Circuit
Figure 6.15 shows the typical application circuit schematic of the ZLG72128. When using the chip-driven digital tube and managing buttons, the user can refer to the circuit for circuit design. The ZLG72128 can only directly drive a 12-bit common cathode digital tube driver. A current limiting resistor is required between the digital tube segment and the ZLG72128 chip pin. The typical value is 270Ω. If it is necessary to increase the brightness of the digital tube, the resistance value can be appropriately reduced. The driving capability of the ZLG72128 is limited. When using a large digital tube, the brightness may not be sufficient. In this case, the current limiting resistor value of the digital tube can be appropriately reduced to increase the brightness. The minimum value is 200 Ω. If the brightness is still insufficient, it must be added the power driver circuit as described in the ZLG72128 User Manual (http://).
In order to stabilize the power supply of the ZLG72128 chip, a 47~470μF electrolytic capacitor is usually connected between VCC and GND. According to the requirements of the I2C bus protocol, pull-up resistors must be connected to the signal lines SCL and SDA, respectively, which is typically 4.7KΩ. When the communication rate is greater than 100kbps, it is recommended to reduce the value of the pull-up resistor. The chip reset pin, RST, is active low and can be tied to the MCU's I/O to control its reset. The KEY_INT pin outputs a key interrupt request signal (active low) that can be connected to the MCU's I/O to get a button press or release event.
Figure 6.15 ZLG72128 Typical Application Circuit
3. Register Details
The ZLG72128 has 12 display buffer registers, DispBuf0~DispBuf11, which directly determine what the digital tube displays. The ZLG72128 provides two display control modes, one is to write font data directly to the video memory, and the other is to automatically decode and display by writing control commands to the command buffer register. Access to these registers is done through the I2C bus interface. The I2C bus device address of the ZLG72128 is 60H (write) and 61H (read), access
The internal registers are implemented by “subaddressâ€.
(1) System register SystemReg (address: 00H)
The 0th bit (LSB) of the system register is called KeyAvi, which indicates whether the button is valid, 0- no button is pressed, and 1 has a button pressed. The other bits of the SystemReg register are temporarily undefined. When a key is pressed, the KEY_INT pin of the ZLG72128 generates a low interrupt request signal. When the key value is read, the interrupt signal is automatically cancelled (turns high) and KeyAvi is also reflected. Under normal circumstances, the MCU only needs to judge the KEY_INT pin. By constantly querying the KeyAvi bit, it can be judged whether there is a key press, which saves an I/O line of the microcontroller, but the I2C bus is in a frequent active state, which consumes more current and is not conducive to anti-interference.
(2) Key value register Key (address: 01H)
If a normal key of K1~K24 is pressed, the microcontroller can read the corresponding key value 1~24 from the key value register Key. If the microcontroller finds that the KEY_INT pin of the ZLG72128 has generated an interrupt request and the key value read from the Key is 0, it indicates that the function key was pressed. The value of the key register Key automatically becomes 0 after being read away.
(3) Combo counter RepeatCnt (address: 02H)
The ZLG72128 provides a combo count function for K1 to K24. The so-called combo means that pressing a normal key does not let go, after a delay of two seconds, it starts to be continuous, and the continuous effective interval is about two hundred milliseconds. This feature is very similar to the keyboard on your computer. Under the premise that the microcontroller can respond to the key interrupt and read the key value in time, when pressing and holding a normal key has not been released: first, an interrupt signal will be generated, and the value of the combo counter RepeatCnt is still 0; After a two-second delay, the interrupt signal is continuously generated. Each time RepeatCent is interrupted, it is automatically incremented by one; when RepeatCnt counts to 255, it is no longer incremented, and the interrupt signal continues to be valid. During this time, the value of the key register is generated each time.
(4) Function key register FunctionKey (address: 03H)
The eight function keys F0 to F7 provided by the ZLG72128. Function keys are often used with normal keys, like the Shift, Ctrl, and Alt keys on a computer keyboard. Of course, the function keys can also be used separately, just like F1~F12 on the computer keyboard. When a function key is pressed, an interrupt signal is generated on the KEY_INT pin as if it were a normal key. The key values of the function keys are saved in the FunctionKey register. The initial value of the function key register FunctionKey is FFH. Each bit corresponds to a function key. The 0th bit (LSB) corresponds to F0, the 1st bit corresponds to F1, and so on, and the 7th bit (MSB) corresponds to F7. When a function key is pressed, the corresponding FunctionKey bit is cleared. Another feature of the function button is the "secondary interrupt". When the button is pressed, an interrupt signal is generated. When the button is raised, an interrupt signal is generated. The normal button only generates an interrupt when it is pressed.
(5) Command buffers CmdBuf0 and CmdBuf1 (addresses: 07H and 08H)
The segment addressing and download display data functions can be realized by writing relevant control commands to the command buffer.
(6) Flash control register FlashOnOff (address: 0BH)
The FlashOnOff register determines the blink frequency and duty cycle. The reset value is 0111 0111B. The upper 4 bits indicate the duration of the flashing time, and the lower 4 digits indicate the duration of the flashing time. By changing the value of FlashOnOff, you can change the blinking frequency and duty cycle at the same time. When FlashOnOff takes the value 00H, the fastest blinking speed can be obtained. The calculation formula of the lighting off time is as follows:
T = N × 50 + 150mS
T is the duration of the light when it is flashing or off. N is the value of the upper 4 bits or lower 4 bits of the register, and the value is 0~15. The fastest flashing frequency is 3.33Hz (the period is 300mS), and the slowest blinking frequency is 0.55. Hz (cycle is 1.8S). Special Note: Setting the value of the FlashOnOff register separately will not see the display flicker, but should be used together with the flash control command.
(7) Blanking register DispCtrl0 (address: 0CH) and DispCtrl1 (address: 0DH)
As shown in Table 6.35, the blanking register is used. The DispCtrl0 and DispCtrl1 registers determine which bits are displayed, corresponding to 1 to 12 bits of the digital tube. When the register bit is 1, the corresponding digital pipe position is not displayed. The reset value is 0x00, that is, the 12 bits of the digital tube are scanned and displayed.
Table 6.35 Blanking Register
In practical applications, it may be necessary to display less than 12 digits. For example, only 8 digits can be displayed. In this case, the value of DispCtrl0 can be set to 0x0F, and the value of DispCtrl1 can be set to 0x00, and the 0~7 digits of the nixie tube are scanned. Display, and bits 8 to 12 are not displayed.
(8) Flashing register Flash0 (address: 0EH) and Flash1 (address: 0FH)
As shown in Table 6.36, the flash register is used. The Flash0 and Flash 1 registers determine which bits are blinking, corresponding to 1 to 12 bits of the digital tube. When the register bit is 1, the corresponding digital tube bit flashes. The reset value is 0x00, that is, the 12 bits of the digital tube are not flashing.
Table 6.36 Flash Register
In practice, some bit flicker may be needed, for example, the last 2 digits flash. In this case, the value of Flash0 can be set to 0x00, and the value of Flash1 can be set to 0x03, then the first and second digits of the nixie flash, and the third ~12 digits will not flash.
(9) Display buffers DispBuf0 to DispBuf11 (address: 10H to 1BH)
DispBuf0~DispBuf11 The values of these 12 registers directly determine the display content of the digital tube. The 8 bits of each register correspond to the a, b, c, d, e, f, g, and dp segments of the digital tube, MSB corresponds to a, and LSB corresponds to dp. For example, the font data of the uppercase letter H is 6EH (without a decimal point) or 6FH (with a decimal point).
4. Detailed Control Commands
The register CmdBuf0 (address: 07H) and CmdBuf1 (address: 08H) together form a command buffer. Segment addressing, downloading display data, and controlling flicker can be achieved by writing relevant control commands to the command buffer.
(1) Segment Addressing (SegOnOff)
As shown in Table 6.37, the segment addressing register, in the segment addressing command, the 12-bit digital tube is treated as 96 segments, and each segment is actually an independent LED.
Double-byte command In the instruction format, the upper 4 bits of CmdBuff0 are “0001â€, and the lowest bit of CmdBuff0 indicates whether the segment is lit, 0—off, and 1—light. B3B2B1B0 of CmdBuff0 is the bit address, which takes values from 0 to 11. S3S2S1S0 is a 4-bit segment address, which takes values from 0 to 7, corresponding to a, b, c, d, e, f, g, and dp of the digital tube.
Table 6.37 Segment Addressing Registers
(2) Download Data and Decode (Download)
As shown in Table 6.38, the download data and decoding register, double-byte command in the instruction format, the upper 4 bits of CmdBuff0 "0010" is the command code A3A2A1A0 is the bit address of the digital tube display data, the bit address number is from left to the order of the right is 11, 10, 9, 8, ..., 0, dp controls whether the decimal point is lit, 0-off, 1-lit. Flash indicates whether to flash, 0-normal display, 1-flash. D4d3d2d1d0 is the data to be displayed, including 10 numbers and 21 letters, and the display data is decoded according to the rules in Table 6.39.
Table 6.38 Download Data, Decoding Register
Table 6.39 Data Table for Downloading Data and Decoding Commands
(3) Reset Command (Reset)
Single-byte command. In the instruction format, the "4" of the upper 4 bits of CmdBuf0 is the command code. Its function is to turn off all LEDs. See Table 6.40 for details.
Table 6.40 Reset Command Register
(4) Test Command (Test)
Single-byte command. In the instruction format, the upper 4 bits of CmdBuf0 are “0100â€, which is the command code. Its function is to flash all LEDs at a rate of 0.5S. See Table 6.41 for details.
Table 6.41 Test Command Register
(5) Left Shift Command (ShiftLeft)
Single-byte command. In the instruction format, the “4101†of the upper 4 bits of CmdBuf0 is the command code. See Table 6.42 for details. The function is based on the bit of the digital tube and is shifted to the left by n bits. The bit that is vacated on the right after the left shift does not display anything, that is, all LEDs are off. The value of n ranges from 1 to 11. The value greater than 11 is invalid. The value of n is determined by the lower 4 bits of CmdBuf0 and is calculated according to the following formula:
n = (b3 × 8) + ( b2 × 4) + (b1 × 2) + b0
Table 6.42 Left Shift Command Register
(6) Cycle Left Shift Command (CyclicShiftLeft)
Single-byte command. In the instruction format, the “0110†of the upper 4 bits of CmdBuf0 is the command code. See Table 6.43 for details. The function is based on the bit of the digital tube, and the loop is shifted to the left by n bits.
After moving left, the right side shows the content removed from the far left. The value of n ranges from 1 to 11. The value greater than 11 is invalid. The value of n is determined by the lower 4 bits of CmdBuf0 and is calculated according to the following formula:
n = (b3 × 8) + ( b2 × 4) + (b1 × 2) + b0
Table 6.43 Cyclic Left Shift Command Register
(7) Right Shift Command (ShiftRight)
Single-byte command. In the instruction format, the “0111†of the upper 4 bits of CmdBuf0 is the command code. See Table 6.44 for details. The function is based on the bit of the digital tube and is shifted to the right by n bits.
The bit that is vacated on the left after the right shift does not display anything, that is, all LEDs are off. The value of n ranges from 1 to 11. The value greater than 11 is invalid. The value of n is determined by the lower 4 bits of CmdBuf0 and is calculated according to the following formula:
n = (b3 × 8) + ( b2 × 4) + (b1 × 2) + b0
Table 6.44 Right Shift Command Register
(8) Cycle Right Shift Command (CyclicShiftRight)
Single-byte command, in the instruction format, the "1000" of the upper 4 bits of CmdBuf0 is the command code. See Table 6.45 for details. The function is based on the bit of the digital tube, and the loop is shifted to the right by n bits. After shifting right, the left side shows the content shifted from the rightmost side. The value of n ranges from 1 to 11. The value greater than 11 is invalid. The value of n is determined by the lower 4 bits of CmdBuf0, and is calculated according to the following formula:
n = (b3 × 8) + ( b2 × 4) + (b1 × 2) + b0
Table 6.45 Loop Right Shift Command Register
(9) Digital tube scanning digit setting command (Scanning)
For single-byte commands, the "4" of the upper 4 bits of CmdBuf0 in the instruction format is the command code, and the number of bits scanned by the digital tube is set. See Table 6.46 for details. The value of n is 0~12, which is greater than 12 and is scanned by 12 bits. The number of scan bits n is valid from the first bit of the bit select to the nth bit of the bit select. The value of n is determined by the lower four bits of CmdBuf0 and is calculated according to the following formula.
n = (b3 × 8) + ( b2 × 4) + (b1 × 2) + b0
Table 6.46 Scan Digits Setting Register
During use, if a 12-digit display is not required, crop from the highest position and set the number of digital scan bits to the corresponding number of digits. After the number of scan bits of the digital tube is reduced, the useful display position is increased due to the more scan time allocated.
> > > 6.5.2 ZLG72128 Initialization
AMetal has provided the ZLG72128 driver function. Before using other functions to manage the digital tube and buttons, the ZLG72128 must be initialized. The prototype of its initialization function (am_zlg72128.h) is:
This function is intended to get the instance handle of ZLG72128. Where p_dev is a pointer to an instance of type am_zlg72128_dev_t and p_devinfo is a pointer to instance information of type am_zlg72128_devinfo_t.
Instance
The definition of the am_zlg72128_dev_t type (am_zlg72128.h) is as follows:
Where g_zlg72128_dev is a user-defined instance whose address is passed as an argument to p_dev.
Instance information
The instance information describes the information about the interrupt pin. The definition of the type am_zlg72128_devinfo_t (am_zlg72128.h) is as follows:
Where use_int_pin indicates whether to use the interrupt output pin (KEY_INT) of the ZLG72128. If the value is TRUE, it indicates that the interrupt pin needs to be used. At this time, int_pin specifies the pin number connected to the main controller (such as LPC824). The key value will be obtained in the pin interrupt; if the value is FALSE, it means no The interrupt pin is used, at which time interval_ms specifies the time interval for querying the key value.
In general, the interrupt pin is used as long as the I/O resources of the master are not very tight. To save an I/O interrupt resource, use_int_pin can be set to FALSE. At this time, the IO interrupt resource will not be occupied, and the system will get the key value from the ZLG72128 by query, which will consume a certain amount of CPU resources. Because every time you have to actively query the key value. Assuming the interrupt pin of the ZLG72128 is used, the host controller uses the LPC824, and the KEY_INT pin of the ZLG72128 is connected to the PIO0_17 of the LPC824. Its instance information is defined as follows:
I2C handle i2c_handle
If the I2C1 of the LPC824 is used to communicate with the ZLG72128, the I2C handle can be obtained by the I2C1 instance initialization function am_lpc82x_i2c1_inst_init() of the LPC82x. which is:
The obtained I2C handle can be passed directly as an argument to i2c_handle.
Example handle
The return value of the am_zlg72128_init() function is the handle of the ZLG72128 instance, which will be used as the argument to the first parameter (handle) of other functional interfaces (digital tube display, button management, etc.).
Its type am_zlg72128_handle_t(am_zlg72128.h) is defined as follows:
If the return value is NULL, the initialization fails; if the return value is not NULL, a valid handle is returned.
Based on the modular programming idea, the definitions of the initialization related instances, instance information, etc. are stored in the corresponding configuration file, and the instance initialization function interface is extracted through the header file. The program examples of the source file and the header file are respectively shown in the program list 6.111 and the program. Listing 6.112.
Listing 6.111 ZLG72128 Instance Initialization Function Implementation (am_hwconf_zlg72128.c)
Listing 6.112 ZLG72128 Instance Initialization Function Declaration (am_hwconf_zlg72128.h)
Subsequent only need to use the parameterless instance initialization function to get the instance handle of ZLG72128. which is:
> > > 6.5.3 Button Management Interface Function
The ZLG72128 supports 32 keys (4 rows and 8 columns matrix keyboard), where the first 3 acts as a normal key, and only one normal key can be pressed at a time. The last action function key, multiple function keys can be pressed simultaneously. The button management has only one registration button callback interface function.
In order to notify the user of the key event in time when the key event is detected (the key is pressed), the user needs to register a callback function, and when a key event occurs, the user registered callback function is automatically invoked. Its function prototype is:
Among them, pfn_key_cb is the registered key callback function, p_arg is the value of the first parameter of the callback function, that is, when the callback function is automatically called when the key event is detected, the value of p_arg is used as the value of the first parameter of the callback function.
The type of callback function am_zlg72128_key_cb_t(am_zlg72128.h) is defined as follows:
It can be seen that the callback function has 4 parameters, and the user can obtain the information related to the button through these parameters. In particular, the first parameter p_arg is a user-defined parameter whose value is the value set by the p_arg parameter when registering the callback function.
Key_val, repeat_cnt, and funo_val represent information about key events. There are three possible key events for ZLG72128:
There is a normal button press (normal button release does not act as a button event)
When a normal key is pressed, key_val indicates the key value of the pressed key. The valid range of the key value is 1 ~ 24, and the key value of the normal key has been defined as a macro in am_zlg72128.h, and the macro name is AM_ZLG72128_KEY_X_Y, where X represents Line number (1 ~ 3), Y means column number (1 ~ 8), such as the key value of the 5th key of the 2nd line: AM_ZLG72128_KEY_2_5.
The normal button is pressed all the time (in the combo state)
When the normal key presses for more than 2 seconds, it enters the combo state. When it is in the combo state, a button event is generated every 200ms, and a button counter is used to count the generated button events. Each button event is generated. The value of the counter is incremented by 1. Since the bit width of the combo counter is 8 bits, the value will not be incremented after the value reaches 255, but the button event will continue to be generated until the key is released and the combo counter is cleared. In the combo state, key_val indicates the key value of the pressed key, and repeat_cnt indicates the value of the combo counter.
The status of the function key changes (the function key is pressed or released, causing the status to change)
The value of funkey_val represents the state of all function keys. The last line has up to 8 keys, from left to right, respectively F0 ~ F7, one-to-one correspondence with bit0~bit7 of funkey_val. A bit value of 0 means that the corresponding function key is pressed, and a bit value of 1 means that the corresponding function key is not pressed. When no function key is pressed, the value of funkey_val is 0xFF. As long as the value of funkey_val changes, a button event is generated and the function key does not provide a combo function. You can use macros in am_zlg72128.h
AM_ZLG72128_FUNKEY_CHECK(funkey_val, funkey_num) to easily determine whether a function key is pressed. Funkey_num is used to indicate the function key to be detected. The value has been defined as a macro. F0 ~ F7 are AM_ZLG72128_FUNKEY_0 ~ AM_ZLG72128_FUNKEY_7 respectively. If the corresponding key is pressed, the macro value is TURE; otherwise, the macro value is FALSE. For example, if you know if F0 is pressed by funkey_val, you can use the following statement:
The function keys are similar to the Ctrl, Alt, Shift keys on the PC. It is easy to implement the combination key using the normal keys and function keys. The sample program for registering the key callback function is shown in Listing 6.113.
Listing 6.113 ZLG72128 Registration Key Callback Function Usage Example
If only the first key of the first line is pressed, the LED0 state is reversed. If the first key of the first row is pressed and the function key F0 is pressed, the LED1 state
Optical Fibre To Rj45 Converter,Sc To Sc Coupler,Sc Duplex Adapter,Sc To St Adapter
Ningbo Fengwei Communication Technology Co., Ltd , https://www.fengweicommunication.com