Sometimes, a microcontroller-based product requires the use of a rotary switch. Since many microcontrollers have built-in ADCs, a low-cost potentiometer can be used as an alternative to a rotary switch when rotary switches are unavailable or too expensive (Figure 1).
Although it only takes a few instructions to digitize a potentiometer setting and make it behave like a switch, one immediate problem is that the switching threshold between one value and the next can become unstable due to electrical or mechanical noise. The solution to this problem is to introduce an upper and lower hysteresis threshold for each transition, so that the potentiometer needs to cross a threshold before the next switch state is valid. For each updated switch state, a new pair of thresholds replaces the previous one. In this way, the hysteresis effect allows for a clean transition between states.
Figure 1: Replacement for a multi-throw switch
This approach has many advantages: single port pin versus multiple port pins for a rotary switch, low cost, greater availability, and debounced switching. The disadvantage is that detent feel is lost. Another feature of the set point is that it can be set to any position, for example to compensate for nonlinear changes in the response of a potentiometer.
The hysteresis is usually slightly higher than any noise that would cause unwanted switching. It is recommended to place a capacitor between the potentiometer contacts and ground to filter out contact noise (Figure 1).
Figure 2 shows the algorithm. Once the potentiometer setting is digitized by the ADC, it is compared to the lower threshold and if it is below the lower threshold, the switch state is gradually reduced and limited to zero. If the potentiometer setting is above the upper threshold, the switch state is gradually increased and limited to the maximum value. If the switch state changes, the upper and lower thresholds are updated and the subroutine is terminated.
Figure 2: Flowchart
To ensure that this hysteresis algorithm works, the potentiometer setting must be read periodically and compared to the last switch state. This is done to distinguish between potentiometer settings that cross a threshold or are at the same value from different states.
It is also necessary to calculate the minimum sampling rate, which can be obtained by dividing the maximum potentiometer rotation rate by the number of switch states. For example, assuming that a single-turn potentiometer knob rotates one full circle in 0.25 seconds and assuming there are seven states, the minimum scanning rate is 28Hz. If the potentiometer value sampling period is lower than the minimum value, the calculated switch state may be incorrect even if the switching direction is correct. If the potentiometer setting value is not continuously changed at a faster rate, subsequent samples will correct the switching state.
Let's create a threshold list for seven switch states. Assume there is an 8-bit ADC. First, the 256-step range of the ADC is divided into seven switch states. The width of each switch state is the ADC range divided by the number of states, which is: 256/7=36.6. Rounding it off, the width of each state is 36, but the two outer states need to be increased to 38 to make the total width 256.
The next step is to determine the boundaries of each switch state. For state 0, the boundaries are 0 to 37 (inclusive). State 1 starts at 38 and ends at 73, and so on for the remaining switch states. The thresholds are determined based on the hysteresis values that increase or decrease to the boundaries. Here, a hysteresis value of 4 is used. The hysteresis amount must not be greater than the width or less than the expected noise. Therefore, the upper threshold is obtained by adding 4 to the upper boundary and the lower threshold is obtained by subtracting 4 from the lower boundary, as shown in Table 1. From this example, it can be seen that switching from state 2 to state 1 requires the potentiometer count to drop to 4 less than the switching point value of 74, so the lower threshold is 70. Conversely, switching from state 1 to state 2 requires the potentiometer count to rise to 4 more than the switching point value of 73, so the upper threshold is 77. The table used for the program code only needs to indicate the upper and lower thresholds, which only requires 14 bytes in this example.
Table 1 Threshold
The code examples (see below) support the Silicon Labs C8051F310 (8051 architecture), but can be easily adapted for other microcontrollers.
;POT2SW INITIALIZATION
MOV UPRVAL, #00H;set upper value to opposite end to force the code to run
MOV LWRVAL, #0FFH;set lower value to opposite end to force the code to run
MOV SWPOS, #03H;initialize switch position to middle
MOV POSMAX, #06H;set maximum SWITCH position value
;SUBROUTINES
POT2SW:;CALCULATE SWITCH POSITION VALUE FROM POTENTIOMETER VALUE IN ACC
;check if pot setting is below lower threshold
CLR C
MOV B, A ;save pot setting to register B
SUBB A, LWRVAL;potval - lwrval
JNC P2S1 ;no carry means potval >= lwrval
DEC SWPOS ;carry means potval < lwrval, so decrement switch position value
;check if switch position is < zero
MOV A, POSMAX;load maximum switch position value
CLR C
SUBB A, SWPOS;max switch value - switch position
JNC P2S2
MOV SWPOS, #00H;reset switch position value to zero since underflow
SJMP P2S2
P2S1: ;check if pot setting is above upper theshold
CLR C
MOV A, UPRVAL
SUBB A, B ;uprval - potval
JNC P2S2 ;no carry means potval <= uprval
INC SWPOS ;carry means potval >uprval, so increment switch position value
;check if switch position is > max
MOV A, POSMAX;load maximum xwitch position value
CLR C
SUBB A, SWPOS
JNC P2S2
MOV SWPOS, POSMAX;reset curve number to max curve value since overflow
P2S2: ;read lower and upper thresholds using switch position value
MOV A, SWPOS ;multiply switch position value by 2
MOV B, #02H
I have AB
MOV B, A ;save multiplied value as table offset
MOV DPTR, #HYSTBL;load base address of table pointer
MOVC A, @A+DPTR;look up table value from base address + offset
MOV LWRVAL, A;read lower threshold value
MOV A, B
INC DPTR ;increment base address
MOVC A, @A+DPTR
MOV UPRVAL, A;read upper threshold value
RIGHT
HYSTBL:;TABLE OF LOWER & UPPER THRESHOLDS FOR SEVEN POSITION SWITCH
DB00D,41D;Switch state 0
DB34D,77D;Switch state 1
DB70D,113D;Switch state 2
DB106D,149D;Switch state 3
DB142D,185D;Switch state 4
DB178D,221D;Switch state 5
DB214D,255D;Switch state 6
Previous article:Discussion on the drying method and issues to be aware of for transformers
Next article:Implementation of a four-quadrant DC/DC switching regulator
- MathWorks and NXP Collaborate to Launch Model-Based Design Toolbox for Battery Management Systems
- STMicroelectronics' advanced galvanically isolated gate driver STGAP3S provides flexible protection for IGBTs and SiC MOSFETs
- New diaphragm-free solid-state lithium battery technology is launched: the distance between the positive and negative electrodes is less than 0.000001 meters
- [“Source” Observe the Autumn Series] Application and testing of the next generation of semiconductor gallium oxide device photodetectors
- 采用自主设计封装,绝缘电阻显著提高!ROHM开发出更高电压xEV系统的SiC肖特基势垒二极管
- Will GaN replace SiC? PI's disruptive 1700V InnoMux2 is here to demonstrate
- From Isolation to the Third and a Half Generation: Understanding Naxinwei's Gate Driver IC in One Article
- The appeal of 48 V technology: importance, benefits and key factors in system-level applications
- Important breakthrough in recycling of used lithium-ion batteries
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- 【MM32 eMiniBoard Review】2a. Found that the virtual serial port may have problems, and the serial port routines cannot receive
- Help! Does anyone know what sensor this is?
- Do you usually read e-books or paper books now?
- ADC12 single channel multiple conversion routine (MSP430F5529)
- Motor startup interferes with the microcontroller problem
- Chapter 3 Interrupts, Clocks, and Low Power Consumption
- The difference between C64x+ and C64x CACHE in the C6000 series
- Realization of voltage to current conversion in Multisim12
- 【Smart Network Desk Lamp】7. Get real-time weather information and analyze it
- [RISC-V MCU CH32V103 Review] +01 Try the use of GPIO to light up the first LED