Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[PIC] i need help with servo motor and mikroc

Status
Not open for further replies.

killer_007

Newbie level 4
Newbie level 4
Joined
Feb 10, 2013
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Visit site
Activity points
1,319
I need a sample code to controlle a servo motor (MG996R) to move from 0 to 45 to 90 to 180 degre and go back to 0 using pic microcontroller and mikroc
 

Try this code:

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
void servoRotate0() //0 Degree
{
  unsigned int i;
  for(i=0;i<50;i++)
  {
    PORTB.F0 = 1;
    Delay_us(800);
    PORTB.F0 = 0;
    Delay_us(19200);
  }
}
 
void servoRotate90() //90 Degree
{
  unsigned int i;
  for(i=0;i<50;i++)
  {
    PORTB.F0 = 1;
    Delay_us(1500);
    PORTB.F0 = 0;
    Delay_us(18500);
  }
}
 
void servoRotate180() //180 Degree
{
  unsigned int i;
  for(i=0;i<50;i++)
  {
    PORTB.F0 = 1;
    Delay_us(2200);
    PORTB.F0 = 0;
    Delay_us(17800);
  }
}
 
void main()
{
  TRISB = 0; // PORTB as Ouput Port
  do
  {
    servoRotate0(); //0 Degree
    Delay_ms(2000);
    servoRotate90(); //90 Degree
    Delay_ms(2000);
    servoRotate180(); //180 Degree
  }while(1);
}

 

Thanks thannara123 ; ligo.george

The code works for another servo motor (TowerPro SG90 9G) after changing the delay value


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
void servoRotate0() 
//0 Degree 
{ 
**unsigned int i; 
**for(i=0;i<50;i++) 
**{ 
****PORTB.F0 = 1; 
****Delay_us(600); 
****PORTB.F0 = 0; 
****Delay_us(19400); 
**} 
} 
* void servoRotate90() //90 Degree 
{ 
**unsigned int i; 
**for(i=0;i<50;i++) 
**{ 
****
PORTB.F0 = 1; 
****Delay_us(1400); 
****PORTB.F0 = 0; 
****Delay_us(18600); 
**} } 
* void servoRotate180() 
//180 Degree 
{ 
**unsigned int i; 
**for(i=0;i<50;i++) 
**{ 
****PORTB.F0 = 1; 
****Delay_us(2400); 
****PORTB.F0 = 0; 
****Delay_us(17600); 
**} 
} 
* void main() { 
**TRISB = 0; 
// PORTB as Ouput Port 
**do 
**{ 
**Delay_ms(1000); 
****servoRotate0(); //0 Degree 
****Delay_ms(3000); 
****servoRotate90(); //90 Degree 
****Delay_ms(3000); 
****servoRotate180(); //180 Degree 
********Delay_ms(3000); 
****servoRotate90(); //90 Degree 
**}
while(1); 
}


But it dose not work for towerpro MG996R
I have tried change the delay value but still nothing
 

Servo motors have an internal circuit which automatically moves the motor from 0 to 270° depending of the input pulse width.
Every servo must have different timing so search the datasheet.

Maybe using the PWM module you can control it easily since the only thing you have to do is to modify a register value, and that module will do all the work.

Ask if you have any doubt.
 

Hello. I program with CCS, never tried with MikroC.

According to what I found PWM unit is controlled with:


PWM1_Init(frequency) --> Set the pulse frequency. Take into account that not every value is available since that frequency is taken from the oscillator, so you must look for any possible value.

PWM1_Set_Duty(dutycycle) --> Set the duty cycle. You can choose any combination. If your MCU PWM module has a 8-bit resulution, you can choose 256 values. 0=0, 50%=127, 100=255.

PWM1_Start() --> Start the modulation. Your pin now has a variable output.

PWM_Stop() --> Stops the modulation.

Do NOT use TMR2 while your PWM module is active. It works with that timer, so the only thing you can do with it is reading it but not modifying it (you can but it won't work properly, but you can do it for some applications).

Once you configure your pin as output and the PWM module, the only thing you have to do is changing the duty (if necessary).

Good bye.
 

See my post from 16th August: Servo tester with picf675 in basic.

This one is written in Proton Basic. You can download for free an old Proton Lite basic compiler with some limits, max 50 lines of code.

This worked for me and I hope it will work for you.
Then you can translate this over to Mikroc as an exercise ?

regards,

Nils
 

Re: i need help with servo motor and mikrochttps://www.edaboard.com/forums/31/

hello,

I wrote a little program to do a 50Hz square wave, with adjustement of duty cycle from 2%up to 98%
so 50Hz is periode of 20mS..
dedicate for Radiocommande Servo
written in MikroC and using 2 timers , duty cyle made by a potentiometer -> analog value 0 to 5V -> ADC input of MCU.

have a look **broken link removed**
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top