_Programming the Atmel AT89C2051_
by Dhananjay V. Gadre

Listing One
1 REM Programmer for ATMEL AT89C2051
2 Print "Atmel AT89C2051 Programmer"
3 Print "  Accepts IntelHex files  "
4 Print "    Dhananjay V. Gadre    " 
5 Print "            1996          "
8 MTOP=41FFH
10 A=GET : IF A=0 THEN 10
20 IF A<> 58 THEN 10
30 PRINT CHR(A),
35 CSUM=0
40 GOSUB 1000
50 LL=A : IF A=0 THEN 230
55 CSUM=CSUM+LL
60 GOSUB 1000
70 ADDR=256*A
75 CSUM=CSUM+A
80 GOSUB 1000
90 ADDR=A+ADDR: IF ADDR > 1FFFH THEN GOTO 4000
100 CSUM=CSUM+A
110 GOSUB 1000: IF A<> 0 THEN 230
130 FOR T=1 TO LL
140 GOSUB 1000
150 CSUM=CSUM+A
160 XBY(ADDR + 4200H)=A 
165 D=CBY(ADDR + 4200H): IF A<>D THEN PRINT "ERROR" 
167 IF A<>D THEN GOTO 3000
170 ADDR=ADDR+1
180 NEXT T
190 GOSUB 1000
200 CSUM=CSUM+A : CSUM=CSUM .AND. 00FFH
210 IF CSUM<>0 THEN 3000
220 PRINT :  GOTO 10
230 GOTO 2000
1000 A=GET: IF A=0 THEN 1000
1010 PRINT CHR(A),:IF A<58 THEN A=A-48: GOTO 1030
1020 A=A-55
1030 C=A
1040 A=GET: IF A=0 THEN 1040
1050 PRINT CHR(A),:IF A<58 THEN A=A-48: GOTO 1070
1060 A=A-55
1070 A=16*C+A
1080 RETURN
2000 MXAD=8001H: EXAD=8000H
2010 XBY(4C00H)=00C2H: XBY(4C01H)=00B2H: XBY(4C02H)=0022H
2020 XBY(4C03H)=00D2H: XBY(4C04H)=00B2H: XBY(4C05H)=0022H
2030 XBY(4C06H)=00C2H: XBY(4C07H)=00B3H: XBY(4C08H)=0022H
2040 XBY(4C09H)=00D2H: XBY(4C0aH)=00B3H: XBY(4C0bH)=0022H
2050 XBY(4C0cH)=00C2H: XBY(4C0dH)=00B4H: XBY(4C0eH)=0022H
2060 XBY(4C0fH)=00D2H: XBY(4C10H)=00B4H: XBY(4C11H)=0022H
2070 XBY(4C12H)=00C2H: XBY(4C13H)=00B5H: XBY(4C14H)=0022H
2080 XBY(4C15H)=00D2H: XBY(4C16H)=00B5H: XBY(4C17H)=0022H
2090 PRINT "Erasing AT89C2051...": CLOCK1: PRINT TIME
2100 rem RST=H, P3.2=H
2110 EX=000Ah: XBY(EXAD)=EX: call 4c06h: call 4c0ch
2115 REM Wait 10 ms 
2120 for t=0 to 5: next t
2125 REM Set P3 for Erase 
2130 MX=0004h: xby(MAXD)=MX: call 4c00h: FOR T=0 TO 5 : NEXT T
2135 REM Set RST to 12V
2140 EX= EX .AND. 00FDH: XBY(EXAD)=EX: FOR T=0 TO 5: NEXT T
2145 REM Set P3.2 to 0 
2150 EX=EX .AND. 00F7H: XBY(EXAD)=EX: FOR T=0 TO 5 : NEXT T
2155 REM Set P3.2 to 1
2160 EX=EX .OR. 0008H: XBY(EXAD)=EX
2165 REM Put RST to 5V 
2170 EX=EX .OR. 0002H: XBY(EXAD)=EX
2180 PRINT "AT89C2051 Erased..."
2190 PRINT "Writing and Verifying..."
2200 FOR COUNT=0 TO 07FFH
2210 VAL1=XBY(4200H+COUNT)
2215 REM Set P3 to WRITE data 
2220 MX=0038H: XBY(MXAD)=MX
2225 REM Put data on P1 
2230 PORT1=VAL1: CALL 4C12H
2235 REM Put RST to 12V
2240 EX=EX .AND. 00F9H: XBY(EXAD)=EX 
2245 REM Put P3.2 to 0
2250 EX=EX .AND. 00F7H: XBY(EXAD)=EX
2260 FOR T=0 TO 5: NEXT T
2265 REM Put P3.2 to 1
2270 EX=EX .OR. 0008H: XBY(EXAD)=EX
2275 REM Disable data from source
2280 CALL 4C15H
2282 EX=EX .OR. 0002H: XBY(EXAD)=EX
2285 REM Set P3 to read data
2290 MX=30H: XBY(MXAD)=MX
2295 REM Read data and compare
2300 port1=255: TEMP=PORT1: IF TEMP <> XBY(4200H+COUNT) THEN GOTO 5000
2305 EX=EX .OR. 1: XBY(EXAD)=EX
2307 EX=EX .AND. 00FEH: XBY(EXAD)=EX
2310 NEXT COUNT
2320 PRINT "Chip Programmed Successfully..."
2330 GOTO 5000
3000 REM Error in HEX file
3010 PRINT "Error in Hex File. Aborting...": END
4000 REM File not suitable for AT89C2051
4010 PRINT "Hex file not suitable for AT89C2051... Aborting": END
5000 print "COUNT=", count : print temp
5005 CALL 4C03H : CALL 4C09H: CALL 4C0FH: CALL 4C15H
5010 PRINT "Turn Power OFF...": PRINT TIME: CLOCK0
5020 END


Listing Two
CPU 8051
ORG 0000
            mov a, #0; clear accumulator
for_ever:   mov p1, a; transfer to Port1 
            inc a
            ljmp for_ever; loop for ever.
end


Listing Three
#include <stdio.h>
#include <dos.h>
#include <conio.h>
#include <process.h>
#include<time.h>

void main(void)
{
int dport_lpt1, cport_lpt1, sport_lpt1, del;
unsigned char adc_status, adc_val, cport, chan_num;
unsigned char nib_0, nib_1, nib_2, nib_3, temp, nibble[5];

/* Sign ON */
clrscr();
printf("MAX186 ADC interface for printer adapter using AT89C2051");
printf("\nD.V.GADRE");

/*Get LPT1 port addresses */
dport_lpt1 = peek(0x40,0x08);
if(dport_lpt1 ==0)
    {
    printf("\n\n\nLPT! not available... aborting\n\n\n");
    exit(1);
    }
printf("\nLPT1 address = %X", dport_lpt1);
cport_lpt1 = dport_lpt1 +2;     /* control port address */
sport_lpt1 = dport_lpt1 + 1;    /* status port address */

/*initialize the ADC strobe signals*/
cport=04;
outportb(cport_lpt1, cport);

/*clear the status of controller*/
/*this generates a low going pulse on C1* pin of the control port*/
cport=cport | 02;
outport(cport_lpt1, cport);
cport=cport & 0xfd;
outport(cport_lpt1, cport);

/* check if ADC is connected & working*/
adc_status=inportb(sport_lpt1);
adc_status=adc_status & 0x08;

if(adc_status == 0) printf("\nADC connected\n");
else {printf("\nADC not connected...Aborting"); exit(1);}

chan_num=0; /*set channel number*/
for(;;)
{
/*set channel number to the controller*/
outportb(dport_lpt1, chan_num);

/*trigger the controller to read the channel number*/
cport=cport | 01;
outportb(cport_lpt1, cport);

/*wait till it reads the channel number and completes conversion*/
adc_status=inportb(sport_lpt1);
adc_status=adc_status & 0x08;
while(adc_status ==0)
{
adc_status=inportb(sport_lpt1);
adc_status=adc_status & 0x08;
}
/*remove the trigger pulse. take it igh to remove the trigger*/
cport=cport & 0xfe;
outportb(cport_lpt1, cport);

/*clear the status of controller*/
cport=cport | 02;

outport(cport_lpt1, cport);
cport=cport & 0xfd;
outport(cport_lpt1, cport);

/*noew read the converted data*/
for(temp=8; temp<11; temp++)
    {
/*set data port to read nibble 0, 1 and 2 in that order*/
        outportb(dport_lpt1, temp);

/*trigger the converter to perform the read nibble process*/
            cport=cport | 01;
            outportb(cport_lpt1, cport);
/*wait till it completes the task*/
        adc_status=inportb(sport_lpt1);
        adc_status=adc_status & 0x08;
        while(adc_status ==0)
        {
        adc_status=inportb(sport_lpt1);
        adc_status=adc_status & 0x08;
        }
/*remove the trigger pulse*/
            cport=cport & 0xfe;
            outportb(cport_lpt1, cport);
/*clear the status of controller*/
cport=cport | 02;
outport(cport_lpt1, cport);
cport=cport & 0xfd;
outport(cport_lpt1, cport);

/*read the nibble and store it temporarily*/
    adc_val=inportb(sport_lpt1);
    nibble[temp-8]=(adc_val ^ 0x80) >> 4;
    }
/*construct the full 12 bit number from the stored nibbles*/
    printf("\n %d mV", nibble[0] + 16*nibble[1] + 256*nibble[2]);
sleep(1); /*sleep for 1 sec*/
  }
}



