1502 views|9 replies

968

Posts

0

Resources
The OP
 

Please ask senior programmers to help look at this problem [Copy link]

邀请:@maychang   @damiaa   @vincentc543   @RichSJ   参与回复

Please look at the following two functions. When the program executes to (1), if an RTC interrupt occurs suddenly, will it cause dyjc not to be equal to 1190 after the close function is executed?

The reasons are as follows: for example, the sentence dyjc = 1190 needs to be completed by 4 machine instructions at the low level. When the second of these 4 instructions is just executed, the RTC interrupt occurs. At this time, the MCU turns to execute multiple machine instructions corresponding to dyjc++. After executing multiple machine instructions corresponding to dyjc++, the third and fourth machine instructions corresponding to dyjc = 1190 are executed. Therefore, after the close() function is executed, the final value of dyjc may not be 1190. Is this analysis correct?

If this is correct, how can we avoid this situation? Thank you.

one,

void close( void)
{
.......

cs_low_fm = 0;
dyjc = 1190; // (1)
...

}

two,

void RTC_IRQHandler(void) // RTC interrupt is executed once per second
{
.......
dyjc++; // (2)
...

}

This post is from stm32/stm8

Latest reply

This post was last edited by vincentc543 on 2022-6-5 23:47 bool flag=0; void close( void) { ....... cs_low_fm = 0; flag=1; //dyjc set value ....... } two, void RTC_IRQHandler(void) // RTC interrupt is executed once per second { ....... dyjc++; // (2) if(flag){ dyjc = 1190; // (1) flag=0; } ....... }   Details Published on 2022-6-5 23:37
 

2w

Posts

0

Resources
2
 

"So maybe after the close() function is executed, the final value of dyjc is not 1190. Is this analysis correct?"

Yes. That is possible.

This post is from stm32/stm8
 
 

2w

Posts

0

Resources
3
 

This is possible as long as the dyjc variable is used in multiple places (in multiple functions).

This post is from stm32/stm8

Comments

Thank you very much. Our leader's heart must have stopped beating.  Details Published on 2022-6-4 16:26
 
 

968

Posts

0

Resources
4
 
maychang posted on 2022-6-4 16:22 As long as the dyjc variable is used in multiple places (in multiple functions), this is possible.

Thank you very much. Our leader's heart must have stopped beating.

This post is from stm32/stm8

Comments

The probability of this error happening is very small. Your timer interrupt occurs once a second. The timer interrupt modifies a variable, and the error will occur when the variable is assigned a value. Therefore, this error may not occur even if the program is run several times, dozens of times, hundreds of times, or thousands of times, and it is difficult to detect. But it is indeed an error  Details Published on 2022-6-4 17:02
 
 
 

2w

Posts

0

Resources
5
 
Yishayishi published on 2022-6-4 16:26 Thank you very much. Then our leader's little heart must have stopped suddenly.

The probability of this error is very small. Your timer interrupt occurs once a second. When the timer interrupt modifies a variable, and the error occurs when the variable is assigned a value, the error will occur. Therefore, this error may not occur even if the program is run several times, dozens of times, hundreds of times, or thousands of times, and it is difficult to detect. But it is indeed an error.

This post is from stm32/stm8
 
 
 

6062

Posts

4

Resources
6
 
This post was last edited by damiaa on 2022-6-4 19:52

void close( void)
{

.......

//Turn off the rtc interrupt here,

dyjc = 1190;

//Here rtc interrupt is turned on.

.......

}

Or dyjc with a flag dyjc_write_flag

Main program:

void close( void)
{

.......

dyjc_write_flag =0;

dyjc = 1190;

if(dyjc_write_flag ==1) //If an RTC interrupt occurs at this time, reassign the value.

{

dyjc = 1190;

dyjc_write_flag =0;

}

.......

}

RTC interrupt

void RTC_IRQHandler(void) //RTC interrupt is executed once per second
{
.......

dyjc_write_flag =1;

dyjc++;

.......

}

This post is from stm32/stm8

Comments

These are good solutions, but the first one has a problem. If an RTC interrupt occurs, turning off the interrupt will cause the interrupt to be lost.  Details Published on 2022-6-5 00:41
 
 
 

148

Posts

0

Resources
7
 
damiaa 发表于 2022-6-4 19:33 void close( void) { ....... ...

These are good solutions, but the first one has a problem. If an RTC interrupt occurs, turning off the interrupt will cause the interrupt to be lost.

This post is from stm32/stm8
 
 
 

148

Posts

0

Resources
8
 

I don't know which chip you are talking about. In the ST chip, I can use assembly language to directly set it to that address, avoiding this problem. Of course, it will bring difficulties to the software update because it is necessary to find the absolute address.

This post is from stm32/stm8
 
 
 

114

Posts

6

Resources
9
 

要改善這問題,用close函數發出dyjc 歸位的旗標.

只在RTC中斷內做dyjc的賦值,應該就沒這問題了

This post is from stm32/stm8
 
 
 

114

Posts

6

Resources
10
 
This post was last edited by vincentc543 on 2022-6-5 23:47

bool flag=0;

void close( void)
{
.......

cs_low_fm = 0;
flag=1; //dyjc set value
.......

}

two,

void RTC_IRQHandler(void) // RTC interrupt is executed once per second
{
.......
dyjc++; // (2)

if(flag){

dyjc = 1190; // (1)

flag=0;

}
.......

}

This post is from stm32/stm8
 
 
 

Guess Your Favourite
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

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