PDA

View Full Version : RS232 comm



longdw
- 10th September 2009, 21:52
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

Darrel Taylor
- 11th September 2009, 04:43
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/attachment.php?attachmentid=2957
<br>

longdw
- 11th September 2009, 19:14
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

EarlyBird2
- 11th September 2009, 21:06
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

dhouston
- 11th September 2009, 22:27
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.

longdw
- 11th September 2009, 23:06
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 !!

Darrel Taylor
- 12th September 2009, 01:44
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. :eek:
<br>

Darrel Taylor
- 13th September 2009, 04:35
So I'm quite sure ... and my bet would be for ... Not OSU. :eek:
OUCH!
And I gave up 10 points. :eek:

USC-18 &nbsp; &nbsp;OSU-15

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

longdw
- 14th September 2009, 15:33
10 points was a good bet going into the game......:D

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!!!!

Darrel Taylor
- 15th September 2009, 03:13
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".