 |
| Author |
Message |
|
|
Posted: Mar 07, 2012 - 05:45 AM |
|

Joined: Apr 23, 2011
Posts: 32
Location: SMILLER
|
|
i am using NXp P9c51rdRD2
i have problems rotating the servo motor(coreless_micro_servo std 0.6m>0 deg and 2.2ms>180 deg,speed >
0.12/60deg)
it moves forwards to 180 degree
at any given value and never turns back
the servo supports movement in both directions
------------------------------------------
my main code:
------------------------------------------
#include <REG51F.H>
#include "servo.h"
void delay(); /* prototype delay() */
void PowerOn()
{
unsigned char inner, outer;
IE = 0x00;
P1 = 0xFF; /* Motor STOP */
for (outer = 0x00; outer < 0x10; outer++) { /* Delay for a while */
for (inner = 0x00; inner < 0xFF; inner++);
}
Servos_init();
IE = 0x80; /* Start interrupt */
}
void main()
{
PowerOn();
begin :
delay_ms();
Servo1_forward();
delay_ms();
delay_ms();
Servo1_back();
delay_ms();
delay_ms();
Servo1_stop();
delay_ms();
delay_ms();
goto begin ;
}
void delay_ms()
{
unsigned int j;
for(j = 0 ; j < 40000 ; j ++)
{
}
}
------------------------------------------
servo.h code:
-------------------------------------------
/*
* Filename : servo.h
* Hardware : Controller -> P89V51RD2
* XTAL -> 11.0592 MHz
* Mode -> 6 Clock/MC
* I/O : P1.4 -> (PWM-CEX1)
* P1.5 -> (PWM-CEX2)
*
*/
#include <REG51F.H>
/* Control the servo at p1.4 */
void Servo1_back()
{
CCAP0H = 240;
}
void Servo1_forward()
{
CCAP0H = 220;
}
void Servo1_stop()
{
CCAP0H = 188;
}
void Servos_init()
{
/* Initial Timer0 Generate Overflow PCA */
TMOD = 0x02; /* Timer0 Mode2 : 8bit auto reload */
TH0 = 0x70; /* 256-240, 8.125usec Auto-relead (20msec/PWM) */
TL0 = TH0;
TCON = 0x10; /* setb TR0, TCON or 0001,0000*/
/*
Initial PWM Period = 20mS (11.0592MHz /6-Cycle Mode)
Initial PCA Count From Timer0 Overflow
1 Cycle of Timer0 = (1/11.0592MHz) x 6 = 0.542uS
Timer0 AutoReload = 144 Cycle = 78.125uS
1 Cycle PCA = [(1/11.0592MHz)x 6]x 144 = 78.125uS
Period 20mS of PCA = 20ms/78.125us = 256(CL Reload)
CL(20mS) = 256 Cycle Auto Reload
Load CCAPxH(0.6mS) = 256-8 = 248
Load CCAPxH(2.2mS) = 255-28 = 228
Load CCAPxH(2.2mS) = 255-28 = 228
*/
CMOD=0x04;
CCAPM0=0x42;
CCAPM1=0x42;
CCAPM2=0x42;
CCAPM3=0x42;
CCAPM4=0x42;
CCAP0H=0x00;
CCAP1H=0x00;
CCAP2H=0x00;
CCAP3H=0x00;
CCAP4H=0x00;
CCON=0x40;
}
did i messed up in calculations??? |
|
|
| |
|
|
|
|
|
Posted: Mar 07, 2012 - 06:07 AM |
|


Joined: Jul 02, 2005
Posts: 5950
Location: Melbourne, Australia
|
|
Ass killer.
This is a forum for the products of the Atmel company ... not NXP. Perhaps you should be asking your questions over at the NXP website. Best of luck.
Cheers,
Ross |
_________________ Ross McKenzie
ValuSoft
Melbourne Australia
|
| |
|
|
|
|
|
Posted: Mar 07, 2012 - 06:17 AM |
|

Joined: Apr 23, 2011
Posts: 32
Location: SMILLER
|
|
i thought the P89c51 has similarity with atmega IC.
anyways i will follow ur advice sir.
thks |
|
|
| |
|
|
|
|
|
Posted: Mar 07, 2012 - 08:26 AM |
|

Joined: Feb 12, 2005
Posts: 16324
Location: Wormshill, England
|
|
The P89C51RD2 has exactly the same PCA module as other Atmel AT89 controllers.
You have not specified what model number of servo you are using.
Most RC type servos have a 1-2ms pulse in a 20ms period.
A 1.5ms pulse is 'neutral'. The servo motor runs as required to shift the actuator to the required position. Once it has got there, the motor need only run if the actuator arm is moved out of place.
So you set the PWM for 1.5ms neutral.
If your PWM frequency is 50Hz (20ms period), your duty-cycle values would be:
Code:
50% 10ms CCAP0H = 256 - 128 = 128
5% 1.0ms CCAP0H = 256 - 12.8 = 243 - normal RC
7.5% 1.5ms CCAP0H = 256 - 19.2 = 237 - normal RC
10% 2.0ms CCAP0H = 256 - 25.6 = 230 - normal RC
11% 2.2ms CCAP0H = 256 - 28.2 = 228
3% 0.6ms CCAP0H = 256 - 7.7 = 248
You have probably noticed that you can't get very good granularity.
Many servos will work a little outside the 1-2ms range. They may not like it though.
Untested. I just did the sums.
David. |
|
|
| |
|
|
|
|
|
|
|