You are on page 1of 3

Assembly Language Program: Example of A/D Conversion

;***********************************************************************
; File name: ADC.ASM
; Date: 9/5/97
;***********************************************************************
; This module performs the A/D converter function in the K0 family.
; The conversion method is based on successive approximation and the
; conversion result is held in the 8-bit A/D conversion result register (ADCR).
;=======================================================================
; - A/D CONVERSION OPERATION IN SOFTWARE START
;
; . Set an A/D channel for A/D conversion.
; . Set a conversion time.
; . Start conversion.
; . Wait for the A/D channel conversion completed.
; . Read the conversion result.
; . Reset the interrupt request flag.
; . Return with the result in the A register.
;
;=======================================================================
; Input voltage and result:
;
; ADCR (result register) = INT [(Vin/Vref) x 256 + 0.5]
;
; Where,
; INT = integer parts of the result
; Vin = analog input voltage
; Vref = AVref0 pin voltage (reference voltage)
; ADCR = the conversion result register
;=======================================================================
;
; Module name: ReadADdata
;
; Input:
; A = channel # (0 to 7)
;
; Return:
; A = The A/D result of the given channel
;
;***********************************************************************
;
; for only DDB-K0070A
;
$set (DDB)
;======================================================================
; Port assignment for A/D
;======================================================================
ADDirPort equ PM1
;======================================================================
;
; Select 100/fx (20 s) if fx = 5.00 MHz
; fr1/fr0/hsc = 101b and Start conversion bit on
;
ConversionTime equ 00100001b
;======================================================================
;
; Internal high RAM allocation
; FWA = 0fb00h
;======================================================================
dseg IHRAM
ds 20h
Stack:
$ej
;======================================================================
; Reset vector
;======================================================================
Vector cseg at 0000h
dw ResetStart
; interrupt vector if any
org 20h
; dw
$ej
;======================================================================
; Main
;======================================================================

Main cseg at 80h


ResetStart:
di ;Disable interrupt
;
; Select register bank 0
;
sel RB0 ;Select register bank 0
;
; Set stack pointer (IHMEM + 20H)
;
movw SP,#Stack ;Stack ptr
;
; Select Master clock (4.19 MHz)
; . MCS = 1, no divided clock
;
mov OSMS,#00000001b
;
; Enable INTP0 for DDB-K0070A
;
$if (DDB)
clr1 PMK0
$endif
;
; Init. ADC register
;
call !InitADC
;
; 16 step pwm output
;
loop:
mov a,#0 ;Channel # 0
call !ReadADdata
br loop
;=======================================================================
;
; Module name: ReadADdata
;
; Input:
; A = channel # (0 to 7)
; Return:
; A = The A/D result of the given channel
;
;=======================================================================
ReadADdata:
; Set A/D channel
add a,a ;position to bit 1
; merge with conversion time and start A/D conversion
or a,#ConversionTime
; Clear interrupt flag
; Start conversion
mov ADM,a
clr1 ADIF
set1 ADM.7
;
; Wait for conversion done
;
wait: bf ADIF,$wait
;
; Read the conversion result
;
mov a,ADCR +
; Stop conversion
clr1 ADM.7
ret
;***********************************************************************
; ADC init
; . Set conversion time
; . Set # of A/D channels
;***********************************************************************
InitADC:
; Set memory expansion mode register
mov ADDirPort,#11111111b
;
; A/D conversion mode register
;

mov ADM,#ConversionTime
;
; A/D input selection (select all 8 channels)
;
mov ADIS,#00001000b
ret
;***********************************************************************
End

You might also like