English
Language : 

MG82FX564AE Datasheet, PDF (107/151 Pages) Megawin Technology Co., Ltd – Dual data pointer
17.2.2. Starting a Conversion
Prior to using the ADC function, the user should:
1) Turn on the ADC hardware by setting the ADCEN bit,
2) Configure the conversion speed by bits SPEED1 and SPEED0,
3) Select the analog input channel by bits CHS2, CHS1 and CHS0,
4) Configure the selected input (shared with P1) to the Input-Only mode by P1M0 and P1M1 registers, and
5) Configure ADC result arrangement using ADRJ bit.
Now, user can set the ADCS bit to start the A-to-D conversion. The conversion time is controlled by bits SPEED1
and SPEED0. Once the conversion is completed, the hardware will automatically clear the ADCS bit, set the
interrupt flag ADCI and load the 10 bits of conversion result into ADCH and ADCL (according to ADRJ bit)
simultaneously.
As described above, the interrupt flag ADCI, when set by hardware, shows a completed conversion. Thus two
ways may be used to check if the conversion is completed: (1) Always polling the interrupt flag ADCI by software;
(2) Enable the ADC interrupt by setting bits EADC (in EXIE1 register) and EA (in IE register), and then the CPU
will jump into its Interrupt Service Routine when the conversion is completed. Regardless of (1) or (2), the ADCI
flag should be cleared by software before next conversion.
17.2.3. Sample Code for ADC
start:
;...
MOV ADCON,#0E2h ;ADCEN=1, turn on ADC hardware
;(SPEED1,SPEED0)=(1,1), Conv. Time= 60 clock cycles
;select AIN0 (P1.2) as analog input
ORL P1M0,#00000100B ;P1M0,bit2=1 ;configure P1.2 as Input-Only Mode
ANL P1M1,#11111011B ;P1M1,bit2=0 ;
ANL AUXR0,#11111011B ;ADRJ=0: ADCH contains B9~B2; ADCL contains B1,B0
;now, suppose the analog input is ready on AIN2 (P1.2)
ORL ADCON,#00001000B ;ADCS=1 Start A-to-D conversion
wait_loop:
MOV ACC,ADCON
JNB ACC.4,wait_loop ;wait until ADCI=1 conversion completed
;now, the 10-bit ADC result is in the ADCH and ADCL.
;...
;...
17.2.4. ADC Conversion Time
The user can select the appropriate conversion speed according to the frequency of the analog input signal. For
example, if SYSCLK =12MHz and a conversion speed of 60 clock cycles is selected, then the frequency of the
analog input should be no more than 200KHz to maintain the conversion accuracy. (Conversion time = 1/12MHz
x 60 = 5us, so the conversion speed = 1/5us = 200KHz.)
17.2.5. I/O Pins Used with ADC Function
The analog input pins used for the A/D converters also have its I/O port ‗s digital input and output function. In
order to give the proper analog performance, a pin that is being used with the ADC should have its digital output
as disabled. It is done by putting the port pin into the input-only mode as described in the Port Configurations
section.
17.2.6. Idle and Power-Down Mode
MEGAWIN
MG82FEL564 Data Sheet
107