c - 带内部振荡器的 sleep 模式(PIC18f46j50)

标签 c embedded microcontroller sleep

我正在对我的 PIC 进行编程,并试图将其置于深度 sleep 模式。 我想使用深度 sleep 功能,唤醒事件应该由 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 天了。 如果有任何帮助,我将不胜感激。

最佳答案

我不确定您是否意识到 DSWDT 周期设置为 8192,普通 WDT 设置为 32768,这意味着根据数据表需要 8.5 秒和 2.25 分钟才能触发。我将它设置为几秒钟而不是用于调试。当您确定它正在工作时,将 WDT 增加到所需的周期。 也许延迟循环太短而无法注意到任何闪烁?没有 sleep 功能时 LED 会闪烁吗? 此行不执行任何操作,因为该位是只读的。

WDTCONbits.DS = 0;

这一行

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

应该是

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

datasheet 表示 1 是深度 sleep ,0 是 sleep 。当您的设备进入正常 sleep 时,WDT 在 2.25 分钟(后分频器为 32768)内触发,而不是 DSWDT 8.5 秒(DSWDT 后分频器为 8192)。

关于c - 带内部振荡器的 sleep 模式(PIC18f46j50),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43698896/

相关文章:

c - 返回 int 的第二个字节的地址或值

c++ - 在 C++ 中确保单个实例但不是全局实例的最佳方法

c - 按位移位 (var Uint8 >> 7) & 0x01u - Misra 兼容

assembly - PCIe 卡上的计算机

c - 将动态 vector 从 C 返回到 R

c - 有没有办法获得有关整数提升的警告?

C数组地址

java - 如何使用JAVA控制通过USB/串口等连接PC的LED灯?我应该使用什么设备?

python - 是什么导致 C 程序不能在 Arduino 中正确地递增变量?

连接路径和基本名称