3666 views|9 replies

5

Posts

0

Resources
The OP

How to jump out of the double loop, encryption program and other issues [Copy link]

 
Question 1: Is there a better way to jump out of a double loop? For example, there is a while loop in a for loop waiting for a certain bit to change, but you are afraid that it will be interrupted and stuck in the while loop, so you count in the while loop: Solution 1: Use goto: while (judgment) { if (i++>30000) goto xxx} It feels very convenient, but teachers and some books say that it is best not to use goto Solution 2: while (judgment) { if (i++>30000) {i1=1; break;} } if (i1==1) break; // (jump out of the for loop here) I would like to ask if there are other ways to jump out of the double loop. Question 2: Is it better to jump into the function directly or to open the function entrance? For example, the key scanning program directly enters KEY_Deal() after obtaining the key value. //Assume that this is the key processing program solution 2: if(SWITCH==0;) return; //This is one of the main program scanning programs. After obtaining the key value, assign SWITCH, open the key processing entrance, let the microcontroller scan over to process the key value, and then the next sentence is KEY_Deal(); My understanding is that solution 2 can reduce the stack and will not affect the key processing speed in a relatively simple main program. Question 3: Will including too many .h files increase the code length? I am now using the tutorial method to include a file called global.h, for example, which includes all the .h files used in the project, and then all .c files only need to include this global.h. I have seen other people's programs include the .h files used in each .c file. Will the global.h solution increase the code length, or will there be other problems? It feels very convenient. Question 4: I want to check whether the data in certain locations of FLASH (such as 0x8555 or something like that) is assigned by myself when the power is turned on for the first time, and then write multiple data to these flash locations when the program is powered on for the first time. This is to prevent copying and burning files. Is this method feasible? I don’t know much about program encryption, please give me some advice. At present, I am learning to master C and understand various electronic components and circuits. The next step is to run embedded systems, learn ARM machines and Linux systems. If there is anything wrong with my idea, please give me some advice.

This post is from Analog electronics

Latest reply

This post was last edited by huo_hu on 2018-9-7 19:37 You don't need to erase when writing 0, but you must erase and encrypt when writing 1. You can only rely on the manufacturer. It doesn't make much sense to do it yourself.   Details Published on 2018-9-7 19:36

6

Posts

0

Resources
2
Is Linux still widely used?
This post is from Analog electronics

874

Posts

1

Resources
3
1. Why don't you recommend using goto? Because using it will make the program less maintainable. It's OK if the program is small, but if the program is large, the maintainer will cry if you jump around like this. 2. You can try using switch case statements. 3. Your habit is not very good. Generally, one c file corresponds to one .h file, so the program is more portable and easier to maintain. 4. I don't understand what you want to ask in your fourth question, so I can't answer it.
This post is from Analog electronics

4002

Posts

0

Resources
4
This post was last edited by huo_hu on 2018-9-7 13:04 while(judgment && i>30000 ){ i++; } //After while ends, add judgment if (i>30000) { Here is the goto code} else {Here is the while exit code}

This post is from Analog electronics

4002

Posts

0

Resources
5
You can use goto, no problem, just don't goto outside the function.
This post is from Analog electronics

5

Posts

0

Resources
6
huo_hu posted on 2018-9-7 13:06 You can use goto, no problem, just don't goto outside the function
OK, I also think that if you just want to jump out of the loop, goto is fine, and it doesn't affect the readability of the program
This post is from Analog electronics

5

Posts

0

Resources
7
Cao Wei 1993 posted on 2018-9-6 20:04 1. Why is it not recommended to use goto? Because after using this, the maintainability of the program becomes worse. It is okay for a small program, but when the program is large, you jump around like this, and then...
OK, I will change the third habit and only let main.c include all .h files
This post is from Analog electronics

4002

Posts

0

Resources
8
Question 2: Is it better to jump into the function directly or to open the function entry? There is no such operation as jumping into the function. The main problem is that there are stack push and pop operations before the function call. Even if there are no parameters and no return values, it will not work. The situation is more complicated, so it is better to let the compiler arrange it. Solution 2: if(SWITCH==0;) return; //This is one of the main program scanning procedures. After obtaining the key value, assign SWITCH, open the key processing entry, let the microcontroller scan over to process the key value, and then the next sentence is... If there is if(SWITCH==0;) return; in the KEY_Deal() function, then you are adding unnecessary details. Question 3: Will including too many .h files increase the code length? Too much content in header files will affect the compilation speed (just a little bit), but it will not affect the code. However, too many header files may have unexpected duplicate names or conflicts. It is of course better to include only the minimum header files without any trouble. Question 4: I want to check whether the data in certain locations of FLASH (such as 0x8555 or something arbitrary) is assigned by myself when the power is turned on for the first time, and then write multiple data to these flash locations when the program is powered on for the first time. This is to prevent copying and burning files. Is this method feasible? I don’t know much about program encryption, so please give me some advice. The data can be defined with define. There are several problems with your method: First, the modification of flash must be erased before writing. Erasing must be done by block, one block at a time. Even if you only write two bytes, you must first cache the data in the original block. The size of stm32 blocks is not uniform. Some blocks at some locations have a size of 32K, which is very troublesome. Second, if someone copies all the contents in your flash, then even the marks you made are copied together. How do you define when it is the first time to power on and execute?
This post is from Analog electronics

赞赏

1

查看全部赞赏


5

Posts

0

Resources
9
huo_hu posted on 2018-9-7 15:14 Question 2: Is it better to jump into the function directly or to open the function entrance? There is no such operation as jumping into the function. The main problem is that there is a stack before the function call...
I use STM8. I can write directly to the flash without block operation. After writing the encryption method, I think it is wrong. I thought before that I would write data to E2PROM after the first power-on, such as writing F to 0x4000. During initialization, I judged whether it was normal for the first power-on based on the 0x4000 data. I ignored that if even E2PROM was read out, the encryption failed. I feel that my encryption thinking is very limited. I don’t know where I can refer to other people’s program encryption methods.
This post is from Analog electronics

4002

Posts

0

Resources
10
This post was last edited by huo_hu on 2018-9-7 19:37 You don't need to erase when writing 0, but you must erase and encrypt when writing 1. You can only rely on the manufacturer. It doesn't make much sense to do it yourself.
This post is from Analog electronics

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Related articles more>>

    EEWorld
    subscription
    account

    EEWorld
    service
    account

    Automotive
    development
    circle

    Robot
    development
    community

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