View previous topic :: View next topic |
Author |
Message |
Guest
|
Servo Not Working |
Posted: Tue Jan 13, 2009 1:34 pm |
|
|
Having trouble controlling servo motor. I'm using CCS 4.068 and Mplab 8.15 with ICD3. Below is my code. I want the program just to hold a servo position forever for now. Logically this program should work but then again I am new to PIC and CCS. Any suggestions?
Code: | #include <18F4520.h>
#device adc=8
#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES LP //Low power osc < 200 khz
#FUSES NOPROTECT //Code not protected from reading
#FUSES BROWNOUT //Reset when brownout detected
#FUSES BORV20 //Brownout reset at 2.0V
#FUSES NOPUT //No Power Up Timer
#FUSES NOCPD //No EE protection
#FUSES STVREN //Stack full/underflow will cause reset
#FUSES NODEBUG //No Debug mode for ICD
#FUSES LVP //Low Voltage Programming on B3(PIC16) or B5(PIC18)
#FUSES NOWRT //Program memory not write protected
#FUSES NOWRTD //Data EEPROM not write protected
#FUSES IESO //Internal External Switch Over mode enabled
#FUSES FCMEN //Fail-safe clock monitor enabled
#FUSES PBADEN //PORTB pins are configured as analog input channels on RESET
#FUSES NOWRTC //configuration not registers write protected
#FUSES NOWRTB //Boot block not write protected
#FUSES NOEBTR //Memory not protected from table reads
#FUSES NOEBTRB //Boot block not protected from table reads
#FUSES NOCPB //No Boot Block code protection
#FUSES LPT1OSC //Timer1 configured for low-power operation
#FUSES MCLR //Master Clear pin enabled
#FUSES XINST //Extended set extension and Indexed Addressing mode enabled
#use delay(clock=8000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
int i;
void main()
{
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF|ADC_TAD_MUL_0);
setup_psp(PSP_DISABLED);
setup_spi(SPI_SS_DISABLED);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
while(1)
{
output_high(PIN_C0);
delay_ms(1.5);
output_low(PIN_C0);
delay_ms(20);
}
} |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jan 13, 2009 1:43 pm |
|
|
Quote: | FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES LP //Low power osc < 200 khz
#FUSES NOPROTECT //Code not protected from reading
#FUSES BROWNOUT //Reset when brownout detected
#FUSES BORV20 //Brownout reset at 2.0V
#FUSES NOPUT //No Power Up Timer
#FUSES NOCPD //No EE protection
#FUSES STVREN //Stack full/underflow will cause reset
#FUSES NODEBUG //No Debug mode for ICD
#FUSES LVP //Low Voltage Programming on B3(PIC16) or B5(PIC18)
#FUSES NOWRT //Program memory not write protected
#FUSES NOWRTD //Data EEPROM not write protected
#FUSES IESO //Internal External Switch Over mode enabled
#FUSES FCMEN //Fail-safe clock monitor enabled
#FUSES PBADEN //PORTB pins are configured as analog input channels on RESET
#FUSES NOWRTC //configuration not registers write protected
#FUSES NOWRTB //Boot block not write protected
#FUSES NOEBTR //Memory not protected from table reads
#FUSES NOEBTRB //Boot block not protected from table reads
#FUSES NOCPB //No Boot Block code protection
#FUSES LPT1OSC //Timer1 configured for low-power operation
#FUSES MCLR //Master Clear pin enabled
#FUSES XINST //Extended set extension and Indexed Addressing mode enabled |
The fuses shown in bold are wrong.
As the note says, "LP" is for crystals of less than 200 KHz in frequency.
I assume you probably want to use the internal oscillator and run it at
8 MHz. To do so, substitute INTRC_IO for the LP fuse.
LVP is wrong in most cases. Hardly anyone has an LVP programmer.
Change it to NOLVP. Do that in all future programs.
XINST is wrong. CCS does not support the extended instruction set.
Use of XINST will result in erratic program execution. Change it to
NOXINST. Do that in all future programs. |
|
|
Guest
|
|
Posted: Tue Jan 13, 2009 1:54 pm |
|
|
Ok I changed what you suggested without any luck. I used the PIC Wizard in CCS to setup chip. Is it better to do this manually? The servo will pulse in the begining but then stops and waits about 20 seconds and pulses again. Anymore suggestions? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
Guest
|
|
Posted: Tue Jan 13, 2009 2:19 pm |
|
|
I had changed it to various numbers but still not working correctly. 1.5ms pulse shoud keep it around its home positon and 18.5ms pulse low to give it a full 20ms cycle. With code set to these values the servo pulses left about 30 degrees every ~20 seconds. Shouldnt it be constantly holding in the home positon? Could I be setting up my delay incorrectly? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jan 13, 2009 2:31 pm |
|
|
Yes, I just noticed your problem. The delay_ms() function cannot take
a floating point parameter. This is in the manual. |
|
|
Guest
|
|
Posted: Tue Jan 13, 2009 5:02 pm |
|
|
That was it!! Its works pretty well. Thanks for your help, from now on im gonna read about a function before I just use it. I have one last question for you though. When you start a new project do you manually set your fuses or do you use the wizard in CCS? What would you recommend? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Jan 13, 2009 5:14 pm |
|
|
I manually do it. The default for most of them is "OFF" anyway, so I
don't think there is a need to specify all of them. You can check the
state of the compiler assigned default values by looking at the end of
the .LST file.
Here are the fuses that I normally specify.
Code: | #include <18F452.h>
#fuses XT,NOWDT,PUT,BROWNOUT,NOLVP
#use delay(clock=4000000) |
|
|
|
|