CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

delay_ms not working

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
maxD



Joined: 16 Dec 2013
Posts: 22

View user's profile Send private message

delay_ms not working
PostPosted: Wed Dec 25, 2013 9:02 pm     Reply with quote

Hi guys I'm doing some experiment using Proteus.
First i push the push button, led starts to blink till i push it again.
But here when i start simulation led turned on automatically and push button never works Question i did this in real pic simulator software and works somehow when i remove the delay_ms. According to my experience i think the problem is with delay_ms...
In Proteus the result same as before. Sorry i know that this site is only for
ccs c problems not for Proteus problems Rolling Eyes but i want help from you guys !! thanks
And how can i add a image here? I want to show you my Proteus design.


Last edited by maxD on Wed Dec 25, 2013 9:36 pm; edited 1 time in total
maxD



Joined: 16 Dec 2013
Posts: 22

View user's profile Send private message

PostPosted: Wed Dec 25, 2013 9:24 pm     Reply with quote

https://www.facebook.com/photo.php?fbid=676530512407916&set=a.676530499074584.1073741827.100001529206440&type=3&theater

Sorry i am unable to find another way to show you this.
here is my code
Code:

#include <16f877A.h>
#FUSES NOWDT,NOLVP,HS
#use delay(clock=8000000)
#define setup_oscillator(OSC_8MHz)

 BYTE blink = 0;

#int_ext
void led()
{
 if(!blink&&!input(PIN_B0))
 {
  delay_ms(20);// for debounce
   blink=1;
 }
 else if(blink&&!input(PIN_B0))
 {
    delay_ms(20);
    blink=0;
 }
 
 else{}
}

void main()
{
set_tris_B(0x01);//b0 is the interrupt input for push button
enable_interrupts(global);
 
enable_interrupts(int_ext);
ext_int_edge(H_TO_L);
 
do{

 if(blink)
   {
    output_high(PIN_B1);//led attached to b1
    delay_ms(1000);
   
    output_low(PIN_B1);
   }
  }while(TRUE);
 
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Dec 25, 2013 11:55 pm     Reply with quote

Of course if you have warnings enabled, you are getting the "Interrupts
disabled to prevent re-entrancy" warning. That's because you have the
CCS delay_ms() function used inside an interrupt routine, and outside
the isr in normal code (in main() in this case).

This has been discussed many times on the forum, and calling the
delay functions is not recommended in interrupt routines, but there
is a work-around. See these posts:
http://www.ccsinfo.com/forum/viewtopic.php?t=38151
http://www.ccsinfo.com/forum/viewtopic.php?t=37931

There are also articles on it in the Interrupts section of the CCS FAQ:
http://www.ccsinfo.com/faq.php
maxD



Joined: 16 Dec 2013
Posts: 22

View user's profile Send private message

PostPosted: Thu Dec 26, 2013 1:01 am     Reply with quote

Thanks
i did as they said deleted the delay_ms(20) and both delay_ms(1000) and made some difference in the circuitry. NOW it works but when i add the delay_ms(1000) with #use delay as i mentioned in the code led turned on when push button pressed and off when again pressed but not blinking still keeps on...
I want 1s time difference in blinking. Please help.
Code:

#DEVICE pic16f877A
#include <16f877A.h>

#FUSES NOWDT,NOLVP,HS

#use delay(clock=8000000)
#define setup_oscillator(OSC_8MHz)
#use delay(clock=8000000)


BYTE blink = 0;

#INT_EXT
void led()
{
 if(!blink&&!input(PIN_B0))
 {
 // delay_ms(20);// for debounce
   blink=1;
 }
 else if(blink&&!input(PIN_B0))
 {
   // delay_ms(20);
    blink=0;
 }
 
 else{}
}


#use delay(clock=8000000)
 void main()
{
 
   set_tris_B(0x01);//b0 is the interrupt input for push button
   enable_interrupts(global);
 
   enable_interrupts(INT_EXT);
   ext_int_edge(H_TO_L);
   output_low(PIN_B1);
 
  do{
 
   if(blink)
   {
    output_high(PIN_B1);//led attached to b1
    delay_ms(1000);
   
    output_low(PIN_B1);
   }
  }while(TRUE);
   
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Dec 26, 2013 1:22 am     Reply with quote

Quote:

do{

if(blink)
{
output_high(PIN_B1);//led attached to b1
delay_ms(1000);

output_low(PIN_B1);

}while(TRUE);

You will not get any visible blink from this code. You turn the LED on
for 1000ms. Then you turn it off. Look at your code closely. How
long is the LED turned off ? Only a few usec. You will never even see it
go off.
See this post which shows the correct delay statements to blink an LED:
http://www.ccsinfo.com/forum/viewtopic.php?t=38316&highlight=blink+led&start=1

Another thing:
Quote:

#DEVICE pic16f877A
#include <16f877A.h>

The #device statement for the PIC is in the 16F877A.h file. You don't
need to put in a separate line for it.
maxD



Joined: 16 Dec 2013
Posts: 22

View user's profile Send private message

PostPosted: Thu Dec 26, 2013 3:26 am     Reply with quote

Thanks so much PCM Programmer my problem 100% solved..!!
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group