基于MPLAB X IDE 配置位配置:
在PICLF1823的数据手册中可以找到两个配置寄存器。一个是配置字1 ,一个是配置字2.
对于初学者来说如此多的配置选项,该如何配置呢?我们要抓主重点。
配置字中最重要的配置选项就是:
看门狗配置 如:WDTE<1;0>; 一般选择关闭看门狗
MCLRE复位脚的配置 如:MCLRE;一般选择复位脚作I/O
震荡器的选择: 如:FOSC<2:0>;根据实际情况配置,我这里一般选择用 INTOSC 内部振荡器.
因为如果这三个没有配置好的话程序根本无法运行。其他配置可以看数据手册此不赘述。
MPLAB X IDE和MPLAB IDE 8不同。
MPLAB IDE 8 可以再界限中选择配置位就行。配置选项可以不写在代码中。
MPLAB X IDE 如果要配置的话必须在代码中写出配置。
1、打开MPLAB X IDE 在菜单栏中 点击Window->PIC Memory Veiws->Configuration bits,先选好配置位:
2、点击“输出生成源代码”,即可生成如下代码:
// PIC16F1824 Configuration Bit Settings
// 'C' source line config statements
#include <xc.h>
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
// CONFIG1
#pragma config FOSC = ECH // Oscillator Selection (ECH, External Clock, High Power Mode (4-32 MHz): device clock supplied to CLKIN pin)
#pragma config WDTE = ON // Watchdog Timer Enable (WDT enabled)
#pragma config PWRTE = OFF // Power-up Timer Enable (PWRT disabled)
#pragma config MCLRE = ON // MCLR Pin Function Select (MCLR/VPP pin function is MCLR)
#pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled)
#pragma config CPD = OFF // Data Memory Code Protection (Data memory code protection is disabled)
#pragma config BOREN = ON // Brown-out Reset Enable (Brown-out Reset enabled)
#pragma config CLKOUTEN = OFF // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
#pragma config IESO = ON // Internal/External Switchover (Internal/External Switchover mode is enabled)
#pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled)
// CONFIG2
#pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off)
#pragma config PLLEN = ON // PLL Enable (4x PLL enabled)
#pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
#pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
#pragma config LVP = ON // Low-Voltage Programming Enable (Low-voltage programming enabled)
3、简化代码
通过用逗号分隔每个设置 / 值对,可以使用一条 pragma 伪指令来编程多个设置。例如,
以上示例可以使用一条 pragma 伪指令指定,如下所示。
#pragma config FOSC = ECH, WDTE = ON ,PWRTE = OFF,MCLRE = ON^^^^^^^
4、注意事项:
config pragma 伪指令不会生成可执行代码。它们都应放置在函数
定义之外,以免影响函数代码的操作。