352
// Function to set PWM duty cycles for both motors void setPWMDutyCycle(int speedA, int speedB) { CCPR1L = (unsigned char)speedA; // Set PWM duty cycle for Motor A CCPR2L = (unsigned char)speedB; // Set PWM duty cycle for Motor B } // Function to run Motor A void runMotorA(int direction, int speed) { setPWMDutyCycle(speed, CCPR2L); // Update PWM for Motor A if (direction == FORWARD) { IN1 = 1; // Forward direction IN2 = 0; } else if (direction == BACKWARD) { IN1 = 0; // Backward direction IN2 = 1; } } // Function to run Motor B void runMotorB(int direction, int speed) { setPWMDutyCycle(CCPR1L, speed); // Update PWM for Motor B if (direction == FORWARD) { IN3 = 1; // Forward direction IN4 = 0; } else if (direction == BACKWARD) { IN3 = 0; // Backward direction IN4 = 1; } } // Function to stop both motors void stopMotors() { CCPR1L = 0; // Stop Motor A CCPR2L = 0; // Stop Motor B IN1 = 0; // Turn off Motor A IN2 = 0; IN3 = 0; // Turn off Motor B IN4 = 0; }