Need Help for the next stage PIC programming

Discussion in 'Embedded Systems and Microcontrollers' started by bluebrakes, Jul 28, 2011.

  1. bluebrakes

    Thread Starter Active Member

    Oct 17, 2009
    245
    7
    Hi,

    I've got pic code working to a stage where I need to take it to the next level.
    Basically the code is for driving 3 stepper motors. Currently Just steps them in one direction.

    So if you send the number 1 through the PC serial port, Stepper 1 steps. number 2 steps stepper 2 and 3 steps stepper 3.

    What I would like to do now is send a message to the pic.
    i.e. 1 500 5 So this translates into Stepper 1, 500 steps at 5ms interval
    , 2 5 1 This translates into Stepper 2, 5 steps, 1ms interval

    My current code....


    Code ( (Unknown Language)):
    1.   LIST P=16F628, R=DEC    ; Use the PIC16F628 and decimal system
    2.  
    3.         #include "P16F628.INC"  ; Include header file
    4.  
    5.         __config  _INTRC_OSC_NOCLKOUT & _LVP_OFF & _WDT_OFF & _PWRTE_ON & _BODEN_ON
    6.  
    7.         CBLOCK 0x20             ; Declare variable addresses starting at 0x20
    8.         count1
    9.          
    10.           dataL
    11.           Check_Temp
    12.         ENDC
    13.        
    14.         #DEFINE STEPPER1 PORTB, 0
    15.         #DEFINE STEPPER2 PORTB, 3
    16.         #DEFINE STEPPER3 PORTB, 4
    17.  
    18.         ORG    0x000            ; Program starts at 0x000
    19. ;
    20. ; --------------------------------
    21. ; SET ANALOG/DIGITAL INPUTS PORT A
    22. ; --------------------------------
    23. ;
    24.     movlw 7
    25.     movwf CMCON        ; CMCON=7 set comperators off        
    26. ;
    27. ; ----------------
    28. ; INITIALIZE PORTS
    29. ; ----------------
    30. ;
    31.     movlw b'00000000'       ; set up portA
    32.         movwf PORTA    
    33.        
    34.         movlw b'00000100'    ; RB2(TX)=1 others are 0
    35.         movwf PORTB
    36.  
    37.         bsf STATUS,RP0          ; RAM PAGE 1
    38.  
    39.         movlw 0xFF
    40.         movwf TRISA        ; portA all pins input
    41.  
    42.         movlw b'11100010'    ; RB7-RB5 and RB1(RX)=input, others output
    43.         movwf TRISB
    44.  
    45. ; ------------------------------------
    46. ; SET BAUD RATE TO COMMUNICATE WITH PC
    47. ; ------------------------------------
    48. ; Boot Baud Rate = 9600, No Parity, 1 Stop Bit
    49. ;
    50.         movlw 0x19              ; 0x19=9600 bps (0x0C=19200 bps)
    51.         movwf SPBRG
    52.         movlw b'00100100'       ; brgh = high (2)
    53.         movwf TXSTA             ; enable Async Transmission, set brgh
    54.  
    55.         bcf STATUS,RP0          ; RAM PAGE 0
    56.  
    57.         movlw b'10010000'       ; enable Async Reception
    58.         movwf RCSTA
    59. ;
    60. ; ------------------------------------
    61. ; PROVIDE A SETTLING TIME FOR START UP
    62. ; ------------------------------------
    63. ;
    64.         clrf dataL
    65. settle  decfsz dataL,F
    66.         goto settle
    67.  
    68.         movf RCREG,W
    69.         movf RCREG,W
    70.         movf RCREG,W            ; flush receive buffer
    71. ;
    72. ; ---------
    73. ; MAIN LOOP
    74. ; ---------
    75. ;
    76.     call message            ; send intro message
    77. loop    call receive            ; wait for a char
    78.     call Check_Input               ; Check to see what type of input it is
    79.         goto loop
    80. ;
    81. ; -------------------------------------------
    82. ; RECEIVE CHARACTER FROM RS232 AND STORE IN W
    83. ; -------------------------------------------
    84. ; This routine does not return until a character is received.
    85. ;
    86. receive btfss PIR1,RCIF         ; (5) check for received data
    87.         goto receive
    88.  
    89.         movf RCREG,W            ; save received data in W
    90.         return
    91. ;
    92. ; -------------------------------------------------------------
    93. ; SEND CHARACTER IN W VIA RS232 AND WAIT UNTIL FINISHED SENDING
    94. ; -------------------------------------------------------------
    95. ;
    96.  
    97. send    movwf TXREG             ; send data in W
    98.  
    99. TransWt bsf STATUS,RP0        ; RAM PAGE 1
    100. WtHere  btfss TXSTA,TRMT        ; (1) transmission is complete if hi
    101.         goto WtHere
    102.  
    103.         bcf STATUS,RP0          ; RAM PAGE 0
    104.          return
    105.      
    106. Check_Input movwf Check_Temp ; Put the character received into memory
    107.    
    108.     MOVLW    0x31                        ; Is it the number 1? If so call Step1 to drive stepper motor 1 one nudge
    109.     SUBWF    Check_Temp, w
    110.     BTFSC    STATUS, Z                ; Does it match?
    111.     CALL step1            ; Yes
    112.  
    113.     MOVLW    0x32                        ; Is it the number 2? If so call step2 to drive stepper motor 2 one nudge
    114.     SUBWF    Check_Temp, w
    115.     BTFSC    STATUS, Z                ; Does it match?
    116.     CALL step2            ; Yes
    117.    
    118.     MOVLW    0x33                        ; Is it the number 3? If so call step3 to drive stepper motor 3 one nudge
    119.     SUBWF    Check_Temp, w
    120.     BTFSC    STATUS, Z                ; Does it match?
    121.     CALL step2            ; Yes
    122.    
    123.    
    124.     MOVLW    0x3F                        ; Is it a question mark? If so call intro message
    125.     SUBWF    Check_Temp, w
    126.     BTFSC    STATUS, Z                ; Does it match?
    127.     CALL message            ; Yes
    128.    
    129.            
    130.     return
    131.  
    132. ; --------------------------------
    133. ; STEPPER MOTOR STEP CONTROL - ONE (RB0)
    134. ;---------------------------------
    135. step1 BSF STEPPER1 ; STEP the Stepper 1 motor
    136. call Delay ; Call Delay so there is enough time for the stepper motor controller to see a high
    137. BCF STEPPER1 ; Turn off the output ready for the next command.
    138. Call Delay
    139. return
    140.  
    141. step2 BSF STEPPER2 ; STEP the Stepper 2 motor
    142. call Delay ; Call Delay so there is enough time for the stepper motor controller to see a high
    143. BCF STEPPER2 ; Turn off the output ready for the next command.
    144. call Delay
    145. return
    146.  
    147. step3 BSF STEPPER3 ; STEP the Stepper 2 motor
    148. call Delay ; Call Delay so there is enough time for the stepper motor controller to see a high
    149. BCF STEPPER3 ; Turn off the output ready for the next command.
    150. call Delay ; Call Delay so there is enough LOW period before going to the next cycle.
    151. return
    152.  
    153.  
    154. ;
    155. ; -------
    156. ; MESSAGE
    157. ; -------
    158. ;
    159. message movlw     'T'        
    160.     call    send
    161.     movlw     'i'
    162.     call    send
    163.     movlw     'm'
    164.     call    send
    165.     movlw     'e'
    166.     call    send
    167.     movlw     ' '
    168.     call    send
    169.     movlw     'L'
    170.     call    send
    171.     movlw     'a'
    172.     call    send
    173.     movlw     'p'
    174.     call    send
    175.     movlw     's'
    176.     call    send
    177.     movlw     'e'
    178.     call    send
    179.     movlw     ' '
    180.     call    send
    181.     movlw     'C'
    182.     call    send
    183.     movlw     'o'
    184.     call    send
    185.     movlw     'n'
    186.     call    send
    187.     movlw     't'
    188.     call    send
    189.     movlw     'r'
    190.     call    send
    191.     movlw     'o'
    192.     call    send
    193.     movlw     'l'
    194.     call    send
    195.     movlw     'l'
    196.     call    send
    197.     movlw     'e'
    198.     call    send
    199.     movlw     'r'
    200.     call    send
    201.    
    202.     movlw     0x0D    ; CR
    203.     call    send
    204.     movlw     0x0A    ; LF
    205.     call    send
    206.     return
    207.  
    208. ; ====================== DELAY ROUTINES ===================================
    209.  
    210. Delay
    211.     movlw    .1
    212.     movwf    count1
    213.  
    214. dloop1    decfsz    count1,f
    215.     goto    dloop1
    216.     goto    $+1
    217.     retlw    0x00
    218.  
    219.         END
     
    Last edited: Jul 28, 2011
  2. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    I prefer to use interrupts to receive serial data so that I can do other things at the same time and not miss data.
    Here's something I wrote, it also has a buffer for the incoming data so the whole input string can be analysed in one go, eg. your 1 500 5.
    It's got some BCD to binary and the reverse which will come in handy too. It's a bit lacking in comments but should give you some ideas.
    http://www.marksphotos.info/fg/
     
  3. bluebrakes

    Thread Starter Active Member

    Oct 17, 2009
    245
    7
    thanks mark.

    not really sure if i really understand what is going on there with your code. Totally agree with you that getting rid the existing method and waiting for a complete instruction string.

    I wondering if the best way might be for it.....

    It waits for a sync character i.e. # This takes it to a routine where the next character (being a number) selects the stepper motor, waits for a space character then......

    It then goes to the next routine which calculates the number of steps before finally going onto the delay selection routine.
     
  4. russ_hensel

    Well-Known Member

    Jan 11, 2009
    818
    47
  5. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    This is the buffer fill section.
    The buffer is set up in the cblock with:
    usartrxbuf:10 ;number after colon is size of buffer

    Code ( (Unknown Language)):
    1.  
    2.     movlw usartrxbuf           ;location of start of buffer
    3.     addwf usartrxcount, W      ;received byte number
    4.     movwf FSR
    5.     movf RCREG, W
    6.     movwf INDF
    7.     incf usartrxcount, F
    8.     sublw 0x0D                 ;check for CR or LF character
    9.     btfsc STATUS, Z
    10.     incf usartdone, F          ;to tell main routine that
    11.     sublw 0x03                 ;the string has been received
    12.     btfsc STATUS, Z
    13.     incf usartdone, F    
    You could work backwards, putting the number of steps and delay into 2 variables, then calling a choice of 3 functions. It saves repeating code.
     
    Last edited: Jul 28, 2011
  6. bluebrakes

    Thread Starter Active Member

    Oct 17, 2009
    245
    7
    can anyone see what's wrong here?

    so the number in the W registry is placed into Check_Temp and it's a character recieved from serial.

    Code ( (Unknown Language)):
    1. runstepper movwf Check_Temp ; Put the character received into memory
    2.     xorlw 0x00 ; Is it Zero?
    3.     btfsc STATUS, Z
    4.     goto main ; if it is zero goto main
    5.    
    6.     BSF STEPPER2 ; pulse stepper high
    7.     call Delay ; call 500us second delay
    8.     BCF STEPPER2
    9.     call Delay
    10.    
    11.     decf Check_Temp, f ; decrease the number until it reaches zero
    12. goto runstepper
     
  7. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,395
    1,607
    How does the decremented value of Check_Temp get back into w for the 2nd pass?

    (Hint: it doesn't)

    Unless your Delay function doesn't change w you need to put it back.
     
Loading...