689 views|14 replies

28

Posts

0

Resources
The OP
 

When I was learning about C51 LED running lights, I wrote a piece of code. There was a sentence in the code that the software did not execute and was skipped directly. [Copy link]

 

#include <REGX52.H>

unsigned char LEDNum; //define an unsigned character variable LEDNum

/*Define an unsigned integer n, which can set any delay time, based on the multiple of 1ms delay*/
void Delay(unsigned int n) //@12.000MHz
{
unsigned char i, j;

while(n--)
{
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
}
}

/*Main function*/
void main()
{
LEDNum=0x01;
while(1)
{
if(LEDNum>=0x81)
LEDNum=0x01;
else
{
LEDNum<<1;
P2=~LEDNum;
Delay(500);
}
}
}

The original intention is: define an unsigned character LEDNum, initially assign a value of 0000 0001, and then judge by the conditional statement. If LEDNum is greater than 1000 0001, reassign it to 0000 0001. If it is less than 1000 0001, execute LEDNum<<1 and shift it left by one bit. However, I found that this sentence was ignored when I executed it in a single step, and it was not executed, so the final result was not right. I would like to ask experts to help point out the cause of the problem. I am a novice and I am very grateful.

This post is from MCU

Latest reply

When the state of P3_0 is 1, P2_0=P2_0 will be executed. Here, change it to P2_0=~P2_0 to reverse the level.   Details Published on 2024-5-23 09:51
 

1w

Posts

25

Resources
2
 

It has been optimized by the compiler and can be executed by turning off optimization.

This post is from MCU
 
 

6069

Posts

4

Resources
3
 

LEDNum<<=1;

This post is from MCU
 
 
 

6060

Posts

6

Resources
4
 

Add a breakpoint and try it. It shouldn't fail to execute.

This post is from MCU
 
Personal signature

在爱好的道路上不断前进,在生活的迷雾中播撒光引

 
 

28

Posts

0

Resources
5
 

Thank you, I found the problem. It should be written as: LEDNum=LEDNum<<1; LEDNum is just shifted left by one position. If LEDNum is not reassigned after the left shift, the value of LEDNum will remain unchanged, so it is optimized. After changing it to an assignment statement, there is no problem and it can be executed. Thank you very much

This post is from MCU

Comments

LEDNum<<=1; LEDNum=LEDNum<<1; should be the same. Try it.  Details Published on 2024-5-22 13:01
 
 
 

1123

Posts

1

Resources
6
 

Accumulate experience...Continue to improve!!!

This post is from MCU
 
 
 

2

Posts

1

Resources
7
 
This post was last edited by suiyue9528 on 2024-5-21 10:32

If the unsigned LEDNum=0x01 is shifted left, the maximum value is 0x80. How can it be greater than or equal to 0x81? This is where the problem lies. When it reaches 0x80, the next shift left is 0, so the final LEDNum is always 0

This post is from MCU

Comments

Thank you very much, it really enlightened me! I will correct it later.  Details Published on 2024-5-21 17:59
Thank you very much, it really enlightened me! I will correct it later.  Details Published on 2024-5-21 17:56
 
 
 

28

Posts

0

Resources
8
 
suiyue9528 posted on 2024-5-21 10:30 Unsigned LEDNum=0x01 keeps shifting left, the maximum value is 0x80, how can it be greater than or equal to 0x81? This is the problem. When it reaches 0x80, shift left again...

Thank you very much, it really enlightened me! I will correct it later.

This post is from MCU
 
 
 

28

Posts

0

Resources
9
 
suiyue9528 posted on 2024-5-21 10:30 Unsigned LEDNum=0x01 keeps shifting left, the maximum value is 0x80, how can it be greater than or equal to 0x81? This is the problem. When it reaches 0x80, shift left again...

When I was learning the code for key-controlled LED on and off today, I used the if...else... statement. The code is as follows:

void main()
{
while(1)
{
if(P3_0==0)
{
Delay(20);
while(P3_0==0);
Delay(20);
P2_0=~P2_0;
}
// else
// P2_0=P2_0 ;
}
}

When executing, I found that when adding the last else statement, the button cannot control the LED on and off, and the state of P2_0 remains unchanged. On the contrary, when removing the else statement, it becomes normal! Please guide me, why is this? Shouldn't there be an else statement after if?

This post is from MCU

Comments

Please let me know if you understand this issue. Thank you very much  Details Published on 2024-5-22 09:52
 
 
 

28

Posts

0

Resources
10
 
Tacking posted on 2024-5-21 17:59 Today, when I was learning the code for key-controlled LED on and off, I used the if...else... statement. The code is as follows: void main() { ...

Please let me know if you understand this issue. Thank you very much

This post is from MCU
 
 
 

56

Posts

0

Resources
11
 

If the unsigned LEDNum=0x01 is shifted left, the maximum value is 0x80. How can it be greater than or equal to 0x81? This is where the problem lies. When it reaches 0x80, the next shift left is 0, so the final LEDNum is always 0

This post is from MCU
 
 
 

6069

Posts

4

Resources
12
 
Tacking posted on 2024-5-21 09:39 Thank you, I found the problem. It should be written as: LEDNum=LEDNum<<1; LEDNum is just shifted left by one bit, and LEDNum is not reassigned after the left shift...

LEDNum<<=1; LEDNum=LEDNum<<1; should be the same. Try it.

This post is from MCU

Comments

LEDNum<<=1 is the same as LED=LEDNum<<1. I wrote LEDNum<<1 before without adding "=". When compiling, this sentence was always optimized away by the program and not executed. I changed it to LEDNum<<=1 and it worked. I've learned a lot. Thank you very much. It turned out that I had a syntax error.  Details Published on 2024-5-24 15:45
 
 
 

2

Posts

1

Resources
13
 
Tacking posted on 2024-5-22 09:52 Please let me know if you understand this issue, thank you very much

When the state of P3_0 is 1, P2_0=P2_0 will be executed. Here, change it to P2_0=~P2_0 to reverse the level.

This post is from MCU

Comments

The idea of my code is that when the state of P3_0 is 1, the state of P2_0 remains unchanged, and when the state of P3_0 is 0, the state of P2_0 flips. The execution result shows that after adding the else statement, the state cannot be flipped, and P2_0 keeps the same state. After removing the else statement, it can flip normally.  Details Published on 2024-5-24 15:51
 
 
 

28

Posts

0

Resources
14
 
damiaa posted on 2024-5-22 13:01 LEDNum<<=1; LEDNum=LEDNum<<1; should be the same. You try it.

LEDNum<<=1 is the same as LED=LEDNum<<1. I wrote LEDNum<<1 before without adding "=". When compiling, this sentence was always optimized away by the program and not executed. I changed it to LEDNum<<=1 and it worked. I've learned a lot. Thank you very much. It turned out that I had a syntax error.

This post is from MCU
 
 
 

28

Posts

0

Resources
15
 
suiyue9528 posted on 2024-5-23 09:51 When the state of P3_0 is 1, P2_0=P2_0 will be executed. Here, change it to P2_0=~P2_0, and the level can be reversed

The idea of my code is that when the state of P3_0 is 1, the state of P2_0 remains unchanged, and when the state of P3_0 is 0, the state of P2_0 flips. The execution result shows that after adding the else statement, the state cannot be flipped, and P2_0 keeps the same state. After removing the else statement, it can flip normally.

This post is from MCU
 
 
 

Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Related articles more>>
Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list