Pages

Friday, March 12, 2021

PIC16F887 ADC servomotor rotation MikroC

Servo Motor

Servo motor is a type of motor that rotate to specific degree assigned by a controller. Some motor rotates within 180 degree, while others rotate up to 360 degree.

PIC16F887 ADC servomotor rotation MikroC
A sample of servo motor from Ali Express
This is a high torque motor that could handle some heavy load especially robot arm. It has three wires - two for supply voltage while the remaining one is control signal.



Supply voltage is typically a +5V DC supply from microcontroller board. Its  control signal is a TTL type logic level with a frequency of about 50Hz. Duty time of control signal is between 1.5ms to 2ms that rotate its angle between 0 and 180 degrees.

Microcontroller Interfacing and Programming

Controller needs to generate Pulse Width Modulation (PWM) signal with a frequency around 50Hz, with a variation of high time between 1.5ms to 2ms to rotate this motor. Without using PWM module of controller, programmer can create a software PWM routine instead. However this method has a timing latency.

PIC16F887 ADC servomotor rotation MikroC
Simulation of this program

A POT connects to AN0 of PIC16F887 that's channel 0 of ADC module. Controller reads and converts this ADC value into timing value of output PWM signal generates a pin RD0. This pin controls the angle of rotation of servo motor.

MikroC Program:

  1. #define Servo PORTD.RD0
  2. #define Max_Degree 2000
  3. unsigned ADC_Value;
  4. unsigned Servo_Value;
  5.  
  6. void main() {
  7. int i,j;
  8. PORTD=0x00;
  9. PORTA=0x00;
  10. TRISA=0x01;
  11. ANSEL=0x01;
  12. TRISD=0x00;
  13. OSCCON=0x70;
  14. ADC_Init();
  15. while(1) {
  16. ADC_Value=ADC_Get_Sample(0);
  17. Servo_Value=ADC_Value/4;
  18. Servo=1;
  19. for(i=0;i<Servo_Value;i++) {
  20.  
  21. delay_us(1);
  22. }
  23. Servo=0;
  24. for(j=0;j<20000-Servo_Value;j++) {
  25.  
  26. delay_us(1);
  27. }
  28. delay_ms(5);
  29. }
  30. }

Click here to download this example from GitHub.



No comments:

Post a Comment