CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Equivalent of pgm_read_byte

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
cvargcal



Joined: 17 Feb 2015
Posts: 134

View user's profile Send private message

Equivalent of pgm_read_byte
PostPosted: Sun Apr 19, 2020 4:47 pm     Reply with quote

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: 9097
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sun Apr 19, 2020 7:39 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sun Apr 19, 2020 7:39 pm     Reply with quote

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: 19215

View user's profile Send private message

PostPosted: Mon Apr 20, 2020 2:54 am     Reply with quote

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: 9097
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon Apr 20, 2020 5:56 am     Reply with quote

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: 19215

View user's profile Send private message

PostPosted: Mon Apr 20, 2020 6:17 am     Reply with quote

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....
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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