Jump to content
Sign in to follow this  
Ywiz

Delays don't work?

Recommended Posts

Does anyone know why i can't get delays to work in c2c?

My code looks like:

 

if(input_pin_port_b(0))

{

 output_low_port_b(2);

}

else

{

 output_high_port_b(2);

 delay_s(2);

}

 

but the delay seems to be ignored...

What should I do to make it work?

 

/Johan

Share this post


Link to post
Share on other sites

You haven't explained what is happening, other than it doesn't work so this makes it real hard to help.

 

Also you have only supplied a tiny fragment of code, break you code down to just a simple main routine and supply the complete code explaining what is happening.

 

A classic fault here is leaving the watchdog timer enabled, so the processor keeps getting reset so the results are what are expected

 

Hope this helps.

 

;)

Share this post


Link to post
Share on other sites

No It isn't the watchdog, I've disabled that.

What I mean when I say nothing happens is that the delay "dissapears". The rest of the application works just fine.

 

What I'm trying to accomplice is to tunr a diode (RB2) on for a second with the push of a button (RB0). The diode is turned on but as soon as the button is released the diode is turned off. I've tried to make it blink by using a delay but then it's lit or turned off (depending on how i set it up) all the time.

 

Here is my complete code:

 

asm __config _LP_OSC & _WDT_OFF & _PWRTE_ON & CP_OFF

#pragma CLOCK_FREQ 4000000

 

void main( void )

{

 set_bit( STATUS, RP0 ); //Select the Register bank 1

 set_tris_a( 0x00 ); //Configure the Port A

 set_tris_b( 0x01 ); //Configure the Port B

 OPTION_REG = 199; //Configure the OPTION register

 clear_bit( STATUS, RP0 ); //Select the Register bank 0

 INTCON = 160; //Configure the INTCON register

 

 while(1)

 {

   if(input_pin_port_b(0))

   {

     output_low_port_b(2);

   }

   else

   {

     output_high_port_b(2);

     delay_s(2);

   }

 }

}

 

/Johan

Share this post


Link to post
Share on other sites

I would recommend not setting the INTCON register.

Leave it to its power on reset value.

 

You haven't defined an interrupt handler, you don't need one for what you are doing, but you are enabling some interrupts.

 

Not sure what device your using, and hence what sources of interrupts are available.

 

If an interrupt is generated, it jumps the control to address 0004, if this doesn't jump to an interrupt handler it may be restarting you program continously. So if the code enters the delay loop it doesn't stay there for long !

 

Hope this work for ya.

 

 

;)

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...