View previous topic :: View next topic |
Author |
Message |
cvargcal
Joined: 17 Feb 2015 Posts: 134
|
Equivalent of pgm_read_byte |
Posted: Sun Apr 19, 2020 4:47 pm |
|
|
Hi, an example arduino use this line:
Code: | char username = "";
if (pgm_read_byte(username) != 0) |
What is the equivalent of pgm_read_byte in ccs or how i can write this line?
thanks |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9243 Location: Greensville,Ontario
|
|
Posted: Sun Apr 19, 2020 7:39 pm |
|
|
A very quick google and that function appears to read from EEPROM. Now depending on what PIC you're using, it could be program flash or EEPROM memory. Not all PICs have both and allow read/writes to those areas ! CCS has functions for both those type of memory.
Which to use depends on, again, what PIC are you using and what and how the data is for. A 'username', I'd probably store in EEPROM not the actual program code space. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Apr 19, 2020 7:39 pm |
|
|
In the Arduino examples I see on the net, they just want to read a constant
string. Is that what you want to do ? In CCS, we don't need a special
function to read a byte from a 'const' array.
Explain what you want to use the pgm_read_byte() function for.
Post some code that completely shows what you want to do. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19538
|
|
Posted: Mon Apr 20, 2020 2:54 am |
|
|
PCM_Programmer has 'put his finger on it'.
In the Arduino, the syntax if you want to place a string into the program
memory, and not automatically copy it 'out' on boot, is:
const char username = "" PROGMEM;
The reason for this is that on the Arduino, the default meaning of 'const'
is the ANSI meaning, where this is a RAM array, which is protected against
being changed. Using the PROGMEM directive instead keeps it in the ROM.
Problem then is that because by default arrays are in RAM, even if declared
'const', the normal access routines don't work, and you have to use
the pgm_read_byte routine to access this array.
In CCS, the default is the other way round. A 'const' array in CCS by
default is held in ROM, and the compiler 'knows' this and will automatically
access it as such. Now the only thing you need to do, to access it using
a 'pointer', is to have the declaration:
#device PASS_STRINGS=IN_RAM
near the top of your code. This then tells the compiler to allow 'pointer'
type accesses to the ROM memory, and with this you can just use:
if (*(username) !=0)
and test the byte.
Now the only 'query' at this point is that this byte is implicitly always
going to be '0', since the string is declared as a zero length string and
will just be stored as a '\0' terminator in memory. Presumably the
intention is to actually have an array of such names, with the last one
being declared like this so the test can be used to determine when this
is reached. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9243 Location: Greensville,Ontario
|
|
Posted: Mon Apr 20, 2020 5:56 am |
|
|
just a comment...
Using 'const'ant to describe a variable located in RAM seems to be the 'wrong' word. RAM by it's nature, can change whereas EEPROM and ROM is kinda permanent. Some kind of random glitch or EMI can change RAM (seen that), though data in EEPROM remains unaffected.
Jay |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19538
|
|
Posted: Mon Apr 20, 2020 6:17 am |
|
|
Yes, but that is the ANSI meaning.
If you think about it, code being written for devices like the PC, unless
for things like the BIOS, do not actually have any capability to store
things in ROM. So const has to be held in RAM. On the PC, the hardware
memory management can be used to try to ensure the value cannot
be changed, but as you say there is always the potential for a value to
become glitched.
That is life.... |
|
|
|