View previous topic :: View next topic |
Author |
Message |
wind88
Joined: 29 Dec 2010 Posts: 37
|
rc hobby servo and button |
Posted: Thu Feb 24, 2011 11:00 am |
|
|
This is a code using button to control the direction of the hobby servo motor. But the code can't be compiled. Is there anything that I did wrong with the code? Thanx a lot
Code: |
#include <16F877A.h>
#device adc=10
#use delay(clock=20000000)
#define XTAL_FREQ 200000000
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#FUSES HS, NOWDT,NOPROTECT,NOLVP,NOWDT,PUT,NOBROWNOUT
#define C0 PIN_C0
#define C1 PIN_C1
#define C2 PIN_C2
#define B1 PIN_B1
void main()
{
while(TRUE)
{
if (input C0==1)
{
output_high(B1);
delay_us(750);
output_low(B1);
delay_us(19250);
}
else if (inputC1==1)
{
output_high(B1);
delay_us(900);
output_low(B1);
delay_us(19100);
}
else if (input C2==1)
{
output_high(B1);
delay_us(1200);
output_low(B1);
delay_us(18800);
}
}
} |
|
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9243 Location: Greensville,Ontario
|
|
Posted: Thu Feb 24, 2011 12:11 pm |
|
|
Since you're not using anything inside these...
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
I'd delete them.
Also
#device adc=10 is not required.
May not make a difference, depending on the version of the compiler, but why not just start with the minimum code? Easier to debug.
The delay_us(xxxxx); might not be correct, again version dependent.
It'd help if you supplied the version as well as the actual error message.
If using MPLAB you'll have to chnage the build configuration in the project's pulldown menu from 'debug' to 'release' and recompile, before you burn a PIC with a PICSTARTPlus programmmer.. |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1934 Location: Norman, OK
|
|
Posted: Thu Feb 24, 2011 5:00 pm |
|
|
Several things:
1. this line is not valid. Look in the CCS manual for the input comand.
2. As temtronic said get rid of the includes. You don't need them in this case. They are way too soon in the program anyway.
3. You need to organize your program as follows:
Device Includes (PIC device header must always be first line)
Fuses
use delay() statement
use RS232 statement(s)
defines
<library.h includes> // if you need them
global variables
Main routine
There are a few exceptions to #3 but it's a good basic outline. _________________ Google and Forum Search are some of your best tools!!!! |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Fri Feb 25, 2011 7:04 am |
|
|
Hi,
I'm sure a 200 Mhz Crystal frequency doesnt help either.
Quote: | #define XTAL_FREQ 200000000 |
hope that helps,
G _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
wind88
Joined: 29 Dec 2010 Posts: 37
|
|
Posted: Fri Feb 25, 2011 10:04 am |
|
|
Code: | #include <16F877A.h>
#use delay(clock=20000000)
#define XTAL_FREQ 20000000
#FUSES HS, NOWDT,NOPROTECT,NOLVP,NOWDT,PUT,NOBROWNOUT
#define C0 PIN_C0
#define C1 PIN_C1
#define C2 PIN_C2
#define B1 PIN_B1
void main()
{
while(TRUE)
{
if (input C0==1)
{
output_high(B1);
delay_us(750);
output_low(B1);
delay_us(19250);
}
else if (inputC1==1)
{
output_high(B1);
delay_us(900);
output_low(B1);
delay_us(19100);
}
else if (input C2==1)
{
output_high(B1);
delay_us(1200);
output_low(B1);
delay_us(18800);
}
}
} |
Thanx everyone.
I'm using ccs 4.093 version
I will look through in the ccs built in for the (input C0==1) function. thanx |
|
|
wind88
Joined: 29 Dec 2010 Posts: 37
|
|
Posted: Sun Feb 27, 2011 3:50 am |
|
|
Code: | #include <16F877A.h>
#use delay(clock=20000000)
#define XTAL_FREQ 20000000
#FUSES HS, NOWDT,NOPROTECT,NOLVP,NOWDT,PUT,NOBROWNOUT
#define C0 PIN_C0
#define C1 PIN_C1
#define C2 PIN_C2
#define B1 PIN_B1
void main()
{
while(TRUE)
{
if( input(PIN_C0) )
output_high(B1);
delay_us(750);
output_low(B1);
delay_us(19250);
if( input(PIN_C1) )
output_high(B1);
delay_us(900);
output_low(B1);
delay_us(19100);
if ( input(PIN_C2) )
output_high(B1);
delay_us(1200);
output_low(B1);
delay_us(18800);
}
} |
this code is still not workable on my hardware:( |
|
|
Gabriel
Joined: 03 Aug 2009 Posts: 1067 Location: Panama
|
|
Posted: Sun Feb 27, 2011 7:06 am |
|
|
hi,
you have 3 buttons and i see no debounce routine.
also, your pulse to the servo is off.
the TOTAL pulse for a servo is about ~2ms(position pulse) , EVERY ~20ms(refresh rate)
you position pulse is 20ms(total), thus your servo has no idea what the hell is going on....
hope that helps.
Gabriel. _________________ CCS PCM 5.078 & CCS PCH 5.093 |
|
|
wind88
Joined: 29 Dec 2010 Posts: 37
|
|
Posted: Sun Feb 27, 2011 9:27 am |
|
|
thanx.
But to set the location of the servo I just need 1 pulse. Is that correct? Or do I need to constantly sending a pulse to the servo motor?
And from this http://www.servocity.com/html/how_do_servos_work_.html
I do need to constantly send pulses to the servo motor. I think the timeframe is correct. Just I need to keep on sending the pulse to it. |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1934 Location: Norman, OK
|
|
Posted: Sun Feb 27, 2011 9:34 am |
|
|
First, you still need to move your FUSES above the Use Delay line
Second, are you sure the PIC is running?
Put in a simple LED blink code to verify it is running and it is
running at the right speed.
Add the following code at the beginning of Main before the other While TRUE statement
Code: |
while(TRUE)
{
output_toggle(Pin_B2);
delay_ms(1000);
}
|
Put an LED on B2 and see if it blinks at a 1 second rate. Once you get that
working then remove this code and work on your other code. _________________ Google and Forum Search are some of your best tools!!!! |
|
|
wind88
Joined: 29 Dec 2010 Posts: 37
|
|
Posted: Sat Mar 05, 2011 9:07 pm |
|
|
Thanx for the tip. I think my code is working now. Thanx. |
|
|
wind88
Joined: 29 Dec 2010 Posts: 37
|
|
Posted: Tue Mar 15, 2011 2:18 pm |
|
|
Why if I put a for(;;) or while(1) then the hobby servo will keep on rotating the 3 diff direction without stopping?
Is there any method to make it stop? And only move if I press a button?
Thank you. |
|
|
wind88
Joined: 29 Dec 2010 Posts: 37
|
|
|
|