简体   繁体   中英

Microcontroller PIC16F877A c coding, to control an occupancy sensor and a photodiode at the same time

i am currently doing a prototype which combine 2 sensors, the PIR motion sensor by cytron.

and a photodiode (tiny, with 3 legs, with the model unknown)

The prototype works in a way that, when there is no light, and there is motion, the led will turn on. Else, it will turn off.

I have wrote codes to test the both sensors separately, it works quite fine.

I face problem of the output of led when i combine the 2 codes. It is as shown in below:

//  include
//==========================================================================

 # include <pic.h>

 # include <htc.h>

//  configuration
//==========================================================================
__CONFIG (0x3F32);

//  define
//==========================================================================
 #define    sensor   RB3    
 #define    led      RA5
 #define led2    RB7
 #define light   RB5
 #define _XTAL_FREQ 4000000
 #define delay ms(x) _delay((unsigned long)((x)*(_XTAL_FREQ/4000.0)))

//  main function
//==========================================================================

void main(void)
{
//  unsigned long delay_time=5000;

TRISA = 0b00000000;
TRISB = 0b01111111;             //Configure Port B     as Input

ADCON1   =0B00000110;

led=0;
led2=0;
int i;

while(1)                    //Infinity Loop
{

    if(!light)
    {   
        if(sensor)
        {
            for(i=5;i>0;i--)
            {
                led2=0;
                led=1;
                __delay_ms(10000);
            }   
        }

        else if(!sensor)
        {   
            if (i>0)
            {   for(i=5;i>0;i--)
                {
                    led2=0;
                    led=1;
                    __delay_ms(10000);
                }
            }
            else if(i<=0)
            {
                led=0;  
                led2=1;
            }
        }
    }

    else if(light)
    {
        led=0;  
        led2=1;
    }

}
}

I appreciate your help in advance. Please help. Thank you.

You declare the variable i but then don't initialize it to a value (I don't think the C compiler initializes it to 0 either, I am not sure). If that was not the case, then imagine the following scenario:, that in the the very first beginning of execution: it was (!light) and (!sensor), it starts comparing i>0 or i<=0 but what is i initially??

you only assume that the if (sensor) body has executed at least once to give i an initial value. I don't know the details of your program requirements or flow, but I see this as an unsafe and a hidden bug.

While I'm no expert at the PIC micro's I wonder whether the method you are using to access the RAx and RBx is the appropriate one ie "led = 0", rather than "led &= ~(1 << led)" or something in like that.

You also seem to not initialize the "int i" variable which I assume would lead to issues with the if statements.

Best regards.

How about something like:

while (1)
{
    led_on = 0;

    if (!light && !sensor)
    {
        if (led_on == 0)
        {
            /* Turn on LED... */
            led_on = 1;
        }
    }
    else if (led_on == 1)
    {
        /* Turn off LED... */
        led_on = 0;
    }
}

when I was working on pic24 family using assembly language if I needed to assign 2 ports consecutively it turned out that the second assignment would not work(unless you are using tristate buffers...I don't know the exact reason but that was the case...).Anyway...what I'm trying to say is that try to give a few mseconds delay after each led value assignments like:

        for(i=5;i>0;i--)
        {
            led2=0;
            _delay_ms(50);
            led=1;
            __delay_ms(10000);
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM