プロセッサ ヘッダファイル (Processor header file)

SFR (Special Function Register) 名をコード中で使用するためには、各プロセッサに対応したヘッダファイルを読み込む必要があります。

#include <p30fxxxx.h>   // xxxxは、プロセッサのデバイス番号

なおこのヘッダファイルは、既定ではC:\Program Files\Microchip\MPLAB C30\support\dsPIC30F\hにあります。

このファイル内では次のような定義がなされており、SFR名を変数名のようにしてレジスタにアクセスできるようになります。

/* CORCON: CPU Mode control Register */
extern volatile unsigned int CORCON __attribute__((__sfr__));

typedef struct tagCORCONBITS {
        unsigned IF     :1;     /* Integer/Fractional mode              */
        unsigned RND    :1;     /* Rounding mode                        */
        unsigned PSV    :1;     /* Program Space Visibility enable      */
        unsigned IPL3   :1;     /* CPU Interrupt Priority Level bit 3   */
        unsigned ACCSAT :1;     /* Acc saturation mode                  */
        unsigned SATDW  :1;     /* Data space write saturation enable   */
        unsigned SATB   :1;     /* Acc B saturation enable              */
        unsigned SATA   :1;     /* Acc A saturation enable              */
        unsigned DL     :3;     /* DO loop nesting level status         */
        unsigned EDT    :1;     /* Early DO loop termination control    */
        unsigned US     :1;     /* Signed/Unsigned mode                 */
        unsigned        :3;
} CORCONBITS;

extern volatile CORCONBITS CORCONbits __attribute__((__sfr__));
__attribute__((__sfr__))は、変数がSFRであることを宣言するMPLAB Cコンパイラのキーワード

レジスタの各ビットにアクセスするには次のように記述します。

CORCONbits.IF

または次のようなマクロが定義されているので、「_IF」と記述することもできます。

/* CORCON */
#define _IF     CORCONbits.IF
#define _RND    CORCONbits.RND
#define _PSV    CORCONbits.PSV
#define _IPL3   CORCONbits.IPL3
#define _ACCSAT CORCONbits.ACCSAT
#define _SATDW  CORCONbits.SATDW
#define _SATB   CORCONbits.SATB
#define _SATA   CORCONbits.SATA
#define _DL     CORCONbits.DL
#define _EDT    CORCONbits.EDT
#define _US     CORCONbits.US

マクロ (Macro)

プロセッサ ヘッダファイルには、上記のSFRに加え次のマクロが定義されています。

インラインアセンブリコード定義 (inline assembler instructions)

#define Nop()    __builtin_nop()
#define ClrWdt() {__asm__ volatile ("clrwdt");}
#define Sleep()  {__asm__ volatile ("pwrsav #0");}
#define Idle()   {__asm__ volatile ("pwrsav #1");}

データメモリ割り当て (allocating data memory )

#define _XBSS(N)    __attribute__((space(xmemory),aligned(N)))
#define _XDATA(N)   __attribute__((space(xmemory),aligned(N)))
#define _YBSS(N)    __attribute__((space(ymemory),aligned(N)))
#define _YDATA(N)   __attribute__((space(ymemory),aligned(N)))
#define _EEDATA(N)  __attribute__((space(eedata),aligned(N)))