UART pic to pic in assembly

Discussion in 'Embedded Systems and Microcontrollers' started by ozirock, Mar 15, 2011.

  1. ozirock

    Thread Starter Member

    Jan 13, 2011
    44
    0
    Hi,

    I'm having trouble communicating between two pics, I'm trying to use UART like in the example here.

    On PIC 1 I am trying to send data to PIC2 and I use the following routine:

    Code ( (Unknown Language)):
    1. send        movwf   TXREG            ; send data in W
    2.  
    3. TransWt     bsf     STATUS,RP0       ; RAM PAGE 1
    4. WtHere      btfss   TXSTA,TRMT       ; (1) transmission is complete if hi
    5.             goto    WtHere
    6.  
    7.             bcf     STATUS,RP0       ; RAM PAGE 0
    8.             return
    I call this using:

    Code ( (Unknown Language)):
    1. Signal_1    movlw   '1'
    2.             call    send
    3.         GOTO    Pulse_Check
    Then on PIC 2 I try to receive it using the following code:

    Code ( (Unknown Language)):
    1. receive     btfss   PIR1,RCIF        ; (5) check for received data
    2.             goto    receive
    3.  
    4.             movf    RCREG,W          ; save received data in W
    5.             return
    which is called using:

    Code ( (Unknown Language)):
    1. Pulse_Check     call    receive
    2.         MOVWF   IN1_S
    This is all pretty much from the page with the example so I presume there is no issue there, I think my problem might be when I'm setting the options for the PIC, this is done as follows:

    Code ( (Unknown Language)):
    1. RESET       MOVLW   B'00000111' ;Disable Comparator module's
    2.         MOVWF   CMCON
    3.         ;
    4.        
    5.         movlw   b'00000000'     ; set up portA
    6.         movwf   PORTA
    7.    
    8.         movlw   b'00000100'     ; RB2(TX)=1 others are 0
    9.             movwf   PORTB
    10.        
    11.         BSF STATUS,RP0  ;Switch to register bank 1
    12.                     ;Disable pull-ups
    13.                     ;INT on rising edge
    14.                     ;TMR0 to CLKOUT
    15.                     ;TMR0 Incr low2high trans.
    16.                     ;Prescaler assign to Timer0
    17.                     ;Prescaler rate is 1:256                                               
    18.         MOVLW   B'11010111' ;Set PIC options (See datasheet).
    19.         MOVWF   OPTION_REG  ;Write the OPTION register.
    20.                     ;
    21.         ;---Configure peripheral interrupts
    22.         MOVLW   B'00100000'     ;Disable all peripheral interrupts except receiver
    23.         MOVWF   PIE1        ;Peripheral interrupt enable/disable
    24.        
    25.         ;---Configure general interrupts
    26.         MOVLW   B'01000000'     ;Disable all interrupts except peripheral
    27.         MOVWF   INTCON      ;Interrupt control register
    28.        
    29.         movlw   0xFF
    30.             movwf   PR2             ;PWM setting
    31.        
    32.         MOVLW   B'00000010'
    33.         MOVWF   TRISB       ;all RB ports are outputs
    34.                    
    35.         MOVLW   B'00011100' ;RA2:RA4 ports are inputs, all others are outputs
    36.         MOVWF   TRISA
    37.        
    38.         ; ------------------------------------
    39.         ; SET BAUD RATE TO COMMUNICATE WITH PC
    40.         ; ------------------------------------
    41.         ; Boot Baud Rate = 9600, No Parity, 1 Stop Bit
    42.         ;
    43.         movlw   0x19            ; 0x19=9600 bps (0x0C=19200 bps)
    44.         movwf   SPBRG
    45.         movlw   b'00100100'     ; brgh = high (2)
    46.             movwf   TXSTA           ; enable Async Transmission, set brgh
    47.        
    48.         BCF STATUS,RP0  ;Switch Back to reg. Bank 0
    49.        
    50.         movlw   b'10010000'     ; enable Async Reception
    51.             movwf   RCSTA
    52.        
    53.         movlw   B'00000100'        
    54.         movwf   T2CON           ;timer 2 on, no prescaler or postscaler
    55.         movlw   B'00001111'
    56.         movwf   CCP1CON
    57.        
    58.         MOVF     CCP1CON,W  ;set CCP1 as PWM
    59.         ANDLW    0xF0
    60.         IORLW    0x0C
    61.             MOVWF    CCP1CON
    62.        
    63.         MOVLW    0x7E       ;126        ;set highest PWM value
    64.         BANKSEL  PR2        ;over this (127) is permanently on
    65.         MOVWF    PR2
    66.         BANKSEL  TMR2
    67.        
    68.         MOVF     T2CON,W    ;set prescaler to 16
    69.         ANDLW    0xF8       ;PWM at 2500HZ
    70.         IORLW    0x02
    71.         MOVWF    T2CON
    72.    
    73.         MOVF     T2CON,W    ;set postscaler to 1
    74.         ANDLW    0x07
    75.         IORLW    0x00
    76.             MOVWF    T2CON
    77.        
    78.         CLRF    CCPR1L
    79.             CLRF    PORTB
    80.             BSF     T2CON, TMR2ON
    81.  
    82. ;
    83. ; ------------------------------------
    84. ; PROVIDE A SETTLING TIME FOR START UP
    85. ; ------------------------------------
    86. ;
    87.             clrf    dataL
    88. settle      decfsz  dataL,F
    89.             goto    settle
    90.  
    91.             movf    RCREG,W
    92.             movf    RCREG,W
    93.             movf    RCREG,W            ; flush receive buffer
    I've seen the interrupts discussed on different pages as being a problem and so I tried to set them up myself but I'm not sure if it's done right. Any guidance would be much appreciated.
     
  2. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    If you are not using an interrupt then they should be disabled. The interrupt flags still get set so you can check them.
    A serial port on your PC (or a USB-RS232 converter) combined with a ST232 chip or similar is very helpful in debugging because it can be hard to know where the problem lies.

    Maybe something in here will help:
    http://forum.allaboutcircuits.com/showthread.php?t=40875
     
  3. ozirock

    Thread Starter Member

    Jan 13, 2011
    44
    0
    I'll definitely look into getting a few st232 or max232 chips as I keep seeing them popping up all over the place for different applications so it would be good to learn how to interface with them.

    I'll disable the interrupts again so and work from there
     
  4. be80be

    Senior Member

    Jul 5, 2008
    431
    57
    You can used this if you don't have a max232
     
  5. mjhilger

    Member

    Feb 28, 2011
    119
    16
    I notice in your send routine, you have no timeout. Is your send routine always returning ? If so you know that your output should be sent. Toggle some unused pin high when you enter the send routine and low on exit. You should be able to view this pin and the serial output pin with a scope to see if it is sending out. Quick check this way (I have read many serial messages using this method in the past). Of course connection to PC or some output device is easier long term diag.
     
Loading...