Components Required:
PIC16F877A microcontroller.
Servo motor.
Power supply for the servo motor (typically 5V).
Proteus simulation environment.
MikroC IDE for PIC programming.
Circuit Connection:
Connect the power supply's positive terminal (usually 5V) to the servo motor's red (power) wire.
Connect the power supply's ground (GND) to both the servo motor's black (ground) wire and the PIC16F877A's ground pin (e.g., pin 8).
Connect the signal wire (usually yellow or white) of the servo motor to one of the PIC16F877A's PWM-capable pins (e.g., CCP1, usually on pin RC2).
MikroC Code:
Here's a simple example of MikroC code to control the servo motor:
c
Copy code
void main() {
TRISC.F2 = 0; // Set CCP1 pin (RC2) as output
CCP1CON = 0x0C; // Set CCP1 module to PWM mode
T2CON = 0x04; // Timer2 settings: prescaler 1:1, TMR2ON = 1
PR2 = 249; // Set PWM period (20ms)
while (1) {
// Move servo to the leftmost position (0 degrees)
CCPR1L = 12; // Set the duty cycle for 0 degrees
Delay_ms(1000); // Wait for 1 second
// Move servo to the middle position (90 degrees)
CCPR1L = 24; // Set the duty cycle for 90 degrees
Delay_ms(1000); // Wait for 1 second
// Move servo to the rightmost position (180 degrees)
CCPR1L = 36; // Set the duty cycle for 180 degrees
Delay_ms(1000); // Wait for 1 second
}
}
Proteus Simulation:
Open Proteus and create a new project.
Add the PIC16F877A microcontroller from the library and connect it to the circuit.
Add a servo motor model from the Proteus library and connect it to the PWM output (RC2) of the PIC16F877A.
Set up a 5V power supply for the servo motor.
Compile your MikroC code and generate a HEX file.
Load the HEX file into the PIC16F877A microcontroller in Proteus.
Run the simulation.
You should see the servo motor moving from 0 to 180 degrees and back in a continuous loop.
Make sure you have the necessary libraries and components installed in Proteus and that your MikroC compiler is set up correctly. This example provides a basic idea of how to interface and control a servo motor with a PIC microcontroller using MikroC and simulate it in Proteus. You can further customize the code to achieve your specific requirements.
23 авг 2024