Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Problem with 16F628A and sevo

Status
Not open for further replies.

sparky72

Member
Recently I put this simple program together to move an s3003 Futaba servo approximately 180 degrees clockwise and back 180 degrees counterclockwise with one push of a momentary switch. I am using the MCLRE pin to reset program when needed. The program does as I expect but the way that I have it is that I cannot get out of the counterclockwise programming to a sleep instruction without screwing up the counterclockwise section of the program. My experience is very limited in programming so I thought I would post this for a little help which would be appreciated very much. Thanks in advance for any help that I may receive. I have attached the source code.

Code:
[ATTACH]50650[/ATTACH]
 

Attachments

  • Servocontrol2.asm
    2.1 KB · Views: 144
The way you have it, you'll never get out of the loop.
Code:
Backward    	
		bsf		PORTB,2				
		call		Delay2			;Delay for 1ms (counterclockwise motion)
		bcf		PORTB,2
	    goto		Backward

How about doing it similar to how you have 'Forward'
Code:
		movlw		d'25'
	    movwf		Count

Forward
		bsf			PORTB,2
		call		Delay3			;delay for 2ms "clockwise motion"
		bcf	    	PORTB,2
		call		Delay1
		call		Delay1
		call		Delay1			;delay for 18ms
		decfsz		Count
		goto		Forward
 
The way you have it, you'll never get out of the loop.
Code:
Backward    	
		bsf		PORTB,2				
		call		Delay2			;Delay for 1ms (counterclockwise motion)
		bcf		PORTB,2
	    goto		Backward

How about doing it similar to how you have 'Forward'
Code:
		movlw		d'25'
	    movwf		Count

Forward
		bsf			PORTB,2
		call		Delay3			;delay for 2ms "clockwise motion"
		bcf	    	PORTB,2
		call		Delay1
		call		Delay1
		call		Delay1			;delay for 18ms
		decfsz		Count
		goto		Forward


Thanks House of Wax for the quick response. I have tried what you suggested but when I do that, It just moves counterclockwise
90 degrees, and I need to go back approx.180 degrees
 
If the following code (what I meant in my earlier post) turns the servo 90 degrees;

Code:
;Turn180 Backwards		
		movlw		d'25'
	        movwf		Count		

Backward    	
		bsf			PORTB,2				
		call		Delay2			;Delay for 1ms (counterclockwise motion)
		bcf			PORTB,2
		decfsz		Count
	    goto		Backward
and you want 180, then how about movlw d'50' instead?
I don't have a servo that works, so I can't try it out myself.
 
If the following code (what I meant in my earlier post) turns the servo 90 degrees;

Code:
;Turn180 Backwards		
		movlw		d'25'
	        movwf		Count		

Backward    	
		bsf			PORTB,2				
		call		Delay2			;Delay for 1ms (counterclockwise motion)
		bcf			PORTB,2
		decfsz		Count
	    goto		Backward
and you want 180, then how about movlw d'50' instead?
I don't have a servo that works, so I can't try it out myself.

Thanks again HouseOFwax. Your latest suggestion helped somewhat. On the backward movement it slows down quite a bit towards the end but is very close to the amount of travel that I need. At least we seem to be on the right track now. I will tweak the count figures a bit and let you know how I make out. I have to leave this project for the time being but will get back to it probably tomorrow. Take care
 
HouseOFwax, after fooling around with the figures a bit as you suggested, I finally have it working suitable for my application. I left the Count for the forward movement figure at 25 and used 55 for the Count figure for the backward motion. This is just a hobby for me, as a retiree and I have learned a lot from people like you and others who are willing to spend their time and share their knowledge with me and others. Thanks again
 
After my last post I was chatting to a friend in the pub, he happened to have a spare servo (microscopic thing) that he has donated to me. I tried your code with it and it 'twitched' a bit.
I have since had a go at it myself.
Code:
	    LIST	        p=16f628A
	    include			"p16f628A.inc"
	    __CONFIG	_CP_OFF & _DATA_CP_OFF &_LVP_OFF & _BOREN_OFF &_MCLRE_OFF & _WDT_OFF & _PWRTE_ON & _INTOSC_OSC_NOCLKOUT		
	    
	    
	    ERRORLEVEL -302

 
		cblock	0x20
				d0
				d1
				d2
				count
				endc
			
				
			
			
		ORG			0x0000
		
		movlw		0x07		    ;like 16f84
		movwf		CMCON
		


Initialization

		banksel		TRISB			;select bank 1
		movlw		b'00000000'		
		movwf		TRISB			;Make TRISB all outputs 
		movwf		TRISA			;Make TRISA all outputs
		banksel		PORTB			;Select Bank 0			
		
		clrf		PORTA
		clrf		PORTB
		

Start
;--------------------------------------------------------------------
		movlw	0x64
		Movwf	Count			;allow travel time and hold
Zero		
		bsf			PORTB,2		
		call		Delay1.25	;Pulse on for 1.25mS, 0 position
		bcf	    	PORTB,2		
		call		Delay20		;Pause for 20mS (servo's expect it)
		decfsz 		count
		goto		Zero


		movlw	0x64
		Movwf	Count			;allow travel time and hold
Ninety
		bsf			PORTB,2
		call		Delay1.5	;Pulse on for 1.5mS, 90 position
		bcf	    	PORTB,2
		call		Delay20		;Pause for 20mS (servo's expect it)
		decfsz 		count
		goto		Ninety
		
		movlw	0x64
		Movwf	Count			;allow travel time and hold
One_eighty		
		bsf			PORTB,2
		call		Delay1.75	;Pulse on for 1.75mS, 180 position
		bcf	    	PORTB,2
		call		Delay20		;Pause for 20mS (servo's expect it)
		decfsz 		count
		goto		One_eighty

		goto	Start
;--------------------------------------------------------------------
Delay1.5				;0.0015 seconds
		movlw 0xF1
		movwf d0
		movlw 0x02
		movwf d1
loop1.5
		decfsz d0, f
		goto loop1.5
		decfsz d1, f
		goto loop1.5

; Rest = 2 Cycles
		goto  $+1
		
		RETLW	0x00
;---------------------		
Delay20					;0.02 seconds
		movlw 0xF8
		movwf d0
		movlw 0x1A
		movwf d1
loop20
		decfsz d0, f
		goto loop20
		decfsz d1, f
		goto loop20

; Rest = 1 Cycles
		nop
		RETLW	0x00
;---------------------	
Delay1.25				;0.00125 seconds
		movlw 0x9E
		movwf d0
		movlw 0x02
		movwf d1
loop1.25
		decfsz d0, f
		goto loop1.25
		decfsz d1, f
		goto loop1.25

; Rest = 1 Cycles
		nop
		RETLW	0x00
	
;---------------------	
Delay1.75				;0.00175 seconds
		movlw 0x44
		movwf d0
		movlw 0x03
		movwf d1
loop1.75
		decfsz d0, f
		goto loop1.75
		decfsz d1, f
		goto loop1.75

; Rest = 1 Cycles
		nop
		RETLW	0x00
	
;-----------------------------------------------
		
    	END

Having looked at this site
and using their suggested timings of 1.25mS and 1.75mS I found that my servo only moved a fraction off centre in each direction. I have since 'played' with the timings and have managed to get very nearly 0 - 180 using timings of 0.5mS and 2.3mS.

The above code drives the same port pin as your original code (since it was based on it).
Have a go, see what you think.
 
HouseOFwax, I tried the above code without changing the delays and I got the same results you had. The servo just twitches. I changed the delay routines as per your suggestion and the servo just moves to the forward position and stays there. I also tried it without going to the 90 degree delay but still to no avail. I guess I will just stay with the code that we came up with last week as it seems to work ok for the application I am using it with. Thanks again for all the trouble you went to, to help me out. It has been a good learning experience for me.
 
You are very welcome to the help, (I've enjoyed it). I have never had the chance to play with a working servo until very late Sunday night (after drinking). It seems that the 1.5mS sets things to 90 degrees 'as a rule' however different types of servos have different limits. The one I have has no markings that I can see. As such, it's just a case of trial and error.

Best wishes.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top