0% found this document useful (0 votes)
94 views1 page

#Include Reg51.h

This document contains C code for controlling a servo motor using an 8051 microcontroller. It defines functions to create delays and rotate the servo to specific angles (0°, 90°, and 180°) with appropriate pulse widths. The main function continuously cycles through these positions with a 10-second pause between each movement.

Uploaded by

som22extc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
94 views1 page

#Include Reg51.h

This document contains C code for controlling a servo motor using an 8051 microcontroller. It defines functions to create delays and rotate the servo to specific angles (0°, 90°, and 180°) with appropriate pulse widths. The main function continuously cycles through these positions with a 10-second pause between each movement.

Uploaded by

som22extc
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

#include <reg51.

h>

sbit servo = P2^0;

void delay_ms(unsigned int ms) {


unsigned int i, j;
for(i = 0; i < ms; i++)
for(j = 0; j < 1275; j++); // 1ms delay
}

void delay_10s() {
unsigned int i;
for(i = 0; i < 10000; i++) {
delay_ms(1); // 10,000 ms = 10 seconds
}
}

void servo_rotate_0() {
servo = 1;
delay_ms(1); // 1ms pulse for 0° position
servo = 0;
delay_ms(19); // 20ms cycle time
}

void servo_rotate_90() {
servo = 1;
delay_ms(1.5); // 1.5ms pulse for 90° position
servo = 0;
delay_ms(18.5); // 20ms cycle time
}

void servo_rotate_180() {
servo = 1;
delay_ms(2); // 2ms pulse for 180° position
servo = 0;
delay_ms(18); // 20ms cycle time
}

void main() {
while(1) {
servo_rotate_0(); // Move servo to 0°
delay_10s(); // Wait for 10 seconds

servo_rotate_90(); // Move servo to 90°


delay_10s(); // Wait for 10 seconds

servo_rotate_180(); // Move servo to 180°


delay_10s(); // Wait for 10 seconds
}
}

You might also like