View previous topic :: View next topic |
Author |
Message |
Sophi
Joined: 14 Jun 2005 Posts: 64
|
Help! something in this code causes reset |
Posted: Thu Aug 18, 2005 12:11 pm |
|
|
HI- I am using a P40 Olimex protoboard with an LCD and an RC servo wired to it. The RC servo has three wires, +5, gnd and signal which is wired to pin 34 (B1) of a 16F877A. I have written a simple loop program for the servo to go counterclockwise then clockwise. The trouble is that the program restarts/resets itself after it executes...and printing the index k to the LCD shows that k=0. If I comment out the
line:for (k=0; k<=5; ++k){ and its corresponding } the program goes to the end and stops, so somehow looping this is causing a reset. I have tried switching out this k loop for a do-while with the same result.
And the servo wire pin 34 is not wired to anything else- it is empty on the Olimex schematic.
Finally-the program with the loop works fine when the variable delay (x and y) is removed by putting the actual numbers in, and k prints as 5, so I would also conclude that the variable delay is making my program hokey.
Any ideas?
Thanks-
Sophi
main()
{
int i = 0;
int k = 0;
float volt = 1;
float ad12; // output of a/d
int x,y;
set_tris_a(0b00100000); // sets port a direction 0=out 1=in
set_tris_b(0xF0); // set B4-7 to output
// and B0-3 inputs
init:
lcd_init();
adc_init();
output_high (PIN_A0); // start init test circuit routine
lcd_putc("\fCode 6\n");
delay_ms (1000);
output_low (PIN_A0); // turn LED off
delay_ms (1000);
output_low (PIN_B1); // servo out
// servo CC and CCW full range
for (k=0; k<=5; ++k){
for (i=1; i<=100; ++i)
{
output_high (PIN_B1); // servo out high 1000 us is CW
delay_us (2000);
output_low (PIN_B1); // servo out (off)
delay_us (19000);
}//end of for
for (i=1; i<=100; ++i)
{
x = 1200;
y = 22000;
output_high (PIN_B1); // servo out high 1200 us using variable is CCW
delay_us (x);
output_low (PIN_B1); // servo out (off)
delay_us (y);
}//end of for
printf(lcd_putc,"%d" ,k);
k++;
}
}
Last edited by Sophi on Thu Aug 18, 2005 12:21 pm; edited 1 time in total |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Aug 18, 2005 12:16 pm |
|
|
I didn't try to analyze your code, but normally you don't do nested
loops like that. The loop counter is normally incremented in its
for() statement, like this:
Code: | for (k=0; k<=5; k++)
{
} |
I think you should do things in the normal way. You will have less
problems. |
|
|
Sophi
Joined: 14 Jun 2005 Posts: 64
|
|
Posted: Thu Aug 18, 2005 12:22 pm |
|
|
thanks PCM-
Do you mean k++ instead of ++k? Or something else.
Sophi |
|
|
Sophi
Joined: 14 Jun 2005 Posts: 64
|
|
Posted: Thu Aug 18, 2005 12:26 pm |
|
|
I can't believe it, by switching to k++, it works! Well, at a pretty slow speed but no more jumping out of the loop.
Thanks so much! |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Aug 18, 2005 12:32 pm |
|
|
Quote: | printf(lcd_putc,"%d" ,k);
k++;
}
} |
Did you remove the extra k++ down at the bottom of your program ? |
|
|
Sophi
Joined: 14 Jun 2005 Posts: 64
|
|
Posted: Thu Aug 18, 2005 12:47 pm |
|
|
OK. extra k++ has been removed.
I spoke too soon about the code working tho. It seems only to work when I switch to delay_ms rather than delay_us, and then the servo motor moves really really slow. Wierd. Any ideas?
And how do I post code in the code box?
Sophi
main()
{
int i = 0;
int k = 0; // loop count
float volt = 1;
float ad12; // output of a/d
float x,y;
set_tris_a(0b00100000); // sets port a direction 0=out 1=in
set_tris_b(0xF0); // set B4-7 to output
// and B0-3 inputs
init:
lcd_init();
adc_init();
output_high (PIN_A0); // init test circuit routine
lcd_putc("\fCode 7\n"); // welcome
delay_ms (1000); //
output_low (PIN_A0); // turn LED off
delay_ms (1000);
output_low (PIN_B1); // servo out
// servo CC and CCW full range
for (k=0; k<=5; k++){
for (i=1; i<=100; i++)
{
output_high (PIN_B1); // servo out high 1000 us is CCW
delay_us (2000);
output_low (PIN_B1); // servo out (off)
delay_us (19000);
lcd_putc("\fCCW\n"); // welcome
} //end of for
for (i=1; i<=100; i++)
{
x = 1.20; //this loop makes the servo move really really slooow
y = 2.20;
output_high (PIN_B1); // servo out high 1200 us using variable is CCW
delay_ms (x);
output_low (PIN_B1); // servo out (off)
delay_ms (y);
lcd_putc("\fCW\n"); // welcome
}//end of for
printf(lcd_putc,"%d" ,k);
}
} |
|
|
Sophi
Joined: 14 Jun 2005 Posts: 64
|
|
Posted: Thu Aug 18, 2005 12:54 pm |
|
|
oops- y = 22.0 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Aug 18, 2005 12:56 pm |
|
|
You need to read the manual on all the CCS functions that you
are using. You are doing some things that are not allowed.
If you read the CCS manual on the delay_us() and delay_ms()
functions, you will see that there are strict limits on the parameters.
You cannot use floating point parameters.
You cannot use 16-bit variables as parameters, only 8 bits.
If you want a 16-bit parameter, it must be a constant.
You must read the manual. |
|
|
Sophi
Joined: 14 Jun 2005 Posts: 64
|
|
Posted: Thu Aug 18, 2005 1:03 pm |
|
|
OK.
thanks- |
|
|
drh
Joined: 12 Jul 2004 Posts: 192 Location: Hemet, California USA
|
|
Posted: Thu Aug 18, 2005 1:41 pm |
|
|
Also, this
Quote: | set_tris_b(0xF0); // set B4-7 to output
// and B0-3 inputs
|
Makes B7-B4 inputs and B3-B0 outputs. _________________ David |
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Thu Aug 18, 2005 7:03 pm |
|
|
Sophi wrote:
Quote: |
And how do I post code in the code box?
|
To keep the source style format in your code, (and to make our life easier )
just click the Code button at the beggining of your code and then click
Code button again at the end of your code.
Also you have the option Preview to test the different editor tools and to see
how will look your post prior to Submit it. If you submit a post, while your
post is the last thread in such subject, you can Edit, correct and Submit it again.
Be aware that every click on Submit will send your whole mssge, so just click
it ONCE to ovoid multiple post.
hope this help,
Humberto |
|
|
Sophi
Joined: 14 Jun 2005 Posts: 64
|
|
Posted: Mon Aug 22, 2005 8:13 am |
|
|
OK I read the manual. Rules are clear.
Variables (us or ms) only 0-255
Constants (us or ms) only 0-65535
Seems to indicate no floats.
I need a 2 variables in us #1 is 1000-2000, the other 22000 minus variable#1. OR a float that is ms 1-2, the other (22-#1). The last three places are the important ones.
To be clear an example of this would be variable #1 is something like 1200us and variable #2 is 20800us.
Can I alter the delay function? And if so, where can I find it?
Should I write my own delay function or are there other built in delay functions? And if so, would I still use
Code: |
#use delay(clock=20000000)
|
?
Sophi |
|
|
Guest
|
|
|
|