View previous topic :: View next topic |
Author |
Message |
louarnold
Joined: 13 May 2010 Posts: 42 Location: Ottawa, Canada
|
delay_ms() doesn't always work. |
Posted: Fri May 21, 2010 11:57 am |
|
|
I'm using a PIC 16F877. The CCS C Compiler is V3.xx in the lab. When I put the "delay_ms(1000)" in the main line of the code, it works fine. But putting the same line into a function makes the code not effect the delay. Why is this?
Ex:
Code: |
wait_one_sec()
{
delay_ms(1000); //Delay 1 sec - doesn't work here.
}
void main(){
Loop forever()
{
output_low(Pin_XX);
delay_ms(1000); //Delay 1 sec - works here
wait_one_sec;
output_high(Pin_XX);
} //end loop
} //end main.
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri May 21, 2010 12:05 pm |
|
|
Your code doesn't compile.
1. You can't declare a function inside main(). Put it above main()
for your test.
2. You can't call the wait_one_sec() function and leave off the empty
parentheses on the end. They must be there.
You need to get a book on C. Or look at a lot of example programs.
Study how they do it. |
|
|
louarnold
Joined: 13 May 2010 Posts: 42 Location: Ottawa, Canada
|
|
Posted: Fri May 21, 2010 12:28 pm |
|
|
PCM programmer wrote: | Your code doesn't compile.
1. You can't declare a function inside main(). Put it above main()
for your test.
2. You can't call the wait_one_sec() function and leave off the empty
parentheses on the end. They must be there.
You need to get a book on C. Or look at a lot of example programs.
Study how they do it. |
All good info. Now can you address the question? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri May 21, 2010 12:38 pm |
|
|
I can, but you need to do these 3 things:
1. Post a compilable test program that shows the problem.
2. Post your full compiler version.
3. Are you testing this in Proteus or in hardware ?
Example of test program. This program can be pasted into an MPLAB
project and it will compile with no errors.
Code: |
#include <16F877.H>
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 20000000)
//==================================
void main()
{
while(1)
{
output_high(PIN_B0); // Blink LED on pin B0
delay_ms(500);
output_low(PIN_B0);
delay_ms(500);
}
}
|
|
|
|
sjb
Joined: 13 Apr 2010 Posts: 34 Location: UK
|
|
Posted: Fri May 21, 2010 12:56 pm |
|
|
your line
is not a call to the wait_one_sec() function. |
|
|
louarnold
Joined: 13 May 2010 Posts: 42 Location: Ottawa, Canada
|
|
Posted: Fri May 21, 2010 1:31 pm |
|
|
sjb wrote: | your line
is not a call to the wait_one_sec() function. |
PCM Programmer's code is pretty much on. But to respond more fully:
The code I gave is "paraphrased" because I no longer have the original code. I modified it all to hell to try to figure out what the problem is. It was not code that I wrote. Nevertheless it compiled.
The code is tested in hardware, downloaded through a PICkit2 programmer into a PIC16F877A development board (really nothing more than LEDs buttons and a pot).
sjb's response was interesting: Given that the parentheses are not there, what function did it call? |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Fri May 21, 2010 1:56 pm |
|
|
I believe will return the address of the function. It will not call the function at all. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri May 21, 2010 2:06 pm |
|
|
It does not compile. It's illegal. It gives this error message:
Quote: |
*** Error 48 "pcm_test.c" Line 14(1,13): Expecting a ( wait_one_sec |
|
|
|
louarnold
Joined: 13 May 2010 Posts: 42 Location: Ottawa, Canada
|
|
Posted: Fri May 21, 2010 2:31 pm |
|
|
PCM programmer wrote: | It does not compile. It's illegal. It gives this error message:
Quote: |
*** Error 48 "pcm_test.c" Line 14(1,13): Expecting a ( wait_one_sec |
|
Yeah, that's what I figured. I probably corrected that in the code because I know the program compiled and was loaded into the PIC (dev board). Now suppose that line is:
wait_one_sec();
That calls the function as I stated it in the original post. But the delay in the function does not happen; this I know to be true. The delay in the main program - exactly the same code - works! Is it a compiler problem (V3.17) - optimization? - a bug?
BTW: If you remove the delay in the main program, there is no evident delay at all!
Last edited by louarnold on Fri May 21, 2010 2:38 pm; edited 1 time in total |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri May 21, 2010 2:34 pm |
|
|
Post a small test program where it fails and post the full compiler version.
It's 4 digits: x.xxx |
|
|
louarnold
Joined: 13 May 2010 Posts: 42 Location: Ottawa, Canada
|
|
Posted: Fri May 21, 2010 2:45 pm |
|
|
PCM programmer wrote: | Post a small test program where it fails and post the full compiler version.
It's 4 digits: x.xxx |
The complete compiler version in the lab is 3.170b (PCM and PCH) That's all I have.
As for the code, I gave it to you in the original post. There is literally nothing more except the header lines in your program. I simply don't have it any longer to post, nor can I reproduce it and test it where I am now. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri May 21, 2010 2:51 pm |
|
|
I don't think I can do anything more on this thread. |
|
|
louarnold
Joined: 13 May 2010 Posts: 42 Location: Ottawa, Canada
|
|
Posted: Fri May 21, 2010 2:56 pm |
|
|
PCM programmer wrote: | I don't think I can do anything more on this thread. |
hahaha. Ok, thanks anyway. |
|
|
vinniewryan
Joined: 29 Jul 2009 Posts: 154 Location: at work
|
|
Posted: Sat May 22, 2010 8:23 pm |
|
|
Hmm, perhaps I'm missing something here, but wouldn't the obvious answer be this?:
Code: |
void wait_one_sec()
{
delay_ms(1000); //Delay 1 sec - doesn't work here.
}
void main()
{
while(1)
{
output_low(Pin_XX);
delay_ms(1000); //Delay 1 sec - works here
wait_one_sec();
output_high(Pin_XX);
} //end loop
} //end main.
|
Call me Mr.Simpleton, but wouldn't this code work just as well as what the code in the original post is trying to do? _________________ Vinnie Ryan |
|
|
sjb
Joined: 13 Apr 2010 Posts: 34 Location: UK
|
|
Posted: Sun May 23, 2010 2:34 am |
|
|
I think we should wait for louarnold. From what he said earlier it sound like he doesn't have access to his code (at the weekend?).
The really glaring errors in the first post code make it very likely we just have not understood the problem, yet. We're guessing, and have suggestion, but I'm not sure are tackling the his problem. |
|
|
|