0

我正在尝试制作一个智能家居自动化设备,当您进入房间时,灯会自动打开,反之亦然进入房间并在最后一个进入房间的人离开后关掉灯房间,但只有在一个人离开后才关灯,这并不酷,我真的不知道问题出在哪里,我的代码看起来还不错。这是代码:

            /* 
             * 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;

                    }
                }

            }

我认为它没有问题,但我不知道这里有什么问题,提前感谢任何将解决这个问题的回复者,这对我来说真的很有帮助

4

2 回答 2

1

首先尝试以可理解的形式减少代码。1:避免goto之类的跳转操作: 2:if语句太多,结果不明确,尽量使用SWITCH语句。

beginning:
                if(( laser1 isOn && laser2 isOn ) || ( laser1 isOff && laser2 isOff )){                     //if both lasers are on
                   goto beginning;

为什么这样做?你永远处于循环中!

于 2014-10-01T09:52:04.930 回答
0

好的,让我们保持简单。

countPeople = 0;

而(1){

if(PORTBbits.RB0 == 0) // People enter
{
    countPeople++; // 
    //TODO: some thing if you want      
}
elseif(PORTBbits.RB0 == 1) // people out or something like this
{
    countPeople--;
}

//if nothing happend 

}

于 2014-10-03T19:02:24.177 回答