Joystick Motor Control

Discussion in 'Embedded Systems and Microcontrollers' started by pancikok, Jun 19, 2011.

  1. pancikok

    Thread Starter New Member

    Oct 31, 2009
    26
    0
    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:
    Code ( (Unknown Language)):
    1.     LIST    p=16F628        ;tell assembler what chip we are using
    2.     include "P16F628.inc"        ;include the defaults for the chip
    3.     ERRORLEVEL    0,    -302    ;suppress bank selection messages
    4.     __config 0x3D18            ;sets the configuration settings (oscillator type etc.)
    5.  
    6.     cblock     0x20             ;start of general purpose registers
    7.  
    8.         count            ;used in looping routines
    9.         count1            ;used in delay routine
    10.         count2
    11.         counta            ;used in delay routine
    12.         countb            ;used in delay routine
    13.         temp
    14.         tmp1            ;temporary storage
    15.         tmp2
    16.  
    17.            HiX            ;result for X pot
    18.         LoX
    19.         HiY            ;result for Y pot
    20.         LoY
    21.         Flags    
    22.    
    23.     endc
    24.  
    25. JOY_PORT    Equ    PORTB
    26. JOY_TRIS    Equ    TRISB
    27. PotX        Equ    0x06            ;input assignments for joystick
    28. PotY        Equ    0x02
    29. MR    Equ    0x04    ;pin for motor reverse
    30. MF    Equ    0x05    ;pin for motor forward
    31. ;pin 3 (RB3) is the PWM channel output -CCP1
    32.  
    33.         org    0x0000
    34.         nop
    35.         nop
    36.         nop
    37.         goto    Start
    38.         org 0x0010
    39.  
    40. Start        movlw    0x07
    41.         movwf    CMCON            ;turn comparators off (make it like a 16F84)
    42.  
    43. Initialise    clrf    count
    44.         clrf    PORTA
    45.         clrf    PORTB
    46.  
    47. SetPorts    bsf     STATUS,        RP0    ;select bank 1
    48.         movlw    0xff            ;make all joystick pins inputs
    49.         movwf    JOY_TRIS
    50.         bcf     STATUS,        RP0    ;select bank 0
    51.  
    52.         call    JOY_Init        ;discharge timing capacitors
    53.         call    PWM_Init        ;  
    54.  
    55. Main
    56.         call    ReadX            ;read X joystick
    57.     ;    call    ReadY            ;read Y joystick
    58.  
    59.         MOVLW    LoX
    60.         CALL    SpeedL        ;half speed forwards
    61.  
    62.        
    63.         goto    Main            ;loop for ever
    64.  
    65. ;joystick routines
    66.  
    67. JOY_Init                ;setup joystick port
    68.     bsf     STATUS,        RP0    ;select bank 1
    69.     bcf    JOY_TRIS,    PotX    ;make PotX an output
    70.     bcf    JOY_PORT,    PotX    ;discharge capacitor
    71.     bcf    JOY_TRIS,    PotY    ;make PotY an output
    72.     bcf    JOY_PORT,    PotY    ;discharge capacitor
    73.     bcf     STATUS,        RP0    ;select bank 0
    74.     retlw    0x00
    75.  
    76. ReadX
    77.     clrf    HiX            ;reset counter registers
    78.     clrf    LoX    
    79.     bsf     STATUS,        RP0    ;select bank 1
    80.     bsf    JOY_TRIS,    PotX    ;make PotX an input
    81.     bcf     STATUS,        RP0    ;select bank 0    
    82.  
    83. x1    
    84.     btfsc    JOY_PORT,    PotX    ;keep going until input high
    85.     goto    EndX
    86.     incfsz    LoX,f
    87.     goto    x1
    88.     incfsz    HiX,f
    89.     goto    x1
    90.  
    91. EndX    bsf     STATUS,        RP0    ;select bank 1
    92.     bcf    JOY_TRIS,    PotX    ;make PotX an output
    93.     bcf    JOY_PORT,    PotX    ;discharge capacitor
    94.     bcf     STATUS,        RP0    ;select bank 0
    95.     retlw    0x00
    96.  
    97. ReadY
    98.     clrf    HiY            ;reset counter registers
    99.     clrf    LoY
    100.     call    Delay5    
    101.     bsf     STATUS,        RP0    ;select bank 1
    102.     bsf    JOY_TRIS,    PotY    ;make PotY an input
    103.     bcf     STATUS,        RP0    ;select bank 0    
    104.  
    105. y1    
    106.     btfsc    JOY_PORT,    PotY    ;keep going until input high
    107.     goto    EndY
    108.     incfsz    LoY,f
    109.     goto    y1
    110.     incfsz    HiY,f
    111.     goto    y1
    112.  
    113. EndY    bsf     STATUS,        RP0    ;select bank 1
    114.     bcf    JOY_TRIS,    PotY    ;make PotY an output
    115.     bcf    JOY_PORT,    PotY    ;discharge capacitor
    116.     bcf     STATUS,        RP0    ;select bank 0
    117.     retlw    0x00
    118.  
    119. ;PWM routines
    120. PWM_Init:
    121.         BANKSEL  PORTB
    122.         BANKSEL  TRISB
    123.         MOVLW    3        ;set PORTB as all outputs
    124.         MOVWF    TRISB
    125.         BANKSEL  PORTB
    126.  
    127.         MOVF     CCP1CON,W    ;set CCP1 as PWM
    128.         ANDLW    0xF0
    129.         IORLW    0x0C
    130.         MOVWF    CCP1CON
    131.  
    132.         MOVLW    126        ;set highest PWM value
    133.         BANKSEL  PR2        ;over this (127) is permanently on
    134.         MOVWF    PR2
    135.         BANKSEL  TMR2
    136.  
    137.         MOVF     T2CON,W    ;set prescaler to 16
    138.         ANDLW    0xF8        ;PWM at 2500HZ
    139.         IORLW    0x02
    140.         MOVWF    T2CON
    141.  
    142.         MOVF     T2CON,W    ;set postscaler to 1
    143.         ANDLW    0x07
    144.         IORLW    0x00
    145.         MOVWF    T2CON
    146.        
    147.         CLRF    CCPR1L        ;set PWM to zero
    148.  
    149.         BSF      T2CON, TMR2ON    ;and start the timer running
    150.     RETURN
    151.  
    152. SpeedL:                ;use value in W to set speed (0-127)
    153.         MOVWF    temp
    154.         BTFSC    temp, 7        ;if more than 128 set speed in reverse
    155.         CALL    ReverseL    ;so '1' is very slow forward
    156.          BTFSS    temp, 7        ;and '129' is very slow reverse
    157.         CALL    ForwardL
    158.         ANDLW    0x7F
    159.         MOVWF   CCPR1L
    160.     RETURN
    161.  
    162. ReverseL:
    163.     BSF    PORTB, MR    ;set pins for reverse
    164.     BCF    PORTB, MF
    165.     RETURN
    166.  
    167. ForwardL:
    168.     BCF    PORTB, MR    ;set pins for forward
    169.     BSF    PORTB, MF
    170.     RETURN
    171.  
    172. ;Delay routines
    173.  
    174. ;Delay routines
    175.  
    176. Delay255    movlw    0xff        ;delay 255 mS
    177.         goto    d0
    178. Delay100    movlw    d'100'        ;delay 100mS
    179.         goto    d0
    180. Delay50        movlw    d'50'        ;delay 50mS
    181.         goto    d0
    182. Delay20        movlw    d'20'        ;delay 20mS
    183.         goto    d0
    184. Delay5        movlw    0x05        ;delay 5.000 ms (4 MHz clock)
    185. d0        movwf    count1
    186. d1        movlw    0xC7            ;delay 1mS
    187.         movwf    counta
    188.         movlw    0x01
    189.         movwf    countb
    190. Delay_0
    191.         decfsz    counta, f
    192.         goto    $+2
    193.         decfsz    countb, f
    194.         goto    Delay_0
    195.  
    196.         decfsz    count1    ,f
    197.         goto    d1
    198.         retlw    0x00
    199.  
    200. ;end of Delay routines
    201.  
    202.  
    203.         end
     
    Last edited: Jun 20, 2011
  2. pancikok

    Thread Starter New Member

    Oct 31, 2009
    26
    0
    Please, people! Anything that could help, is appreciated!
     
  3. nerdegutta

    Moderator

    Dec 15, 2009
    2,515
    785
  4. pancikok

    Thread Starter New Member

    Oct 31, 2009
    26
    0
    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?!?!
     
  5. nerdegutta

    Moderator

    Dec 15, 2009
    2,515
    785
  6. pancikok

    Thread Starter New Member

    Oct 31, 2009
    26
    0
    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!
     
  7. stahta01

    Member

    Jun 9, 2011
    133
    21
    Any reason you believe your joystick is any thing like what the code is designed to work with? Tim S.
     
  8. stahta01

    Member

    Jun 9, 2011
    133
    21
  9. pancikok

    Thread Starter New Member

    Oct 31, 2009
    26
    0
    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!
     
  10. pancikok

    Thread Starter New Member

    Oct 31, 2009
    26
    0
    Everything was same,still doesn't work (((
     
  11. stahta01

    Member

    Jun 9, 2011
    133
    21
    Last edited: Jun 20, 2011
  12. pancikok

    Thread Starter New Member

    Oct 31, 2009
    26
    0
    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.
     
    Last edited: Jun 21, 2011
  13. stahta01

    Member

    Jun 9, 2011
    133
    21
    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: Jun 21, 2011
    pancikok likes this.
Loading...