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 rotary switches when they are unavailable or too expensive (Figure 1).
Although it only takes a few instructions to digitize the potentiometer setpoint so that it behaves like a switch, one immediate problem is that the value at the switching threshold between one value and the next will be unstable due to electrical or mechanical noise. This problem is solved by introducing upper and lower hysteresis thresholds 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 thresholds. In this way, the hysteresis effect allows a clean switch between states.
Figure 1: Alternative to 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 of this approach is that it loses detent feel. Another feature of the setpoint is that it can be set to any position, for example to compensate for nonlinear changes in the potentiometer's response.
The hysteresis is usually slightly higher than any noise that would cause undesired switching. It is recommended to place a capacitor between the potentiometer contacts and ground to filter contact noise (Figure 1).
Figure 2 outlines the algorithm. Once the potentiometer setting is digitized by the ADC, the value is compared to the lower threshold. 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 the 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 the threshold from different states or are at the same value from the same state.
Here, 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 a single-turn potentiometer knob that rotates one full turn 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 is not continuously changed at a fast rate, the subsequent sampling will correct the switching state. [page]
Create a threshold list for seven switching states. Assume an 8-bit ADC. First, the 256-step range of the ADC is divided into seven switching states. The width of each switching state is the ADC range divided by the number of states, that is: 256/7=36.6. This is rounded to 36 for each state, 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 switching 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 switching states. The threshold is determined based on the hysteresis value added or reduced to the boundary. 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 boundary plus 4 gives the upper threshold, and the lower boundary minus 4 gives the lower threshold, as shown in Table 1. From this example, it can be seen that switching from state 2 to state 1 requires the potentiometer to fall by 4 less than the switch point value of 74, so the lower threshold is 70. Conversely, switching from state 1 to state 2 requires the potentiometer to rise by 4 more than the switch 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, Thresholds.
The code examples (see below) support the Silicon Labs C8051F310 (8051 architecture), but can be easily adapted for other microcontrollers.
OT2SW 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 V ALUE 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 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
MUL 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
RET
HYSTBL: ;TABLE OF LOWER & UPPER THRESHOLDS FOR SEVEN POSITION SWITCH
DB 00D, 41D ;Switch state 0
DB 34D, 77D ;Switch state 1
DB 70D, 113D ;Switch state 2
DB 106D, 149D;Switch state 3
DB 142D, 185D;Switch state 4
DB 178D, 221D;Switch state 5
DB 214D, 255D;Switch state 6
Reference address:Using a potentiometer instead of a rotary switch
Although it only takes a few instructions to digitize the potentiometer setpoint so that it behaves like a switch, one immediate problem is that the value at the switching threshold between one value and the next will be unstable due to electrical or mechanical noise. This problem is solved by introducing upper and lower hysteresis thresholds 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 thresholds. In this way, the hysteresis effect allows a clean switch between states.
Figure 1: Alternative to 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 of this approach is that it loses detent feel. Another feature of the setpoint is that it can be set to any position, for example to compensate for nonlinear changes in the potentiometer's response.
The hysteresis is usually slightly higher than any noise that would cause undesired switching. It is recommended to place a capacitor between the potentiometer contacts and ground to filter contact noise (Figure 1).
Figure 2 outlines the algorithm. Once the potentiometer setting is digitized by the ADC, the value is compared to the lower threshold. 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 the 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 the threshold from different states or are at the same value from the same state.
Here, 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 a single-turn potentiometer knob that rotates one full turn 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 is not continuously changed at a fast rate, the subsequent sampling will correct the switching state. [page]
Create a threshold list for seven switching states. Assume an 8-bit ADC. First, the 256-step range of the ADC is divided into seven switching states. The width of each switching state is the ADC range divided by the number of states, that is: 256/7=36.6. This is rounded to 36 for each state, 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 switching 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 switching states. The threshold is determined based on the hysteresis value added or reduced to the boundary. 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 boundary plus 4 gives the upper threshold, and the lower boundary minus 4 gives the lower threshold, as shown in Table 1. From this example, it can be seen that switching from state 2 to state 1 requires the potentiometer to fall by 4 less than the switch point value of 74, so the lower threshold is 70. Conversely, switching from state 1 to state 2 requires the potentiometer to rise by 4 more than the switch 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, Thresholds.
The code examples (see below) support the Silicon Labs C8051F310 (8051 architecture), but can be easily adapted for other microcontrollers.
OT2SW 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 V ALUE 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 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
MUL 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
RET
HYSTBL: ;TABLE OF LOWER & UPPER THRESHOLDS FOR SEVEN POSITION SWITCH
DB 00D, 41D ;Switch state 0
DB 34D, 77D ;Switch state 1
DB 70D, 113D ;Switch state 2
DB 106D, 149D;Switch state 3
DB 142D, 185D;Switch state 4
DB 178D, 221D;Switch state 5
DB 214D, 255D;Switch state 6
Previous article:Master MCU software design principles to improve DC motor control accuracy
Next article:In-depth study of MMU of bootloader under ADS
Recommended Content
Latest Microcontroller Articles
He Limin Column
Microcontroller and Embedded Systems Bible
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
MoreSelected Circuit Diagrams
MorePopular Articles
- 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
MoreDaily News
- 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!
- Rambus Launches Industry's First HBM 4 Controller IP: What Are the Technical Details Behind It?
- 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
Guess you like
- LSM6DSR iNEMO inertial module related information
- Qorvo Receives Samsung Mobile Quality Award
- Meditations on MCU——static
- A Field Guide to the Internet of Things
- Comparison of these two circuit structures
- Can the op amp inputs exceed the power supply range?
- The fight against the epidemic has made the improvement of medical electronic technology a focus of attention. Let’s take a look at this article. What do you think?
- BLDC speed control system based on GD32F350
- SDRAM controller.rar
- Sharing--Wireless Charging Bluetooth Speaker Manufacturing Process and PCB Design Appreciation