problem with USART Rs-232 PIC programming

Discussion in 'Embedded Systems and Microcontrollers' started by walid el masry, Dec 4, 2009.

  1. walid el masry

    Thread Starter Active Member

    Mar 31, 2009
    132
    0
    hi every body

    these days iam learning interfacing PIC using it's USART module with another PIC or either with PC but 1st i do search and find that it is commonly used in Asynchronous Mode and that was discussed in details in Microchip DeviceDoc on

    ww1.microchip.com/downloads/en/DeviceDoc/usart.pdf

    which discusses using the USART module in microchip PICs in Asynchronous Mode so after reading i thought 8 bit mode will be a good start so i wrote a program for 2 pic16f876a 1st for a transmitter and the other for a receiver
    the transmitter will test the statue of switches on PORTB and every switch press will send a different value but as a start the code will test RP0 only and send 0XFF in case of it was High and the receiver will get that value and mov it on PORTB and blink the LEDs

    the code uses the same baud rate discussed in the DeviceDoc and i ensured that i did like the PDF and datasheet said specially the datasheet in page 109 the paragraph named

    "ADDRESSABLE UNIVERSAL SYNCHRONOUS ASYNCHRONOUS RECEIVER TRANSMITTER (USART)"

    it said that

    "Bit SPEN (RCSTA<7>) and bits TRISC<7:6> have to be set in order to configure pins RC6/TX/CK and RC7/RX/DT as the Universal Synchronous Asynchronous Receiver Transmitter."

    and that what i did already in the code but unfortunately the circuit doesn't work when simulate it in Proteus 7 Professional

    here is the code for the transmitter

    Code ( (Unknown Language)):
    1.  
    2.     LIST P=16F876A
    3.     #INCLUDE<P16F876A.INC>
    4.     __CONFIG _CP_OFF&_LVP_OFF&_BODEN_OFF&_PWRTE_ON&_WDT_OFF&_XT_OSC
    5. TXDataByte EQU 0X20
    6. RXDataByte EQU 0X21
    7.     ORG 0X00
    8. INITIATE
    9.     BSF STATUS,RP0;-------I'm in Bank(1)
    10.     MOVLW 0XFF;-----------Initiate Ports
    11.     MOVWF TRISB
    12.     MOVLW B'11000000'
    13.     MOVWF TRISC
    14.     MOVLW D'25';----------Initiate Baude Rate Register Generator
    15.     MOVWF SPBRG
    16.     BSF TXSTA,2;-------High Baud Rate
    17.     MOVLW B'00100100';----Initiate TXSTA Register
    18.     MOVWF TXSTA
    19.     BCF STATUS,RP0;-------I'm in Bank(0)
    20.     BSF RCSTA,7
    21.     MOVLW B'10010000';----Initiate RCSTA Register
    22.     MOVWF RCSTA
    23.     MOVLW 0XFF
    24.     MOVWF TXDataByte
    25. MAIN
    26.     BTFSS PORTB,0
    27.     GOTO MAIN;NO
    28. ;WAIT_RELESE;YES
    29.     ;BTFSC PORTB,0
    30.     ;GOTO WAIT_RELESE;NO
    31. WaitTX;YES
    32.     BTFSS PIR1,TXIF
    33.     GOTO WaitTX;NO
    34.     MOVF TXDataByte;YES
    35.     MOVWF TXREG
    36.     BCF PIR1,TXIF
    37.     GOTO MAIN
    38.     END
    39.  
    and here ther code for the receiver

    Code ( (Unknown Language)):
    1.  
    2.     LIST P=16F876A
    3.     #INCLUDE<P16F876A.INC>
    4.     __CONFIG _CP_OFF&_LVP_OFF&_BODEN_OFF&_PWRTE_ON&_WDT_OFF&_XT_OSC
    5. TXDataByte EQU 0X20
    6. RXDataByte EQU 0X21
    7.     ORG 0X00
    8. INITIATE
    9.     BSF STATUS,RP0;-------I'm in Bank(1)
    10.     MOVLW 0X00;-----------Initiate Ports
    11.     MOVWF TRISB
    12.     MOVLW B'11000000'
    13.     MOVWF TRISC
    14.     MOVLW D'25';----------Initiate Baude Rate Register Generator
    15.     MOVWF SPBRG
    16.     BSF TXSTA,2;-------High Baud Rate
    17.     MOVLW B'00100100';----Initiate TXSTA Register
    18.     MOVWF TXSTA
    19.     BCF STATUS,RP0;-------I'm in Bank(0)
    20.     BSF RCSTA,7
    21.     MOVLW B'10010000';----Initiate RCSTA Register
    22.     MOVWF RCSTA
    23.     MOVLW 0X00
    24.     MOVWF PORTB
    25. MAIN
    26. WaitRX
    27.     BTFSS PIR1,RCIF
    28.     GOTO WaitRX;NO
    29.     MOVF RCREG;YES
    30.     MOVWF PORTB
    31.     BCF PIR1,RCIF
    32.     GOTO MAIN
    33.     END
    34.  
    and that is the circuit i build on Proteus

    [​IMG]

    and at the end the project files

    http://www.4shared.com/file/165679401/3eb67abe/USART_RS232.html


    thnx 4 ur time

     
  2. walid el masry

    Thread Starter Active Member

    Mar 31, 2009
    132
    0
    52 views with no replay of course there is some thing wrong and i got it i missed using the MAX232 in my circuit so i modified the circuit and the programs in tx and rx so and the new code of the tx just take the value of "TXDataByte" and semd it all the time and the rx get it and mov it to "PORTB" which blink the "LEDs" but after all of that it not working

    see that's the modified project

    code for tx

    Code ( (Unknown Language)):
    1.  
    2.     LIST P=16F876A
    3.     #INCLUDE<P16F876A.INC>
    4.     __CONFIG _CP_OFF&_LVP_OFF&_BODEN_OFF&_PWRTE_ON&_WDT_OFF&_XT_OSC
    5. TXDataByte EQU 0X20
    6.     ORG 0X00
    7. INITIATE
    8. ;---------------------;
    9.     BANKSEL SPBRG        ;|
    10.     MOVLW D'25'            ;| initiate SPBRG baud rate generator register - 9600    
    11.     MOVWF SPBRG            ;|    
    12. ;---------------------;
    13.     BANKSEL TRISC        ;|
    14.     MOVLW B'11000000'    ;| initiate TRISC behavior of PORTC
    15.     MOVWF TRISC            ;|
    16. ;---------------------;
    17.     BANKSEL TXSTA        ;|
    18.     MOVLW B'00100100'    ;| initiate TXSTA TRANSMIT STATUS AND CONTROL REGISTER
    19.     MOVWF TXSTA            ;|
    20. ;---------------------;
    21.     BANKSEL RCSTA        ;|
    22.     MOVLW B'10010000'    ;| initiate RCSTA RECEIVE STATUS AND CONTROL REGISTER
    23.     MOVWF RCSTA            ;|
    24. ;---------------------;
    25.     BANKSEL TXDataByte    ;|
    26.     MOVLW 0XFF            ;| initiate TXDataByte byte to be sent
    27.     MOVWF TXDataByte    ;|
    28. ;---------------------;
    29. MAIN
    30.     BANKSEL PIR1
    31.     BTFSS PIR1,TXIF
    32.     GOTO MAIN;NO
    33.     BANKSEL TXDataByte;YES
    34.     MOVF TXDataByte
    35.     BANKSEL TXREG
    36.     MOVWF TXREG
    37.     GOTO MAIN
    38.     END
    39.  
    and that's the code for rx

    Code ( (Unknown Language)):
    1.  
    2.     LIST P=16F876A
    3.     #INCLUDE<P16F876A.INC>
    4.     __CONFIG _CP_OFF&_LVP_OFF&_BODEN_OFF&_PWRTE_ON&_WDT_OFF&_XT_OSC
    5. RXDataByte EQU 0X20
    6.     ORG 0X00
    7. INITIATE
    8. ;---------------------;
    9.     BANKSEL SPBRG        ;|
    10.     MOVLW D'25'            ;| initiate SPBRG baud rate generator register - 9600    
    11.     MOVWF SPBRG            ;|    
    12. ;---------------------;
    13.     BANKSEL TRISC        ;|
    14.     MOVLW B'11000000'    ;| initiate TRISC behavior of PORTC
    15.     MOVWF TRISC            ;|
    16. ;---------------------;
    17.     BANKSEL TXSTA        ;|
    18.     MOVLW B'00100100'    ;| initiate TXSTA TRANSMIT STATUS AND CONTROL REGISTER
    19.     MOVWF TXSTA            ;|
    20. ;---------------------;
    21.     BANKSEL RCSTA        ;|
    22.     MOVLW B'10010000'    ;| initiate RCSTA RECEIVE STATUS AND CONTROL REGISTER
    23.     MOVWF RCSTA            ;|
    24. ;---------------------;
    25.     BANKSEL TRISB        ;|
    26.     MOVLW B'00000000'    ;| initiate TRISB behavior of PORTC
    27.     MOVWF TRISB            ;|
    28. ;---------------------;
    29.     BANKSEL PORTB        ;|
    30.     MOVLW B'00000000'    ;| initiate PORTB value of PORTB
    31.     MOVWF PORTB            ;|
    32. ;---------------------;
    33. MAIN
    34.     BANKSEL PIR1
    35.     BTFSS PIR1,RCIF
    36.     GOTO MAIN;NO
    37.     BANKSEL RCREG;YES
    38.     MOVF RCREG
    39.     BANKSEL PORTB
    40.     MOVWF PORTB
    41.     GOTO MAIN
    42.     END
    43.  
    and that's the new circuit

    [​IMG]

    and that's the project files

    http://www.4shared.com/file/166405688/8e14442/modified_project.html

     
  3. BMorse

    Senior Member

    Sep 26, 2009
    2,675
    234
    for one, you have the PIC set to use an external oscillator.... and you do not have any on your circuit!!

    And sending data from one Micro to another, you do not need the Mx232 IC's, since you do not need to translate it to RS232.....

    You have defined your baud rate at 9600, is that based on what oscillator speed???
     
  4. walid el masry

    Thread Starter Active Member

    Mar 31, 2009
    132
    0



    you don't have to put it cause it is already embedded in the simulation see the properties of the microcontroller U1 & U2 and you will find it
     
  5. BMorse

    Senior Member

    Sep 26, 2009
    2,675
    234
    here is the code I used to communicate with an RS232 Level Vacuum Fluorescent Display, including Setup and TX/RX routines........
    this was used with a 20Mhz Crystal..... (HS_OSC)

    Code ( (Unknown Language)):
    1. ;====================================================================================
    2. UART_Setup    ;Initialize UART to 9600,8,N,1
    3.         movlw    0x81        ;0x81 = 9600 Baud / 20Mhz xtal / BRGH=1/ BRG16=0
    4.         movwf    SPBRG
    5.         movlw    b'00100100'    ;brgh = high
    6.         movwf    TXSTA        ;enable async transmit
    7.         bcf        STATUS,RP0    ;bank 0
    8.         movlw    b'10010000'    ;enable async receive
    9.         movwf    RCSTA
    10.         ;have delay after here to have a startup settle time
    11.         retlw    0
    12. ;====================================================================================
    13. ;Wait for a Char from RS232 - return in W
    14. Receive
    15.         btfss    PIR1,RCIF        ;wait for byte
    16.         goto     Receive
    17.         movf    RCREG,w            ;return received byte in w
    18.         retlw    0
    19. ;====================================================================================
    20. ;transmit a char to RS232
    21. Transmit
    22.         movwf    TXREG        ;transmit byte in w
    23.         bsf        STATUS,RP0    ;Bank 1
    24. send1
    25.         btfss    TXSTA,TRMT    ;is TX done???
    26.         goto     send1        ;no, check again
    27.         bcf        STATUS,RP0    ;Bank 0
    28.         retlw    0            ;ok, return back to calling function
    29. ;==========================================================================================
     
  6. walid el masry

    Thread Starter Active Member

    Mar 31, 2009
    132
    0
    i already test sim without max232 and didn't work


    aha thnx for the code i will see and replay you very soon
     
  7. walid el masry

    Thread Starter Active Member

    Mar 31, 2009
    132
    0
    i agree with you about the max232 but i thought it was the problem cause i did every thing :( and it seems iam not :)

    about your code that's my notes

    1) i know that "BRG16" isn't a register or bit but what did you mean with "BRG16=0"

    2) in UART_Setup you didn't goto bank1 so that you return from it in "bcf STATUS,RP0" and i wonder how did it work with you and the "TXSTA" needs to goto ban1 and also "
    SPBRG" :)

    3) what did you mean with "retlw 0" i don't understand it's function in your code & i think you don't need it or i miss some thing

    4) the only critical difference between me and you is in the tx code cause you tested "TXSTA,TRMT" but for me was "PIR1,TXIF"


    any way i trying it again now
     
  8. walid el masry

    Thread Starter Active Member

    Mar 31, 2009
    132
    0
    you forget to initiate TRISC by setting the bin 6,7 or you didn't include that part of code
     
  9. BMorse

    Senior Member

    Sep 26, 2009
    2,675
    234

    Sorry, I forgot to mention this was code for Pic16f887 uC...


    1. BRG16 is BIT#3 of the BAUDCTRL register of the F887
    2. Before I called UART_Setup, I had switched to BANK 1.
    3. retlw 0 = just returns you back to the calling function.
    4. Yes, you can check this bit to see if the TX buffer is done transmitting.
     
  10. walid el masry

    Thread Starter Active Member

    Mar 31, 2009
    132
    0
    finally after modification it works, see that the new one

    tx code

    Code ( (Unknown Language)):
    1.  
    2.     LIST P=16F876A
    3.     #INCLUDE<P16F876A.INC>
    4.     __CONFIG _CP_OFF&_LVP_OFF&_BODEN_OFF&_PWRTE_ON&_WDT_OFF&_XT_OSC
    5. TXDataByte EQU 0X20
    6.     ORG 0X00
    7. INITIATE
    8. ;---------------------;
    9.     BSF STATUS,RP0
    10.     MOVLW D'25'            ;| initiate SPBRG baud rate generator register - 9600    
    11.     MOVWF SPBRG            ;|    
    12. ;---------------------;
    13.     MOVLW B'11000000'    ;| initiate TRISC behavior of PORTC
    14.     MOVWF TRISC            ;|
    15. ;---------------------;
    16.     MOVLW B'00100100'    ;| initiate TXSTA TRANSMIT STATUS AND CONTROL REGISTER
    17.     MOVWF TXSTA            ;|
    18.     BSF STATUS,RP0
    19. ;---------------------;
    20.     BCF STATUS,RP0
    21.     MOVLW B'10010000'    ;| initiate RCSTA RECEIVE STATUS AND CONTROL REGISTER
    22.     MOVWF RCSTA            ;|
    23. ;---------------------;
    24.     MOVLW 0XFF            ;| initiate TXDataByte byte to be sent
    25.     MOVWF TXDataByte    ;|
    26. ;---------------------;
    27. MAIN
    28.     BTFSS PIR1,TXIF
    29.     GOTO MAIN;NO
    30.     MOVF TXDataByte;YES
    31.     MOVWF TXREG
    32.     GOTO MAIN
    33.     END
    34.  
    rx code

    Code ( (Unknown Language)):
    1.  
    2.     LIST P=16F876A
    3.     #INCLUDE<P16F876A.INC>
    4.     __CONFIG _CP_OFF&_LVP_OFF&_BODEN_OFF&_PWRTE_ON&_WDT_OFF&_XT_OSC
    5. RXDataByte EQU 0X20
    6.     ORG 0X00
    7. INITIATE
    8. ;---------------------;
    9.     BSF STATUS,RP0
    10.     MOVLW D'25'            ;| initiate SPBRG baud rate generator register - 9600    
    11.     MOVWF SPBRG            ;|    
    12. ;---------------------;
    13.     MOVLW B'11000000'    ;| initiate TRISC behavior of PORTC
    14.     MOVWF TRISC            ;|
    15. ;---------------------;
    16.     MOVLW B'00000000'    ;| initiate TRISB behavior of PORTC
    17.     MOVWF TRISB            ;|
    18. ;---------------------;
    19.     MOVLW B'00100100'    ;| initiate TXSTA TRANSMIT STATUS AND CONTROL REGISTER
    20.     MOVWF TXSTA            ;|
    21. ;---------------------;
    22.     BCF STATUS,RP0
    23.     MOVLW B'10010000'    ;| initiate RCSTA RECEIVE STATUS AND CONTROL REGISTER
    24.     MOVWF RCSTA            ;|
    25. ;---------------------;
    26.     MOVLW B'00000000'    ;| initiate PORTB value of PORTB
    27.     MOVWF PORTB            ;|
    28. ;---------------------;
    29. MAIN
    30.     BTFSS PIR1,RCIF
    31.     GOTO MAIN;NO
    32.     MOVF RCREG,W;YES
    33.     MOVWF PORTB
    34.     GOTO MAIN
    35.     END
    36.  
    and that's is the new circuit

    [​IMG]

    but i note that when i'm working using crystal 20 MHz and after modifying the configuration word and the "SPBRG" value the simulation gives me "Simulation is not running in real time due to excessive CPU load." but i think it is matter of real world and sim world but what i can't get it that why there are two ways to test the chip if not busy before sending any thing and i mean

    "TXSTA,TRMT" & "PIR1,TXIF" what is the point :)

    and about the "retlw 0" i guess "return" will work also :)

    any way thnx 4 help
    i appreciate it :p
     
  11. walid el masry

    Thread Starter Active Member

    Mar 31, 2009
    132
    0
    another question, i plan to interface pic to pc using rs-232 in the future so i have to write a code using any language and i guess vb.net will work for me but can i interface Proteus to the real com port in the pc or i have to build the real circuit ?
     
  12. BMorse

    Senior Member

    Sep 26, 2009
    2,675
    234

    I have never used Proteus before So I can not answer that for you.... but when you do interface to PC, make sure you use the MAX232 IC for level translation.....
     
  13. walid el masry

    Thread Starter Active Member

    Mar 31, 2009
    132
    0
    yes yes i know about the MAX232 and thnx again
     
Loading...