2560 views|9 replies

7815

Posts

57

Resources
The OP
 

[Old post 3] You want to specify the address of a variable? In fact, it needs a shared memory mechanism [Copy link]

As usual, I look at the old posts - here, I apologize to the authors of these old posts at once: to facilitate my memory, I added some words before the title of each old post, namely CHECKED MARKED LEFT. CHECKED means I have read it; MARKED means I have dug into this old post, which is what I am doing now. LEFT means I will MARKED it later, but I will mark it today. No matter what, the author of any post should be respected, but these posts are basically more than 10 years old, and the authors and those enthusiastic hosts who answered may have long gone. I will think of a new way to organize it as soon as possible to avoid offending you in this way. Okay, let's get back to the topic. This time I'm talking about this post. [url=【MARKED】When defining global variables in C language, how to specify the address of the variable when defining the variable https://en.eeworld.com/bbs/forum ... 7922&fromuid=115166 (Source: Electronic Engineering World-Forum)]When defining global variables in C language, how to specify the address of the variable when defining the variable [/url] This content was created by EEWORLD forum user Xin Xin. If you need to reprint or use it for commercial purposes, you must obtain the author's consent and indicate the source


This post is from Programming Basics

Latest reply

To be able to write such a program, you must be very familiar with hardware and memory space allocation. Otherwise, it is easy to get lost in it  Details Published on 2018-9-6 22:24
Personal signature

强者为尊,弱者,死无葬身之地

 

7815

Posts

57

Resources
2
 
First of all, regarding this title, this is a complete question in itself. I can first give a simple answer that in "normal C", this is not possible in theory, because the allocation of variables occurs at compile time, on the stack, and only the heap space can be freely allocated and reclaimed by the programmer. But even so, the programmer still cannot specify the address. However, in the microcontroller, for a reason that I still can't explain clearly, damn it, it can. Although the method used is a little unfamiliar to novices, it is roughly in the form of: (int *)0x0001023 ..... Yes, I admit that when I saw this title, my first reaction was that he thought he was asking this question. Even after reading the description of the host in the topic building, we all think so. Fortunately, although the host didn't say much, probably less than 100 words, he soon mentioned the original reason for doing so
I am now writing a program for a single-chip microcomputer. Due to the tight project schedule, I have divided the work between two people. The younger one does not know assembly language, so he has to write the program in C. The other one is very good at assembly language, but does not know C language. Our current idea is that the two of us will write the program together and finally run it together. We want to achieve data interaction through a public data space
This post is from Programming Basics
 
Personal signature

强者为尊,弱者,死无葬身之地

 
 

424

Posts

8

Resources
3
 
To be able to write such a program, you must be very familiar with hardware and memory space allocation. Otherwise, it is easy to get lost in it
This post is from Programming Basics
 
 
 

7815

Posts

57

Resources
4
 
This post was last edited by Xin Xin on 2018-9-6 22:33 I will not mention the answer to this question, although I did not read the answer below carefully. But for this question itself, it is no longer a problem for me, and I don’t even need to specify any addresses. So, my thoughts quickly turned to another question. The question of asking questions and communication. In fact, in life, this kind of thing does happen to us very often - The problem we say is often not the same as the problem we really face. After a period of thinking and preliminary attempts, if we cannot solve a practical problem we face, we will more or less get our own preliminary solution direction for this problem. For example, the host was influenced by assembly language - I can almost guess that the host’s understanding of C language at that time was far less than his familiarity with assembly language. Because for this problem, people like me who have been writing C for many years and are completely accustomed to the C way of thinking will immediately think that it doesn’t matter where the memory address is allocated. What’s important is that we can directly get its address (when it is a left value) or the memory block where it is located (when it is a right value) by referencing the variable name (more precisely, from a broader perspective, I prefer to use identifiers to express this concept). When I was studying 51, I did learn and was very proficient in assembly. As a student, I wrote an assembly program with 3,000 lines of code. But because the time I used assembly was relatively short, I even forgot that there is no concept of variables in assembly, and there is no real sub-function. In assembly, everything is just an address and the content pointed to by the address. Whether it is a function, an array, or a memory space, we record the address value through an identifier. Then read and write the address and the content pointed to by the address through basic assembly instructions, that’s all. I’ve gone too far. Back to the point, I actually understand why the host understands the realization of data coupling through shared memory as a problem of specifying the address when allocating variables. But——


This post is from Programming Basics
 
Personal signature

强者为尊,弱者,死无葬身之地

 
 

7815

Posts

57

Resources
5
 
Please note that I didn't think about this until I saw the post of the original poster. My first reaction was to tell him that in a microcontroller, you can do this and that. I never thought that, in fact, for his question, this thing could be avoided. This is why someone gave a similar answer to me in the first post.
This post is from Programming Basics
 
Personal signature

强者为尊,弱者,死无葬身之地

 
 

7815

Posts

57

Resources
6
 
lehuijie posted on 2018-9-6 22:24 To be able to write such a program, you must be very familiar with hardware and memory space allocation. Otherwise, it is easy to get lost in it
This is actually not difficult. If you write a lot of microcontroller programs, you will encounter this problem. You can understand it by searching Baidu. As for C, its foundation is actually the two keywords address and memory.
This post is from Programming Basics
 
Personal signature

强者为尊,弱者,死无葬身之地

 
 

7815

Posts

57

Resources
7
 
It was not until the 5th post that I saw an answer similar to what I had found:
If it is multi-threaded or multi-CPU, you can do this. If it is a single CPU and single thread, then it is best to use C as the main program, write the module in assembly, follow the C calling convention to call, and use parameters to pass data. [/quote] But this person's answer is indeed more complete than my first reaction. As simple as it is, multi-threading is rarely encountered in single-chip programs. Of course, few people notice that in the most basic single-chip bare metal program, even if there is only one basic timer interrupt, in fact, this program is already a two-thread code. Then, inspired by this answer, the author continued to further describe the environment he faced [quote] Thank you all for your help, I am very grateful! Now my idea is that I will write the main program, and then my colleague will mainly write some interrupts, running on the same MCU. The microcontroller used is MSP430. When an interrupt occurs, the interrupt program written by my colleague is triggered. After the interrupt program is processed, the processed data in the interrupt is placed in a storage area. Then I will fetch the data in that storage area in the main program, and then perform the corresponding operation. There are also some tasks such as LCD display, and I also plan to do this. For example, what kind of data do I want to display? I first write the data in the storage area, and then call the assembly program written by my colleague. The assembly program of my colleague takes out the data in the specified area and then displays it on the LCD. How about this method? Is there anything to pay attention to? Also, where should I put the selected public storage area? Will my public storage area be destroyed due to stack allocation during the program running? I hope experts can help me.
This post is from Programming Basics
 
Personal signature

强者为尊,弱者,死无葬身之地

 
 

7815

Posts

57

Resources
8
 
First of all, I think 5L's answer is basically impeccable. There is nothing to say. Then, what about the further supplement to the original poster? In fact, his question is nothing special. The only thing to note is that if the refresh frequency of the display screen is not selected appropriately, then it is very likely that the data you see is not the latest data. This is not difficult to solve. That is, you can refresh the display when the data is updated. The current single-chip microcomputer is very fast, not to mention that this is a 16-bit MSP430, I think the speed is completely fine. Moreover, unless your LCD driver is processed in an interrupt - if so, I think it is probably unnecessary. In short, I think that in this case, you can just call the shared data directly. If you want to handle it more simply - that is, you don't even need to use this public data area directly to couple the two modules, then it can be even simpler. Just make the display refresh code into a function call, as a callback function, and call a refresh directly every time the data in the public data area is updated or other strategies (such as timing strategies), and the matter is over. - In fact, I think I may have complicated the problem because of my guess. But even so, what I mean is that I still didn't encounter any difficulties, and there was no need to specify any address at all.
This post is from Programming Basics
 
Personal signature

强者为尊,弱者,死无葬身之地

 
 

7815

Posts

57

Resources
9
 
Final words First of all, I think this kind of "the problem mentioned is not actually the fundamental problem" is very common. Many times, this often happens between sales and customers. Because sometimes people don't really understand, or can't really express their ideas accurately. Or to put it more exaggeratedly - it's because people can't express themselves in a language that the listener can correctly understand. At this time, what we need to do is not to teach you this and that, and you don't need to improve your speaking ability and comprehension ability. In fact, what we need to do is patience. For example, the host, in fact, he is in the theme building, although he has a bad title, but his brief further supplement has fully expressed the real problem he faces. This is very good. In addition, people who read this post need patience. Frankly speaking, when I stayed away from the board and stopped posting, I became impatient with simple problems and messy problems. I have no patience for problems that can't bring me fame and money... Earlier, in fact, many times I didn't really understand the problem, and it was very likely that I had a preconceived idea when I saw the title at first glance. I don't mean any disrespect, but I think the two brothers in the sofa bench post are actually the same. The description in the topic post is actually enough to expose the problem. Any experienced MCU C programmer can give the same answer as 5L. Including myself, today, facing this old problem, I am calm and calm. After taking a shower, washing clothes, waiting for a break, and giving instructions on a post at will, I still ignore the description in the topic post in my first reaction. Therefore, you must be patient. Not only the person asking the question should be patient, but the person listening should also be patient, and then skills and wisdom.
This post is from Programming Basics
 
Personal signature

强者为尊,弱者,死无葬身之地

 
 

7815

Posts

57

Resources
10
 
I seem to be criticizing these posts that are so old, but in fact, I am encouraging myself. Originally, I regarded this activity more as a task, because these questions were either impossible to answer because I did not understand the specific background. Or for me today, basically 99% of them are no longer a problem. However, after writing the third post, I found that I gained a lot of things beyond technology.
This post is from Programming Basics
 
Personal signature

强者为尊,弱者,死无葬身之地

 
 

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