Has any one ever declare variable by give address? Such as I need to declare the variable name "temp" at address 0x0180. How to implement it in C ? I tried to find it in manual, but never help.
Well you can certainly assign 0x180 to a pointer and then write through that to write a char/int/long value (depending on pointer type) at 0x180 onwards but this is VERY dangerous in a C program. How do you know that the compiler hasn't already located one of the other variables at location 0x180?
About the ONLY reason I can think of for needing variables at absolute addresses is when you want to run two co-operating programs (a bootloader and an application) in the same AVR. But if you are doing this there's far better methods than just agreeing on some common address that you don't know the existing usage of!
(to pass a value from boot to app an unused SFR is probably a far better idea)
About the ONLY reason I can think of for needing variables at absolute addresses ...
You can put lipstick on a pig, but it is still a pig.
I've never met a pig I didn't like, as long as you have some salt and pepper.
I need to send out more variable value on the terminal not array variable, my idea if I could declare the variable by give them the address, it is easy to use the pointer to point them for send out to the terminal. Actually if we declare variable, the complier will automatically give them the address, but not a sequence. I need to do in sequence.
variables+sequences = arrays ?!?
Doesn't the C compiler do an equally valid job of locating arrays for you as it does for individual variables then?
Different C compilers will have different mechanisms.
For the "in sequence" part, many compilers WILL allocate memory in sequence. But it might depend.
The short answer is that I'm sure that many of us have had an application similar to yours, and rarely or ever resorted to specifically allocating at an address. There are many alternatives. A structure comes to mind. Arrays as well, but you have discarded them for some reason. Drop into ASM and use .ORG . Write the whole thing in ASM (right, js?). Each in its own linker section, and then place them where you desire. Or, with my compiler, just put them next to each other in the order desired.
Or, with my compiler, just put them next to each other in the order desired.
I ever do like this in some other C complier, it provide me "_directmap" command, like this
_directmap unsigned char temp = 0x0180;
_directmap unsigned char temp = 0x0180;
I found this methods in manual, after tried it work fine. So, I never consider on a structure. I will try on structure.
Have you told us yet which compiler you are using?
And I still don't understand how locating things at absolute addresses makes them any easier to send to the terminal? If you need to know the location of a variable (that the compiler assigned for you) what's wrong with the "address of" operator '&' ? (unless it's an array in which case the variable name itself provides its address as it's really a pointer already)
I'm guessing, Cliff, that OP is building some kind of string or packet and doesn't want to rebuild from scratch each time. Sure would be easier if we knew the destination when we pack for the journey. [Which compiler, work so far, desired output, problem areas, ...]
Aaaah--so we now finally know it is GCC.
Those users frequent here, but you might re-phrase and re-post on the GCC forum. Have you done a good search for prior discussions? IIRC there have been several.
 Yup, a search of the GCC forum for "absolute address" quickly uncovers a number of pertinent threads.
Yeah but the majority of those threads basically say (like this one) - "why bother?" This whole things seems to be an arseways solution to a problem that doesn't actually exist
I know how to write circular buffers in both C and assembler. The issue here is that by making the buffer 2^n bytes long and positioning it at a 2^n byte boundary, the compiler doesn't need math to do the wrapping. The code:
index = (index+1) & 0x1f;
will actually require mathematical tests if the first byte of "buffer" doesn't sit at an address for which the lowest 5 bits are zero.
How can I ensure that "buffer" will be placed to meet this condiction?
I just split the post you made to this thread to here:
as it has NOTHING ot do with the existing subject of this thread - please start new threads in future or append to on topic threads.
I thought it relevant because I also require a variable stored at an absolute address, or at least one that meets a specific condition. However, if that isn't close enough, then thanks for splitting it.
No this is C - the linker decides where to position things in memory - not the programmer (usually) - but let's continue this in the new thread...
© 2019 Microchip Technology Inc.