Hi Sai, How is your circuit looks like? Do you have a 100nF ceramic capacitor close to the Vss/Vdd pins? How is your switches connected? I have the impression that you may have both GPIO3 and 4 high so that the servo wants to move to one position then 20 ms later to the other? An 'else' would help on this (see on the code below). ANSEL and CMCON is not initialised, that could cause some trouble too. Not sure which compiler you use but have you checked if the __delay_us() function takes an int or unsigned int as opposed to char or byte? (for this reason I have changed the duty cycle in the code to __delay_ms() instead) Also I should use an OR to make the pin high (that might does not matter this case but looks better to me). Furthermore you do not need to wait exactly 20ms at each frame, so you can normalise the algorithm. It does not even really matter if the frequency is perfectly the same or not as long as there is at least 6ms off periods. Overall the algorithm I would try is like this: voidmain ( void ) { CMCON =3D 0x07; ANSEL =3D 0; TRISIO =3D ( 1 << 3 ) | ( 1 << 4 ); while ( 1 ) { if ( GPIO3 ) { GPIO |=3D ( 1 << 2 ); __delay_ms ( 1 ); GPIO &=3D ~ ( 1 << 2 ); } else if ( GPIO4 ) { GPIO |=3D ( 1 << 2 ); __delay_ms ( 2 ); GPIO &=3D ~ ( 1 << 2 ); } // wait for around 20 ms including duty cycle __delay_ms ( 18 ); } } Tamas On 10 February 2012 02:35, yamanoor sairam wrote: > Hello All, > > I am trying to control a servo motor using a PIC12F675 with a toggle > switch. When the switch is one state, the servo goes forward and the othe= r > state, the motor goes reverse. > > Here is something that I tried: > > _CONFIG(WDTDIS&PWRTEN&MCLRDIS&BOREN&UNPROTECT&INTCLK); > #define_XTAL_FREQ4000000 > > voidmain(void){ > TRISIO=3D(1<<3)|(1<<4); > while(1){ > if(GPIO3) > { > GPIO=3D(1<<2); > __delay_us(900); > > > GPIO&=3D~(1<<2); > __delay_us(100); > __delay_ms(19); //here the total time pulse is 20 ms including ON time > > > > } > if(GPIO4) > { > GPIO=3D(1<<2); > __delay_us(2100); > > > GPIO&=3D~(1<<2); > __delay_us(900); > __delay_ms(17); > > } > > } > } > > My main need is that the servo should spin in opposite directions wheneve= r > the switch changes state. The motor changes position only once and nothin= g > happens after that. > > I am not able to find out what is wrong here. I also tried making the > motor move in opposite directions without any switch control. The servo > does not spin as it should. It stays at the same position and makes some > jerky movements. > > I am looking forward to your suggestion regarding the same. > > 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 .