MSP430 capacitive touch wheel and LED PWM output design
[Copy link]
This application document introduces the PWM software driving technology of capacitive touch wheel and multiple independent LEDs using MSP430 microcontroller. The solution realizes capacitive touch wheel control through 4 I/O ports, and the I/O ports cooperate with transistors to drive LEDs to achieve effects such as LED breathing and track lights. This solution provides an effective and low-cost solution for products that require capacitive touch wheel control and LED tracking display and other gorgeous lighting effects.
Introduction
As a practical and fashionable human-computer interaction method, capacitive touch technology has been widely used in various electronic products, ranging from light switches to tablet computers and touch tables. What follows is a test of how product designers can use their wisdom to design the product user interface to be convenient and simple while presenting a gorgeous appearance of the product, thereby bringing a good user experience.
LED display is widely used in capacitive touch product design because of its user-friendly interface and ability to reflect touch location information in real time. This design uses a large number of LEDs to achieve special effects such as breathing lights and track lights, and can provide design references for products with adjustment functions such as lighting, volume, and temperature.
Texas Instruments' MSP430 series of microcontrollers are known for their low power consumption and rich peripheral modules. For capacitive touch applications, the MSP430's PIN RO capacitive touch detection method supports direct connection of IO ports to detection electrodes without the need for any peripheral devices, greatly simplifying circuit design. The MSP430G2XX5 used in this design document supports up to 32 IO ports and can drive more than 24 LED lights to achieve ideal display effects.
1. Capacitive touch wheel implementation solution
The MSP430 capacitive touch wheel solution uses 4 IO ports to complete 4-channel capacitance detection. With the help of special electrode patterns, the wheel design can be realized.
1.1 Capacitive touch implementation principle
MSP430 supports a variety of capacitive touch detection methods according to different models, including RC oscillation, comparator, and PIN RO. This design uses the PIN Relaxation Oscillator method. The principle is shown in Figure 1. The internal detection circuit of the chip pin is composed of a Schmitt trigger, an inverter, and a resistor. The oscillation signal is converted into a pulse signal through the Schmitt trigger, and then fed back to the RC circuit through the inverter. The output of the Schmitt trigger is counted through Timer_A, and the counting result is obtained by setting the measurement window Gate. When a finger touches the electrode, the C on the electrode changes, causing the oscillation frequency to change, so that different counting results can be obtained in a fixed-length measurement window. Once the difference exceeds the threshold, a touch event can be triggered in combination with a certain filtering algorithm.
Figure 1 PIN RO schematic diagram
1.2 Wheel Algorithm
The four button electrodes are crossed in a zigzag shape as shown in Figure 2 to form a wheel electrode. The size of the wheel can be appropriately scaled according to the needs of product design. The graphic design in Figure 2 is suitable for a wheel with a diameter of about 30 mm.
Figure 2 Rotating wheel electrode design
When the user operates on the wheel, the electrode at the corresponding position of the finger will obtain the highest signal value, the channel close to the finger will have a relatively high signal value, and the channel farthest from the finger will detect the smallest signal value, as shown in Figure 3:
Figure 3 Signal values measured on different electrodes when a finger touches
At this time, the position of the finger on the wheel or slider can be calculated by using the difference in signal values on different channels. The steps for position calculation are as follows:
a. Use the sorting method to find the electrode with the largest signal among the four electrodes
index = Dominant_Element(groupOfElements, &measCnt[0]);
b. Add the signal of the electrode found to the signal of the adjacent electrode
position = measCnt[index] + measCnt[index+1] + measCnt[index-1];
If the result of the addition is greater than the threshold, it is considered that a touch event has occurred and the subsequent position calculation continues. The reason for adding the previous and next signals is that the finger may be between the two electrodes during the operation, so the signals obtained on the two electrodes will not be very high. The signals need to be added before they can be compared with the threshold.
c. When calculating the position coordinates, first get an approximate position based on the filtered index value, then make corrections based on the signal strength of the adjacent electrodes of the index to get the final coordinate value
position = index*(groupOfElements->points/groupOfElements->numElements);
position += (groupOfElements->points/groupOfElements->numElements)/2;
position += (measCnt[index+1]*(groupOfElements->points/groupOfElements->numElements))/100;
position -= (measCnt[index-1]*(groupOfElements->points/groupOfElements->numElements))/100;
d. The code needs to be processed separately for the case where index is 0 or 3, but the calculation method is the same as above.
The resolution of the wheel here, that is, the number of segments a wheel circle is divided into, is set based on points. Assuming that the user only needs to distinguish 24 positions, points can be set to 24. Of course, it can also be set to 64, 128, or even higher, depending on the size of the wheel, the design of the electrode pattern, and the number of electrodes. For example, if high precision like 1024 is required, the number of electrodes needs to be increased from 4 to 8 or more.
2.LED PWM drive solution implementation
To achieve the LED breathing effect, the LED needs to be dimmed by PWM. To achieve the track light effect, each LED must be controlled by independent PWM.
This application uses 24 LEDs, which requires 24 PWM output controls. The MSP430G2955 has 32 IO ports. The IO ports are combined with the TIMER timer to support 24 software PWM outputs.
3. Design Example
This example uses the Texas Instruments MSP430G2955, completes capacitive touch detection through 6 IOs, 24 IOs drive 24 LEDs, and reserves a communication port. The design example is shown in Figure 4
Figure 4 Example demonstration diagram
3.1 Circuit Design
The schematic design is shown in Figure 4. The MCU supplies power to VCC through a 5V to 3.3V LDO. The purpose of using LDO is to ensure the stability of the power supply so that the touch circuit will not produce excessive signal deviation due to power supply noise when detecting signals. The resistor connected in series on the electrode is used as an ESD protection device and can be omitted if the product structure design is reasonable. The UART port is reserved in the circuit to communicate with the main control system.
Figure 5 MCU circuit
The LED driving circuit is shown in Figure 5. Since the current of each LED is about 10mA, if 24 LEDs are on at the same time, it will be 240mA, which cannot be driven directly through the MCU IO port. A transistor and a current limiting resistor are added to each LED to achieve the control of 24 LEDs.
Figure 6 LED drive circuit
3.2 Code Design
3.2.1 LED Driver
Before writing code to control the LED lighting sequence, first define the specifications related to PWM output:
The PWM output duty cycle is set to 50%.
The frequency is 5KHz, and the brightness level is divided into 24 levels. The LED is turned off at level 0 and is the brightest at level 23.
Use 2 TIMERs to control PWM output.
TIMERA0 interrupt frequency is 24X5KHz=120KHz.
The TIMERB interrupt interval is set to 10ms, and the LED brightness level is changed in TIMERB.
Through the cooperation of two TIMER interrupts, 24 independent PWM outputs can be controlled. When a touch event occurs, the brightness level of the corresponding LED is assigned according to the touch position, and then the brightness level is slowly reduced to zero in the TIMERB interrupt, so that the corresponding LED can gradually dim after the finger leaves the electrode.
The program flow charts in the two TIMER interrupts are as follows: Figure 7 and Figure 8
Figure 7 TIMERB flow chart
Figure 8 TIMERA0 flow chart
3.2.2 Wheel
The capacitive touch software library of Texas Instruments supports the signal detection of capacitive buttons and the calculation of wheel coordinates. The position value of the current touch event can be obtained by configuring the relevant parameters of the software library and calling the function. You can refer to the touch button software library of Texas Instruments for a detailed introduction.
When the user slides on the wheel, the LED track display should be N lights lit up at the same time. The light where the finger is located is the brightest, and the lights on the track that has been slid before are dimmer. The value of N is determined by the operator's sliding speed. If the sliding speed is fast enough, 24 LED lights will be lit up at the same time, but the brightness will be different.
When sliding very fast, there will be a problem that the scanning cycle of the capacitive button cannot keep up with the sliding speed, resulting in discontinuous changes in the coordinates. As a result, the LED trajectory is discontinuous, and some of the N consecutive LEDs are not lit. In order to solve this problem, an interpolation algorithm needs to be added after the wheel coordinate calculation to supplement the missed coordinates when the user operates too quickly, so that the LED trajectory is continuous.
The interpolation method can decide whether to perform interpolation by comparing the current position with the previous position. Here, an interpolation threshold InterpolationThreshold needs to be set. When the position jump distance exceeds the threshold, interpolation will not be performed, otherwise erroneous operation will occur.
if((WheelPosition-LastPosition)< InterpolationThreshold)
{
for (j=1;j<=(WheelPosition-LastPosition+1);j++)
{
SetLightLevel(LastPosition+j);
}
}
In addition, two special cases need to be handled, namely the forward and reverse sliding operations passing through the wheel coordinate 0 point.
4. Summary
This article introduces the use of MSP430G series single chip to achieve capacitive touch wheel and 24 independent PWM output LED control solutions, which is of great use value in some situations where low-cost product design and multiple LED special effects control are required.
Reference Documentation
1. MSP430x2xx Family User's Guide (SLAU144H)
2. MSP430_SLAS800_G2x55
3. Capacitive Touch Software Library Programmer's Guide (Rev. A) (slaa490a)
|