Introduction: 89S52 Programmer Using Arduino Uno

About: An Electronics engineer and a hobbyist. I love to keep experimenting with microcontrollers.

Hello everyone, today we will learn to program the 89S52 micro-controller using the arduino board. The 89S52 uC is slightly different from the 89C51 uC as it has ISP(In System Programming) feature.

It has SPI pins MOSI, MISO and SCK which will be used to upload the hex file to the uC. The original creator of this project is Nick Pablo and a huge thanks to him.

This project will be helpful for those who are willing to start with the 89S52 uC and don't want to invest on an additional programmer for the 89S52 uC.

You just need your arduino board and few additional components to make this project.

So read the entire instructable and follow all the steps and you will be able to program the 89S52 uC using your arduino board.

Step 1: Working of the Programmer

The working of this Programmer is very simple. This diy programmer uses a software called "8051 Programmer". The 8051 Programmer software comprises of few buttons which send corresponds to a character. When we press a button in the 8051 Programmer software (e.g. Identify) a character is sent to the Arduino. The Arduino code has several case statements which are executed according to the received character. This process happens within a fraction of seconds. The Arduino acts as a mediator between the software and the 89S52 uC.

You can refer the above diagram to understand the working of this project.

Step 2: Get the Necessary Supplies

I suggest you to buy the components from UtSource.net .They provide high quality components at affordable rates. And these components are delivered at your door step on time. The project we are making today can also be soldered on a printed circuit board. Check out UTSource PCB Services for PCB manufacturing.

The list of components is as follows :

89S52 uC

Arduino Uno Board

10 uF capacitor

push button

header wires

10K ohm Resistor

220 ohm Resistor

Led *1 (For Testing)

22 pF capacitor *2

11.0592 MHz Crystal Oscillator

Breadboard to mount all the components.

Step 3: Circuit Diagram

Make all the connections on the bread board as shown in the circuit diagram above.

Currently we are doing the connections on a BB but I would recommend to at least make a PCB for more simplicity.

You can make an arduino shield for this circuit for frequent use.

Connect the Rest, clock, Miso and Mosi pins of the 89S52 to the Arduino's digital pins 2, 3, 4 and 5.

Connect the push button and the capacitors as shown in the diagram.

Now connect the Xtal osc. to pin 18 and 19 respectively.

Step 4: Programming Your Arduino Board

Now compile and upload the code given below to your arduino board.

/****************************************************PROGRAMMING AN ATMEL AT89S51/52 USING ARDUINO
RELEASED AS IS WITHOUT WARRANTY 
I AM NOT LIABLE FOR ANY DAMAGE DONE TO YOUR HARDWARE
THIS PROJECT IS FOR EDUCATIONAL PURPOSES ONLY
Credits to NICK PABLO for the Arduino Sketch
TIKTAK (C) 2014*****************************************************/ #define dummyData 0xAA
#define RDY 75
#define NRDY 76
const int _MISO = 4;
const int _MOSI = 5;
const int _CLK = 3;
const int RST = 2;
int state;
/* 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
}
void loop()
{ 
  while (!Serial.available()); // wait for character
        if (Serial.available() > 0)
        switch (Serial.read())
        {
        case 'p': Serial.write(progEnable());
                  break; 
        case 'r': readProgmem(); 
                  Serial.write(data);
                  break;
        case 'a': while(!Serial.available());
                  AL = Serial.read();
                  break; 
        case 'A': while(!Serial.available()); 
                  AH = Serial.read();
                  break; 
        case 'd': while(!Serial.available()); 
                  data = Serial.read();
                  break; 
        case 'S': AH = 0;
                  AL = 0;
                  SigH = readSign();
                  Serial.write(SigH);
                  break; 
        case 's': AH = 2;
                  AL = 0;
                  SigL = readSign();
                  Serial.write(SigL);
                  AH = 1;
                  AL = 0;
                  SigL = readSign();
                  Serial.write(SigL);
                  break; // read SigL
        case 'o': digitalWrite(RST,1);break; 
        case 'c': digitalWrite(RST,0);break; 
        case 'e': eraseChip();
                  Serial.write(RDY);
                  break; 
        case 'j': break;
        case 'w': writeProgmem();
                  break;
  }
}
unsigned char SendSPI(unsigned char data)
{ 
  uint8_t retval = 0;
  uint8_t intData = data;
  int t;
      for (int ctr = 0; ctr < 7; ctr++)
      { 
         if (intData & 0x80) digitalWrite(_MOSI,1);
            else digitalWrite(_MOSI,0);digitalWrite(_CLK,1); 
        delayMicroseconds(1);  t = digitalRead(_MISO); 
        digitalWrite(_CLK,0); if (t) retval |= 1; else retval &= 0xFE;
            retval<<=1;
            intData<<= 1;
        delayMicroseconds(1); 
    }if (intData & 0x80) digitalWrite(_MOSI,1);
    else digitalWrite(_MOSI,0);
    digitalWrite(_CLK,1);
    delayMicroseconds(1); 
    t = digitalRead(_MISO);
    digitalWrite(_CLK,0);
        if (t) retval |= 1; 
    else retval &= 0xFE;
return retval; 
}
byte progEnable()
{ 
    SendSPI(0xAC);
    SendSPI(0x53);
    SendSPI(dummyData);
    return SendSPI(dummyData);
}void eraseChip()
{
     SendSPI(0xAC);
     SendSPI(0x9F);
     SendSPI(dummyData);
     SendSPI(dummyData);
     delay(520);
}
void readProgmem()
{
    SendSPI(0x20);
    SendSPI(AH);
    SendSPI(AL);
    data = SendSPI(dummyData);
}
void writeProgmem()
{
    SendSPI(0x40);
    SendSPI(AH);
    SendSPI(AL);
    SendSPI(data);
}
void writeLockBits()
{
    SendSPI(0xAC);
    SendSPI(lockByte);
    SendSPI(dummyData);
    SendSPI(dummyData);
}
void readLockBits()
{ 
    SendSPI(0x24);
    SendSPI(dummyData);
    SendSPI(dummyData);
    lockByte = SendSPI(dummyData);
}
byte readSign()
{
    SendSPI(0x28);
    SendSPI(AH);
    SendSPI(AL);
    return SendSPI(dummyData);
}

Step 5: Download the Keil Software and the 8051 Programmer

Now in order to download the keil software go to www.keil.com and click on download tab.

This will open a new screen showing various products.

Select the C51 option.Fill all the details given in the form.

Now you should be able to download the software.

Once you download the keil software you need to get one more software.

The 8051 Programmer.

You can download the 8051 Programmer Software by clicking here.

Now you are just few steps away from getting your 89S52 programmed.

Step 6: Writing Code for the 89S52 UC and the Circuit

Below is a simple led blink program to test the 89S52 uC.

You can copy the notepad code to your keil editor or else you can directly upload the hex file to your 89S52 uC.

Now do the connections as shown in the diagram above.

Connect a led with series resistor to pin P1.1 of the 89S52 uC.

Here you need to take a note that the 89S52's pins are assigned as output by default. But if you need to interface a switch to the 89S532 uC then you have to declare them as inputs.

Follow the pin diagram to do the connections.

Once the 89S52 is programmed its time test the output.

I have added a small .gif file to show you the working of the programmer.

Hope you like this instructable. Don't forget to follow me here for more electronics projects.