Jump to content
Sign in to follow this  
zhi

problem with delay_x() function

Recommended Posts

This is my first time using delay_x() function, but doesn't seem work right. I don't see any problem with the following code. However, it's not working - the LEDs are not blinking.

Without the call to delay_s(), works fine.

HELP!!

 

 

//Timing settings

#pragma CLOCK_FREQ 4000000

 

void main(void)

{

   //Hardware Initialization

   disable_interrupt( GIE );

   set_bit( STATUS, RP0 );

   set_tris_a( 2 );

   set_tris_b( 0 );

   clear_bit( STATUS, RP0 );

   output_port_a( 0 );

   output_port_b( 0 );

 

   while(1)

   {

       output_port_b(0xff);

       delay_s(1);

       output_port_b(0x00);

   }

}

 

cheers!!

 

Zhi

Share this post


Link to post
Share on other sites

The problem is you have a delay after switching on your LED's, but not after you switch them off.

This means that are off for such a short time you won't see it.

 

I've added an additional delay in the code below to make it work.

 

//Timing settings

#pragma CLOCK_FREQ 4000000

 

void main(void)

{

  //Hardware Initialization

  disable_interrupt( GIE );

  set_bit( STATUS, RP0 );

  set_tris_a( 2 );

  set_tris_b( 0 );

  clear_bit( STATUS, RP0 );

  output_port_a( 0 );

  output_port_b( 0 );

 

  while(1)

  {

      output_port_b(0xff);

      delay_s(1); // delay to keep leds on for one second

      output_port_b(0x00);

      delay_s(1); // delay to keeps led off for one second

  }

}

 

Regards

Dave

Share this post


Link to post
Share on other sites

That's what I thought at the beginning. I used a oscillascope to probe it. Don't see any activity on any of the output pins.

 

However, if I commend out the delay_ function and let it run in the while() loop. I see a pulse stream out of the output pins, which what I was expecting. So I conclude it's the delay that some suspend the execution.

 

Is there any register bit I forgot to set in order to use the delay_x function?

Share this post


Link to post
Share on other sites

The delay_x functions work by creating a software delay loop.

 

Consider how long the pulse will be with the LED's off - it will be about 4 microseconds with a repetition rate of 1000,000 microseconds (1 second), so you are looking for something that will be hard to trigger on with your scope.

 

Please try the modified code, I sure it will resolve your problem.

 

Regards

Dave

Share this post


Link to post
Share on other sites

I did - adding the second delay function call.

All LED on PortB pins are lit. No other activity seem going on.

Share this post


Link to post
Share on other sites

What is the status of the watchdog, if the watchdog keeps reseting the processor before the timedelay complete, the LED's will never go off because the code will never get that far.

 

Try disabling the watchdog.

 

Regards

Dave

Share this post


Link to post
Share on other sites

I always have the WatchDog timer off.

Can you try it and see if it works? It's kind of frustrated to see this simple code not working.

 

asm __config _XT_OSC & _WDT_OFF & _PWRTE_OFF
 
//Timing settings
#pragma CLOCK_FREQ 4000000

void main(void)
{
   //Hardware Initialization
   disable_interrupt( GIE );
   set_bit( STATUS, RP0 );
   set_tris_a( 16 );
   set_tris_b( 0 );
   clear_bit( STATUS, RP0 );
   output_port_a( 0 );
   output_port_b( 0 );


   while(1)
   {
       output_port_b(0xff);
       delay_s(1);
       output_port_b(0x00);
       delay_s(1);        
   }
}

 

thanks!

 

Zhi

Share this post


Link to post
Share on other sites

I always have the WatchDog timer off.

Can you try it and see if it works? It's kind of frustrated to see this simple code not working.

 

asm __config _XT_OSC & _WDT_OFF & _PWRTE_OFF
 
//Timing settings
#pragma CLOCK_FREQ 4000000

void main(void)
{
   //Hardware Initialization
   disable_interrupt( GIE );
   set_bit( STATUS, RP0 );
   set_tris_a( 16 );
   set_tris_b( 0 );
   clear_bit( STATUS, RP0 );
   output_port_a( 0 );
   output_port_b( 0 );


   while(1)
   {
       output_port_b(0xff);
       delay_s(1);
       output_port_b(0x00);
       delay_s(1);        
   }
}

 

thanks!

 

Zhi

Share this post


Link to post
Share on other sites

Alright, found the problem. It's the target selection from the C2C IDE. It should be p16f84a, but it wasn't.

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...