delay

Discussion about projects that used PIC Microcontroller, Hardware Interface, Programming Algorithm and etc......

delay

Postby kitzai » Wed Mar 28, 2012 3:42 am

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 ?
kitzai
Greenhorn
 
Posts: 2
Joined: Wed Mar 28, 2012 3:32 am

Re: delay

Postby ABSF » Wed Mar 28, 2012 10:26 am

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
The next war will determine NOT who is right BUT what is left.
User avatar
ABSF
Professional
 
Posts: 810
Joined: Wed Nov 10, 2010 9:32 am
Location: E Malaysia

Re: delay

Postby Idris » Wed Mar 28, 2012 11:41 am

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.
Cytron Technologies invest time and resources providing tutorial, training and support for STEM education and maker movement. We need your support by purchasing products from Cytron Technologies. Thanks.
http://www.cytron.com.my
User avatar
Idris
Moderator
 
Posts: 409
Joined: Thu Mar 22, 2012 5:28 pm
Location: Pulau Pinang

Re: delay

Postby kitzai » Wed Mar 28, 2012 2:26 pm

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
"
kitzai
Greenhorn
 
Posts: 2
Joined: Wed Mar 28, 2012 3:32 am

Re: delay

Postby Idris » Wed Mar 28, 2012 2:48 pm

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;
          }
       }
    }
Cytron Technologies invest time and resources providing tutorial, training and support for STEM education and maker movement. We need your support by purchasing products from Cytron Technologies. Thanks.
http://www.cytron.com.my
User avatar
Idris
Moderator
 
Posts: 409
Joined: Thu Mar 22, 2012 5:28 pm
Location: Pulau Pinang

Re: delay

Postby ABSF » Wed Mar 28, 2012 10:20 pm

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
The next war will determine NOT who is right BUT what is left.
User avatar
ABSF
Professional
 
Posts: 810
Joined: Wed Nov 10, 2010 9:32 am
Location: E Malaysia

Re: delay

Postby dunan » Wed Nov 14, 2012 5:41 pm

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.
dunan
Greenhorn
 
Posts: 3
Joined: Wed Nov 14, 2012 5:16 pm

Re: delay

Postby dunan » Wed Nov 14, 2012 6:15 pm

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.
dunan
Greenhorn
 
Posts: 3
Joined: Wed Nov 14, 2012 5:16 pm

Re: delay

Postby zhenning » Wed Nov 14, 2012 7:53 pm

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?
zhenning
Enthusiast
 
Posts: 351
Joined: Thu Dec 30, 2010 12:32 am

Re: delay

Postby dunan » Wed Nov 14, 2012 10:16 pm

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.
dunan
Greenhorn
 
Posts: 3
Joined: Wed Nov 14, 2012 5:16 pm

Next

Return to PIC Microcontroller

Who is online

Users browsing this forum: No registered users and 3 guests

cron