Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
247 views
in Technique[技术] by (71.8m points)

c - How do wake up LED after SLEEP() for 16 secs?

So I want to simulate a bedroom. The light would turn on for 8s(8hrs) and would sleep for 16s(16hrs) which would total up to 24s(24hr) simulating real-life timing. Right now the code works normally, only the interrupt has errors. Initially, I thought my code would work. However, I forgot that for

SLEEP()

any source of interrupt will wake it up, resulting in my LED waking up after 10ms. How do I make it so that my LED wakes up after sleeping for 16 secs?

// 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);
}
question from:https://stackoverflow.com/questions/65643538/how-do-wake-up-led-after-sleep-for-16-secs

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...