problem with USART Rs-232 PIC programming

Thread Starter

walid el masry

Joined Mar 31, 2009
133
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

Rich (BB code):
    LIST P=16F876A
    #INCLUDE<P16F876A.INC>
    __CONFIG _CP_OFF&_LVP_OFF&_BODEN_OFF&_PWRTE_ON&_WDT_OFF&_XT_OSC
TXDataByte EQU 0X20
RXDataByte EQU 0X21
    ORG 0X00
INITIATE
    BSF STATUS,RP0;-------I'm in Bank(1)
    MOVLW 0XFF;-----------Initiate Ports
    MOVWF TRISB
    MOVLW B'11000000'
    MOVWF TRISC
    MOVLW D'25';----------Initiate Baude Rate Register Generator
    MOVWF SPBRG
    BSF TXSTA,2;-------High Baud Rate
    MOVLW B'00100100';----Initiate TXSTA Register
    MOVWF TXSTA
    BCF STATUS,RP0;-------I'm in Bank(0)
    BSF RCSTA,7
    MOVLW B'10010000';----Initiate RCSTA Register
    MOVWF RCSTA
    MOVLW 0XFF
    MOVWF TXDataByte
MAIN
    BTFSS PORTB,0
    GOTO MAIN;NO
;WAIT_RELESE;YES
    ;BTFSC PORTB,0
    ;GOTO WAIT_RELESE;NO
WaitTX;YES
    BTFSS PIR1,TXIF
    GOTO WaitTX;NO
    MOVF TXDataByte;YES
    MOVWF TXREG
    BCF PIR1,TXIF
    GOTO MAIN
    END
and here ther code for the receiver

Rich (BB code):
    LIST P=16F876A
    #INCLUDE<P16F876A.INC>
    __CONFIG _CP_OFF&_LVP_OFF&_BODEN_OFF&_PWRTE_ON&_WDT_OFF&_XT_OSC
TXDataByte EQU 0X20
RXDataByte EQU 0X21
    ORG 0X00
INITIATE
    BSF STATUS,RP0;-------I'm in Bank(1)
    MOVLW 0X00;-----------Initiate Ports
    MOVWF TRISB
    MOVLW B'11000000'
    MOVWF TRISC
    MOVLW D'25';----------Initiate Baude Rate Register Generator
    MOVWF SPBRG
    BSF TXSTA,2;-------High Baud Rate
    MOVLW B'00100100';----Initiate TXSTA Register
    MOVWF TXSTA
    BCF STATUS,RP0;-------I'm in Bank(0)
    BSF RCSTA,7
    MOVLW B'10010000';----Initiate RCSTA Register
    MOVWF RCSTA
    MOVLW 0X00
    MOVWF PORTB 
MAIN
WaitRX 
    BTFSS PIR1,RCIF
    GOTO WaitRX;NO
    MOVF RCREG;YES
    MOVWF PORTB
    BCF PIR1,RCIF
    GOTO MAIN
    END
and that is the circuit i build on Proteus



and at the end the project files

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


thnx 4 ur time

 

Thread Starter

walid el masry

Joined Mar 31, 2009
133
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

Rich (BB code):
    LIST P=16F876A
    #INCLUDE<P16F876A.INC>
    __CONFIG _CP_OFF&_LVP_OFF&_BODEN_OFF&_PWRTE_ON&_WDT_OFF&_XT_OSC
TXDataByte EQU 0X20
    ORG 0X00
INITIATE
;---------------------;
    BANKSEL SPBRG        ;|
    MOVLW D'25'            ;| initiate SPBRG baud rate generator register - 9600    
    MOVWF SPBRG            ;|    
;---------------------;
    BANKSEL TRISC        ;|
    MOVLW B'11000000'    ;| initiate TRISC behavior of PORTC
    MOVWF TRISC            ;|
;---------------------;
    BANKSEL TXSTA        ;|
    MOVLW B'00100100'    ;| initiate TXSTA TRANSMIT STATUS AND CONTROL REGISTER
    MOVWF TXSTA            ;|
;---------------------;
    BANKSEL RCSTA        ;|
    MOVLW B'10010000'    ;| initiate RCSTA RECEIVE STATUS AND CONTROL REGISTER
    MOVWF RCSTA            ;|
;---------------------;
    BANKSEL TXDataByte    ;|
    MOVLW 0XFF            ;| initiate TXDataByte byte to be sent
    MOVWF TXDataByte    ;|
;---------------------;
MAIN
    BANKSEL PIR1
    BTFSS PIR1,TXIF
    GOTO MAIN;NO
    BANKSEL TXDataByte;YES
    MOVF TXDataByte
    BANKSEL TXREG
    MOVWF TXREG
    GOTO MAIN
    END
and that's the code for rx

Rich (BB code):
    LIST P=16F876A
    #INCLUDE<P16F876A.INC>
    __CONFIG _CP_OFF&_LVP_OFF&_BODEN_OFF&_PWRTE_ON&_WDT_OFF&_XT_OSC
RXDataByte EQU 0X20
    ORG 0X00
INITIATE
;---------------------;
    BANKSEL SPBRG        ;|
    MOVLW D'25'            ;| initiate SPBRG baud rate generator register - 9600    
    MOVWF SPBRG            ;|    
;---------------------;
    BANKSEL TRISC        ;|
    MOVLW B'11000000'    ;| initiate TRISC behavior of PORTC
    MOVWF TRISC            ;|
;---------------------;
    BANKSEL TXSTA        ;|
    MOVLW B'00100100'    ;| initiate TXSTA TRANSMIT STATUS AND CONTROL REGISTER
    MOVWF TXSTA            ;|
;---------------------;
    BANKSEL RCSTA        ;|
    MOVLW B'10010000'    ;| initiate RCSTA RECEIVE STATUS AND CONTROL REGISTER
    MOVWF RCSTA            ;|
;---------------------;
    BANKSEL TRISB        ;|
    MOVLW B'00000000'    ;| initiate TRISB behavior of PORTC
    MOVWF TRISB            ;|
;---------------------;
    BANKSEL PORTB        ;|
    MOVLW B'00000000'    ;| initiate PORTB value of PORTB
    MOVWF PORTB            ;|
;---------------------;
MAIN
    BANKSEL PIR1
    BTFSS PIR1,RCIF
    GOTO MAIN;NO
    BANKSEL RCREG;YES
    MOVF RCREG
    BANKSEL PORTB
    MOVWF PORTB
    GOTO MAIN
    END
and that's the new circuit



and that's the project files

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

 

BMorse

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

__CONFIG _CP_OFF&_LVP_OFF&_BODEN_OFF&_PWRTE_ON&_WDT_OFF&_XT_OSC
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???
 

Thread Starter

walid el masry

Joined Mar 31, 2009
133
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???

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
 

BMorse

Joined Sep 26, 2009
2,675
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)

Rich (BB code):
;====================================================================================
UART_Setup    ;Initialize UART to 9600,8,N,1
        movlw    0x81        ;0x81 = 9600 Baud / 20Mhz xtal / BRGH=1/ BRG16=0
        movwf    SPBRG
        movlw    b'00100100'    ;brgh = high
        movwf    TXSTA        ;enable async transmit
        bcf        STATUS,RP0    ;bank 0
        movlw    b'10010000'    ;enable async receive
        movwf    RCSTA
        ;have delay after here to have a startup settle time
        retlw    0
;====================================================================================
;Wait for a Char from RS232 - return in W
Receive
        btfss    PIR1,RCIF        ;wait for byte
        goto     Receive
        movf    RCREG,w            ;return received byte in w
        retlw    0
;====================================================================================
;transmit a char to RS232
Transmit
        movwf    TXREG        ;transmit byte in w
        bsf        STATUS,RP0    ;Bank 1
send1
        btfss    TXSTA,TRMT    ;is TX done???
        goto     send1        ;no, check again
        bcf        STATUS,RP0    ;Bank 0
        retlw    0            ;ok, return back to calling function
;==========================================================================================
 

Thread Starter

walid el masry

Joined Mar 31, 2009
133
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
 

BMorse

Joined Sep 26, 2009
2,675
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

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.
 

Thread Starter

walid el masry

Joined Mar 31, 2009
133
finally after modification it works, see that the new one

tx code

Rich (BB code):
    LIST P=16F876A
    #INCLUDE<P16F876A.INC>
    __CONFIG _CP_OFF&_LVP_OFF&_BODEN_OFF&_PWRTE_ON&_WDT_OFF&_XT_OSC
TXDataByte EQU 0X20
    ORG 0X00
INITIATE
;---------------------;
    BSF STATUS,RP0
    MOVLW D'25'            ;| initiate SPBRG baud rate generator register - 9600    
    MOVWF SPBRG            ;|    
;---------------------;
    MOVLW B'11000000'    ;| initiate TRISC behavior of PORTC
    MOVWF TRISC            ;|
;---------------------;
    MOVLW B'00100100'    ;| initiate TXSTA TRANSMIT STATUS AND CONTROL REGISTER
    MOVWF TXSTA            ;|
    BSF STATUS,RP0
;---------------------;
    BCF STATUS,RP0
    MOVLW B'10010000'    ;| initiate RCSTA RECEIVE STATUS AND CONTROL REGISTER
    MOVWF RCSTA            ;|
;---------------------;
    MOVLW 0XFF            ;| initiate TXDataByte byte to be sent
    MOVWF TXDataByte    ;|
;---------------------;
MAIN
    BTFSS PIR1,TXIF
    GOTO MAIN;NO
    MOVF TXDataByte;YES
    MOVWF TXREG
    GOTO MAIN
    END
rx code

Rich (BB code):
    LIST P=16F876A
    #INCLUDE<P16F876A.INC>
    __CONFIG _CP_OFF&_LVP_OFF&_BODEN_OFF&_PWRTE_ON&_WDT_OFF&_XT_OSC
RXDataByte EQU 0X20
    ORG 0X00
INITIATE
;---------------------;
    BSF STATUS,RP0
    MOVLW D'25'            ;| initiate SPBRG baud rate generator register - 9600    
    MOVWF SPBRG            ;|    
;---------------------;
    MOVLW B'11000000'    ;| initiate TRISC behavior of PORTC
    MOVWF TRISC            ;|
;---------------------;
    MOVLW B'00000000'    ;| initiate TRISB behavior of PORTC
    MOVWF TRISB            ;|
;---------------------;
    MOVLW B'00100100'    ;| initiate TXSTA TRANSMIT STATUS AND CONTROL REGISTER
    MOVWF TXSTA            ;|
;---------------------;
    BCF STATUS,RP0
    MOVLW B'10010000'    ;| initiate RCSTA RECEIVE STATUS AND CONTROL REGISTER
    MOVWF RCSTA            ;|
;---------------------;
    MOVLW B'00000000'    ;| initiate PORTB value of PORTB
    MOVWF PORTB            ;|
;---------------------;
MAIN
    BTFSS PIR1,RCIF
    GOTO MAIN;NO
    MOVF RCREG,W;YES
    MOVWF PORTB
    GOTO MAIN
    END
and that's is the new circuit



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
 

Thread Starter

walid el masry

Joined Mar 31, 2009
133
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 ?
 

BMorse

Joined Sep 26, 2009
2,675
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 ?

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.....
 
Top