Switch + Delay Function

General discussion on mikroC.
Post Reply
Author
Message
awachat
Posts: 92
Joined: 23 Jan 2011 10:21
Location: Japan

Switch + Delay Function

#1 Post by awachat » 28 May 2011 05:50

Hi All

Thanks for your support always. I am learning c coding and facing some problems while using delay function.

I am using PIC 6 board, I want to use delay function (2000 MS) to switch ON LED(RB0). When I will press RD0 Switch again it must go off as soon as I press RD0. So while switch on LED RB0 I need 2000 MS delay.

can some one guide me ?

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Code: Select all


bit oldstate0;
bit oldstate1;                                            // Old state flag
bit oldstate2;                                            // Old state flag
bit oldstate3;                                            // Old state flag
bit oldstate4;                                            // Old state flag
bit oldstate5;                                            // Old state flag
bit oldstate6;                                            // Old state flag
bit oldstate7;                                            // Old state flag





void main() {

  ANSEL  = 0;                                         // Configure AN pins as digital I/O
  ANSELH = 0;
  
  C1ON_bit = 0;                                     // Disable comparators
  C2ON_bit = 0;

  TRISB = 0xff;                                   // set portB as input
  PORTB = 0xff;                                   // Initial PORTC value

  TRISD = 0xff;                                   // set portD as output
  PORTD = 0xff;                                   // Initial PORTD value

  TRISC = 0x00;
  PORTC = 0x00;

 oldstate0 = 0;
 oldstate1 = 0;
 oldstate2 = 0;
 oldstate3 = 0;
 oldstate4 = 0;
 oldstate5 = 0;
 oldstate6 = 0;
 oldstate7 = 0;
 
 delay_ms (2000);
 TRISB0_bit = 0;

  [color=#FF4000]do {
    if (Button(&PORTD, 0, 1, 1)) {
      oldstate0 = 1;
    }
    
    if (oldstate0 & Button(&PORTD, 0, 1, 0)) {
    delay_ms (2000);
    TRISB0_bit = ~TRISB0_bit;
    oldstate0 = 0;
    }[/color]
  
    if (Button(&PORTD, 1, 1, 1)) {
      oldstate1 = 1;
    }
    if (oldstate1 && Button(&PORTD, 1, 1, 0)) {
      TRISB1_bit = ~TRISB1_bit;
      oldstate1 = 0;
    }
    
    if (Button(&PORTD, 2, 1, 1)) {
      oldstate2 = 1;
    }
    if (oldstate2 && Button(&PORTD, 2, 1, 0)) {
      TRISB2_bit = ~TRISB2_bit;
      oldstate2 = 0;
    }
    
    if (Button(&PORTD, 3, 1, 1)) {
      oldstate3 = 1;
    }
    if (oldstate3 && Button(&PORTD, 3, 1, 0)) {
      TRISB3_bit = ~TRISB3_bit;
      oldstate3 = 0;
    }

    if (Button(&PORTD, 4, 1, 1)) {
      oldstate4 = 1;
    }
    if (oldstate4 && Button(&PORTD, 4, 1, 0)) {
      TRISB4_bit = ~TRISB4_bit;
      oldstate4 = 0;
    }

    if (Button(&PORTD, 5, 1, 1)) {
      oldstate5 = 1;
    }
    if (oldstate5 && Button(&PORTD, 5, 1, 0)) {
      TRISB5_bit = ~TRISB5_bit;
      oldstate5 = 0;
    }

    if (Button(&PORTD, 6, 1, 1)) {
      oldstate6 = 1;
    }
    if (oldstate6 && Button(&PORTD, 6, 1, 0)) {
      TRISB6_bit = ~TRISB6_bit;
      oldstate6 = 0;
    }


    if (Button(&PORTD, 7, 1, 1)) {
      oldstate7 = 1;
    }
    if (oldstate7 && Button(&PORTD, 7, 1, 0)) {
      TRISB7_bit = ~TRISB7_bit;
      oldstate7 = 0;
    }

  } while(1);
}
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

Best Regards,
DJ

awachat
Posts: 92
Joined: 23 Jan 2011 10:21
Location: Japan

Re: Switch + Delay Function

#2 Post by awachat » 29 May 2011 15:18

Any update ?

Muphy
Posts: 318
Joined: 24 Feb 2008 14:05
Location: Stonehaven Scotland

Re: Switch + Delay Function

#3 Post by Muphy » 29 May 2011 16:11

You have not said which PIC you are using however what you will need is an interrupt either to catch a button being pressed or as a timer to keep an LED on for a designated period of time.

Just using a delay without some way of catching a button press will not work.

Depending on the PIC you are using, some pins have an Interrupt On Change capability, read the datasheet and find out if that is the case with your PIC.


HTH,


M

awachat
Posts: 92
Joined: 23 Jan 2011 10:21
Location: Japan

Re: Switch + Delay Function

#4 Post by awachat » 30 May 2011 13:36

Hi Murphy

Thanks for your reply.

I was able to add delay while switch ON but delay is coming while switch OFF as well. I want to switch off using button and using same it must get on with delay. I have never used interrupt using Time based as I dont know C syntax but learning it if you can guide me I will write code.



do {
if (Button(&PORTD, 0, 1, 1)) {
oldstate0 = 1;
}

if (oldstate0 & Button(&PORTD, 0, 1, 0)) {
delay_ms (2000);
TRISB0_bit = ~TRISB0_bit; // This is just toggling the last status so delay is getting add while switch OFF and ON

oldstate0 = 0;
}

awachat
Posts: 92
Joined: 23 Jan 2011 10:21
Location: Japan

Re: Switch + Delay Function

#5 Post by awachat » 30 May 2011 13:42

I am using PIC16f887

Muphy
Posts: 318
Joined: 24 Feb 2008 14:05
Location: Stonehaven Scotland

Re: Switch + Delay Function

#6 Post by Muphy » 30 May 2011 15:47

I'm up to my ears tiling a bathroom (day off), will have a look later tonight.

M

Muphy
Posts: 318
Joined: 24 Feb 2008 14:05
Location: Stonehaven Scotland

Re: Switch + Delay Function

#7 Post by Muphy » 30 May 2011 19:38

awachat,

I played around with your code to give you an example of how to use the interrupt. I changed the pins so that B0 becomes the button and D0 becomes the LED. Press B0 and the LED will light, press it again before the end of the delay and it will go out.

This code is illustrative only, it is only intended to demonstrate what I think you are trying to do. I've tested with a 16F877A on an EasyPIC5 and it works. I also compiled it as a 16F887 and it compiles.

This interrupt will only work this way with RB0, there is however another interrupt that will catch any of RB4-RB7 if you need more buttons.

The principle is the same and the datasheet will tell you the registers to set and the registers to test in the interrupt.

I hope this will be of some help to you.

Code: Select all

bit LED_On;

/*********************************************************************
Example of using an interrupt to catch a button press and
turn an LED with a delay of 2000mS and turn the LED off again
if the interrupt occurs again within the 2000mS delay

Use RB0 as the interrupt button and RD0 as the LED which is illuminated

**********************************************************************/

void interrupt()
{
     //Test if RB0 has been pressed  INTF bit of INTCON Register
     
     if(INTF_bit)
     {
         if(LED_On==0)
         {
             LED_On=1;
         }
         else
         {   LED_On=0;
             RD0_bit=0;
         }
         //  Clear the flag
         INTF_bit=0;
     }
}



void main() 
{

     /* Need to tell the chip whether to interrupt on rising or falling
        edge of RB0 - I chose rising edge, this is set on the INTEDG bit
        of the OPTION_REG register
     */
     
     INTEDG_bit=1;
     
     /* Enable INTCON register bit INTE to enable the RB0 interrupt */
     
     INTE_bit=1;

     TRISB = 0xff; // set portB as input
     PORTB = 0x00;

     TRISD = 0x00; // set portD as output
     PORTD = 0x00; // Initial PORTD value

     //Enable Interrupts
     GIE_bit=1;
     
     do
     {
         if(LED_On)
         {
             RD0_bit=1;
             Delay_ms(2000);
             RD0_bit=0;
             LED_On=0;
         }


     } while(1);
}

Rgds,


M

awachat
Posts: 92
Joined: 23 Jan 2011 10:21
Location: Japan

Re: Switch + Delay Function

#8 Post by awachat » 31 May 2011 15:32

Hi

Thanks for your update but its not working on PIC 6 board. I am attaching ASM file please take a look.

Regards,
DJ

Muphy
Posts: 318
Joined: 24 Feb 2008 14:05
Location: Stonehaven Scotland

Re: Switch + Delay Function

#9 Post by Muphy » 31 May 2011 15:55

awachat,

I'm sorry but I do not have the time to read your ASM code (not attached anyway), have you done the basics and ensured that the led's are enabled and if you press the RD0 button, the RD0 LED lights and same for RB0. How are the jumpers set for PORT B, are they pulled high, pulled low or floating? Are the LED's turned on?

I also commented out some code to do with the analog pins, this should have no effect if you run the code I sent as my code does not use PORT A.

As I said, my code was intended only as an example, I had a quick read of the datasheet for the PIC you are using and I saw nothing that would suggest that this code would not work but as I do not have the same hardware as you I cannot guarantee it. Perhaps someone else can run the example with a 16F887 and an easyPIC6.


M

awachat
Posts: 92
Joined: 23 Jan 2011 10:21
Location: Japan

Re: Switch + Delay Function

#10 Post by awachat » 03 Jun 2011 05:44

Hi Muphy

Thanks for your reply.

I check your code but it’s difficult to understand for me. I will re write my query again.

I am looking for code which turn on LED with 2000ms delay after pressing switch and if same switch get press then LED must switch OFF immediately.

Regards,
DJ

Muphy
Posts: 318
Joined: 24 Feb 2008 14:05
Location: Stonehaven Scotland

Re: Switch + Delay Function

#11 Post by Muphy » 03 Jun 2011 07:41

And the code I wrote does exactly that !!!

I'm sorry but I cannot make it any more simple than that. I'm not being rude but perhaps you need to learn more about PIC's and how they work and the language/compiler.

M

Post Reply

Return to “mikroC General”