如何解决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 举报,一经查实,本站将立刻删除。