|
|
View previous topic :: View next topic |
Author |
Message |
ringram2077 Guest
|
The simplest part won't work !! |
Posted: Tue Oct 03, 2006 5:54 am |
|
|
For some reason I cannot make the last statement delay_ms(500); execute. I have tried every location within the brackets after the else statement. I have only listed the main() part of the program. The program works perfectly except for the delay. Please help before I loose my mind !
Code: | void main() {
BYTE time;
byte k;
byte seq;
const int table[40] = {0,0,0,0,0,0,0,0,0,0,7,7,7,7,7,7,7,7,7,7,7,7,7,0,0,0,7,0,7,0,7,0,7,0,7,0,7,0,7,0};
init();
while (TRUE){
wait_for_low_to_high();
set_rtcc(0);
wait_for_low();
time = get_rtcc();
if (time < 90){
for (k=0; k<=40; ++k){
seq = table[k];
if (seq == 0)
off1ms();
else
on1ms();
}
}
}
delay_ms(500);
} | |
|
|
rberek
Joined: 10 Jan 2005 Posts: 207 Location: Ottawa, Canada
|
|
Posted: Tue Oct 03, 2006 6:11 am |
|
|
Your delay statement is outside the while(TRUE) loop. Why would you expect it to execute? |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue Oct 03, 2006 6:23 am |
|
|
Next time please give us a small but complete program including the #fuse and '#use delay' lines. In this situation especially the '#use delay' line is extremely important, can you post these lines now?
Also mention your compiler version and processor type always as it may be a problem specific to a compiler/processor combination.
And another bug:
Code: | for (k=0; k<=40; ++k){ | Change the '<=' to '=' or you will loop 41 times instead 0f 40. |
|
|
Guest
|
|
Posted: Tue Oct 03, 2006 6:24 am |
|
|
Yea, I know, but it doesn't matter where it is located within the brackets after the else statement, it will not execute. I should have listed it like this so people wouldn't think I was a complete idiot.
Code: | void main() {
BYTE time;
byte k;
byte seq;
const int table[40] = {0,0,0,0,0,0,0,0,0,0,7,7,7,7,7,7,7,7,7,7,7,7,7,0,0,0,7,0,7,0,7,0,7,0,7,0,7,0,7,0};
init();
while (TRUE){
wait_for_low_to_high();
set_rtcc(0);
wait_for_low();
time = get_rtcc();
if (time < 90){
for (k=0; k<=40; ++k){
seq = table[k];
if (seq == 0)
off1ms();
else
on1ms();
}
}
delay_ms(500);
}
} |
|
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue Oct 03, 2006 6:48 am |
|
|
Did you see my post above? |
|
|
Guest
|
|
Posted: Tue Oct 03, 2006 6:52 am |
|
|
Here is the additional program info:
Code: | #include <16F877A.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=8000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#include <ctype.h>
#define IROUT PIN_B2 |
Let me also mention that the delay_ms() function will work if I locate it in the upper part of the program. I have looked at the assembly listing and it appears that the code never makes it to the delay_ms(500).
Thanks |
|
|
rwyoung
Joined: 12 Nov 2003 Posts: 563 Location: Lawrence, KS USA
|
|
Posted: Tue Oct 03, 2006 7:02 am |
|
|
Still not a complete program because you don't show what on1ms() and off1ms() do. Also wait_for_low_to_high().
Are you sure you are returning from those functions or that they aren't somehow corrupting the program flow? Looked at the LST file? _________________ Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month! |
|
|
Guest
|
|
Posted: Tue Oct 03, 2006 7:39 am |
|
|
Here is the complete program. The generates the pulse sequence as intended and I have seen it on the oscilloscope. Just seems that the delay_ms() won't execute in the location that I need it to be.
Thanks
Code: | #include <16F877A.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=8000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#include <ctype.h>
#define IROUT PIN_B2
void init() {
output_low(IROUT);
setup_counters( RTCC_INTERNAL, RTCC_DIV_32 ); /* 69, 95, 120 RC values*/
}
void wait_for_low_to_high() {
while(input(PIN_B1)) ; /* if it's high, wait for a low */
delay_us(3); /* account for fall time */
while(!input(PIN_B1)); /* wait for signal to go high */
}
void wait_for_low() {
delay_us(3); /* account for rise time */
while(input(PIN_B1)); /* wait for signal to go high */
}
void on1ms() {
byte i;
for (i=1; i<=171; ++i) /* make a 1ms pulse of 38.6KHZ carrier*/
output_toggle(IROUT);
delay_us(4);
}
void off1ms() {
byte i;
for (i=1; i<=173; ++i)
output_low(irout);
delay_us(4);
}
void main() {
BYTE time;
byte k;
byte seq;
const int table[40] = {0,0,0,0,0,0,0,0,0,0,7,7,7,7,7,7,7,7,7,7,7,7,7,0,0,0,7,0,7,0,7,0,7,0,7,0,7,0,7,0};
init();
while (TRUE){
wait_for_low_to_high();
set_rtcc(0);
wait_for_low();
time = get_rtcc();
if (time < 90){
for (k=0; k<=40; ++k){
seq = table[k];
if (seq == 0)
off1ms();
else
on1ms();
}
}
delay_ms(500);
}
} |
|
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue Oct 03, 2006 11:07 am |
|
|
I had a look at the generated assembly code from compiler version v3.226 and at first glance it looks alright.
Which compiler version are you using? |
|
|
Guest
|
|
Posted: Tue Oct 03, 2006 2:58 pm |
|
|
I'm new to this compiler and new to C, so guess I'm a NEWbee.
Anyway looks like the compiler version is 4.002d .
I have been looking at the listing also and it does appear that the delay function gets placed in the assembly correctly and it does appear to get called, however by watching the IROUT led it does not apperar that the delay delays. The IROUT led just keeps pusling continiously. This is strange and I'm sure it's something I've done incorrectly. I initially wrote the program in Picbasic Pro and and had that working to perfection but felt I needed to punish myself and try my hand at PIC C .
I will keep looking.
Thanks |
|
|
rberek
Joined: 10 Jan 2005 Posts: 207 Location: Ottawa, Canada
|
|
Posted: Tue Oct 03, 2006 3:06 pm |
|
|
I'm sure this will be pointed out ot you by other people, but if you are using Version 4, is is basically beta and not to be trusted. Download the latest revision of Version 3 (I think 3.249) and try it.
I personally will not bother upgrading to Version 4 until others on this board start to smile favourably upon it. |
|
|
Guest
|
|
Posted: Tue Oct 03, 2006 4:17 pm |
|
|
Yea, OK on the beta version.
Quote: | Change the '<=' to '=' or you will loop 41 times instead 0f 40. |
You know, this seemed odd to me, but I saw it a lot in example code. I assumed that for j = 0 to <=40 would be the same as for J=0 to 39 since 39 is < 40. I that not the case ? I just looked in a C book and the first example uses the <= rather than = . Why is this ? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Oct 03, 2006 4:21 pm |
|
|
He meant to say, change it to '<'.
This will loop 40 times:
Code: | for (k=0; k < 40; k++)
{
// do something
} |
|
|
|
Guest
|
|
Posted: Tue Oct 03, 2006 4:23 pm |
|
|
But just for my information would j = 0 to <=40 loop 40 times or 41 times ?
Thanks |
|
|
Guest
|
|
Posted: Tue Oct 03, 2006 4:32 pm |
|
|
Wahhhhhhooooooo!!!!!
Hey i can't believe it ! That thing about the :
Code: | Change the '<=' to '=' or you will loop 41 times instead 0f 40. |
That was the problem !! Change it to <40 and the delay_ms(500) works perfectly !!!! Thank you for posting the bug.
|
|
|
|
|
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
|