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

Labels

ADC (10) Analog (14) Arduino (12) Atmega16 (19) Audio (2) AVR (20) Charger (1) Cortex-M0 (1) Counter (10) CPLD (25) Digital I/O (22) Display (34) EEPROM (2) Environment Sensor (1) esp8266 (2) Experiment Board (10) I2C (4) Interrupt (7) LCD (1) LDmicro (29) measurement and instrumentation (7) Microchip Studio (3) MikroC (1) One-Shot (3) OpAmp (1) PCB (31) PIC16 Microcontrollers (16) PIC16F877A (2) PIC16F887 MikroC (22) PLC (35) PWM (11) Regulator (1) RTC (2) Sensor (8) Shift Registers (5) SPI (5) Timer (34) UART (2) ultra-sonic sensor (1) USB (1) VHDL (21) xc8 (1) XC95108 (9) XC9536 (15) XC9572 (1) Xilinx (23) Xilinx ISE (22)