Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
50% found this document useful (2 votes)
1K views

Assembly & C Program For Servo Motor With 8051

The document contains two programs for controlling a servo motor using an 8051 microcontroller. The first program uses timers to generate pulses of different durations on pin P2.0 to rotate the servo to 0, 90, and 180 degrees, with 1 second delays between positions. The second program is a C program that interfaces with a servo motor by pulsing pin P2.0 high for different durations to achieve the same three positions, looping indefinitely. Both programs utilize timing loops or timer-based delays to control the servo motor's position.

Uploaded by

Khuram Shahzad
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
50% found this document useful (2 votes)
1K views

Assembly & C Program For Servo Motor With 8051

The document contains two programs for controlling a servo motor using an 8051 microcontroller. The first program uses timers to generate pulses of different durations on pin P2.0 to rotate the servo to 0, 90, and 180 degrees, with 1 second delays between positions. The second program is a C program that interfaces with a servo motor by pulsing pin P2.0 high for different durations to achieve the same three positions, looping indefinitely. Both programs utilize timing loops or timer-based delays to control the servo motor's position.

Uploaded by

Khuram Shahzad
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

ORG 0H

START: JB P3.2,START ;if button is pressed, will start the program

TRY: MOV TMOD,#10H ;For clockwise, timer 1 mode 1


AGAIN: MOV TL1,#0ABH
MOV TH1,#0F9H
SETB P2.1 ;start the motor
SETB TR1
BACK: JNB TF1,BACK
CLR TR1
CLR TF1
CLR P2.1 ;stop the motor

DELAY: MOV R0,#030H ;For anticlockwise


LOOP1: MOV R1,#0FFH
LOOP2: DJNZ R1,LOOP2
DJNZ R0,LOOP1

END

----------
another program
ORG 00H //Start the program
MAIN:
MOV TMOD, #01H ;using Timer 0 in Mode 1
LCALL zero_degrees ;Function to move to position = 0 deg
LCALL delay ;Function to create a delay of 1 sec

LCALL ninety_degrees ;Function to move to position = 90 deg


LCALL delay ;Function to create a delay of 1 sec

LCALL one_eighty_degrees ;Function to move to position = 180 deg


LCALL delay ;Function to create a delay of 1 sec

SJMP MAIN ;to repeat the loop until manually stopped


RET

zero_degrees: //To create a pulse of 1ms


MOV TH0, #0FCH //(FFFF - FC19 + 1)H = (03E7)H
MOV TL0, #19H //equal TO (1000)D = 1ms
SETB P2.0 ;Make P2.0 HIGH
SETB TR0 ;Start the timer 0
WAIT1:JNB TF0, WAIT1 ;Wait till the TF0 flag is set
CLR P2.0 ;Make P2.0 LOW
CLR TF0 ;Clear the flag manually
CLR TR0 ;Stop the timer 0
RET

ninety_degrees: //To create a pulse of 1.5ms


MOV TH0, #0FAH //(FFFF - FA25 + 1)H = (05DB)H
MOV TL0, #25H //equal to (1500)D = 1.5ms
SETB P2.0 ;Make P2.0 HIGH
SETB TR0 ;Start the timer 0
WAIT2:JNB TF0, WAIT2 ;Wait till the TF0 flag is set
CLR P2.0 ;Make P2.0 LOW
CLR TF0 ;Clear the flag manually
CLR TR0 ;Stop the timer 0
RET

one_eighty_degrees: //To create a pulse of 2ms


MOV TH0, #0F8H //(FFFF - F831 + 1)H = (07CF)H
MOV TL0, #31H //equal to (2000)D = 1.5ms
SETB P2.0 ;Make P2.0 HIGH
SETB TR0 ;Start the timer 0
WAIT3:JNB TF0, WAIT3 ;Wait till the TF0 flag is set
CLR P2.0 ;Make P2.0 LOW
CLR TF0 ;Clear the flag manually
CLR TR0 ;Stop the timer 0
RET

delay: //To create a delay of 1sec


MOV R4,#64H ;100us * 100us * 100us = 1s
LOOP1:MOV R3,#64H
LOOP2:MOV R2,#64H
LOOP3:DJNZ R2,LOOP3
DJNZ R3,LOOP2
DJNZ R4,LOOP1
RET

-----------------
C program to interface servo motor with 8051
#include<reg52.h>
#include<stdio.h>
#include <intrins.h>

sbit servo_pin = P2^0;


void delay(unsigned int);
void servo_delay(unsigned int);
void main()
{
servo_pin = 0;
do
{
//Rotate to 0 degree
servo_pin = 1;
servo_delay(50);
servo_pin = 0;

delay(1000);

//Rotate to 90 degree
servo_pin=1;
servo_delay(82);
servo_pin=0;

delay(1000);
//Rotate to 180 degree
servo_pin=1;
servo_delay(110);
servo_pin=0;

delay(1000);
}while(1);
}

void delay(unsigned int ms)


{
unsigned long int us = ms*1000;
while(us--)
{
_nop_();
}
}

void servo_delay(unsigned int us)


{
while(us--)
{
_nop_();
}
}

You might also like