PIC程序不起作用

时间:2015-03-11 23:03:48

标签: automation microcontroller pic

我的第一个有用的项目,用于制作智能家居自动化系统,其中2个激光器放在房间的入口处,根据你从LDR首先切割的激光检测你是否进出如果你进入你切割第一个激光然后第二个激光切换,反之亦然,它打开或关闭灯,它还计算人们进入和关闭最后一个灯关闭后的灯关闭,代码无法正常工作我尝试使用它和IDK为什么每当我只剪切1个激光器时灯亮,代码用MPLAB写入并用XC8编译,mcu:pic16f877a

main.cpp中:

#include "define.h"

int main(){
    int counter = 0;
    TRISB = 255;
    OPTION_REG = 0b01111111;
    TRISC = 0;
    PORTC = 0;

    while(1){
        if(laser1 == 1 && laser2 == 0 && counter == 0){
            for(int x = 0; x <= 3000; x++){
                if(laser2 == 1){
                    PORTCbits.RC0 = 1;
                    counter++;
                    x = 0;
                    break;
                }
                __delay_ms(1);
            }

        }else if(laser1 == 1 && laser2 == 0 && counter >= 1){
            for(int y = 0; y <= 3000; y++){
                if(laser2 == 1){
                    counter++;
                    y = 0;
                    break;
                }
                __delay_ms(1);
            }
        }else if(laser1 == 0 && laser2 == 1){
            for(int z = 0; z <= 3000; z++){
                if(laser1 == 1){
                    counter--;
                    z = 0;
                    if(counter == 0){
                        PORTCbits.RC0 = 0;
                    }else{
                        PORTCbits.RC0 = 1;
                    }
                    break;
                }
                __delay_ms(1);
            }

        }

    }
}

define.h:

/* 
 * File:   define.h
 * Author: Fady
 *
 * Created on March 11, 2015, 10:51 PM
 */

#ifndef DEFINE_H
#define DEFINE_H

#ifdef  __cplusplus
extern "C" {
#endif


// PIC16F877A Configuration Bit Settings

// 'C' source line config statements

#include <xc.h>

#define laser1 PORTBbits.RB0
#define laser2 PORTBbits.RB1
#define LED PORTCbits.RC0
#define _XTAL_FREQ 4000000
// #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)


#ifdef  __cplusplus
}
#endif

#endif  /* DEFINE_H */
电路是一个非常简单的电路,:

其中我只将PORTC引脚0的引脚连接到地,而引脚1和引脚上的2个LDR连接到地。 2除了电源,当然还有vcc和MCLR以及gnd和4MHz晶体

1 个答案:

答案 0 :(得分:0)

我试着把代码搞砸了一下,最后它工作了我从else if语句中移除了else,如果它和神奇的工作很好,这里是完整的代码:

#include "define.h"

int main(){
    int counter = 0;
    TRISB = 255;
    OPTION_REG = 0b01111111;
    TRISC = 0;
    PORTC = 0;

    while(1){
        if(laser1 == 1 && laser2 == 0){
            for(int x = 0; x <= 3000; x++){
                if(laser2 == 1){
                    LED = 1;
                    counter++;
                    x = 0;
                    break;
                }
                __delay_ms(1);
            }

            __delay_ms(250);
        }
        if(laser1 == 0 && laser2 == 1){
            for(int x = 0; x <= 3000; x++){
                if(laser1 == 1){
                    counter--;
                    if(counter == 0){
                        LED = 0;
                    }
                    x = 0;
                    break;
                }
                __delay_ms(1);
            }
            __delay_ms(250);

        }

    }
}