what's wrong with my servo control code?

Status
Not open for further replies.

kcy

New Member
Yo everyone this is my first ever post on this awesome forum I am currently working on my final year project to build a four-legged, stair-climbing robot...I am still relatively new to PIC and MPLAB stuff so I am looking for some advice and help here...

I am writing a test servo code which moves the servo position to 180 degrees and then back to 90 degrees...When I test it, the servo stops when it only reaches the 90 degrees position and kinda stuck at that position instead of moving to 180 then back to 90...Sometimes it even jitters but it just won't move beyond 90 degrees...Is it something wrong with the code I have written, perhaps a problem with my delay? I am using a 20Mhz crystal clock...

Code:
; RC3 is output to servo

				list			p=16f877a
				include		   <p16f877a.inc>
				__CONFIG   		0x3FF2
	
D1				EQU			0x20					
D2				EQU			0x21
D3				EQU			0x22	
COUNT			     EQU			0x23			

				ERRORLEVEL		-305				
				ORG			0x200				
				GOTO			IOSETUP

IOSETUP	
				ERRORLEVEL		-302		
				BANKSEL			TRISC				
				MOVLW			b'00000000'			
				MOVWF			TRISC				 
				BANKSEL			PORTC				
				ERRORLEVEL		+302	
				CLRF			PORTC				
				GOTO			MAIN

MAIN			
				BANKSEL			PORTC				
	
TURN180	
				MOVLW			d'25'            ; repeat to hold the servo at 180 degree before moving on
				MOVWF			COUNT		
LOOP			     BSF			PORTC,3
				CALL			 DELAY2m        ; generates 2mS pulse for 180 degree turn
				BCF			 PORTC,3
				CALL			 DELAY18m      ; total 20mS
				DECFSZ			COUNT
				GOTO			LOOP
				GOTO			TURN90

TURN90
				BSF			PORTC,3
				CALL			DELAY1p5m      ; generates 1.5mS pulse for 90 degree turn
				BCF			PORTC,3
				CALL			DELAY18p5m    ; total 20mS
				GOTO			TURN90


DELAY1p5m		 MOVLW			d'1'					
				MOVWF			D3
				MOVLW			d'10'
				MOVWF			D2
				MOVLW			d'249'
				MOVWF			D1
				DECFSZ			D1
				GOTO			$-1
				DECFSZ			D2
				GOTO			$-5
				DECFSZ			D3
				GOTO			$-9
				RETURN

DELAY2m			  MOVLW			d'1'					
				MOVWF			D3
				MOVLW			d'14'
				MOVWF			D2
				MOVLW			d'237'
				MOVWF			D1
				DECFSZ			D1
				GOTO			$-1
				DECFSZ			D2
				GOTO			$-5
				DECFSZ			D3
				GOTO			$-9
				RETURN

DELAY18p5m		 MOVLW			d'1'					
				MOVWF			D3
				MOVLW			d'121'
				MOVWF			D2
				MOVLW			d'254'
				MOVWF			D1
				DECFSZ			D1
				GOTO			$-1
				DECFSZ			D2
				GOTO			$-5
				DECFSZ			D3
				GOTO			$-9
				RETURN

DELAY18m		  MOVLW			d'1'				
				MOVWF			D3
				MOVLW			d'119'
				MOVWF			D2
				MOVLW			d'251'
				MOVWF			D1
				DECFSZ			D1
				GOTO			$-1
				DECFSZ			D2
				GOTO			$-5
				DECFSZ			D3
				GOTO			$-9
				RETURN

				END
Any advice and help would be greatly appreciated...Thanks

p/s sorry for the messy code...I wrapped
Code:
 tags around it but it still turns out like this :confused:
 
Last edited:
Hi KCY
what is your configuration
_config 0x3FF2 i could not create on MPLAB 7.5

you have ofcourese mentioned 20Mhz crystal oscillator
by keeping watchdog OFF the programm was running on MPlab simulator
i have even removed the -302 and -305 etc errror levels and complied.

can you please elaborate regarding CONFIG value
 
Is there a reason why you start your code at 0x200 instead of 0x0000?

Apart from the org, everything looks fine except you only allow 0.5 seconds for the servo to go to 180. Try changing the value of count from 25 to 200. This will allow your servo 4 seconds to get to the 180 position.

Mike.
Sarma, In MPLAB you can look at the configuration for 3ff2. It is quite normal.
 
@mvs sarma
do you mean this? I got this from Configure-->Configuration bits

Oscillator HS
Watchdog Timer Off
Power up Timer On
Brown Out Detect On
Low Voltage Program Enabled
Flash Program Write Write Protection Off
Data EE Read Protect Off
Code Protect Off

@Pommie
I am using a PIC microcontroller kit which requires me to start my code at 0x200...I already changed my value of count to 200...When I test it the servo only moves to 90 degree and then stop moving :< What have I done wrong?
 
Your code looks correct, the only thing that looks suspicious is having "Low Voltage Programming" enabled. Is this a requirement of the kit?

Can you verify that the pic is actually running the code? Check the output with a scope, put an LED on another port pin etc.

BWT, the code will by default continue to the next location. There is no need to put a goto there.
I.E.
Code:
				GOTO			TURN90

TURN90

Mike.
 

Sorry I double-checked and found out that the PIC microcontroller kit that I am using has already set the configurations as below:
i. Oscillator = HS
ii. Watchdog Timer = Off
iii. Power Up Timer = On
iv. Brown Out Detect = Off
v. Low Voltage Program = Disabled
vi. Flash Program Write = Write Protection Off
vii. Data EE Read Protect = Off
viii. Code Protect = Off

This should be in effect instead of the __CONFIG 0x3FF2 command...
Done deleted the redundant GOTO code
I don't have a scope so I can't check with it...I ran a LED flash program before and it worked without any problem, if that is what you mean...
 
Last edited:
I just tried your code on a 16F876A and it works fine. The pulses show up on the scope just as expected. The only thing I changed was the org to 0000 and turning the Low Voltage Prog off. So, the problem has to be hardware.

Mike.
P.S. the 876 is the same as the 877 but with fewer pins. I just happened to have one linked up and so it was easy to just try it and see.

Edit, try changing your config to 0x3F72 to turn of LVP.
 
Last edited:

With bootloader firmware resized in PIC, user code must start from address 0x200. For assembly language, reset vector must start at address 0x200 while interrupt vector should start at 0x204.

I am quoting the instructions from my PIC kit manual...Since the org 0x200 seems necessary for my pic, I left it as it is (I received an error when I tried to change the 0x200 to 0x00) and only changed 0x3FF2 to 0x3F72 but it's still the same situation

I had tried writing code for the servo to only turn 180 and turn 90 respectively and they worked fine...But when I want it to turn 180 then back to 90 then it just won't work...This means the servo is getting the signal and PIC is working fine...?

Does this have anything to do with my power supply? I am using a 9V battery and my PIC kit has an onboard voltage regulator, 7805 which provides stable 5V output...I am very confused :<
 
Last edited:
I suspect that the servo is reseting your pic. Can you supply your servo from a separate supply such as a 6V battery. Only connect the ground and signal wires from your servo to your pic kit and connect the ground and +ve to the separate battery. You could also try changing the 9V battery for a wall wart and see if that improves things.

Mike.
 
I am testing it on a breadboard so like what you say, I need to connect only ground and signal wires from servo to pic kit, then connect ground and +ve from 6V battery to breadboard right?
 
kcy said:
I am testing it on a breadboard so like what you say, I need to connect only ground and signal wires from servo to pic kit, then connect ground and +ve from 6V battery to breadboard right?

If breadboard and servo are the same circuit then Yes.

Mike.
 
I am still not really familiar with electronics stuff :< I am connecting my servo through header pins (breadboard connector) to the breadboard. I connect the ground and +ve from 6V battery to the servo and the signal wire from pic to servo...So should I connect the pic ground to the breadboard?

After connecting everything how do I know if the servo is resetting my pic?
 
Last edited:

Yes, you should connect the ground from pic to your breadboard.

Your pic may have been reset by the motor starting up. With a seperate supply that shouldn't happen.

Mike.
 
I connected everything and power on but nothing happens...Only the servo is moving a little from getting power supply...Seems like the servo is not receiving signal from the pic...Is something wrong with my connection? I connected the ground from pic to breadboard to the outer horizontal row of the breadboard while the ground and +ve from 6V battery as well as signal wire from pic is connected to the centre row of the breadboard...
 
You should have the following connections.

Gnd of Pic to servo Gnd to battery -ve.
Signal from Pic to Servo.
Servo +ve to battery +ve.

Mike.
 
 
Pommie said:
You should have the following connections.

Gnd of Pic to servo Gnd to battery -ve.
Signal from Pic to Servo.
Servo +ve to battery +ve.

Mike.

Yup I connected correctly and the servo received signal from the pic...yet the situation is still the same, that is the servo moves to 90 degree position and stuck there So my last option is to change my 9V battery? What's a wall wart? Is power adapter okay?
 
Hi
A small power-supply brick with integral male plug, designed to plug directly into a wall outlet; called a ‘wart’ because when installed on a power strip it tends to block up at least one more socket than it uses. These are frequently associated with modems and other small electronic devices which would become unacceptably bulky or hot if they had power supplies on board (there are other reasons as well having to do with the cost of UL certification).
 
 
by using the config definitions you have indicated the config values work out to that

second issue i was telling was about the patch for delay2m subroutine-- and better to test its correctness -- perhaps all the delay patches for that matter

-- what i was trying to know is whether during the delay period any input to PIC is expected from the Servo ??
 
Last edited:
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…