Mplab pic程序无效

时间:2014-09-30 20:00:43

标签: pic mplab home-automation

我正在尝试制作一个智能家居自动化设备,当你进入房间时,灯会自动打开,反之亦然我用于我的项目两个激光器来指定人们是否进入或离开房间我还想要照片计数有多少人进入房间,并在最后一个进入房间之后关灯关闭这样做我使用pic16f877a ic和mplab作为我的ide与xc8编译器,到目前为止程序工作得很好,但它没有计算进入房间的人,但只有在一个人离开之后才关灯,这不是很酷,而且我真的不知道问题在哪里,我的代码看起来还不错。这是代码:

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

                    }
                }

            }

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

2 个答案:

答案 0 :(得分:1)

首先尝试以可理解的形式减少代码。 1st:避免像goto一样跳跃操作: 第二:如果语句结果不清楚太多,请尝试使用SWITCH语句。

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

为什么要这样做? 你永远在循环!

答案 1 :(得分: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 

}