; Test the Analog-to-Digital Converter, ; connected on address line WFE0xh, ; !INTR being connected to !INT0 (P3.2). org 00h ljmp start org 100h start: mov dptr, #0FE00h ; Initialize AD mov a, #0 movx @dptr, a mov dptr, #0FE00h movx a, @dptr lcall init ; Initialize serial port loop: lcall getchr ; Wait for any character lcall readadc ; Read from A/D lcall prthex ; Print result lcall crlf sjmp loop readadc: ; Reads the value of the A/D and stores it in the accumulator. mov dptr, #0FE00h ; Start conversion mov a, #0 movx @dptr, a wait: ; Wait for conversion to complete. jb P3.2, wait mov dptr, #0FE00h ; Read digital value. movx a, @dptr ret init: ; set up serial port with a 11.0592 Mhz crystal ; use timer 1 for 9600 baud serial communications mov tmod, #20h ; set timer 1 for auto reload - mode 2 mov tcon, #41h ; run counter 1 mov th1, #0fdh ; set 9600 baud with xtal=11.059mhz mov scon, #50h ; set serial control reg for 8 bit data ; and mode 1 ret getchr: ; This routine "gets" or receives a character from the PC, transmitted over ; the serial port. RI is the same as SCON.0 - the assembler recognizes ; either shorthand. The 7-bit ASCII code is returned in the accumulator. jnb ri, getchr ; wait till character received mov a, sbuf ; get character and put it in accumulator anl a, #7fh ; mask off 8th bit clr ri ; clear serial "receive status" flag ret sndchr: ; This routine "sends" or transmits a character to the PC, using the serial ; port. The character to be sent is stored in the accumulator. SCON.1 and ; TI are the same as far as the assembler is concerned. clr scon.1 ; clear the ti complete flag. mov sbuf, a ; move a character from acc to the sbuf txloop: jnb scon.1, txloop ; wait till chr is sent ret ;=============================================================== ; subroutine prthex ; this routine takes the contents of the acc and prints it out ; as a 2 digit ascii hex number. ;=============================================================== prthex: push acc lcall binasc ; convert acc to ascii lcall sndchr ; print first ascii hex digit mov a, r2 ; get second ascii hex digit lcall sndchr ; print it pop acc ret ;=============================================================== ; subroutine binasc ; binasc takes the contents of the accumulator and converts it ; into two ascii hex numbers. the result is returned in the ; accumulator and r2. ;=============================================================== binasc: mov r2, a ; save in r2 anl a, #0fh ; convert least sig digit. add a, #0f6h ; adjust it jnc noadj1 ; if a-f then readjust add a, #07h noadj1: add a, #3ah ; make ascii xch a, r2 ; put result in reg 2 swap a ; convert most sig digit anl a, #0fh ; look at least sig half of acc add a, #0f6h ; adjust it jnc noadj2 ; if a-f then re-adjust add a, #07h noadj2: add a, #3ah ; make ascii ret ;=============================================================== ; subroutine crlf ; crlf sends a carriage return line feed out the serial port ;=============================================================== crlf: mov a, #0ah ; print lf lcall sndchr cret: mov a, #0dh ; print cr lcall sndchr ret