Servo motor control using AT89S52
Today we would like to control TowerPro MG996R servo motor using AT89S52,
Actually AT89S52 is not supported hardware PWM, so we needs use two timer to
simulation PWM.
Actually AT89S52 is not supported hardware PWM, so we needs use two timer to
simulation PWM.
This's use breadboard
Here we use three buttons to control the Servo rotation, left button is let motor to rotate to 0 degree. middle button is let motor to rotate to 90 degree, and right button is let motor to rotate to 180 degree.
Here have three buttons according to control Servo to rotation
Schematic
Above video is show you the project I have press buttons each other, then the Servo motor will rotation according to the button.
#include <regx52.h> #include <intrins.h> #include <stdio.h>//700us = 0 degree //1.5ms = 90 degree //2.5ms = 180 degree void delay(unsigned int ms){ unsigned long int us = ms*1000; while(us--){ _nop_(); } } void delay_us(unsigned int us){ int i = 0; for(i=0; i<us;i++); } void delay_motor(unsigned int us){ while(us--){ _nop_(); } } void time0() interrupt 1{ // Using Time0 to delay 20ms TL0 = (65535 - 20000) & 0xFF; TH0 = ((65535 - 20000) & 0xFF00) >> 8; // Rotate to 0 degree if(P1_1){ /* As normally 65535-500 is 0.5ms, it's should be rotate to 0 dgress. But I don't know why not roate to 0 degree, TL1 = (65535 - 700) & 0XFF; TH1 = ((65535 - 700) & 0xFF00) >> 8; */ TL1 = (65535 - 700) & 0XFF; TH1 = ((65535 - 700) & 0xFF00) >> 8; } // Rotate to 90 degree if(P1_2){ TL1 = (65535 - 1500) & 0XFF; TH1 = ((65535 - 1500) & 0xFF00) >> 8; } // Rotate to 180 degree if(P1_3){ TL1 = (65535 - 2500) & 0XFF; TH1 = ((65535 - 2500) & 0xFF00) >> 8; } // Turn OFF the motor pin P1_0 = 1; // Turn ON Timer TR1 = 1; } void timer1() interrupt 3{ //Turn ON the motor pin P1_0 = 0; TR1 = 0; } void main(){ IE = 0x8A; TMOD = 0x11; TR0 = 1; TF0 = 1; P1_0 = 1; P1_1 = 1; P1_2 = 1; P1_3 = 1; while(1){ if(P1_1){ P0_1 = 0; //ON LED delay_us(922*5); P0_1 = 1; delay_us(922*5); } if(P1_2){ P0_2 = 0; //ON LED delay_us(922*5); P0_2 = 1; delay_us(922*5); } if(P1_3){ P0_3 = 0; //ON LED delay_us(922*5); P0_3 = 1; delay_us(922*5); } } }
Comments
Post a Comment