Multiple #pragma lines

Discussion in 'Embedded Systems and Microcontrollers' started by blah2222, Apr 30, 2013.

  1. blah2222

    Thread Starter Well-Known Member

    May 3, 2010
    Hi all,

    Looking into some C18 code I came across this code snippet before the main code:

    Code ( (Unknown Language)):
    2. // Define the globals for the USB data in the USB RAM of the PIC18F*550
    3. #pragma udata
    4. #pragma udata USB_VARIABLES=0x500
    5. unsigned char ReceivedDataBuffer[64];
    6. unsigned char ToSendDataBuffer[64];
    7. #pragma udata
    9. USB_HANDLE USBOutHandle = 0;
    10. USB_HANDLE USBInHandle = 0;
    11. BOOL blinkStatusValid = FLAG_TRUE;
    Why is #pragma udata called three times with two of those times having nothing to the right of it and why don't the unsigned char's have #pragma udata in front of them?

    Thank you,
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    #pragma is a magical C command that bows to non-portability. It is a pragmatic way to define information that falls outside of the general C specification. Here it is used to direct where variables are stored memory. Generally this is knows as a "#pragma sectiontype" declaration. There are 4 types in C18:

    program memory

    • code - contains executable instructions
    • romdata - contains variables and constants

    data memory

    • udata - contains statically allocated uninitialized uservariables
    • idata - contains

    We have 2 forms here:
    Code ( (Unknown Language)):
    1. #pragma udata
    "udata" is uninitialized data, the data that C does not initialize in any way, which is something C normally does by default. The pragma instructs the compiler that all variables to follow (unless later instructed otherwise) are to be uninitialized.
    Code ( (Unknown Language)):
    1. #pragma udata {Label}={address}
    This further defines where the data will be stored. It defines a section {Label} that starts at {address} There needs be a linker script to define this memory section.

    Putting two sectiontypes together like so:
    Code ( (Unknown Language)):
    1. #pragma udata
    2. #pragma udata USB_VARIABLES=0x500
    is redundant, the first sectiontype may be deleted.

    See the userguide for further info.