Hello All, I apologize for not getting back to the list to let you know whether I got = this problem solved. Unfortunately, I could not achieve speed control of my= servo. All I am able to do is make my servo to known angles and known rate= s. To recap, I am trying to achieve speed control of my servo using PIC12F675. There seems to be a problem is using the Hitech C delay functions. You cann= ot pass variables to the Hitech C delay functions. Hence I had to work around something like: while(i>0) { __delay_us(1); } Where i is the variable meant for changing the time delays. As suggested on= this list earlier, I had even tries using for loops. But I was not success= ful at all. I read somewhere that a possible reason for the problem could b= e that it is not possible to produce very small time delay cycles like 1 mi= crosecond and hence I decided to go in steps of 10 microseconds. But neithe= r for loops nor while loops are fixing my issue of time delay. Since the time delay is not perfect, my voltage regulator is boiling and th= e servo is not spinning at all. If I switch my compiler from Hitech to CCS, will it solve my problem? All I= need is a function where I can pass a variable as an argument to the delay= function. I am nervous to switch the compiler because while conducting the= experiments, I switched to the MikroC IDE. I got frustrated with the fact = that I couldn't set config words like I could do in MPLAB.=A0 I tried even setting macros for time delays as suggested in one of the micr= ochip forums. I got a lot of compiler errors and I just got frustrated out = of not being able to fix this problem. I am looking forward to hearing your suggestions on fixing the time delay i= ssue. Sai ----- Original Message ----- From: Tamas Rudnai To: Microcontroller discussion list - Public. Cc:=20 Sent: Wednesday, February 15, 2012 3:22 AM Subject: Re: [PIC]: Servo motor control - 12F675 Hi Sai, You need to maintain similar logic to this (need to wait variable time for duty cycle and you should not worry about how many cycles you are doing this -- you need to maintain servo pulses all times anyway, so it is better just limiting movements). BTW: You also can have one potmeter to active speed and direction: if you have a number from 0..1023 then subtract 512 from it and you will get +-512 so the upper half position will cause servo go upwards, lower position to go downwards... Anyway, here is the pseudo code with the button + potmeter version: // loop forever while (1) { =A0 =A0 // put your potmeter acquisition here =A0 =A0 .... =A0 =A0 // change servo position by speed and direction =A0 =A0 if ( GPIO & (1<<3) ) { =A0 =A0 =A0 =A0 pw +=3D quotient ); =A0 =A0 } =A0 =A0 else { =A0 =A0 =A0 =A0 pw -=3D quotient ); =A0 =A0 } =A0 =A0 // limit servo movements =A0 =A0 if ( pw < 1000 ) { =A0 =A0 =A0 =A0 pw =3D 1000; =A0 =A0 } =A0 =A0 if ( pw > 2000 ) { =A0 =A0 =A0 =A0 pw =3D 2000; =A0 =A0 } =A0 =A0 // wait for variable time =A0 =A0 for ( i =3D pw; i > 0; i-- ) { =A0 =A0 =A0 =A0 __delay_us(1); =A0 =A0 } =A0 =A0 __delay_ms(18); } Tamas On 15 February 2012 02:55, yamanoor sairam wrote: > Hello All, > > I guess this is my third mail on the same topic. I am trying to control a > servo motor using PIC 12F675. I was successful in driving the servo as we= ll > as achieving the speed control for the servo. Thanks for the help from th= e > members of this list. > > Now, my speed control is quite flaky. I tried tweaking my code here and > there but I have had no success so far. > > My main objective is that the servo should spin 0 to 90 and back based on > the data from toggle switch. I achieved this goal. When I turn a > potentiometer, the speed at which the servo rotates should change. I > achieved this too. However, the servo is not rotating 90 degrees at all t= he > time. > > It rotates 90 ish -120 degrees at high speeds and rotates only 30-45 > degrees on low speeds (or some weird behavior is happening). I want my > servo to rotate 90 degrees but just my speed should vary. > > Can any one point me to the error in my code? > > All this code is inside a while loop. > > GO_DONE=3D1; > while(GO_DONE); > V_in=3D(ADRESH<<8)|ADRESL; > quotient=3DV_in/200; // I am dividing this because I will have integers > between 1 to 5 > if(quotient<=3D1) > { > quotient=3D1; > } > if(GPIO&(1<<3)) > { > while(counter>1000) > { >=A0 counter=3Dcounter-(20*quotient); > GPIO|=3D(1<<2); >=A0 cnt_1=3Dcounter; >=A0 while(cnt_1>0) >=A0 { > __delay_us(1); >=A0 cnt_1--; > } > GPIO&=3D~(1<<2); > __delay_ms(18); > } > cnt=3D1000;//reset of counter for reverse direction > } > elseif(!(GPIO&(1<<3))) > { > while(cnt<1500) > { > cnt=3Dcnt+(20*quotient); > GPIO|=3D(1<<2); > cnt_2=3Dcnt; > while(cnt_2>0) > { > __delay_us(1); > cnt_2--; > } > GPIO&=3D~(1<<2); > __delay_ms(18); > } > counter=3D1500;//reset of counter for forward direction > } > } > > > Any help is appreciated > > Sai > } > > -- > http://www.piclist.com PIC/SX FAQ & list archive > View/change your membership options at > http://mailman.mit.edu/mailman/listinfo/piclist > --=20 int main() { char *a,*s,*q; printf(s=3D"int main() { char *a,*s,*q; printf(s=3D%s%s%s, q=3D%s%s%s%s,s,q,q,a=3D%s%s%s%s,q,q,q,a,a,q); }", q=3D"\"",s,q,q,a=3D"\\",q,q,q,a,a,q); } --=20 http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist --=20 http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist .