Pages

Wednesday, 11 December 2013

8051 LED TOGGLE PROGRAM CODE

You might have seen a "Hello World!" program code example while learning some programming language. While it is useful for getting familiar with the basics of that language, it also gives you a head start in that environment. If you ever want to learn some new programming language, you might want to Google out a "Hello world!' program code to get started. The code I am about to discuss serves the same purpose for the 8051 microcontroller because it is the most simplest task to perform. Play close attention if you want everything to work properly. 


LED TOGGLE

LED (Light Emitting Diodes) are simple lights that you can turn ON/OFF. They glow when they are ON and they don't when they are not. They look somewhat familiar to the picture below


Standard RED LEDs
The bigger leg represents the POSITIVE terminal while the shorter one is definitely NEGATIVE. So you take care of the legs when you use them (high voltage goes to POSITIVE while NEGATIVE is connected to GROUND of the power supply). In our diagrams, we are used to representing it with the symbol as shown so don't get confused. The left terminal is POSITIVE while the right end is NEGATIVE terminal.


LED Symbol


INTERFACING LED TO 8051

There are two genuine methods of interfacing (in simple words, connecting) LED to your 8051. Both will work just fine but have opposite programming techniques.


LED INTERFACE 1

LED INTERFACE 2

The resistor is important in interface 2 to limit the flowing current and avoid damaging the LED and/or MCU but I wouldn't care much about the resistor in interface 1 as 8051 MCU won't/can't damage the LED but let's not go into the safety problems. If you ask me, I would go for interface 1 without using any resistor at all! because it has worked for me every time. 

I will explain the program code in detail but let me tell you beforehand, as a conceptual hint, the difference between these two interfaces. 
  • Interface 1 will glow LED ONLY if the PIN value is HIGH as current flows towards ground.
  • Interface 2 will glow LED ONLY if the PIN value is LOW as current flows towards PIN due to its lower potential.

PROGRAM CODE

Let me go straight into the code now. I will explain both Assembly and C Language code so let's get started. You can download the program code in ZIP form by following the link in the download section at the end of this post. I am sure you know how to create HEX file using KEIL and then simulate the code using PROTEUS and when you're done verifying your code, you can easily program the 8051 MCU.

For practical testing, you will need a test circuit for the 8051 MCU which is the most fundamental requirement of the project and you can't help but implement it for successful output. Honestly, it's very simple once you get used to it, I promise.


ASSEMBLY LANGUAGE CODE
It will be tricky to explain each portion of the code. You need to learn ASSEMBLY Language to understand it all properly. The flow of the program is such that it toggles PIN1.0 of PORT1. In other words,

  • Move 00000001 to PORT1 (each port is 8bit wide)
  • Wait for some time
  • Move PORT1 value to accumulator where we can complement it using CPL and thus it becomes 11111110 (PIN1.0 is being toggled)
  • Move this new value back to PORT1
  • Wait for some time and repeat the process.

So the value of PIN1.0 keeps toggling (ON/OFF) after some delay. The delay is implemented by using three nested loops. The loops will run 10 x 200 x 200 times and I don't know the exact amount of time it takes because I am not interested in it at the moment. For now, let's just say that the delay is long enough for the human eye to witness the LED blinking. 

The same code in C language will look somewhat like this


C LANGUAGE CODE
As you can see, it becomes relatively easier to write in C language. Just use normal C operators to manipulate data and values. Now the most important thing in this code is that it is good practise to give the MCU an infinite task to do (by using infinite while loop) so that it will never get out of the loop, stop and just sit there looking at your face. It must constantly perform some operation.

The delay function used in this code is also a general one and it roughly provides the required delay in milliseconds as defined by the input parameter. So we are talking of one second when we say delay(1000). You can use this function for now but don't ever use it when timing is crucial and you need more accuracy. 

Here is the simulation design for PROTEUS that I used. As I said, I wouldn't care much about the resistor. Neither in the simulation nor in practical scenario.

PROTEUS SIMULATION DESIGN

I have also included the design file in the download package. I hope that this post has given you an idea of how things are done in real. It gets easier with practise. Any suggestion/feedback is highly appreciated as I might have missed something. Remember that there is always an easier way around in programming so I encourage you to learn, understand and then venture on your own adventure. Goodluck for the implementation. 



This ZIP package contains the following files
  • Assembly Language Code
  • C Language Code
  • Compiled HEX file
  • Proteus Design File
You can use all these files at your will but remember that they are for the sake of understanding the concept. You have to know what's going on and then it won't hurt if you just copy/paste but if you do it without caring about understanding, well all I can say is that it's not good for development. 

39 comments:

  1. Good day sir' i'm an elecronics hobbiest and had been using proteus in my small pcb design of analogue circuits. Lately i found interest in coding and eager to learn, can you please give some inputs on easier programming language aside from C?

    Thanks in advance!

    ReplyDelete
    Replies
    1. Honestly speaking, I haven't yet searched for another programming language aside from C when it comes to microcontroller programming. It is quite basic and easy to learn. Normally people start by learning C as their first programming language plus you can get plenty of help form different resources. As you get used to it, you can easily shift to another language whenever required.

      Delete
  2. Crystal clear for any beginner. Thank you

    ReplyDelete
  3. hi 8051
    please can you tell me that for connecting a 8051 on a breadboard for blinking LED's we need an (external)oscillator or not?

    ReplyDelete
    Replies
    1. Of course. 8051 won't work without the external oscillator.

      Delete
  4. How to glow 3 different pattern with led's using single switch??

    ReplyDelete
    Replies
    1. You can use a three-way switch to have three unique combinations. Each combination will trigger a specific LED pattern.

      Delete
    2. MK-TECHNOLOGIES10/18/2015 11:56 pm

      Ya it can we glow 3 different patterns of leds with single switch

      Delete
  5. i need a program to glow even or odd led in 8 led s using loops

    ReplyDelete
    Replies
    1. That's pretty simple. I am sure you can do it yourself. You can take my code and modify it to achieve your desired result.

      Delete
  6. Replies
    1. Thank you for appreciating :)

      Delete
    2. sir plz send me assembly language for digital temperature sensor project

      Delete
    3. I am afraid I cannot send you the code. Perhaps you can try doing it yourself and let me know if you are stuck somewhere.

      Delete
  7. i want to glow 8 led simunteneously when only one key is pressed

    ReplyDelete
    Replies
    1. Sure. Connect 8 LEDs with any port then use the PORT NAME instead of PIN NUMBER and they will glow all at once.

      Delete
  8. Nicely Explained, it work. THX

    ReplyDelete
  9. this is really good... have a better understanding of this topic now...

    ReplyDelete
  10. its hardware parts??

    ReplyDelete
    Replies
    1. Microncontroller, LED, Reset Circuitry, Resistor, Power supply :)

      Delete
  11. how to display decrementing series values in led7segment using pointers?

    ReplyDelete
    Replies
    1. I assume you're talking about Seven Segment Display (SSD)? If yes, you need to understand the basics of SSD in my other post.

      8051 SEVEN SEGMENT DISPLAY (SSD) INTERFACE

      Then you can easily create a pointer that points to a counter variable. Decrement the value and display it on SSD.

      Hope this helps!

      Delete
  12. how to display decremented series of values in led7 segment using dptr pointer in the program

    ReplyDelete
    Replies
    1. Store a value somewhere. Point to that value. Send it to the Seven Segment Display and decrement until you reach your terminating point.

      Delete
  13. I understand your points Sir but I have a doubt. How would we program the ports of microcontroller to be as inputs or outputs. I know that to set any port as input we have to make that port high but, should this be done at the first lines of programming or it can be done at any time.

    your sincerely
    Jasser

    ReplyDelete
    Replies
    1. By default, all the PINs of 8051 microcontroller are configured as INPUT i-e they are HIGH and you can use them as INPUT directly. Otherwise you have to set them LOW for use as OUTPUT. And you can do this at any stage in the code, not necessarily at the start. But it's a good practice to write all declarations at the beginning so that it's easy to understand.

      Delete
  14. whats the clock frequency of the crystal used ?

    ReplyDelete
    Replies
    1. For this particular case, you could in fact use any crystal. I have used 12MHz in the Proteus example.

      Delete
  15. THANK YOU! THIS SURE HELPED ME GET STARTED!

    ReplyDelete
  16. Beautifully explained!! V clear!! Crystal clearly explained!! Any beginner can understand!! All your blog's are too good!! I follow it regularly!!

    ReplyDelete