|
|
View previous topic :: View next topic |
Author |
Message |
art
Joined: 21 May 2015 Posts: 181
|
EX_EXTEE.C question (2) |
Posted: Fri May 19, 2017 5:06 pm |
|
|
Hi ,
I'm trying to understand EX_EXTEE.C example.
I have few questions, and I need someone to explain what does this below code means :
1. Code: |
#if sizeof(EEPROM_ADDRESS)==1
address = gethex();
|
2. Code: |
#if EEPROM_SIZE>0xfff
|
using eeprom 2416 the last address should be 7ff. Why it is 0xfff
3. Code: |
address = (address<<8)+gethex();
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
Re: EX_EXTEE.C question (2) |
Posted: Fri May 19, 2017 6:07 pm |
|
|
art wrote: |
1.
#if sizeof(EEPROM_ADDRESS)==1
address = gethex();
|
The section above checks if the eeprom is a small eeprom, where the
highest address can be contained in one byte. Example:
Quote: | 2401.c
#define EEPROM_ADDRESS BYTE |
art wrote: |
2.
#if EEPROM_SIZE>0xfff
using eeprom 2416 the last address should be 7ff. Why it is 0xfff
|
The last address for 2416 is 0x7FF. So, the above code says
"don't execute this section if you have a 2416".
Instead, it executes the section below, where it gets a single hex digit.
This gets the MSB, which is 7:
Code: | address = gethex1(); |
art wrote: |
3. address = (address<<8)+gethex();
|
This takes the MSB (0x07) and shifts it into the upper byte in a 16-bit
address. It then gets the LSB (0xFF) and puts it into the lower byte
position. Then you get 0x07FF as the result.
They are writing the #if #else #endif statements to make the program
work with many different sizes of eeproms. The #define statements
for EEPROM_SIZE and EEPROM_ADDRESS are given in the individual
driver files, such as 2401.c, 2416.c, 24256.c, etc. |
|
|
art
Joined: 21 May 2015 Posts: 181
|
|
Posted: Fri May 19, 2017 7:13 pm |
|
|
Thanks PCM for the explaination.
If i use 24256 eeprom, it seems that i can eliminate all the lines and use only
Code: |
address = gethex();
|
Is it correct? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri May 19, 2017 8:41 pm |
|
|
No. That function only gets one byte (two hex digits). The eeprom size
of a 24LC256 is given in the file, as shown below. It is 0x7FFF in hex.
That's two bytes.
Quote: | 24256.c
#define EEPROM_SIZE 32768 |
This is the code required to get two bytes of address:
Code: |
address = gethex(); // Get the MSB
address = (address << 8) + gethex(); // Get LSB and combine with MSB
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19588
|
|
Posted: Fri May 19, 2017 11:20 pm |
|
|
It's perhaps worth understanding that the lines are eliminated for you.
Inside the actual code, 'if' etc., have code associated with them. So if something is not used, it is worth seeing if you can eliminate it.
However the stuff here is not actually 'code', but 'preprocessor directives'
#IF, is a directive to the compiler saying 'when compiling do this'. Note the 'when compiling'.
So the same source file will generate the code for two, three or four address nibbles, depending on the address being (in hex) 0 to FF, 100 to FFF, or 1000 to FFFF.
As PCM_programmer shows, you have to 'walk through', and put the value that your EEPROM has into the tests, to see what code would actually be compiled.
So if the EEPROM_ADDRESS will fit into one byte you just get:
Code: |
address = gethex();
|
If the size is > 0xFFF, you get:
Code: |
address = gethex();
address = (address<<8)+gethex();
|
While if it is between these two sizes (so 0x100 to 0xFFF), you get:
Code: |
address = gethex1();
address = (address<<8)+gethex();
|
If you then look at gethex, versus 'gethex1', you see that the latter returns just one hex digit, while the former gives two. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|