Assembly & C Program For Servo Motor With 8051

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy