🔧 What Does a Servo Need?
A servo motor expects a control signal (PWM) every 20 milliseconds (50 times per second). This signal must stay HIGH for a specific time between:
- 0° angle → 500 microseconds HIGH pulse
- 90° angle → 1500 microseconds HIGH pulse
- 180° angle → 2500 microseconds HIGH pulse
So, the width of the HIGH pulse determines the angle of rotation. After the HIGH pulse, the signal is kept LOW for the remaining duration of the 20 ms period.
📐 Calculating Pulse Width from Angle
The pulse width is calculated using the formula:
1 | pulseWidth = 500 + (angle × 2000) / 180 |
This converts any angle between 0 and 180 into a pulse width between 500 µs and 2500 µs. The result is the time for which the servo signal must stay HIGH.
⏱️ Timer Conversion for 8051
The 8051 Timer0 is configured in 16-bit Mode 1. With a standard 11.0592 MHz crystal, each timer tick is approximately 1.085 microseconds. Since we need to delay for a specific number of microseconds, we convert the required pulse width to timer ticks.
Floating point is avoided, so a fixed-point approximation is used:
1 | ticks = pulseWidth × 59 / 64 |
This conversion gives the number of timer ticks needed to match the desired pulse width in microseconds. The 16-bit timer is then loaded with 65536 - ticks
so it overflows after the correct delay.
⚙️ How the Servo Pulse is Generated
The servo signal is generated in two phases using Timer0:
- ON Time: The servo control pin is made HIGH and the timer runs for the calculated pulse width.
- OFF Time: The servo control pin is made LOW and the timer runs for the remaining time (i.e., 20000 µs – pulseWidth) to complete the 20 ms cycle.
This ON and OFF pattern is repeated every 20 milliseconds to keep the servo stable at the desired angle.
📌 Summary
- The angle is converted to pulse width between 500 and 2500 microseconds.
- Pulse width is converted to timer ticks using a scaling factor for 1.085 µs tick time.
- Timer0 is used to generate both ON and OFF pulses in each 20 ms cycle.
- This method enables precise control of servo angle without floating-point operations.
This approach works perfectly for 8051-based embedded systems using SDCC, and is efficient for controlling standard hobby servo motors.
Leave Your Reply
You must be logged in to post a comment.