Hi, i am using pic16f877a too. also writing the similar code.
My project is to turn on led, if motion is detected(cytron pir sensor module), and when the photodiode detected a low light intensity.
The photodiode is working fine. However, the motion sensor, some how responded differently.
Below is my code snippet:
- CODE: SELECT_ALL_CODE
//==========================================================================
// Author : Cytron Technologies
// Project : Alarm System: Motion Detector
// Project description : This project will use PIC16F876A (microcontroller)
// to control the alarm system (buzzer & LED) with
// a PIR Sensor (Motion Detector).
//==========================================================================
// include
//==========================================================================
#include <pic.h>
#include <htc.h>
__CONFIG (0x3F32);
#define sensor RB3
#define led RE0
#define led2 RB7
#define light RB5
#define _XTAL_FREQ 4000000
//#define delay ms(x) _delay((unsigned long)((x)*(_XTAL_FREQ/4000.0)))
void main(void)
{
// unsigned long delay_time=5000;
TRISE = 0b00000000;
TRISB = 0b01111111; //Configure Port B as Input
//setup ADC
// ADCON1 = 0b01111111; //set ADx pin digital I/O
led=0;
led2=0;
unsigned long i=0;
unsigned long j=0;
while(1) //Infinity Loop
{
if(!light)
{
if(sensor)
{
for(j=5;j>0;j--){
led2=0;__delay_ms(50);
led=1; __delay_ms(5000);
}
}
else if(!sensor)
{
led=0;__delay_ms(50);
led2=1;__delay_ms(50);
}
}
else if(light)
{
led=0;__delay_ms(50);
led2=1;__delay_ms(50);
}
}
}
I hope you guys can help me out about this. I am lost.
Looking forward.
Thank you.