Page 1 of 2

delay

PostPosted: Wed Mar 28, 2012 3:42 am
by kitzai
CODE: SELECT_ALL_CODE
#include <pic.h>
//   configuration
//==========================================================================
__CONFIG (0x3F32);

//   define
//==========================================================================

#define   sensor      RB2
#define   led         RC0

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

void main(void)

{

   TRISB = 0b11111111;                  //Configure Port B as Input
   TRISC = 0b00000000;                  //Configure Port C as Output
   
   led=0;

   while(1)                        //Infinity Loop
   {   
      //scan input
      if(sensor=1)
      led=1;
   
    
   }
}




im using motion sensor as my input,led is output.if motion sensor is detected,i want led turn on (2 minute).after 2minute just turn off.what code i need to addon ?

Re: delay

PostPosted: Wed Mar 28, 2012 10:26 am
by ABSF
To delay 2 minutes, try this

CODE: SELECT_ALL_CODE
void delay_2m(void)
{  for (unsigned char x=0; x<120; x++)
{  __delay_ms(1000);
}
}


But in the beginning of your program, you might need

#include <htc.h>
#define _XTAL_FREQ 4000000 //your xtal frequency

I assume you know how to add those to your program... :)

p/s sorry for my POOR C

Allen

Re: delay

PostPosted: Wed Mar 28, 2012 11:41 am
by Idris
CODE: SELECT_ALL_CODE
#include <pic.h>
#include <htc.h>
__CONFIG(0x3F32);

#define   sensor    RB2
#define   led       RC0
#define   _XTAL_FREQ       20000000   // Crystal 20MHz

void main(void)
{
   unsigned int s;

   TRISB = 0b11111111;   //Configure Port B as Input
   TRISC = 0b00000000;   //Configure Port C as Output
   led=0;
   while(1)   //Infinity Loop
   {
      //scan input
      if(sensor=1)
      {
         led=1;
         for(s=0;s<120;s++) __delay_ms(1000);   // delay 120 second aka 2 minutes
         led=0;
      }
   }
}

Try this.

Re: delay

PostPosted: Wed Mar 28, 2012 2:26 pm
by kitzai
Idris WROTE:
CODE: SELECT_ALL_CODE
#include <pic.h>
#include <htc.h>
__CONFIG(0x3F32);

#define   sensor    RB2
#define   led       RC0
#define   _XTAL_FREQ       20000000   // Crystal 20MHz

void main(void)
{
   unsigned int s;

   TRISB = 0b11111111;   //Configure Port B as Input
   TRISC = 0b00000000;   //Configure Port C as Output
   led=0;
   while(1)   //Infinity Loop
   {
      //scan input
      if(sensor=1)
      {
         led=1;
         for(s=0;s<120;s++) __delay_ms(1000);   // delay 120 second aka 2 minutes
         led=0;
      }
   }
}

Try this.



i try ady,but mplab show got error"Error [1274] C:\Users\kitzai\Desktop\MOTION DIY\forum.c; 22. delay exceeds maximum limit of 197120 cycles
"

Re: delay

PostPosted: Wed Mar 28, 2012 2:48 pm
by Idris
You need to use the latest MPLAB + Hi Tech C Compiler. Your compiler cannot build __delay_ms(1000); another method is, reduce the value of 1000 to 10, then increase the loop value. Make sure the total delay is 120000 ms.

Try this,
CODE: SELECT_ALL_CODE
    #include <pic.h>
    #include <htc.h>
    __CONFIG(0x3F32);

    #define   sensor    RB2
    #define   led       RC0
    #define   _XTAL_FREQ       20000000   // Crystal 20MHz

    void main(void)
    {
       unsigned int s;

       TRISB = 0b11111111;   //Configure Port B as Input
       TRISC = 0b00000000;   //Configure Port C as Output
       led=0;
       while(1)   //Infinity Loop
       {
          //scan input
          if(sensor=1)
          {
             led=1;
             for(s=0;s<12000;s++) __delay_ms(10);   // delay 120 second aka 2 minutes
             led=0;
          }
       }
    }

Re: delay

PostPosted: Wed Mar 28, 2012 10:20 pm
by ABSF
kitzai WROTE:i try ady,but mplab show got error"Error [1274] C:\Users\kitzai\Desktop\MOTION DIY\forum.c; 22. delay exceeds maximum limit of 197120 cycles


Where was this limit of 197120 cycles coming from? MPLAB or PICC?

197120 is between 2^17 and 2^18 and not divisible by 2. Was this delay has some relationship with the xtal Freq? The lower the xtal freq, one would be able to do a longer delay in ms?

Allen

Re: delay

PostPosted: Wed Nov 14, 2012 5:41 pm
by dunan
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.

Re: delay

PostPosted: Wed Nov 14, 2012 6:15 pm
by dunan
Hi, i have just posted. I have change #define _XTAL_FREQ 4000000 into #define _XTAL_FREQ 20000000, 20 mega.

However, the result is still no change.
Pls help. Thank you.

Re: delay

PostPosted: Wed Nov 14, 2012 7:53 pm
by zhenning
dunan WROTE: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.


How different? What is the problem? and what is the response that you want?

Re: delay

PostPosted: Wed Nov 14, 2012 10:16 pm
by dunan
Hi.
I would like to see the led turn on, when the PIR sensed a motion, at the same time photodiode show low light intensity.
The current problem is when the photodiode detecting low light(when i use sth to cover it), and when the PIR sense motion(when i trigger a motion), the led dun switch on. Sometimes, when i did not touch the PIR sensor, the light switch on by its own.