现在的位置: 首页 > 综合 > 正文

MPLAB程序中如何设置配置字

2019年03月26日 ⁄ 综合 ⁄ 共 11928字 ⁄ 字号 评论关闭

http://blog.csdn.net/greatwgb/article/details/7878288

基于MPLAB
X IDE配置字(configuration word)设置讲解

在不同的芯片中其配置字不尽相同,但是管家最常用到的有如下几部分:
1,芯片的振荡模式,有内外之分,还有三个速度的选择
2,片内看门狗的启用,配置字中有专门控制控制片看门狗的开口或关闭,不能用软件控制
3,上电复位延时定时器PWRT的启用。
4,低电压检测复位BOR模块的启用
5,代码保护控制,一旦启用代码保护功能,就可以防止程序被非法读出,注意的是,对于EPROM型的窗口片,一旦用户代码保护打开,就不能被关闭,所以调试是一定不能打开代码保护。对Flash型芯片,对芯片实施完全擦除后,包括代码保护等其他配置位信息还可恢复而能再次改写
6针对不同的芯片还有低压编程等控制。Mplab中有一个configuration bit的控制窗口可设置配置字

简单点:

Oscillator选择HS;

XINST = OFF // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy
mode))

Internal/External Oscillator Switchover bit 选择disable;

Watchdog Timer选择disable;(看门狗在调试模式下无效,只有在烧写程序时才能将看门狗打开。)

其余选项均选择disable。

PIC
里面关于 __CONFIG( ) 配置位

一.配置字
      PIC单片机的配置字可以用__CONFIG命令来定义:
    #i nclude
         __CONFIG(x)   ;其中x是配置字,头文件中定义了相应的配置说明符。

如:
        __CONFIG(WDTDIS & XT & UNPROTECT);
       这将关闭看门狗,设置XT振方式,程序不加密。注意:不同的配置符间用'&'相联,未定义的部分保留未编程状态。详细的情况请参考头文件及PIC数据手册

二.ID 位置
      有些PIC单片机在程序空间外还有ID空间,可用下面的方法来定义:
#i nclude
      __IDLOC(x) ;其中x是ID标示。

如:
      __IDLOC(15F0);
   将ID的四个单元定义为:1, 5, 15, 0. ID的具体位置由所指定的PIC芯片自动设定。

三.EEPROM 数据
       有些PIC单片机支持用外部编程器对内部的EEPROM进行编程。
      __EEPROM_DATA()可以将用于初始化的数据放入HEX文件中。

如:
       __EEPROM_DATA(0, 1, 2, 3, 4, 5, 6, 7)
可将0-7八个数放入HEX文件中,在用外部的编程器进行编程时将 这八个数写入PIC单片机中。
__EEPROM_DATA不是用于运行时写入EEPROM数据的,在运行时请用 EEPROM_READ(), EEPROM_WRITE()。

========================================================================

PIC18__config配置位

     PIC18的__CONFIG设置与PIC16的不一样。

PIC16的如:

        __CONFIG(_CP_OFF&_WDT_ON&_BODEN_ON&_PWRTE_ON&_RC_OSC); 是写在一起的。

PIC18 不写在一起,CONFIG1..7是分开写的。如:

      __CONFIG(1,IESODIS&FCMDIS&HS);
      __CONFIG(2,BOREN&PWRTEN&WDTDIS&WDTWINDIS&WDTPS32K);
      __CONFIG(3,MCLREN);
      __CONFIG(4,DEBUGEN&XINSTEN&STVRDIS);
      __CONFIG(5,UNPROTECT);

__CONFIG(N,X) 函数原型在PIC18.H文件中(PICC软件安装目录下的include文件夹中)。里面的各项设置在PIC18Fxx22.H中。N代表配置信息;X代表设置变量。如果要实现处理器的一些特殊功能,则需要查找相关的帮助文件,根据帮助文件的提示,在同文件中定义相关的变量。

上面的各项设置意义是:

     //各项内容在PIC18FXX22.H文件中定义
    //1.振荡器切换OFF&故障保护时钟监视器OFF&高速模式
    //2.欠压复位ON&上电延时ON&看门狗OFF&看门狗分频数
    //3.mclr用于复位脚
    //4.使能后台调试器&使能指令集扩展&堆栈溢出不复位
   //5.代码不保护

==========================================================================

       笔者在使用picc18进行基于PIC18F452的软件开发时,发现对于18系列的单片机,手动设置配置位非常麻烦,而且容易出错,所以考虑在源程序中设定配置位,这样编译后,无论是用什么烧写工具,都不需要再进行配置位的设定了。
      对于HI-TECH C18 , 配置字的设定通过宏 __CONFIG(n,x)来实现. 
宏__CONFIG(n,x)的声明在文件pic18.h中,各位元的声明在对应芯片型号的头文件中.18F452的例程如下: 
//***************************************************************************************
    #include <pic18.h> 
//***************************************************************************************
//*The declaration of __CONFIG(n,x) is in pic18.h                    * 
//*The __CONFIG(n,x) directive defines configuration data within is in pic18fxx2.h        * 
//***************************************************************************************
    __CONFIG(1,RC) ; 
    __CONFIG(2,PWRTDIS & WDTPS1 & WDTEN ) ; 
    __CONFIG(4,STVRDIS) ; 
    void main(void) 

//your code 

//*************************************************************************
在pic18.h中,__CONFIG(n,x)的声明如下     
#define    __CONFIG(n, x)    asm("\tpsect config,class=CONFIG");\ 
            asm("global config_word" ___mkstr(n)); \ 
            asm("config_word" ___mkstr(n)":"); \ 
            asm("\torg ("___mkstr(n)"-1)*2"); \ 
            asm("\tdw "___mkstr(x)) 
部分在pic18.h中的声明大体如下,具体请参考对应的头文件。 
// Configuration bit values 
// Config. Register 1 
#define OSCSEN        0xDFFF        // enable oscillator system clock 
#define OSCSDIS        0xFFFF 
// oscilator types 
#define RCRA6        0xFFFF        // RC w/OSC2 config as RA6 
#define HSPLL        0xFEFF        // HS w/PLL Enabled, Clk Freq = 4xFreq Osc. 
#define ECRA6        0xFDFF        // EC w/OSC2 config. as RA6 
#define ECDB4        0xFCFF        // EC w/OSC2 as divide by 4 clock output 
#define RC        0xFBFF 
#define HS        0xFAFF 
#define XT        0xF9FF         
#define LP            0xF8FF 

// Config. Register 2 
// Brown Out reset 
#define    BOREN        0xFFFF        // Brown-out reset enable
n代表 Config. Register n(代表配置信息), 例如__CONFIG(1,x); 1 代表Config. Register 1, X可为OSCSEN,RC等。
!!!最后还应该注意的是如果在源程序中进行配置位设置,如果使用的软件版本(比如使用picc18的8.20版本和MPLAB6.30)较低,虽然编译不会有问题,但可能会出现配置位设置混乱,推荐使用picc18的8.35版本和MPLAB7.0以上。安装HTSOFT的软件时,也必须保证完全安装,尤其是一定要安装MPLAB_toolsuites工具。

==========================================================================

如下:PIC18F452中PIC18.h     (此时源程序,哪位高人可否解析下?共同学习。)

/* Store a word value to a particular configuration word register eg.
 * __CONFIG(4, DEBUGEN & LVPDIS); // write to config word 4
 * config mask attributes (such as DEBUGEN) defined in chip-specific
 * header file. */
#define __CONFIG(n, x) asm("\tpsect config,class=CONFIG");\    /**\t是制表符,格式需要;psect 是定义class的关键字,意思是将以下的代码放入CONFIG class 中,并命名为config(注意大小写);config为自定义的class名,可以为任何其他名字;CONFIG说明了这个class的起始地址,在连续命令中指定CONFIG的范围delta=2,规定就是这样的。**/
   asm("global config_word" ___mkstr(n)); \    /***__mkstr(x)是一个将数字转换为字符的宏,目的是让编译器把一个数字当作是一个字符串来处理,如果不用__mkstr,你需要写成__CONFIG("0x0F")的形式,使用了__mkstr,直接写成__CONFIG(0x0F)的形式就行了,这一切只是为了用户更友好的使用这些宏,没什么特别的意思

***/

Microchip 几种编译器 在代码中 对CONFIG 位的配置   

 

;************************************************************************************

;*CONFIG Setting for PIC16/18              *

;*This text is mainly on the method for the setting of configuration bits with both assembler&C. *

;************************************************************************************

--------------  MPASM for PIC16Fxxx  
----------------------------------------------------

;*******************************************************************************************

;*1.To set CONFIG with assembler                    *

;*With the source code written in assembler the configuration bits can been set by micro __CONFIG  *

;*The micro __CONFIG  is after with the include file.                 *

;*******************************************************************************************

;*******************************************************************************************

;*Example 1,  CONFIG set for 16F877A                   *

;*******************************************************************************************

 list p=16f877A ; list directive to define processor   ;list指令用来定义处理器

 #include <p16f877A.inc> ; processor specific variable definitions

;************************************************************************

;*Configuration bits              *

;*The __CONFIG directive defines configuration data within the .ASM file.  *

;* The labels following the directive are defined in the P16F877A.INC file.  *

;*The PIC16F877A Data Sheet explains the functions of the configuration bits. *

;************************************************************************

__CONFIG _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_ON & _RC_OSC & _WRT_OFF & _LVP_ON & _CPD_OFF

; variable definitions

;your code

;;

END ;end of program

;**********************************************************************************

-------------------   MPASM for PIC18Fxxxx   -----------------------------------

;**************************************************************************************

;*Example 2, CONFIG set for 18F452. As the configuration bits is more complex than 16F877,     *

;*it maps to more bytes in program memory instead of  one word.          *

;**************************************************************************************

 LIST P=18F452   ;directive to define processor

 #include <P18F452.INC> ;processor specific variable definitions

;****************************************************************************

;*Configuration bits               *

;*The __CONFIG directive defines configuration data within the .ASM file.   *

;* The labels following the directive are defined in the P18F452.INC file.   *

; *The PIC18FXX2 Data Sheet explains the functions of the configuration bits.  *

;*****************************************************************************

 __CONFIG _CONFIG1H, _OSCS_OFF_1H & _HS_OSC_1H

 __CONFIG _CONFIG2L, _BOR_OFF_2L & _PWRT_OFF_2L

 __CONFIG _CONFIG2H, _WDT_OFF_2H

 __CONFIG _CONFIG3H, _CCP2MX_OFF_3H

 __CONFIG _CONFIG4L, _STVR_OFF_4L & _LVP_OFF_4L & _DEBUG_OFF_4L

 __CONFIG _CONFIG5L, _CP0_OFF_5L & _CP1_OFF_5L & _CP2_OFF_5L & _CP3_OFF_5L

 __CONFIG _CONFIG5H, _CPB_OFF_5H & _CPD_OFF_5H

 __CONFIG _CONFIG6L, _WRT0_OFF_6L & _WRT1_OFF_6L & _WRT2_OFF_6L & _WRT3_OFF_6L

 __CONFIG _CONFIG6H, _WRTC_OFF_6H & _WRTB_OFF_6H & _WRTD_OFF_6H

 __CONFIG _CONFIG7L, _EBTR0_OFF_7L & _EBTR1_OFF_7L & _EBTR2_OFF_7L & _EBTR3_OFF_7L

 __CONFIG _CONFIG7H, _EBTRB_OFF_7H

;******************************************************************************

;Variable definitions

;your code

;;

 END; end of program

;******************************************************************************

-------------------------  Hi-Tech PICC  (16Fxxxx)-------------------------------------

;******************************************************** 

;*2.To set CONFIG with C language       *

;********************************************************

;************************************************************************************

;*a. Here is a example for  HI-TECH PICC             *

;************************************************************************************

//**************************************************************************************

#include <pic.h>

#include <pic168xa.h>

//***************************************************************************************

//*The declaration of __CONFIG() is in pic.h     *

//*The __CONFIG(x) directive defines configuration data within is in pic168xa.h  *

//***************************************************************************************

__CONFIG(XT&WDTDIS&PWRTEN&BORDIS&LVPDIS&DUNPROT&DEBUGDIS&UNPROTECT);

void main(void)

{

//your code

}

//***************************************************************************************

//****************************************************************************

//*In pic.h the definition of __CONFIG(x) is as follows:       *

/*#define __CONFIG(x) asm("\tpsect config,class=CONFIG,delta=2");\  *

   asm("\tdw "___mkstr(x))         */

//* pic168xa.h defines all the configuation bit values      *

//*#define CONFIG_ADDR 0x2007 

--------------------- Hi-Tech PICC18   ------------------------------------------------

****************************************************************************************

//*b.In HI-TECH C18 , use macro __CONFIG(n,x)

//***************************************************************************************

 #include <pic18.h>

//***************************************************************************************

//*The declaration of __CONFIG(n,x) is in pic18.h     *

//*The __CONFIG(n,x) directive defines configuration data within is in pic18fxx2.h  *

//***************************************************************************************

 __CONFIG(1,RC) ;

 __CONFIG(2,PWRTDIS & WDTPS1 & WDTEN ) ;

 __CONFIG(4,STVRDIS) ;

 

 void main(void)

{

//your code

}

//*************************************************************************

//************************************************************************

//*In pic18.h the definition of __CONFIG(n,x) is as follows     * 

//*#define __CONFIG(n, x) asm("\tpsect config,class=CONFIG");\   *

//*   asm("global config_word" ___mkstr(n)); \      *

//*   asm("config_word" ___mkstr(n)":"); \       *

//*   asm("\torg ("___mkstr(n)"-1)*2"); \        * 

//*   asm("\tdw "___mkstr(x))          *

//* pic18fxx2.h defines the configuration bit values,         *

//************************************************************************

--------------------    MPLAB C18   ----------------------------------------

;*******************************************************************************

;*3.For MPLAB C18 V2.40 or higher, use #pragma config directive           *

;*for example                     * 

//******************************************************************************

 #include <p18f452.h>

//*******************************************************************************

//*Configuration settings may be specified with multiple #pragma config directives.       *

//*MPLAB C18 verifies that the configuration settings specified are valid for the processor*

//*for which it is compiling                                                              *   

//*The labels following the directive "pragma config" are defined in the P18F452.h file.    *

//*******************************************************************************

 #pragma config OSC=HS

 #pragma config  PWRT=ON

 #pragma config  BOR=OFF, BORV=42

 #pragma config  WDT=OFF

 #pragma config   CCP2MUX=ON

 #pragma config   STVR=OFF, LVP=OFF, DEBUG=OFF

 #pragma config   CPD=OFF

 

void main(void)

{

//your code

}

//********************************************************************************

【上篇】
【下篇】

抱歉!评论已关闭.