具有内部振荡器的休眠模式(PIC 18f46j50)

时间:2017-04-29 18:03:40

标签: c embedded microcontroller sleep

我正在编程我的PIC,我正试图将其置于深度睡眠模式。 我想使用深度睡眠功能,唤醒事件应该由WDT中断创建。但我的问题是,无法达到中断。设备将继续睡眠。

#include <xc.h> 

// 'C' source line config statements

// CONFIG1L
#pragma config WDTEN = ON       // Watchdog Timer (Enabled)
#pragma config PLLDIV = 1       // PLL Prescaler Selection bits (No prescale (4 MHz oscillator input drives PLL directly))
#pragma config STVREN = ON      // Stack Overflow/Underflow Reset (Enabled)
#pragma config XINST = OFF       // Extended Instruction Set (Enabled)

// CONFIG1H
#pragma config CPUDIV = OSC1    // CPU System Clock Postscaler (No CPU system clock divide)
#pragma config CP0 = OFF        // Code Protect (Program memory is not code-protected)

// CONFIG2L
#pragma config OSC = HS         // Oscillator (HS, USB-HS)
#pragma config T1DIG = ON       // T1OSCEN Enforcement (Secondary Oscillator clock source may be selected)
#pragma config LPT1OSC = OFF    // Low-Power Timer1 Oscillator (High-power operation)
#pragma config FCMEN = ON       // Fail-Safe Clock Monitor (Enabled)
#pragma config IESO = ON        // Internal External Oscillator Switch Over Mode (Enabled)

// CONFIG2H
#pragma config WDTPS = 32768    // Watchdog Postscaler (1:32768)

// CONFIG3L
#pragma config DSWDTOSC = INTOSCREF// DSWDT Clock Select (DSWDT uses INTRC)
#pragma config RTCOSC = INTOSCREF// RTCC Clock Select (RTCC uses INTRC)
#pragma config DSBOREN = ON     // Deep Sleep BOR (Enabled)
#pragma config DSWDTEN = ON     // Deep Sleep Watchdog Timer (Enabled)
#pragma config DSWDTPS = 8192  // Deep Sleep Watchdog Postscaler (1:8,192 (8.5 seconds))

// CONFIG3H
#pragma config IOL1WAY = ON     // IOLOCK One-Way Set Enable bit (The IOLOCK bit (PPSCON<0>) can be set once)
#pragma config MSSP7B_EN = MSK7 // MSSP address masking (7 Bit address masking mode)

// CONFIG4L
#pragma config WPFP = PAGE_63   // Write/Erase Protect Page Start/End Location (Write Protect Program Flash Page 63)
#pragma config WPEND = PAGE_WPFP// Write/Erase Protect Region Select (valid when WPDIS = 0) (Page WPFP<5:0> through Configuration Words erase/write protected)
#pragma config WPCFG = OFF      // Write/Erase Protect Configuration Region (Configuration Words page not erase/write-protected)

// CONFIG4H
#pragma config WPDIS = OFF      // Write Protect Disable bit (WPFP<5:0>/WPEND region ignored)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.




void sleep_f()
{
    //INTCON  &= ~0xF8;           /* Disable all interrupt sources */
    //INTCON3 &= ~0x38;
    PIR2bits.OSCFIF = 1;
    //RCONbits.IPEN = 0;   
    // OSCCON        = 0b01110000;
     OSCCONbits.IDLEN = 0 ;  // 0 Sleep mode a 1 idle mod 
    //WDTCONbits.SWDTEN = 1;  /* Enable the regular Watch Dog Time out too */
    WDTCONbits.REGSLP = 0; 
    //DSCONLbits.DSBOR = 1;
    //DSCONHbits.RTCWDIS = 0;
    DSCONHbits.DSEN = 0; // We run deep slep
    Sleep();  
}
void main()
{
    TRISD6 = 0;
    TRISDbits.TRISD4 = 0;
    LATD6  = 1;
            int i;
    while (1){
    LATD4 = 0; 
    int i;
    for (i = 0;i < 10000; i++) asm("nop");
    LATD4 = 1;  // Blink led if everything is OK. 
    sleep_f();
    DSCONLbits.RELEASE = 0;
    WDTCONbits.DS = 0;
    for (i = 0;i < 30000; i++) asm("nop");
    }
}

你知道我的代码有什么问题吗? 我已经累了3天了。 我将不胜感激任何帮助。

1 个答案:

答案 0 :(得分:0)

我不确定您是否意识到DSWDT周期设置为8192且正常WDT设置为32768,这意味着根据数据表触发需要8.5秒和2.25分钟。我将其设置为几秒钟而不是调试。当您确定它正在工作时,将WDT增加到所需的时间段。 也许延迟循环太短而不能注意到任何闪烁?没有睡眠功能,LED闪烁吗? 这行不做任何事情,因为这个位是只读的。

WDTCONbits.DS = 0;

这一行

DSCONHbits.DSEN = 0; // We run deep slep

应该是

DSCONHbits.DSEN = 1; // We run deep sleep

数据表指出1是深度睡眠,0是睡眠。当您的设备进入正常睡眠状态时,WDT在2.25分钟内触发(后缩放器32768)而不是DSWDT 8.5秒(DSWDT后分频器为8192)。