Joystick Motor Control

Thread Starter

pancikok

Joined Oct 31, 2009
26
Hello people!
I am working on a radio controlled motor application. I constructed the circuits mostly based on Nigel Goodwin's PIC tutorials. Actually, the code is also basically from those tutorials. I just combined, copied, pasted some code with another. These are , PWM, Manchester encoding, Joystick reading and delay code parts. So i haven't written any of this routines. As i said i just combined parts that i need in order to meet my needs.

To test the Analog Joystick reading part (Tutorial 4) i connected joystick to PIC16F628 (PIC that i used) and PIC to motor driver (L293D), and a DC motor to it. The overall schematic should be included in the attachment. For a start i tried to read only X axis of analog joystick. So as a result, i wanted to get the motor roll forward and reverse according to X axis position. Also, i am using mini analog joystick, rather than PC analog joystick. The picture should also be included in the attachment.
So the problem is, it doesn't work. As soon as i turn the power on, the motor starts to turn. That's all. Please, help to figure out whats wrong. It should be in the code, that i have messed up.
This is the code:
Rich (BB code):
    LIST    p=16F628        ;tell assembler what chip we are using
    include "P16F628.inc"        ;include the defaults for the chip
    ERRORLEVEL    0,    -302    ;suppress bank selection messages
    __config 0x3D18            ;sets the configuration settings (oscillator type etc.)

    cblock     0x20             ;start of general purpose registers

        count            ;used in looping routines
        count1            ;used in delay routine
        count2
        counta            ;used in delay routine
        countb            ;used in delay routine
        temp
        tmp1            ;temporary storage
        tmp2

           HiX            ;result for X pot
        LoX
        HiY            ;result for Y pot
        LoY
        Flags    
    
    endc

JOY_PORT    Equ    PORTB
JOY_TRIS    Equ    TRISB
PotX        Equ    0x06            ;input assignments for joystick
PotY        Equ    0x02
MR    Equ    0x04    ;pin for motor reverse
MF    Equ    0x05    ;pin for motor forward
;pin 3 (RB3) is the PWM channel output -CCP1

        org    0x0000
        nop
        nop
        nop
        goto    Start
        org 0x0010

Start        movlw    0x07
        movwf    CMCON            ;turn comparators off (make it like a 16F84)

Initialise    clrf    count
        clrf    PORTA
        clrf    PORTB

SetPorts    bsf     STATUS,        RP0    ;select bank 1
        movlw    0xff            ;make all joystick pins inputs
        movwf    JOY_TRIS
        bcf     STATUS,        RP0    ;select bank 0

        call    JOY_Init        ;discharge timing capacitors
        call    PWM_Init        ;   

Main
        call    ReadX            ;read X joystick
    ;    call    ReadY            ;read Y joystick

        MOVLW    LoX
        CALL    SpeedL        ;half speed forwards

        
        goto    Main            ;loop for ever

;joystick routines

JOY_Init                ;setup joystick port
    bsf     STATUS,        RP0    ;select bank 1
    bcf    JOY_TRIS,    PotX    ;make PotX an output
    bcf    JOY_PORT,    PotX    ;discharge capacitor
    bcf    JOY_TRIS,    PotY    ;make PotY an output
    bcf    JOY_PORT,    PotY    ;discharge capacitor
    bcf     STATUS,        RP0    ;select bank 0
    retlw    0x00

ReadX
    clrf    HiX            ;reset counter registers
    clrf    LoX    
    bsf     STATUS,        RP0    ;select bank 1
    bsf    JOY_TRIS,    PotX    ;make PotX an input
    bcf     STATUS,        RP0    ;select bank 0    

x1    
    btfsc    JOY_PORT,    PotX    ;keep going until input high
    goto    EndX
    incfsz    LoX,f
    goto    x1
    incfsz    HiX,f
    goto    x1

EndX    bsf     STATUS,        RP0    ;select bank 1
    bcf    JOY_TRIS,    PotX    ;make PotX an output
    bcf    JOY_PORT,    PotX    ;discharge capacitor
    bcf     STATUS,        RP0    ;select bank 0
    retlw    0x00

ReadY
    clrf    HiY            ;reset counter registers
    clrf    LoY
    call    Delay5    
    bsf     STATUS,        RP0    ;select bank 1
    bsf    JOY_TRIS,    PotY    ;make PotY an input
    bcf     STATUS,        RP0    ;select bank 0    

y1    
    btfsc    JOY_PORT,    PotY    ;keep going until input high
    goto    EndY
    incfsz    LoY,f
    goto    y1
    incfsz    HiY,f
    goto    y1

EndY    bsf     STATUS,        RP0    ;select bank 1
    bcf    JOY_TRIS,    PotY    ;make PotY an output
    bcf    JOY_PORT,    PotY    ;discharge capacitor
    bcf     STATUS,        RP0    ;select bank 0
    retlw    0x00

;PWM routines
PWM_Init:
        BANKSEL  PORTB
        BANKSEL  TRISB
        MOVLW    3        ;set PORTB as all outputs
        MOVWF    TRISB
        BANKSEL  PORTB

        MOVF     CCP1CON,W    ;set CCP1 as PWM
        ANDLW    0xF0
        IORLW    0x0C
        MOVWF    CCP1CON

        MOVLW    126        ;set highest PWM value
        BANKSEL  PR2        ;over this (127) is permanently on
        MOVWF    PR2
        BANKSEL  TMR2

        MOVF     T2CON,W    ;set prescaler to 16
        ANDLW    0xF8        ;PWM at 2500HZ
        IORLW    0x02
        MOVWF    T2CON

        MOVF     T2CON,W    ;set postscaler to 1
        ANDLW    0x07
        IORLW    0x00
        MOVWF    T2CON
        
        CLRF    CCPR1L        ;set PWM to zero

        BSF      T2CON, TMR2ON    ;and start the timer running
    RETURN

SpeedL:                ;use value in W to set speed (0-127)
        MOVWF    temp
        BTFSC    temp, 7        ;if more than 128 set speed in reverse
        CALL    ReverseL    ;so '1' is very slow forward
         BTFSS    temp, 7        ;and '129' is very slow reverse
        CALL    ForwardL
        ANDLW    0x7F
        MOVWF   CCPR1L
    RETURN

ReverseL:
    BSF    PORTB, MR    ;set pins for reverse
    BCF    PORTB, MF
    RETURN

ForwardL:
    BCF    PORTB, MR    ;set pins for forward
    BSF    PORTB, MF
    RETURN

;Delay routines

;Delay routines

Delay255    movlw    0xff        ;delay 255 mS
        goto    d0
Delay100    movlw    d'100'        ;delay 100mS
        goto    d0
Delay50        movlw    d'50'        ;delay 50mS
        goto    d0
Delay20        movlw    d'20'        ;delay 20mS
        goto    d0
Delay5        movlw    0x05        ;delay 5.000 ms (4 MHz clock)
d0        movwf    count1
d1        movlw    0xC7            ;delay 1mS
        movwf    counta
        movlw    0x01
        movwf    countb
Delay_0
        decfsz    counta, f
        goto    $+2
        decfsz    countb, f
        goto    Delay_0

        decfsz    count1    ,f
        goto    d1
        retlw    0x00

;end of Delay routines


        end
 

Attachments

Last edited:

Thread Starter

pancikok

Joined Oct 31, 2009
26
Thank you for post!

yes, of course i have read the datasheet. Well, i can't see the difference between pin outs. Input 1, 2, outputs, enable, and Vss,Vs are all connected correct, i gues??!!? Maybe you could help me figure out?!?!
 

Thread Starter

pancikok

Joined Oct 31, 2009
26
well it looks more complex to me ))) if i cant get to work the one that i already have, it would be more problematic for me right now! Moreover, i already tested my PWM part, just running the motor in different speeds and directions, and it worked just fine.
The main thing i am interested now is to read joystick and apply this to motor.
But, thanks anyway!
 

Thread Starter

pancikok

Joined Oct 31, 2009
26
Yes, of course. As far as i could understand, the code counts the time that is needed to charge the capacitor, hereby detecting the position of potentiometer of an analog joystick. In original code, it also reads the switch buttons of joystick, but i don't have any buttons, so i cut off that part. The rest is the same i guess. In my joystick it has one pin for X axis, and one for Y, same as in PC joystick i guess.

And, the link is useful, let me check it with modified code again.
Thanks a lot!
 

Thread Starter

pancikok

Joined Oct 31, 2009
26
well, actually i have already seen that article, and several others before i could understand how joystick works. But now i got confused. Because in the following link they connect the first pin (+5V) to ground.
http://www.yourwarrantyisvoid.com/2010/02/03/basic-stamp-a-quick-guide-on-using-a-legacy-joystick/

In my joystick, there are 3 pins for each axis, one for positive supply, one for ground and the middle one is for value that varies. So it should be connected to ground.

Moreover, i tried to connect the joystick directly to motor as shown in the attachment. The motor didn't respond to any kind of position change.
 

Attachments

Last edited:

stahta01

Joined Jun 9, 2011
133
Feel free to do it a new way; but, I can no longer help you.
(I barely understand how the code is supposed to work using a simple RC time constant if you vary the applied voltage as your circuit does I see no way in the world the results will be easy to figure out.)

Edit: I am guessing the Joystick input is supposed to be a simple RC circuit. This is a series circuit where an cap is in series with an resistance. In this case, I think it needs a fixed voltage applied with an varying resistance. When you ground the other side of other pot, it means the circuit is no longer just a series circuit.

I do not think your circuit matches what the code wants.
I suggest you ask the site that supplied the code what the Joystick circuit is supposed to do.

Just because a pot has three connections DOES NOT mean you must hook them all up. This is one of the mistakes half the students in the PIC class I am Teaching Assistance (TA) for do every year.

Tim S.
 
Last edited:
Top