2014-09-30 47 views
0

我試圖製作一個智能家庭自動化設備,當你進入房間時燈會自動打開,反之亦然,我用我的項目兩個激光來指定人們是進入還是離開房間還希望圖片來統計有多少人進入房間,並在最後一個進入房間後關掉燈離開做這個我用pic16f877a ic和mplab作爲我的IDE與xc8編譯器,到目前爲止該程序工作如此之好,但它不計算進入房間的人,但只有在離開後才關燈,這不是很酷,我真的不知道問題出在哪裏,我的代碼看起來沒問題。這裏是代碼:Mplab圖片程序不能正常工作

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

        } 
       } 

      } 

我認爲這沒有問題,但我不知道什麼是錯在這裏反正在此先感謝誰是要去解決任何回答者這一點,真的會爲我

回答

1
有很大的幫助

首先嚐試以可理解的形式減少代碼。 1st:避免像goto這樣的跳轉操作: 2nd:如果語句結果不明確,請嘗試使用SWITCH語句。

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

爲什麼要這樣做? 你永遠循環!

+0

是的,直到任何人決定切割激光線然後它檢查是否是它的第一或第二將其切斷以指定是否進入或離開房間的人 – ilouy 2014-10-03 03:43:03

+0

它不算人進入房間或​​出來 – ilouy 2014-10-03 03:47:00

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 

}在一個永遠循環

+0

我只是做了這個,但它不是在真正的工作 – ilouy 2014-10-04 06:21:51

+0

如何檢查誰在誰?畫一些從頭開始,我會給你解釋代碼。 – SparrowBg 2014-10-06 08:16:14