Datasheet

PICkit™ 3 Debug Express Lessons
© 2009 Microchip Technology Inc. DS41370C-page 25
3.3 LESSON 3: ROTATE LED
This lesson builds on the previous two lessons to introduce defining global variables
and code sections, and to add rotation to the LED display. It will light up LED 0, then
shift it to LED 1, then to LED 2 and on up to LED 7, and back to LED 0.
In this and following lessons, please open the lesson workspace in the MPLAB IDE
upon starting the lesson.
3.3.1 Allocating File Register Memory
In the source code file 03 Rotate LED.c for Lesson 3 the global variable,
LED_Number, is declared as in Figure 3-16.
FIGURE 3-16: LESSON 3 GLOBAL VARIABLE DECLARATION
The directive #pragma udata is used prior to declaring the variable LED_Number to
indicate to the compiler that the following declarations are data variables that should be
placed in the PIC18FXXXX file registers. This differs from PC compilers where
instructions and variables share the same memory space due to the Harvard
architecture of the PIC18FXXXX as discussed in Section 2.1 of this document.
There are two directives for use with #pragma when defining variables:
Data declarations can also be given a section name. The section name may be used
with a Linker Script SECTION entry to place it in a particular area of memory. See
Section 2.9 of the “MPLAB C18 C Compiler User’s Guide” (DS51288) for more
information on using sections with Linker Scripts. Even without a Linker Script section,
the #pragma udata directive may be used to specify the starting address of the data
in the file registers. For example, to place LED_Number at the start of file register Bank
3 declare the udata section as:
#pragma udata mysection = 0x300 unsigned char
LED_Number; // 8-bit variable unsigned int
AnotherVariable;
Other variables declared in a udata or idata section will be placed at subsequent
addresses. For instance, the 16-bit integer
AnotherVariable above would occupy
address 0x301 and 0x302
.
Note that function local variables will be placed on the software stack.
Key Concepts
- The directives #pragma udata and #pragma idata are used to allocate
memory for static variables in the file registers.
- The directive #pragma code is used to indicate a section of instructions to
be compiled into the program memory of the PIC18FXXXX.
- The directive #pragma romdata is used for constant (read-only) data
stored in the program memory. This is used with the keyword rom.
- Constant data can be stored in program memory so as not to use up file
register RAM.
udata Uninitialized data. The following data is stored uninitialized in the file register
space.
idata Initialized data. The following data is stored in the file register space. The initial-
ization values are stored in program memory, and then moved by the start-up
initialization code into file registers before program execution begins.
/** V A R I A B L E S *************************************************/
#pragma udata // declare statically allocated uninitialized variables
unsigned char LED_Number; // 8-bit variable