1678 views|13 replies

6

Posts

0

Resources
The OP
 

Help! TX-1C development board 51 serial interrupt if statement cannot be executed (it looks simple, but I can't adjust it) [Copy link]

 

邀请:@weizhongc   @huo_hu   @Blkhumor   @kiverin   参与回复

I encountered a seemingly simple 51 MCU serial communication problem , but I have been struggling for two days and still can't debug it.

1. Purpose:
Use Guo Tianxiang's development board to achieve the effect of LED flashing when the serial port inputs 0x01

2. The code is as follows:
void Usart() interrupt 4
{
unsigned char receiveData;
receiveData=SBUF; //Output received data
RI = 0; //Clear receive interrupt flag

//ledFlash(); //The MCU placed outside can command the external small light to flash
if(receiveData==0x01)
{
ledFlash();
}

SBUF=receiveData; //Put the received data into the send register
while(!TI); //Wait for the data to be sent to complete
TI=0; //Clear the send completion flag
}

3. Question:
1) Put the ledFlash() function that realizes the flashing of the LED outside the if statement and execute it directly, so that the flashing effect can be realized directly when the byte arrives.
2) However, when ledFlash() is moved into the if statement, for some reason, the flashing effect of the small light cannot be realized when downloaded to the microcontroller, but the program sent to the buffer of stc-isp is still normal. As shown in the following figure

4. Try:
Use Keil serial port simulation debugging. When 0x01 is sent, it can enter the if statement and execute ledFlash() normally, as shown in Figure

Please tell me, what is going on, and how to solve the problem?

This post is from 51mcu

Latest reply

Does the 51 microcontroller have this problem? But you can try it.   Details Published on 2022-12-10 21:17
 

6787

Posts

2

Resources
2
 

Are you sure that what you sent through the serial port is HEX 0x01, not the string 0x01?

This post is from 51mcu
 
 
 

6

Posts

0

Resources
3
 
wangerxian posted on 2022-12-5 17:21 Are you sure that the serial port you sent is HEX 0x01, not string 0x01?

Tried them all

This post is from 51mcu
 
 
 

6

Posts

0

Resources
4
 
wangerxian posted on 2022-12-5 17:21 Are you sure that the serial port you sent is HEX 0x01, not string 0x01?

Is it a code optimization problem in the Keil compilation environment?

This post is from 51mcu

Comments

It is possible to choose not to optimize.  Details Published on 2022-12-6 10:29
It is possible to choose not to optimize.  Details Published on 2022-12-6 08:48
 
 
 

6069

Posts

4

Resources
5
 
goodshot posted on 2022-12-5 19:16 Is it a code optimization problem in the Keil compilation environment?

It is possible to choose not to optimize.

This post is from 51mcu
 
 
 

6787

Posts

2

Resources
6
 
goodshot posted on 2022-12-5 19:16 Is it a code optimization problem in the Keil compilation environment?

Give it a try. Generally, strange problems are caused by optimization.

Logically speaking, debugging should produce the same results as not debugging.

This post is from 51mcu

Comments

Which optimization to choose?  Details Published on 2022-12-7 09:25
 
 
 

6069

Posts

4

Resources
7
 

Also pay attention to memory overflow:

For example, array out of bounds

Pointer usage issues (pointing to undefined memory, etc.)

Accessing a null pointer is also not possible.

This post is from 51mcu

Comments

No array used  Details Published on 2022-12-7 09:24
 
 
 

6

Posts

0

Resources
8
 

Post the source code

#include "reg52.h" //This file defines some special function registers of the microcontroller

typedef unsigned int u16; //declare and define the data type
typedef unsigned char u8;

sbit led=P1^0; //Define the P2.0 port of the microcontroller as led


sbit beep=P2^3; //buzzer pin


/*******************************************************************************
* Function name: delay
* Function function: delay function, when i=1, the delay is about 10us
******************************************************************************/
void delay(u16 i)
{
while(i--);
}

/***********************************************************************************
* Function name: main
* Function function: main function
* Input: None
* Output: None
**********************************************************************************/
void ledFlash()
{
int i=3;
while(i--)
{
led=0;
delay(50000); //about 450ms delay
led=1;
delay(50000); //about 450ms delay
}
}


/*******************************************************************************
* Function name: UsartInit()
* Function function: Set serial port
* Input: None
* Output: None
******************************************************************************/
void UsartInit()
{
SCON=0X50; //Set to working mode 1
TMOD=0X20; //Set counter working mode 2
PCON=0X80; //Double the baud rate
TH1=0XF3; //Set the initial value of the counter, note that the baud rate is 4800
TL1=0XF3;
ES=1; //Turn on the receive interrupt
EA=1; //Turn on the total interrupt
TR1=1; //Turn on the counter
}

/*******************************************************************************
* Function name: main
* Function function: main function
* Input: None
* Output: None
**********************************************************************************/
void main()
{
UsartInit(); // Serial port initialization
while(1);
}

/*******************************************************************************
* Function name: Usart() interrupt 4
* Function: serial communication interrupt function
* Input: None
* Output: None
******************************************************************************/
void Usart() interrupt 4
{
u8 receiveData;
if(RI)
{

receiveData=SBUF;//Output the received data

//ledFlash();//Put it outside and the MCU can command the external small light to flash



if(receiveData==0x01)
{
//Put it in the judgment sentence, through the serial port simulation of keil and STC-ISP, you can single-step into the if statement to execute ledFlash() to realize the small light flashing
//But if it is downloaded to the MCU, the small light flashing effect cannot be realized
ledFlash();

}

RI = 0;//Clear the receive interrupt flag


// SBUF=receiveData; //Put the received data into the transmit register
// while(!TI); //Wait for the data to be sent to complete
// TI=0; //Clear the send completion flag

}
}

This post is from 51mcu
 
 
 

6

Posts

0

Resources
9
 
damiaa posted on 2022-12-6 11:42 Memory overflow should also be noted: For example, the use of array out-of-bounds pointers (pointing to undefined memory, etc.) and accessing null pointers are also not allowed...

No array used

This post is from 51mcu

Comments

KeilC51 compiler has an optimization setting. Different optimization settings will produce different compilation results. The highest optimization setting can be set to 9 levels. Keil C51 compiler optimization selection is set to 0 (no optimization).  Details Published on 2022-12-7 11:32
 
 
 

6

Posts

0

Resources
10
 
wangerxian posted on 2022-12-6 10:29 Try it. Generally, strange problems are caused by optimization. In theory, debugging should be the same as not debugging.

Which optimization to choose?

This post is from 51mcu

Comments

Just set the optimization to Level 0, that is, no optimization. Try it first. But from experience, this phenomenon does not seem to be caused by optimization.  Details Published on 2022-12-7 14:14
 
 
 

6069

Posts

4

Resources
11
 

KeilC51 compiler has an optimization setting. Different optimization settings will produce different compilation results. The highest optimization setting can actually be set to 9 levels.

Keil C51's compiler optimization selection is set to 0 (no optimization).

This post is from 51mcu
 
 
 

6787

Posts

2

Resources
12
 
goodshot published on 2022-12-7 09:25 What optimization should I choose

Just set the optimization to Level 0, that is, no optimization. Try it first. But from experience, this phenomenon does not seem to be caused by optimization.

This post is from 51mcu

Comments

Can you make the suspected variable volatile?  Details Published on 2022-12-10 01:06
 
 
 

224

Posts

0

Resources
13
 
wangerxian posted on 2022-12-7 14:14 Directly set the optimization to Level 0, that is, no optimization. Try it first. But from experience, this phenomenon does not seem to be caused by optimization.

Can you make the suspected variable volatile?

This post is from 51mcu

Comments

Does the 51 microcontroller have this problem? But you can try it.  Details Published on 2022-12-10 21:17
 
 
 

6787

Posts

2

Resources
14
 
starcat123 posted on 2022-12-10 01:06 Can you make the suspected variable volatile?

Does the 51 microcontroller have this problem? But you can try it.

This post is from 51mcu
 
 
 

Find a datasheet?

EEWorld Datasheet Technical Support

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