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.

Mixing it up a bit

Status
Not open for further replies.

SwingeyP

Member
I have the oshon pic basic compiler. I am writing a routine to pulse 3 servos at the same time. Now it sems I can't do this in pic basic so I have included some simple ASM o see what happens. It compiles ok pic basic but wont produce the assembly. Can anyone tell me why please?

For i = 1 To 10
ServoOut leg1servo1, midposition
ServoOut leg1servo2, midposition
ServoOut leg1servo3, midposition
Next i

For i = 1 To 10
ASM:bsf PORTB,7
ASM:bsf PORTB,6
ASM:bsf PORTB,5
WaitMs 10
ASM:bsc portb,7
ASM:bsc PORTB,6
ASM:bsc PORTB,5
Next i


Actually I guess it would be better to put a binary pattern on port b so they all go high an dlow around the same time. How do I get this to compile to assembly with oshon?

Regards - Paul
 
Doing simultaneous pulses of a servo is going to be much more complicated than that.
Most PIC basics have a 'servout' function which simply delays for the duration of the pulse, but you can't do them at the same time. I don't know much about the basic you're using though.

I don't understand the purpose of your loop, from what I read you're looping 10 times to send servo pulses to each of three servos for 10ms and then nothing else. Servo pulses have to be repeated every 20ms, 10ms is better but you have to use a timer to run a routine which sends these servoout commands once every 20ms minimum to each of the servo's you're trying to control. Keep in mind you will hit a hard limit of between 8 and 10 servos you can control, with a repition rate of 20ms, and 4-5 with a 10ms repition and that's assuming that's the ONLY thing the PIC is doing. Keep in mind there will still be a delay of between 2-4ms between each servo pulse, so the motor movements can never occur at exactly the same time, not with basic.
 
Last edited:
Hi Thanks for explaining what I mentioned in more detail. The loop is imaterial really I just wanted to show that I was mixing asm and basic. OSHON compiles the pic basic ok but I get errors creating the ASM. I can't find the asm.lst to see the errors reported. I just need to know how to mix asm and basic in oshon.

Regards - Paul
 
Hi Thanks for explaining what I mentioned in more detail. The loop is imaterial really I just wanted to show that I was mixing asm and basic. OSHON compiles the pic basic ok but I get errors creating the ASM. I can't find the asm.lst to see the errors reported. I just need to know how to mix asm and basic in oshon.

Regards - Paul
hi Paul,
I use Oshonsoft.
Post your full code including the ASM sections and I will try to debug it for you.
 
Hello again Eric.

The code isn't really much at all. I just wanted to see what would happen if I tried it. I would like to pulse multiple servos at the same time. Unfortunately the servoout will delay until it has completed making this impossible in picbasic. So I thought I wonder what happens if I do this.

Symbol leg1servo1 = PORTB.7 'J3 - Pin 28'
Symbol leg1servo2 = PORTB.6 'J4 - Pin 27'
Symbol leg1servo3 = PORTB.5 'J5 - Pin 26'

Const midposition = 128 'Set mid position for servos'


AllDigital

Config RA0 = Input
Config RA1 = Input
Config RA2 = Input
Config RA3 = Input
Config RA4 = Output
Config RA5 = Output

Config PORTB = Output

Config PORTC = Output

Dim i As Byte



Gosub centerservos

End



'Routine to centre all the servos'
centerservos:
For i = 1 To 10
ServoOut leg1servo1, midposition
ServoOut leg1servo2, midposition
ServoOut leg1servo3, midposition
Next i

For i = 1 To 10
ASM:bsf PORTB,7
ASM:bsf PORTB,6
ASM:bsf PORTB,5
WaitMs 10
ASM:bsc portb,7
ASM:bsc PORTB,6
ASM:bsc PORTB,5
Next i
Return

and track it with the scope. It compiles ok in pic basic but fails to create the asm file. Also I can't find the error listing for the asm file.

Regards - Paul
 
hi Paul
Your main problem is/was here, wrong statement you should use bcf to clear a bit.
Also observe the required formatting.

Code:
centerservos:
For i = 1 To 10
ServoOut leg1servo1, midposition
ServoOut leg1servo2, midposition
ServoOut leg1servo3, midposition
Next i

For i = 1 To 10
ASM:        bsf PORTB,7
ASM:        bsf PORTB,6
ASM:        bsf PORTB,5
WaitMs 10
ASM:        bcf PORTB,7 '  you had bsc???
ASM:        bcf PORTB,6
ASM:        bcf PORTB,5
Next i
Return
Code:
    list p=16f877a
    #include <p16f877a.inc>
    radix dec
; Compiled with: PIC Simulator IDE v6.85
; Microcontroller model: PIC16F877A
; Clock frequency: 4.0 MHz
;
;       The address of 'leg1servo1' (bit) (global) is 0x6,7
;       The address of 'leg1servo2' (bit) (global) is 0x6,6
;       The address of 'leg1servo3' (bit) (global) is 0x6,5
;       The value of 'midposition' (global) is 128
;       The address of 'i' (byte) (global) is 0x2C
    i EQU 0x2C
; Begin
    R0L EQU 0x20
    R0H EQU 0x21
    R1L EQU 0x22
    R1H EQU 0x23
    R2L EQU 0x24
    R2H EQU 0x25
    R3L EQU 0x26
    R3H EQU 0x27
    R4L EQU 0x28
    R4H EQU 0x29
    R5L EQU 0x2A
    R5H EQU 0x2B
    ORG 0x0000
    BCF PCLATH,3
    BCF PCLATH,4
    GOTO L0002
    ORG 0x0004
    RETFIE
; Begin of program
L0002:
; 1: Symbol leg1servo1 = PORTB.7 'J3 - Pin 28'
; 2: Symbol leg1servo2 = PORTB.6 'J4 - Pin 27'
; 3: Symbol leg1servo3 = PORTB.5 'J5 - Pin 26'
; 4: 
; 5: Const midposition = 128 'Set mid position for servos'
; 6: 
; 7: 
; 8: AllDigital
    BSF STATUS,RP0
    MOVLW 0x06
    MOVWF 0x1F
    MOVLW 0x07
    MOVWF 0x1C
; 9: 
; 10: Config RA0 = Input
    BSF TRISA,0
; 11: Config RA1 = Input
    BSF TRISA,1
; 12: Config RA2 = Input
    BSF TRISA,2
; 13: Config RA3 = Input
    BSF TRISA,3
; 14: Config RA4 = Output
    BCF TRISA,4
; 15: Config RA5 = Output
    BCF TRISA,5
; 16: 
; 17: Config PORTB = Output
    CLRF TRISB
; 18: 
; 19: Config PORTC = Output
    CLRF TRISC
    BCF STATUS,RP0
; 20: 
; 21: Dim i As Byte
; 22: 
; 23: 
; 24: 
; 25: Gosub centerservos
    CALL L0001
; 26: 
; 27: End
L0003:    GOTO L0003
; 28: 
; 29: 
; 30: 
; 31: 'Routine to centre all the servos'
; 32: centerservos:
L0001:
; 33: For i = 1 To 10
    MOVLW 0x01
    MOVWF 0x2C
L0004:
    MOVF 0x2C,W
    SUBLW 0x0A
    BTFSS STATUS,C
    GOTO L0005
; 34: ServoOut leg1servo1, midposition
    MOVLW 0x80
    MOVWF R4H
    MOVF R4H,F
    BTFSC STATUS,Z
    GOTO L0007
    BSF PORTB,7
L0006:
    NOP
    NOP
    NOP
    NOP
    NOP
    NOP
    DECF R4H,F
    BTFSS STATUS,Z
    GOTO L0006
    BCF PORTB,7
L0007:
; 35: ServoOut leg1servo2, midposition
    MOVLW 0x80
    MOVWF R4H
    MOVF R4H,F
    BTFSC STATUS,Z
    GOTO L0009
    BSF PORTB,6
L0008:
    NOP
    NOP
    NOP
    NOP
    NOP
    NOP
    DECF R4H,F
    BTFSS STATUS,Z
    GOTO L0008
    BCF PORTB,6
L0009:
; 36: ServoOut leg1servo3, midposition
    MOVLW 0x80
    MOVWF R4H
    MOVF R4H,F
    BTFSC STATUS,Z
    GOTO L0011
    BSF PORTB,5
L0010:
    NOP
    NOP
    NOP
    NOP
    NOP
    NOP
    DECF R4H,F
    BTFSS STATUS,Z
    GOTO L0010
    BCF PORTB,5
L0011:
; 37: Next i
    MOVLW 0x01
    ADDWF 0x2C,F
    BTFSS STATUS,C
    GOTO L0004
L0005:
; 38: 
; 39: For i = 1 To 10
    MOVLW 0x01
    MOVWF 0x2C
L0012:
    MOVF 0x2C,W
    SUBLW 0x0A
    BTFSS STATUS,C
    GOTO L0013

[B]; 40: ASM:        bsf PORTB,7
    bsf PORTB,7
; 41: ASM:        bsf PORTB,6
    bsf PORTB,6
; 42: ASM:        bsf PORTB,5
    bsf PORTB,5
; 43: WaitMs 10
    MOVLW 0x0A
    MOVWF R0L
    CLRF R0H
    CALL W001
; 44: ASM:        bcf PORTB,7
    bcf PORTB,7
; 45: ASM:        bcf PORTB,6
    bcf PORTB,6
; 46: ASM:        bcf PORTB,5
    bcf PORTB,5
; 47: Next i
    MOVLW 0x01
    ADDWF 0x2C,F
    BTFSS STATUS,C
    GOTO L0012
L0013:
; 48: Return
    RETURN
[/B]; End of program
L0014:    GOTO L0014
; Waitms Routine
W001:    MOVF R0L,F
    BTFSC STATUS,Z
    GOTO W002
    CALL W003
    DECF R0L,F
    NOP
    NOP
    NOP
    NOP
    NOP
    GOTO W001
W002:    MOVF R0H,F
    BTFSC STATUS,Z
    RETURN
    CALL W003
    DECF R0H,F
    DECF R0L,F
    GOTO W001
W003:    MOVLW 0x0C
    MOVWF R2H
W004:    DECFSZ R2H,F
    GOTO W004
    NOP
    NOP
    MOVLW 0x12
    MOVWF R1L
W005:    DECFSZ R1L,F
    GOTO W006
    CALL W007
    CALL W007
    NOP
    NOP
    RETURN
W006:    CALL W007
    GOTO W005
W007:    MOVLW 0x0D
    MOVWF R2L
W008:    DECFSZ R2L,F
    GOTO W008
    NOP
    RETURN
; End of listing
    END
 
Last edited:
Ahhh. Stupid error. Thanks Eric. - I have now been playing a bit more and have looked at the code generated by servoout. Although a useful command im sure you could do this programatically in picbasic to have pulses at the same time.

This code seems to work ok on the scope - ok its in simultion but im sure it would work ok.

Symbol leg1servo1 = PORTB.7 'J3 - Pin 28'
Symbol leg1servo2 = PORTB.6 'J3 - Pin 28'
Symbol leg1servo3 = PORTB.5 'J3 - Pin 28'


Const loopvalue = 10
Const delvalue = 100
AllDigital

Config PORTB = Output

ServoOut leg1servo1, loopvalue

leg1servo2 = 1
leg1servo3 = 1
WaitUs delvalue
leg1servo2 = 0
leg1servo3 = 0

End
 
hi Paul,
Is this what you are wanting to do.?? all go high/low at exactly the same time.?
I have used a 16F877A as the PIC.

Code:
For i = 1 To 10
ASM:        movlw 0xe0
ASM:        iorwf PORTB,F
WaitMs 10
ASM:        movlw 0x1f
ASM:        andwf PORTB,F
Next i
 
Last edited:
Hi Eric. Yes thanks for that. I was really toying with it at the moment to see how I can do things differently. I want to control six legs on my robot but im not happy with servoout only sending one pulse at a time. The idea is to send pulses to multiple servos at the same time. Then of course i need to fathom out duration of the pulse as servoout does and I also want to add a speed constant in the future. Im learning someting new everyday ;-)
 
Hi Eric. Yes thanks for that. I was really toying with it at the moment to see how I can do things differently. I want to control six legs on my robot but im not happy with servoout only sending one pulse at a time. The idea is to send pulses to multiple servos at the same time. Then of course i need to fathom out duration of the pulse as servoout does and I also want to add a speed constant in the future. Im learning someting new everyday ;-)
hi Paul,
I guess you know that you can do the same thing in Basic as that ASM snippet.
 
Hi Eric. Yes I did. im having fun with timing now. Moving things out of phase etc... This is great news for my walking gaits. I was told that this couldn't be done with servoout so thought well there has to be a way of doing what im after. I tried the ASM route and then thought well I could do that anyway in basic. Much easier ... ;-)

Symbol leg1servo1 = PORTB.7 'J3 - Pin 28'
Symbol leg1servo2 = PORTB.6 'J3 - Pin 28'
Symbol leg1servo3 = PORTB.5 'J3 - Pin 28'


Const loopvalue = 10
Const delvalue = 100
AllDigital

Config PORTB = Output

ServoOut leg1servo1, loopvalue

leg1servo2 = 1
WaitUs 50
leg1servo3 = 1
WaitUs 50
leg1servo2 = 0
WaitUs 50
leg1servo3 = 0

End
 
The first servoout is there to just show me what it does on the scope. The next bit is producing the same pulse with a delay to put them slightly out of phase.
 
Status
Not open for further replies.

Latest threads

Back
Top