使用電位計替代旋轉(zhuǎn)開關(guān)
以七種開關(guān)狀態(tài)為例創(chuàng)建一個閾值列表。假設(shè)有一個8位ADC。首先,ADC的256步范圍被分割為七種開關(guān)狀態(tài)。各開關(guān)狀態(tài)的寬度為ADC范圍除以狀態(tài)數(shù),即:256/7=36.6。對其四舍五入,將各狀態(tài)的寬度定為36,但是兩個外端狀態(tài)需增加至38,以使總寬度為256。
下一步是確定各開關(guān)狀態(tài)的邊界。對于狀態(tài)0,邊界為0~37(包含兩端)。狀態(tài)1從38開始到73結(jié)束,其余開關(guān)狀態(tài)依此類推。根據(jù)增加或降低至邊界的遲滯數(shù)值確定閾值。此處使用“4”這一遲滯值。遲滯量既不得大于寬度,也不得低于預(yù)期的噪聲。因此,上邊界加4即可得出上閾值,下邊界減4即可得出下閾值,如表1所示。從該例可發(fā)現(xiàn),從狀態(tài)2切換為狀態(tài)1需要使電位計數(shù)值下降至比切換點數(shù)值74小4,因此下閾值為70。相反,從狀態(tài)1切換為狀態(tài)2需要使電位計數(shù)值上升至比切換點數(shù)值73高4,因此上限閾值為77。用于程序代碼的表格僅需標(biāo)明上、下閾值,在此例中僅需14個字節(jié)。
表1,閾值。
代碼示例(見下)支持Silicon Labs的C8051F310(8051架構(gòu)),但也可以很容易地改編用于其他微控制器。
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 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 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
評論