prog51b

  Because the prototyping shield is only having a 17 x 10 prototyping holes, a pin grabber has been used to connect the target MCU to the power supply rail.
 

   schematic

 

/*
Arduino Serial Mode Programmer for AT89S51/52 MCS-51 Microcontroller from Atmel
Program written by Nick Pablo for DEP Mechatronics (formerly NIPA Electronic Innovations)
http://www.nipahut.com.ph
This sketch has been tested to work smoothly with the host program Prog51 (v1.0) which is
available at our download page.
*/

#define dummyData 0xAA
#define RDY 75
#define NRDY 76

const int MISO = 11;
const int MOSI = 12;
const int CLK = 10;
const int RST = 13;
const char * prodID = "DEPM01";
const byte version = 0x1;

/* Variable definition block */

byte data;
byte AL,AH; // 16-bit address
byte lockByte; // embed lock bits here
byte SigH,SigL; // Signature Bytes

void setup()
{
pinMode(MISO, INPUT);
pinMode(MOSI, OUTPUT);
pinMode(CLK, OUTPUT);
pinMode(RST, OUTPUT);
Serial.begin(115200); // depends on the setting of the host PC

}
/* You will use this part only for the S5x series */
unsigned char spi_send( unsigned char data)
{ // it is the responsibility of the calling routine to assert CS pin first
unsigned char retVal = 0;
unsigned char intData = data;
int t;
// 7 Most significant bits
for (int itr = 0; itr < 7; itr++)
{ if (intData & 0x80) digitalWrite(MOSI,1);
else digitalWrite(MOSI,0);
digitalWrite(CLK,1); // CLK High for 1us
delayMicroseconds(1); //must be 1/16 of the MCU crystal or XTAL1 clock input.

//digitalWrite(CLK,0);
t = digitalRead(MISO); // read serial out data
digitalWrite(CLK,0);
if (t) retVal |= 1; else retVal &= 0xFE;
retVal<<=1;
intData<<= 1;
delayMicroseconds(1); // CLK Low for 1us
}
// last bit
if (intData & 0x80) digitalWrite(MOSI,1);
else digitalWrite(MOSI,0);
digitalWrite(CLK,1);
delayMicroseconds(1); // around xxkhz clock speed.

t = digitalRead(MISO);
digitalWrite(CLK,0);
if (t) retVal |= 1; else retVal &= 0xFE;

return retVal; // if you just want to send, discard this return value

}

byte enableProg_S()
{ spi_send(0xAC);
spi_send(0x53);
spi_send(dummyData);
return spi_send(dummyData);
}
void eraseChip_S()
{spi_send(0xAC);
spi_send(0x9F);
spi_send(dummyData);
spi_send(dummyData);
delay(520);
}
void readProgmem_S()
{// the variable data will be updated after this procedure
spi_send(0x20);
spi_send(AH);
spi_send(AL);
data = spi_send(dummyData);
}
void writeProgmem_S()
{
spi_send(0x40);
spi_send(AH);
spi_send(AL);
spi_send(data);
}
void writeLockBits_S()
{
spi_send(0xAC);
spi_send(lockByte);
spi_send(dummyData);
spi_send(dummyData);
}
void readLockBits_S()
{ // lockByte will be updated by this procedure
spi_send(0x24);
spi_send(dummyData);
spi_send(dummyData);
lockByte = spi_send(dummyData);
}
byte readSign_S()
{
spi_send(0x28);
spi_send(AH);
spi_send(AL);
return spi_send(dummyData);
}


void loop()
{ while (!Serial.available()); // wait for character
if (Serial.available() > 0)
switch (Serial.read())
{
case 'p': Serial.write(enableProg_S());
          break; // programming enable
case 'r': readProgmem_S(); // read data pointed by AH-AL pair
          Serial.write(data);
          break;
case 'a': while(!Serial.available()); // wait for character
          AL = Serial.read();
          break; // set AL
case 'A': while(!Serial.available()); // wait for character
          AH = Serial.read();
          break; // set AH
case 'd': while(!Serial.available()); // wait for character
          data = Serial.read();
          break; // set data
case 'S': AH = 0;
          AL = 0;
          SigH = readSign_S();
          Serial.write(SigH);
          break; // read SigH
case 's': AH = 2;
          AL = 0;
          SigL = readSign_S();
          Serial.write(SigL);
          AH = 1;
          AL = 0;
          SigL = readSign_S();
          Serial.write(SigL);
          break; // read SigL
case 'L': readLockBits_S();
          Serial.write(lockByte);
          break; // read Lockbits
case 'l': while (!Serial.available()); // wait for lockbit codes
          lockByte = Serial.read();
          writeLockBits_S();
          break; // write Lockbits
case 'o': digitalWrite(RST,1);break; // open software for programming
case 'c': digitalWrite(RST,0);break; // close software for exit
case 'e': eraseChip_S();
          Serial.write(RDY);
          break; // erase chip
case 'f': Serial.println(prodID);break; // firmware ID; must send back the progID;
case 'v': Serial.print(version,BYTE);break; // firmware version number
case 'j': break;
case 'w': writeProgmem_S();
          break;
}



}