RS232 comm


Closed Thread
Results 1 to 10 of 10

Thread: RS232 comm

  1. #1
    Join Date
    Sep 2009
    Location
    Columbus, Ohio
    Posts
    6

    Default RS232 comm

    I am somewhat new to PIC.
    Am trying to use PBP, and Labusb to Labx1 to send a char over nullmodem cable (db9).
    I send a "D" and get ascii chars 56 & 254 or various other chars depending on code. I have tried many,,, many dif combos but,,,, no good results.

    Code follows (sorry for all the comented out statements (shows things that I tried and didn't work)).

    Sending code:
    Define LOADER_USED 1


    ' RESET_ORG can be set to move the BASIC program out of the way
    ' of any boot loader running from location 0, such as the
    ' Microchip USB boot loader
    'Define RESET_ORG 800h



    ' Define LCD registers and bits
    Define LCD_DREG PORTD
    Define LCD_DBIT 4
    Define LCD_RSREG PORTE
    Define LCD_RSBIT 0
    Define LCD_EREG PORTE
    Define LCD_EBIT 1


    ADCON1 = 15 ' Set PORTA and PORTE to digital
    Low PORTE.2 ' LCD R/W line low (W)
    Pause 100 ' Wait for LCD to start up




    CLEAR ;read and write hardware USART
    B1 VAR BYTE ;initialize USART
    TRISC = %10111111 ;set TX (PORTC.6) to out, rest in
    SPBRG = 25 ;set baud rate to 2400
    RCSTA = %10010000 ;enable serial port and continuous receive
    TXSTA = %00100000 ;enable transmit and asynchronous mode
    'define hser_rcsta 90h
    'define hser_txsta 20h
    ' define hser_txsta 24h
    ' define hser_spbrg 9fh
    ' baudcon=8

    Define OSC 48 ' Core is running at 48MHz
    define hser_baud 2400
    Data_out var byte


    Startup:
    'b1 = 1
    Data_out = "B"

    Pause 500 ' Wait for LCD to startup

    Lcdout $fe, 1 ' Initialize and clear display

    LCDOUT "Pgm Starting"

    Pause 500 ' Wait for LCD visual
    ;echo received characters in infinite loop
    mainloop:
    Pause 500 ' Wait .5 second

    Lcdout $fe, $c0, "Sending ", data_out ' Move to line 2 and display
    ' Serout PORTC.6,0,[b1] not working is set to true mode
    ' txreg = b1
    hserout[data_out]
    ' serout portc.6,4,["D"]
    Pause 1000 ' Wait for LCD visual
    END

    Receiving code:

    Define OSC 4 ' Core is running at 4MHz
    Define LOADER_USED 1

    ' Define LCD registers and bits
    Define LCD_DREG PORTD
    Define LCD_DBIT 4
    Define LCD_RSREG PORTE
    Define LCD_RSBIT 0
    Define LCD_EREG PORTE
    Define LCD_EBIT 1
    ADCON1 = 7 ' Set PORTA and PORTE to digital
    Low PORTE.2 ' LCD R/W line low (W)
    OPTION_REG.7 = 0 ' Enable PORTB pullups





    ' CLEAR ;read and write hardware USART
    ' B1 VAR BYTE ;initialize USART
    recchar var byte
    TRISC = %10111111 ;set TX (PORTC.6) to out, rest in
    ' SPBRG = 25 ;set baud rate to 2400
    RCSTA = %10010000 ;enable serial port and continuous receive
    TXSTA = %00100000 ;enable transmit and asynchronous mode
    define hser_rcsta 90h
    ' define hser_txsta 24h
    ' define hser_spbrg 9fh
    ' define baudcon=8


    define hser_baud 2400

    Startpgm:
    Pause 500 ' Wait for LCD to startup

    Lcdout $fe, 1 ' Initialize and clear display

    ' LCDOUT "Pgm Starting" , 10 ,13
    recchar = "A"
    ' Lcdout $fe, $c0, "Rec=" ' Move to line 2 and display

    ;echo received characters in infinite loop
    mainLOOP:

    GOSUB CHARIN ;get a character from serial input, if any
    IF recchar = "A" THEN mainLOOP ;no character yet
    GOSUB CHAROUT ;send character to serial output
    GOTO mainLOOP ;do it forever
    ;subroutine to get a character
    ;from usart receiver
    CHARIN:
    ' B1 = 0 ;preset to no character received
    IF PIR1.5 = 1 THEN ;if receive flag then...
    ' B1 = RCREG ;get received character to b1
    recchar = rcreg
    ' hserin[recchar] ;get received character to serial_in
    ' serin portc.7,4,[recchar] ;get received character to serial_in
    ' b1 = 1
    ENDIF
    CIRET:
    RETURN ;go back to caller
    ;subroutine to send a character
    CHAROUT: ;to usart transmitter

    Lcdout dec recchar ' LCD display
    pause 2000
    Lcdout $fe, 1 ' Initialize and clear display
    recchar = "A"
    ' B1 = 0

    ' IF PIR1.4 = 0 THEN CHAROUT ;wait for transmit register empty
    ' TXREG = B1 ;send character to transmit register
    RETURN ;Go back to caller
    END

  2. #2
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959

    Default

    At 48Mhz ... You have to use the 16-bit baud rate generator to get 2400 baud. It's too slow for the normal BRG.

    Try it like this

    DEFINE HSER_RCSTA 90h ' Enable serial port & continuous receive
    DEFINE HSER_TXSTA 20h ' Enable transmit, BRGH = 0
    DEFINE HSER_CLROERR 1 ' Clear overflow automatically
    DEFINE HSER_SPBRG 225 ' 2400 Baud @ 48MHz, 0.0%
    SPBRGH = 4
    BAUDCON.3 = 1 ' Enable 16 bit baudrate generator

    Or for other baud rates, use mister-e's calculator (EUSART calc)...

    PicMultiCalc V_1.3.1
    http://www.picbasic.co.uk/forum/atta...achmentid=2957

    DT

  3. #3
    Join Date
    Sep 2009
    Location
    Columbus, Ohio
    Posts
    6

    Default

    I tried mister e (handy tool).

    I now send a "D" and receive a 0.

    Tried pumping it up to 9600 baud and many other things but it is always a 0 or doesn't work at all.

    If I go back and try serialcom.exe from PC to try & get something to work right, would I use a straight cable instead of a null modem cable ?

    Thanks Darrel

  4. #4
    Join Date
    Jun 2009
    Location
    Sc*nthorpe, UK
    Posts
    333

    Default

    RS232 has two equipment types Data Terminal Equipment (DTE) and Data Connect Equipment (DCE). Your computer is a DTE and has a male connector as all DTE equipment has, that is if the RS232 standard has been followed. All equipment intended to connect to a computer RS232 port is DCE and has a female connector. In the early days null modems were intended to be used to connect two computers and therefore a null modem cable was female to female. This was before networks were invented. The problem now is that we have DCE to DCE equipment which requires a male to male connector, which are hard to find and easy to make. So if the two pieces of equipment have the same gender a null modem is required, RX and TX are crossed. If one is male and the other female then a straight serial cable is needed.

    Unless of course you have a piece of equipment that has not followed the RS232 convention and there are some trust me.

    Clear as mud?

    Steve

  5. #5
    Join Date
    Dec 2005
    Posts
    1,073

    Default

    It's not clear to me what you have at each end of the RS232 link. Are you trying to communicate from a PIC to a PC or to another embedded device?

    The hardware UART output is non-inverted while a PC expects it inverted and most embedded devices expect it non-inverted.

  6. #6
    Join Date
    Sep 2009
    Location
    Columbus, Ohio
    Posts
    6

    Default

    Problem solved.
    Went back to serialcom.exe and got PC talking to Labxusb and then to labx1 after making suggested chgs.

    Now labxusb and labx1 are talking fine.

    Thanks all!!!!!

    Darrel Now that that's working hopefully OSU will not get blown out by USC !!

  7. #7
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959

    Default

    Great!
    Glad you got it working.

    I'm originally from Columbus, and now live in California.
    So I'm quite sure ... and my bet would be for ... Not OSU.

    DT

  8. #8
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959

    Default

    Quote Originally Posted by Darrel Taylor
    So I'm quite sure ... and my bet would be for ... Not OSU.
    OUCH!
    And I gave up 10 points.

    USC-18    OSU-15

    %$#@!& College Football
    Definitely not a Blow Out.
    DT

  9. #9
    Join Date
    Sep 2009
    Location
    Columbus, Ohio
    Posts
    6

    Talking

    10 points was a good bet going into the game......

    Now that I got RS232 its on to USB and then the real target Cheap Ethernet.
    I saw you had some posts on Ethernet sooooo I may be hitting you for more info. later. Thanks!!!!

  10. #10
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959

    Default

    Quote Originally Posted by longdw View Post
    Now that I got RS232 its on to USB and then the real target Cheap Ethernet.
    When you're ready for USB let me know. Got some good stuff.

    But the ethernet... I just rely on the Lantronix ports for that.
    So much easier, but not "Cheap".
    DT

Similar Threads

  1. Universal remote and Rs232
    By Michael in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 7th February 2010, 17:55
  2. UART vs software RS232
    By Michael in forum mel PIC BASIC Pro
    Replies: 27
    Last Post: - 5th September 2008, 18:27
  3. PIC18F4680 to PC via MAX232 (RS232 serial) no output
    By opticsteam1 in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 14th April 2008, 20:39
  4. Big characters on HD44780 4x20
    By erpalma in forum mel PIC BASIC Pro
    Replies: 23
    Last Post: - 7th January 2007, 02:21
  5. Comm example: Pic to Pic
    By Chadhammer in forum Code Examples
    Replies: 4
    Last Post: - 14th November 2006, 23:26

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts