Microcontroller › 8051 › PWM what servos need with AT89c51RD2
- This topic has 0 replies, 1 voice, and was last updated 12 years, 11 months ago by László Nagy.
-
AuthorPosts
-
December 12, 2011 at 11:47 am #1437László NagyParticipant
I just met Arduino platform and get really surprised. Why? Because it is easy to use.
You don’t need to understand how controller works, how have to initialize functions via registers and so on. You don’t need to know the controller itself.
Because of this your project can be developed fast and easy. You will be success!
But…
I have an old 8051 Trainer bult in the scool with a powerful AT89c51RD2 PLCC44 processor.
Codes can be written in C in Keil UVison IDE and controller can be programmed Flip by Atmel.
This controller has PCA module with several functions like PWM. 5 independent PWM output can be produced with common freguency. Only Duty cyle can be individually set. Common frequency is sufficient if 4 servos are driven since frequency is same but Duty cycle.
Frequency is set about 20ms – 50Hz. Duty cycle depends on the direction or state of the servo. About 1ms for 0, 1,5ms for 90 and 2ms for 180 degrees.
Tower Pro SG90 micro servo is driven by this application. This servo is not that reliable one but good enough for test projects.
Test code:
/*
CSAMPLE1.C
*/#include <reg51xD2.h> /* define 8051 registers */
#define FRQ20ms 0xDB // (219 dec) at XTAL 11.0592 MHz
#define Left 0xE3 // 228 decimal
#define Kozep 0xEE // 238d
#define Right 0xF9 // 250d// 250 – 228 = 22. 180/22=8 degrees by steps.
int port2; // variable for Polling P2 since keypad. Servo state is set by buttons. Button0 for Left, Button1 for Middle, Button2 for Right position.
//********************** Prototipes **************************
void pwmInit(void); //Prototipe of PWM initialization//*********************** Main ******************************
void main(void)
{
pwmInit();while(1){ //Polling P2 and drive the servo according to the entry.
port2 = P2;
switch(port2)
{
case 0xFE: //Keypad is working with negative logic. Input is pulled-up with resistirs to VCC normally.
CR=0; // In case of pressed button related input goes to GND.
CCAP0H = Left; //PWM0 is set to Left
P0=port2;
port2=0;
CR=1;
return;
case 0xFD:
CR=0;
CCAP0H = Kozep; //PWM0 is set to Middle
P0=port2;
port2=0;
CR=1;
return;
case 0xFB:
CR=0;
CCAP0H = Right; //PWM 0 is set to Right.
P0=port2;
port2=0;
CR=1;
return;
case 0xF7:
CR=0;
CCAP1H = Left; //PWM1 is set toLeft
P0=port2;
port2=0;
CR=1;
return;
case 0xEF:
CR=0;
CCAP1H = Kozep; //PWM1 is set to Middle
P0=port2;
port2=0;
CR=1;
return;
case 0xDF:
CR=0;
CCAP1H = Right; //PWM1 is set to Right
P0=port2;
port2=0;
CR=1;
return;
}
}
}
//********************** Functions and Procedures ***********************
void pwmInit(void){
CKRL=FRQ20ms; //Set of built in Prescaler – 20ms/50Hz
CCAPM0=0x42; // PCA module is set to PWM mode and driven by interrupt
CCAPM1=0x42;
} -
AuthorPosts
- You must be logged in to reply to this topic.