⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 example_place_constants_inpm.c

📁 Mplab C30编译器
💻 C
字号:

/* Example illustrating how to locate a constant at a specific address 
   in Program Memory */

 /***********************************************************************
 *  In this example, the constant table is located at a specific address*
 *  in program memory. When a constant is specifically placed at an     *
 *  address in program memory, it must be placed in its own PSV section *
 *  using the space(psv) attribute. If a device has only one PSV page   *
 *  (16K instruction words or less), the (psv) section and (auto_psv)   *
 *  section will share the same PSV page by default.                    *
 *                                                                      *
 *  The __builtin_tblpage() and __builtin_tbloffset() helper functions  *
 *  can be used to find the address of a constant stored in program     *
 *  memory. If you want to access constant table, you must manually     *
 *  manage PSVPAG and restore its original value. The __builtin_psvpag()*
 *  helper function may be used to assist with this task.               *
 *                                                                      *
 *  Note: It is not possible to place a constant at a specific address  *
 *        in Program Memory using the space(auto_psv) attribute. Only   *
 *        the space(psv)attribute may be used to perform this task.     *
 *                                                                      *
 *  You can view the results of the printf statements by enabling the   *
 *  MPLAB SIM tool then enabling the UART1 I/O with Output set to       *
 *  Window. The printf results are then displayed in the output window. *
 *                                                                      *
 *  A defined heap size is required since we are using a standard I/O   *
 *  function.                                                           *
 *                                                                      *
 *                                                                      *
 *  For additional information about dsPIC architecture and language    *
 *  tools, refer to the following documents:                            *
 *                                                                      *
 *  MPLAB C30 Compiler User's Guide                        : DS51284    *
 *  dsPIC 30F MPLAB ASM30, MPLAB LINK30 and Utilities                   *
 *                                           User's Guide  : DS51317    *
 *  Getting Started with dsPIC DSC Language Tools          : DS51316    *
 *  dsPIC 30F Language Tools Quick Reference Card          : DS51322    *
 *  dsPIC 30F 16-bit MCU Family Reference Manual           : DS70046    *
 *  dsPIC 30F Programmer's Reference Manual                : DS70030    *
 *                                                                      *
 *  Example file has been compiled with MPLAB C30 v1.30.00              *
 *                                                                      *
 ************************************************************************
 *                                                                      * 
 *    Author:                                                           *
 *    Company:                                                          * 
 *    Filename:       Ex_Locate_Constant_inPM.c                         *
 *    Date:           02/01/2005                                        *
 *    File Version:   1.00                                              *
 *    Other Files Required: p30f6014.gld                                *
 *    Tools Used: MPLAB IDE -> 7.01.00                                  *
 *                Compiler  -> 1.30.00                                  *
 *                Assembler -> 1.30.00                                  *
 *                Linker    -> 1.30.00                                  *
 *                                                                      *
 ***********************************************************************/

 /* Include the appropriate header (.h) file, depending on device used */
 /* Example (for dsPIC30F5013): #include <p30f5013.h>                  */
 /* Alternatively include the generic header file, p30fxxxx.h, as shown 
    below.                                                              */

	#include "p30fxxxx.h"
	#include "stdio.h"

const unsigned __attribute__ ((space(psv), address (0x2000)))
table[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};


int main( void )
{
	unsigned psv_shadow;
	unsigned sum=0, u;
	long addr;

	/* enable PSV functionality */
	CORCONbits.PSV=1;

	/* compute the address of table and print it */
	addr = ((long) __builtin_tblpage(table) << 16) +
	__builtin_tbloffset( table );

	/* print the address of table */
	printf ("table[ ] is stored at address 0x%lx\n", addr);
	
	/* save the PSVPAG */
	psv_shadow = PSVPAG;
	
	/* set the PSVPAG for accessing table[] */
	PSVPAG = __builtin_psvpage ( table );

	/* sum the values in table[] */
	for (u=0; u<10; u++) {
		sum += table[u];
	}

	/* restore the PSVPAG for the compiler-managed PSVPAG */
	PSVPAG = psv_shadow;
	
	/* print the sum */
	printf ("sum is %d\n", sum);

	while ( 1 );
}

/*  The equivalent constant definition for the array table in assembly 
    language appears below. The .align directive is optional and represents 
    the default alignment in program memory. Use of * as a section name 
    causes the assembler to generate a unique name based on the source file 
    name.

        .section *,address(0x2000),psv
        .global _table
        .align 2
_table:
        .word 0,1,2,3,4,5,6,7,8,9

   In order to allocate table in data memory, the space(psv) attribute could 
   be changed to space(data). In this case, the specified address would be a 
   data memory address. In the absence of a space attribute, the keyword 
   const directs the C compiler to allocate the variable in the same space as 
   other compiler constants. Constants are allocated in program memory by 
   default, or in data memory if the constants-in-data memory model is selected. */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -