MOV P0M0, #00H
MOV P0M1, #00H
MOV P1M0, #00H
MOV P1M1, #00H
MOV P2M0, #00H
MOV P2M1, #00H
MOV P3M0, #00H
MOV P3M1, #00H
MOV P4M0, #00H
MOV P4M1, #00H
MOV P5M0, #00H
MOV P5M1, #00H
MOV P1M0,#00H ;Set P1.0 as ADC input
MOV P1M1,#01H
MOV P_SW2,#80H
MOV DPTR,#ADCTIM ; Set ADC internal timing
MOV A,#3FH
MOVX @DPTR,A
MOV P_SW2,#00H
MOV ADCCFG,#0FH ;Set the ADC clock to the system clock/2/16
MOV ADC_CONTR,#80H ;Enable ADC module
ORL ADC_CONTR,#40H ;Start AD conversion
NOP
NOP
MOV A,ADC_CONTR ;Query ADC completion flag
JNB ACC.5,$-2
ANL ADC_CONTR,#NOT 20H ;Clear completion flag
MOV ADCCFG,#00H ;Set result to align left
MOV A,ADC_RES ;A stores the upper 8 bits of the ADC
’
s 10-bit result
MOV B,ADC_RESL ;B[7: 6] stores the lower 2 bits of the 10-bit ADC result, B [5: 0] is 0
; MOV ADCCFG,#20H ;Set result to align right
; MOV A,ADC_RES ;A [1: 0] stores the upper 2 bits of the 10-bit result of the ADC, and A [7: 2] is 0
; MOV B,ADC_RESL ;B stores the lower 8 bits of the ADC’s 10-bit result
SJMP $
END
17.5.4 ADC converts multiple times to take average automatically
C language code
//Operating frequency for test is 11.0592MHz
#include "reg51.h"
#include "intrins.h"
sfr ADC_CONTR = 0xbc;
sfr ADC_RES = 0xbd;
sfr ADC_RESL = 0xbe;
sfr ADCCFG = 0xde;