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.

Pic 16f877

Status
Not open for further replies.

uaefame

New Member
Here is a general question> Suppose now I want to turn a servo motor first 45 degree and stay there for 2 second then come back to zero?
is it possible?
 

uaefame

New Member
I want to get a hexapod robot stand up. Here is my analysis timing.
T=20ms ( Period)
On Time for 12motors 1.65ms
On Time for 3motors 1.9ms
On Time for 3motors 1.2ms

I want to drive 18 servo motors togather. The interesting part is they share same command.

I can't find an easy way to start any help will be appericated.

Thanks in advance
 

uaefame

New Member
I started writting my own codes using the help of PIC delay generator website
Code:
   list      p=16F877         ; list directive to define processor
    #include <p16F877.inc>         ; processor specific variable definitions

errorlevel -302

	cblock 20h
	d1
	d2
	endc

org 0h
nop 
bsf STATUS,RP0
bcf STATUS,RP1
movlw 0x0
movwf TRISB
movlw 0x0
movwf TRISC
movlw 0x0
movwf TRISD
bcf STATUS,RP0
bcf STATUS,RP1
nop
start 
movlw b'11111111'
movwf PORTB
movlw b'11111111'
movwf PORTC
movlw b'11111111'
movwf PORTD
call Delay1ms200us
movlw b'11111000'
movwf PORTB
call Delay450us
movlw b'00000000'
movwf PORTB
movlw b'10000000'
movwf PORTC
call Delay250us
movlw 0x0
movwf PORTC
movlw 0x0
movwf PORTD
call Delay18ms100us
goto start

Delay450us
			;2243 cycles
	movlw	0xC0
	movwf	d1
	movlw	0x02
	movwf	d2
Delay450us_0
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	Delay450us_0

			;3 cycles
	goto	$+1
	nop

			;4 cycles (including call)
	return

Delay1ms200us
			;5993 cycles
	movlw	0xAE
	movwf	d1
	movlw	0x05
	movwf	d2
Delay1ms200us_0
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	Delay1ms200us_0

			;3 cycles
	goto	$+1
	nop

			;4 cycles (including call)
	return

Delay250us
			;1243 cycles
	movlw	0xF8
	movwf	d1
	movlw	0x01
	movwf	d2
Delay250us_0
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	Delay250us_0

			;3 cycles
	goto	$+1
	nop

			;4 cycles (including call)
	return

Delay18ms100us
			;90493 cycles
	movlw	0xB2
	movwf	d1
	movlw	0x47
	movwf	d2
Delay18ms100us_0
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	Delay18ms100us_0

			;3 cycles
	goto	$+1
	nop

			;4 cycles (including call)
	return




END

When I test the code in real life nothing happen to the servo?

Any Idea why?
 

futz

Active Member
When I test the code in real life nothing happen to the servo? Any idea why?
How are your servos wired? Can you post a simple schematic of even just one servo and how you have it all connected.

Servos, especially as many as you want to control, can be a bit power greedy. You should power them from a separate supply so they don't cause the PIC's power to dip when they move, causing the PIC to reset. Of course you must connect the PIC's and the servos' grounds together. The servos' data lines can connect direct to the PIC pins.

I cleaned up your code some and am reposting it below. It appears that you want to control 24 servos. Your main loop pulses the top 4 bits of PortB for 1.2ms/20ms, the lower 4 bits of PortB and the lower 7 bits of PortC for 1.65ms/20ms and the high bit of PortC and all of PortD for 1.9ms/20ms.

04 servos - 1.20ms pulse
11 servos - 1.65ms pulse
09 servos - 1.90ms pulse

Typical servos want 1.5ms/20ms for center, 1ms/20ms for full clockwise and 2ms/20ms for full anti-clockwise. You have to test yours to see if they differ from the above timings.

Code:
	#include <p16F877.inc>
	errorlevel -302

	cblock	0x20
	d1,d2
	endc

	org	0x000
	banksel	TRISB		;bank 1
	clrf	TRISB
	clrf	TRISC
	clrf	TRISD
	banksel	PORTB		;bank 0
start	movlw	b'11111111'
	movwf	PORTB
	movwf	PORTC
	movwf	PORTD
	call	Delay1ms200us
	movlw	b'11111000'
	movwf	PORTB
	call	Delay450us
	crf	PORTB
	movlw	b'10000000'
	movwf	PORTC
	call	Delay250us
	clrf	PORTC
	clrf	PORTD
	call	Delay18ms100us
	goto	start

Delay450us
	movlw	0xC0
	movwf	d1
	movlw	0x02
	movwf	d2
Delay450us_0
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	Delay450us_0
	goto	$+1
	nop
	return

Delay1ms200us
	movlw	0xAE
	movwf	d1
	movlw	0x05
	movwf	d2
Delay1ms200us_0
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	Delay1ms200us_0
	goto	$+1
	nop
	return

Delay250us
	movlw	0xF8
	movwf	d1
	movlw	0x01
	movwf	d2
Delay250us_0
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	Delay250us_0
	goto	$+1
	nop
	return

Delay18ms100us
	movlw	0xB2
	movwf	d1
	movlw	0x47
	movwf	d2
Delay18ms100us_0
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	Delay18ms100us_0
	goto	$+1
	nop
	return

	END
 
Last edited:
Status
Not open for further replies.

EE World Online Articles

Loading
Top