My LED's are not Blinking

Thread Starter

@vajra

Joined May 2, 2018
154
.I got my first microcontroller programmer for 8051. I am trying to blink eight LED's.

I wrote program to blink all LED's

My program
Code:
#include <REG51.h>

sbit LED_Port = P2;          // All LED's connected to port P2

void Delay (unsigned int loop);

void main(void)
{
  LED_Port = 0x000000;       // All LED's  OFF
  while (1)
  {
    LED_Port = 0x11111111;    // Turn ON All LED's
    Delay(60000);             // Wait 60 ms
    LED_Port = 0x00000000;    // turn off All LED's
    Delay(30000);             // wait 30ms
  }
}
void Delay (unsigned int loop)  // Delay function
{
    unsigned int i;
  for  (i = 0; i <loop; i++);
  {
  }
}
But only one led is blinking P2.7 and remaining is ON. I cross checked all the connections and found that connections are correct

What is wrong, please help me
 

Attachments

danadak

Joined Mar 10, 2018
4,057
If LEDs are connected from pin to Vdd then writing "0" to the port
will turn them all on.

Without a schematic hard to help you.

Also I do not see any setup to tell the port its output ?

Regards, Dana.
 

MrChips

Joined Oct 2, 2009
30,705
You are using the wrong data representation.
0x11111111 is hexadecimal representation, not binary.

Try this instead:

Code:
while (1)
  {
    P2 = 0;    // Turn ON All LED's
    Delay(60000);             // Wait 60 ms
    P2 = 0xFF;    // turn off All LED's
    Delay(30000);             // wait 30ms
  }
 

Thread Starter

@vajra

Joined May 2, 2018
154
Thanks MrChips, Ian Rogers you saved my life

I wrote a four code three of them working to blink multiple Led's but forth one is not working

Code 1 working
Code:
#include <REG51.h>
void Delay (unsigned int loop);
void main(void)
{

while (1)
  {
    P2 = 0;            // Turn ON All LED's
    Delay(60000);      // Wait 60 ms
    P2 = 0xFF;         // turn off All LED's
    Delay(30000);      // wait 30ms
  }
}
void Delay (unsigned int loop)  // Delay function
{
    unsigned int i;
  for  (i = 0; i <loop; i++);
  {
  }
}
code 2 working
Code:
#include <REG51.h>

#define LED_Port P2
void Delay (unsigned int loop);
void main(void)
{

while (1)
  {
    LED_Port = 0;            // Turn ON All LED's
    Delay(60000);      // Wait 60 ms
    LED_Port = 0xFF;         // turn off All LED's
    Delay(30000);      // wait 30ms
  }
}
void Delay (unsigned int loop)  // Delay function
{
    unsigned int i;
  for  (i = 0; i <loop; i++);
  {
  }
}
code 3 working
Code:
#include <REG51.h>

#define LED_Port P2
void Delay (unsigned int loop);
void main(void)
{

while (1)
  {
 
    Delay(60000);      // Wait 60 ms
    LED_Port =~ LED_Port;        // turn on/off All LED's
  
  }
}
void Delay (unsigned int loop)  // Delay function
{
    unsigned int i;
  for  (i = 0; i <loop; i++);
  {
  }
}
code 4 not working
Code:
#include <REG51.h>

sbit LED_Port = P2;
void Delay (unsigned int loop);
void main(void)
{

while (1)
  {
    LED_Port = 0;            // Turn ON All LED's
    Delay(60000);      // Wait 60 ms
    LED_Port = 0xFF;         // turn off All LED's
    Delay(30000);      // wait 30ms
  }
}
void Delay (unsigned int loop)  // Delay function
{
    unsigned int i;
  for  (i = 0; i <loop; i++);
  {
  }
}
Can you please tell me what's the wrong why this code blink only one led P2.0 ?
 

shteii01

Joined Feb 19, 2010
4,644
4th code:-

sbit LED_port = P2; this is only one bit..

why don't you want to use #define LED_port P2
that was the first thing that jumped at me.

sbit is set bit, like Ian wrote and the command says, it sets a bit, one bit, not all of them.

then you assign this one bit to a port, but the software should be assigning it to a single bit that is part of the port. so right there you have a problem where one part of the statement is doing one thing and the second part of the statement is doing a different thing and both parts don't mesh together.

you can be a control obsessed and do:
sbit LED_1=P2.0;
sbit LED_2=P2.1;
sbit LED_3=P2.2;
and so on
 

Thread Starter

@vajra

Joined May 2, 2018
154
4th code:-

sbit LED_port = P2; this is only one bit..
I understood if we need to set only one bit. we can use sbit to set one bit

why don't you want to use #define LED_port P2
There is no any specific reason. I was confuse

You are using the wrong data representation.
0x11111111 is hexadecimal representation, not binary.
I have confusion Can we assign only hex value to port? We cant assign binary value to port ?
 

MrChips

Joined Oct 2, 2009
30,705
This is a common misconception.

It doesn't matter if you use decimal, hexadecimal, or binary representation. They are always translated into binary.
For example:

P2 = 255;
P2 = 0xFF;
P2 = 0b11111111; // assuming the compiler supports binary format

all do the same thing, i.e. set all eight bits of port P2 to 1.

P2.0 refers to a single bit of port P2.
Hence you can write
P2.0 = 0;
P2.0 = 1;

If you declare a single bit port as

sbit LED_port = P2.0

then
LED_port = 0;
LED_port = 1;

is used to control that single bit.

Don't use sbit unless you want to control single bits.

Use:
#define LED_port P2
 

Thread Starter

@vajra

Joined May 2, 2018
154
This is a common misconception.
P2 = 0b11111111; // assuming the compiler supports binary format
I understood its depend on compiler. Keil compiler doesn't support binary ?

When I run following code it show errors
Code:
#include <REG51.h>
#define LED_Port = P2;             // All LED's connected to port P2
void Delay (unsigned int loop);
void main(void)
{
  LED_Port = 0b000000;            // All LED's  OFF
  while (1)
  {
    LED_Port = P2 = 0b11111111;    // Turn ON All LED's
    Delay(60000);             // Wait 60 ms
    LED_Port = 0b00000000;    // turn off All LED's
    Delay(30000);             // wait 30ms
  }
}
void Delay (unsigned int loop)  // Delay function
{
    unsigned int i;
  for  (i = 0; i <loop; i++);
  {
  }
}
Keil Build output error
compiling led.c...
led.c(6): error C141: syntax error near '='
led.c(6): error C141: syntax error near '='
led.c(6): error C251: illegal octal digit
led.c(9): error C141: syntax error near '='
led.c(9): error C141: syntax error near '='
led.c(9): error C251: illegal octal digit
led.c(11): error C141: syntax error near '='
led.c(11): error C141: syntax error near '='
led.c(11): error C251: illegal octal digit
led.c - 9 Error(s), 0 Warning(s).
 

shteii01

Joined Feb 19, 2010
4,644
I understood its depend on compiler. Keil compiler doesn't support binary ?

When I run following code it show errors
Code:
#include <REG51.h>
#define LED_Port = P2;             // All LED's connected to port P2
void Delay (unsigned int loop);
void main(void)
{
  LED_Port = 0b000000;            // All LED's  OFF
  while (1)
  {
    LED_Port = P2 = 0b11111111;    // Turn ON All LED's
    Delay(60000);             // Wait 60 ms
    LED_Port = 0b00000000;    // turn off All LED's
    Delay(30000);             // wait 30ms
  }
}
void Delay (unsigned int loop)  // Delay function
{
    unsigned int i;
  for  (i = 0; i <loop; i++);
  {
  }
}
Keil Build output error
Ok.
I think you have two syntax errors according to my 8051 textbook.

First.
When using sbit, the correct bit assignment is:
sbit LED1=P2^0;
There is no period (dot) between 2 and 0.

Second.
When doing define, the syntax is:
#define LED P2;
There are no equal sign, just spaces.


At this point I have to tell you that I should not be telling you the basic syntax. You should have a textbook with examples where you should look up the absolute basic things like these. I wish you luck and I am out of here.
 

Thread Starter

@vajra

Joined May 2, 2018
154
At this point I have to tell you that I should not be telling you the basic syntax. You should have a textbook with examples where you should look up the absolute basic things like these. I wish you luck and I am out of here.
That was my bad

I think correct way to assign binary value LED_Port = 11111111;
When I run following code led 3, led4 and led5 does not blink
Code:
#include <REG51.h>
#define LED_Port P2             // All LED's connected to port P2
void Delay (unsigned int loop);
void main(void)
{
  LED_Port = 000000;            // All LED's  OFF
  while (1)
  {
    LED_Port = 11111111;    // Turn ON All LED's
    Delay(90000);             // Wait 60 ms
    LED_Port = 00000000;    // turn off All LED's
    Delay(30000);             // wait 30ms
  }
}
void Delay (unsigned int loop)  // Delay function
{
    unsigned int i;
  for  (i = 0; i <loop; i++);
  {
  }
}
I don't know why led0, led1, led2 , led6 and led7 are blinking and why led 3, led4 and led5 aren't blinking ? l
 
Last edited:

MrChips

Joined Oct 2, 2009
30,705
You still have not gotten the hang of it.

What do you think the following lines do (for example)?:

P2 = 0101;
P3 = 1111;


Answer:

0101 is one hundred and one.
This is 01100101 in binary

1111 is one thousand, one hundred and eleven
This is 10001010111 in binary.
 
Top