|
|
View previous topic :: View next topic |
Author |
Message |
Atomix182
Joined: 04 Apr 2007 Posts: 2
|
PIC18F4450 generate 20 ms for SERVO |
Posted: Wed Apr 04, 2007 5:08 am |
|
|
Hi,
I am a student of university and i make a board with microcontroller PIC18F4450. The board controls a Servo Motor Futaba S3003(in future 8 Servo). I have a big problem about Clock. I use USB connection and i see the example ex_usb_hid.c (in examples directory).
I use 20 Mhz External osc, a Quarz.
I read datasheet of the microcontroller, and i see this:
I must have 4 Mhz input of the PLL and output is 96 Mhz.
I use PLL5, so 20Mhz/5 -> 4 Mhz for the PLL.
Next I use CPUDIV1, so 96Mhz/2 -> 48 Mhz .
it's okay ?
Fuses are:
HS with PLL, HSPLL, PLL5 and CPUDIV1.
Code: |
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=48000000)
|
What's mean USBDIV ?
delay clock it's correct(clock = 48 Mhz ??) ?
For generate PWM for servo i use #int_rcc because i will use more servo.
Code: |
setup_counters( RTCC_INTERNAL, RTCC_DIV_1 | RTCC_8_BIT);
enable_interrupts(INT_RTCC);
enable_interrupts(GLOBAL);
|
So clock_isr function will be call every
1/(48000000/(256*4)) = 21,3 us ? right ?
For 20 ms period(SERVO), i must wait:
20 ms / 21.3 us = (about) 939 times.
And for 1.5 ms (center of SERVO) will be:
20 ms / 939 div = 0.021 ms/div
1.5 ms / (20 ms / 939 div) = 70.4 times
it's right ?
I make some test, but i discover this:
for center servo times are 50 and not 70.4 ??
How it's possible this ?
50 x 21.3 us = 1065 us = 1,065 ms -> FULL LEFT not CENTER ?!?!
Maybe i wrong about setup timer ? or clock it's not 48 Mhz ???
Thank You. |
|
|
Ttelmah Guest
|
|
Posted: Wed Apr 04, 2007 5:24 am |
|
|
USBDIV, selects whether the USB clock comes from the PLL/divider, or directly from the crystal. Won't effect you, unless using USB, but is 'right' as set.
Look at the actual chip data sheet. Figure 2.1, shows the oscillator layout, and you can see the 'USBDIV' switch, and what it selects between.
Your problem is almost certainly that the chip is running out of time.
You are interrupting every 256 intructions in the processor. Now handling an interrupt (even if you do _nothing_), takes typically perhaps 60-70 instruction times. You only have to be doing a tiny amount of code in the 'handler', to run out of time, and then the effective rate will run slow. You are just trying to do things faster than the chip really supports. Switch to using RTCC_DIV_2, and make sure that your code in the handler is _quick_. Look at how long maths operations for example take...
Best Wishes |
|
|
Atomix182
Joined: 04 Apr 2007 Posts: 2
|
PIC18F4450 generate 20 ms for SERVO |
Posted: Wed Apr 04, 2007 5:34 am |
|
|
Okay i try to setup timer more slowly
this is my code in #int_rtcc
per = Period
pws = How much signal is on HIGH(5V)
pos = Position of Servo (70 times on 939 times)
MOTOR = PIN output
Code: |
#int_rtcc // This function is called every time
void clock_isr()
{ // the RTCC (timer0) overflows (255->0).
if (pws <= pos)
{
OUTHIGH(MOTOR);
pws = pws + 1;
}
else
{
OUTLOW(MOTOR);
}
per = per + 1;
if (per == 939)
{
per = 0;
pws = 0;
}
}
|
Thank You again for reply so fast |
|
|
|
|
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
|