我正在尝试制作一个智能家居自动化设备,当您进入房间时,灯会自动打开,反之亦然进入房间并在最后一个进入房间的人离开后关掉灯房间,但只有在一个人离开后才关灯,这并不酷,我真的不知道问题出在哪里,我的代码看起来还不错。这是代码:
/*
* File: main.c
* Author: Fady
*
* Created on September 23, 2014, 9:56 PM
*/
#include <stdio.h>
#include <stdlib.h>
/*
*
*/
// PIC16F877A Configuration Bit Settings
// 'C' source line config statements
#include <xc.h>
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
// CONFIG
#pragma config FOSC = XT // Oscillator Selection bits (XT oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = ON // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3/PGM pin has PGM function; low-voltage programming enabled)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
#define _XTAL_FREQ 4000000
#define laser1 PORTBbits.RB0
#define laser2 PORTBbits.RB1
#define isOn ==0
#define isOff ==1
int x1 = 0;
int x2 = 0;
int main() {
char people = 0;
nRBPU = 0;
TRISBbits.TRISB0 = 1; //laser 1 for input
TRISBbits.TRISB0 = 1; //laser 2 for input
TRISCbits.TRISC0 = 0; //output LED for output
PORTCbits.RC0 = 0;
while(1){
beginning:
if(( laser1 isOn && laser2 isOn ) || ( laser1 isOff && laser2 isOff )){ //if both lasers are on
goto beginning;
}else if(laser1 isOff && laser2 isOn){ //if laser1 is off and laser2 is on
readO:
if(laser2 isOff){
if(people == 0){
people = 1;
PORTCbits.RC0 = 1;
}if(people >= 1){
people++;
}
}else{
for(; x2 <= 1000; x2++){
__delay_ms(1);
goto readO;
}
}
x2 = 0;
}else if(laser1 isOn && laser2 isOff){ //if laser1 is on and laser2 is off
readC:
if(laser1 isOff){
people--;
if(people == 0){
PORTCbits.RC0 = 0;
}
}else{
for(; x1 <= 1000; x1++){
__delay_ms(1);
goto readC;
}
}
x1 = 0;
}
}
}
我认为它没有问题,但我不知道这里有什么问题,提前感谢任何将解决这个问题的回复者,这对我来说真的很有帮助