Tutorials/Experiments on AT89S52 Microcontroller Programming

Being inspired by this post in the Microcontroller Section, I am opening this thread with my little knowledge and experience on AT89S52 Microcontroller for those who are at the side of 'Learning/Practicing the Fundamentals of Microcontroller' rather than 'Practicing (only) the Steps of Making Projects with Microcontroller'. The academicians suggest that the 8-bit MCU (the AT89S52) is an ideal chip for this purpose. Once the concepts of 89S52 Architecture and Instruction Set become clear, the Learners can easily switchover to the AVR Architectures.

Tutorial/Experiment Sheets will be made available for downloading from a Site whose address will be provided through links via this thread. In the meantime, please arrange the following Setup:

1. Universal ROM Programmer (1) or In-system Programmer for AT89S52 (1) or 8051 Microcontroller Learning System
2. Standard-sized Breadboard (1)
3. 2.2k Resistors (1/4 w) (5)
4. RED/GRN LEDs (6)
5. Male-Male Jumper wires (40)
6. 100uF/16V Capacitors (2)
7. 4.7k Resistors (5)
8. 0.1uf/63V (104) capacitors (5)
9. 12 MHz Crystal (2)
10. AT89S52 Microcontroller (2)
11. Push (auto spring return) Switch (Breadboard Mountable) (2)
12. MIDE-51 Assembler/SDCC for AT89S52/8051 MCU
13. Data Sheets of AT89S52 MCU
14. MCS-51 Application Notes (An Introduction to the Intel MCS-51 Single-Chip Microcontroller Family)
15. MCS-51 Application Notes (Using the Intel MCS-51 Boolean Processing Capabilities)
16. MCS-51 Application Notes (MCS-51 Instruction Set)
17. MCS-51 Application Notes (MCS-51 Architecture)
18. Diagrams of this post.
19. +5V (max 1A) Power Supply
20. Introduction to 8051 Microcontroller

Experiment-1: Blinking LED1 connected at the P1.1-pin of 89S52 MCU. Click here1 for the Experiment Sheets, and click here2 for the LEDP11.asm program. Click here3 for an introduction to 8051 Microcontroller.

To be honest I am not understanding the code. Will you please type it in "C" language?

The styles of C Codes of the SDCC Compiler of MIDE-51 Studio is a bit different from that of Arduino IDE. Therefore, it is time for you to decide to begin MCU Learning using Arduino UNO or to follow the odd paths (the assembly programming) for Learning the 89S52. Assembly Language is practised, as @westfw has noticed, by the serious programmers.

My suggestion would be to begin learning the ATmega328P using Arduino UNO and the standard C/C++ platform. Arduino Forum is very resourceful in this regard.

Moreover, learning process of 89S52 using ROM Programmer is a cumbersome one -- you need to take out the MCU from the breadboard, put it into the ZIP socket of the programmer, finish the programming, put it again on the breadboard, and then testing. After a few trials, the MCU might become useless as one or more pins of the MCU could be broken.

An In-system Programmer that supports 89S52 could be a useful gadget in this case; but, the gadget is rarely available. An ISP Programmer allows uploading codes into the target MCU in an interactive way for as as many time as much is required without removing the chip from the system .

But I think beginning with 8051 will help me in future? After learning 8051 i can easily switch to AVR's?

Try this experiment on the familiarisation and operation of Arduino UNO; you may be interested to begin learning ATmega328P (8-bit AVR MCU) directly. Learning and Practising (or practising and learning) simultaneously ensures good progress. At the same time, you may also practice the programming of 89S52 MCU.

I know how to program an Arduino. I am just curious about the At89S52. As I have a huge interest in microcontrollers.

Why it is in the bar? It belongs to Microcontrollers.

I know how to program an Arduino. I am just curious about the At89S52. As I have a huge interest in microcontrollers.

Please visit this link-1 to have information on AT89S52 Microcontroller Learning System, and this link-2 for Software Tools.

Please visit this link-1 to have information on AT89S52 Microcontroller Learning System

Is that $130 just for the bare PCB, or is that the full kit? (seems like too much for the first, and perhaps not enough for the latter.)
Alas, this sort of pricing is why the Arduino was able to take over!

In fact, I have no idea about the pricing and vendor of the 8051 Learning System of this link-1. I tried to collect some information from net for @PhysX and accordingly passed to him. About this link-2: as far as I know (I have also used it for some times), there is a free (censored) portable Keil-C Compiler for 89S52 MCU.

I collected all the components. Now teach me how to program in C

@PhysX

The following C-codes are prepared using MIDE-51 SDCC Compiler. This program is intended to blink LED1 connected at PI.1 of the circuit of Post#1. The program is compiled well; but, I could not test it due to my own problems which are:

  1. My self-made ISP Programmer works well for downloading the hex file created by MIDE-51 Assembler. It is creating problem to download hex file (really big compare to hex file created by MIDE-51 Assembler) created by MIDE-51 SDCC.

  2. My old ROM Programmer (Model: TIP2005) is also creating problem during stand-alone programming.

#include <8052.h>

void delay(unsigned int y);

void main()
{
  while(1)
  { 
    P1_1 = 1;     /*wrting LH at P1.1*/
    delay(30000);
    P1_1 = 0; /*writing LL at P1.1*/
    delay(30000);
  }
}

void delay(unsigned int y)
{
  int i;
  for (i = 0; i<y; i++)
  {
    ;
  }
}

Listing of Hex File creadted by MIDE-51 SDCC for the above C Program

:03000000020003F8
:03005C0002005F40
:05005F0012006480FEA8
:0E006400D291907530120076C29190753012D4
:04007200007680EEA6
:08007600AA82AB837C007D002F
:0E007E008C068D07C3EE9AEF9B50070CBC005A
:04008C00F10D80EE04
:01009000224D
:06003200E478FFF6D8FDA2
:080010007900E94400601B7A4D
:05001800009000957846
:03001D000075A0CB
:0A00200000E493F2A308B800020503
:08002A00A0D9F4DAF275A0FF81
:080038007800E84400600A7939
:030040000075A0A8
:0600430000E4F309D8FC03
:080049007800E84400600C7926
:0B00510000900000E4F0A3D8FCD9FAF6
:03000300758107FD
:0A000600120091E582600302005F22
:040091007582002252
:00000001FF

Listing of Source Code and Hex File created by MIDE-51 ASM for the corresponding Assembly Program

ORG 0000H
 LJMP 0030H
 
 ORG 0030H
 MOV SP, #70H
 MOV PSW, #00H
 
L1: SETB P1.1
 LCALL DELAY
 CLR P1.1
 LCALL DELAY
 LJMP L1
 
DELAY: MOV R6, #0FFH
HERE2: MOV R7, #0FFH
HERE1: DJNZ R7, HERE1
 DJNZ R6, HERE2
 RET
 
 END
:03000000020030CB
:1000300075817075D000D291120043C291120043B5
:0C0040000200367EFF7FFFDFFEDEFA22AA
:00000001FF

ISP Programmer ROM Programmer

Please Test the program as per instructions of Experiment-1 of Post#1 and tell if the LED1 blinks!

I hope that you understand my limitations. I am a man who programmed for many many years in Machine and ASM languages for 6802, 8085, 8086, 80286, 80386, 8051, ATmega32A; practised some basic DOS based C/C++; I have hardly used MIDE-51 SDCC and Keil-C Compiler. Just recently, I have engaged with embedded C Programming due to my obligation of delivering lessons to my hundreds of undergraduate students who are using Aduino UNO as a Compulsory Course Item.

I appreciate your efforts and goodwill wholeheartedly towards the learning process of 89S52. the 89S52 is a chip that is likened by many people including me and you . I would like to suggest you to collect an 89S52 based Learning System and practice Keil Portable C (free). This one is much much better than the MIDE-51 SDCC.

Good Luck!

The LED1 blinks thanks Mr. Golam"JEM"Mostofa!! I used an usbasp avr programmer with a modified firmware to upload the HEX file generated by the KEIL IDE. I also needed a 4MHZ crystal to upload the hex. Internet has many solutions. Please Upload the next tutorial as soon as possible :slight_smile: :slight_smile:

Congratulations for making the circuit working! For your efforts, you deserve +1.

I would like to see the photographs of your gadgets.

The next Tutorial/Experiment-2 will be (probably!) on CC-type 7-segment Display Unit. I will use MIDE-ASM/SDCC and if possible KeilC for programming.

Please note that the 89S52 does not support TWI Bus; so, we will not be able to program many of the up-to-date sensors/devices like BMP180, DS1307, DS3231, SD Card, and etc.

Experiment-2 Driving CC-type 7-segment Multiplexed Display Unit using 89S52 MCU


Figure-1: 2-digit cc-type multiplexed display unit

Task-A Add the binary numbers 0x37 and 0x33 and show the result on DP0-DP1 positions of the display unit of Fig-1.

Program Codes using MIDE-51 SDCC (untested)

#include <8052.h>

void delay(unsigned int y);

void main()
{
  
  static idata unsigned char lupTable[] = {
  0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F, 0x77, 0x7C, 0x39, 0x5E, 0x77, 0x71
                                           };
  
  unsigned char x, y, z;
  
  z = 0x37 + 0x33;  //z = 6A
  
  //HEX to CC-code Conversion
  x = z>>4;
  x = lupTable[x];    //x contains cc-code of 6
  y = z & 0x0F;
  y = lupTable[y];    //y contains cc-code of A
  
  while(1)
  { 
    P2 = x;     //show 6 at DP0 position
    P1_1 = 0;     
    P1_2 = 1;
    delay(300);
    
    P2 = y; //show A at DP1 position
    P1_1 = 1;
    P1_2 = 0;
    delay(300);
  }
}

void delay(unsigned int y)
{
  int i;
  for (i = 0; i<y; i++)
  {
    ;
  }
}

Task-B Add the binary numbers 0x23 and 0x17 and show the corresponding decimal number 58 on the display unit of Fig-1.

Unfortunately i removed all my circuitry without taking a picture. I am sorry about that. Please give me the next tutorial and i will give you the video of the working gadgets!!

Forum members must be willing to see the progress of your efforts having had seen the pictorial view of your setup. Heve you solved Task-B of Experiment-2? Please upload the camera capture where we will be able to see 58 on DP0-DP1 positions of Post#15.

The next Experiment could be on AD0834 Serial ADC.