SLEEP() 16 秒后如何唤醒 LED?

问题描述

所以我想模拟一间卧室。灯会打开 8 秒(8 小时),并会休眠 16 秒(16 小时),这将总共达到 24 秒(24 小时),模拟现实生活中的时间。 现在代码工作正常,只有中断有错误。最初,我认为我的代码会起作用。 但是,我忘记了

SLEEP()

任何中断源都会唤醒它,导致我的 LED 在 10 毫秒后唤醒。如何让 LED 在休眠 16 秒后唤醒?

// PIC18F4520 Configuration Bit Settings 
#pragma config OSC = XT // Oscillator Selection bits (XT oscillator) 
#pragma config LVP = OFF // Single-Supply ICSP Enable bit 
#pragma config WDT = OFF // Watchdog Timer Enable bit 
#include <xc.h>
#include <stdlib.h>
#define _XTAL_FREQ 4000000 

void interrupt tmr0 (void); 
void chk_light(void);
void chk_bulb(void);
int sch_ctr = 0;

void main(void){

    OSCCON=0b10000000;      //Device enter IDLE mode when SLEEP() is called.
    ADCON1=0x0F;            //Configure all ports as digital
    TRISA=0b11000000;       //Configure RA<5:0> as outputs (LEDs)
    TRISC=0b00000000;
    TRISD=0b00001111;       //Configure RD<1:2> as input toggle switch 
    TRISE=0b11101100;

    PORTA=0;

    INTCONbits.GIEH = 0; //disable all interrupts
    RCONbits.IPEN = 1; // enable priority
    INTCON2bits.TMR0IP = 1; //Set timer0 interrupt to high priority
    TMR0H = 0x00; // clear high byte register
    TMR0L = 0x00; //clear low byte register
    T0CON = 0b10001000;
    INTCONbits.TMR0IF = 0; //clear timer0 interrupt flag
    INTCON2bits.INTEDG0=1;      //Configure INT0 interrupt on rising edge
    INTCONbits.TMR0IE = 1; //enable timer0 interrupt
    INTCONbits.GIEH=1; //enable all interrupts

    chk_light();
}

void interrupt tmr0 (void)
{
    if(INTCONbits.TMR0IF){ //check for timer0 overflow
        TMR0H = 0x0B; //preload value into high byte register
        TMR0L = 0xDC; //preload value into low byte register
        sch_ctr++; //increase every 10ms
            if(sch_ctr%2400==0){
            chk_bulb(); //every 24000ms (24s)
        }
        
        INTCONbits.TMR0IF = 0; //clear interrupt flag
    }
}

void chk_light(void){
    char i;
    if(PORTDbits.RD1==1){

        for(i=0;i<8;i++){
            PORTA=0b11111111;       //turn on LEDS for 8 sec    
            __delay_ms(1000);
        }
        PORTA=0;
        SLEEP();
    }
}

void chk_bulb(void){
    PORTCbits.RC2=1;    //turn on lightbulb
    __delay_ms(500);
    PORTCbits.RC2=0;    //turn off lightbulb
    __delay_ms(500);
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...