View previous topic :: View next topic |
Author |
Message |
maxD
Joined: 16 Dec 2013 Posts: 22
|
delay_ms not working |
Posted: Wed Dec 25, 2013 9:02 pm |
|
|
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 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 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
|
|
Posted: Wed Dec 25, 2013 9:24 pm |
|
|
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
|
|
Posted: Wed Dec 25, 2013 11:55 pm |
|
|
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
|
|
Posted: Thu Dec 26, 2013 1:01 am |
|
|
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
|
|
Posted: Thu Dec 26, 2013 1:22 am |
|
|
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
|
|
Posted: Thu Dec 26, 2013 3:26 am |
|
|
Thanks so much PCM Programmer my problem 100% solved..!! |
|
|
|