PDA

View Full Version : PIC to serial with visual basic



mbw123
- 31st March 2007, 01:27
Hello,

I am trying to hook up my PIC18F4550 to my serial port and send data using Visual Basic. The code for the PIC is below. I have a 22K resistor on the RX line of the PIC. The RTS and CTS lines I have working (thanks to skimask) but I can't seem to get the RX to work. The PC should transmit a "4" and the PIC should receive it and test its value. For visual basic the instruction I am using is: SerialPort.Write("4"). I believe I have the right baud and everything setup (9600) but I can't be sure (the rest is the default setup). Any help is appreciated! Thanks,

-Mike

Code on PIC:
------------------
Include "modedefs.bas"
define HSER_CLROERR 1
define HSER_RCSTA 90h
define HSER_BAUD 9600
define HSER_TXSTA 20h
define OSC 4

rts var porta.2
cts var portd.3
led var portd.2
rx var portc.7
OK var word

input rts
output cts
output led

ADCON1 = %00001111

low led : low cts

main:

while rts = 1

high cts
SERIN rx,N9600,OK
if ok = "4" then
high led
endif

wend

low cts

goto main

skimask
- 31st March 2007, 01:37
Code on PIC:
------------------
Include "modedefs.bas"
define HSER_CLROERR 1 : define HSER_RCSTA 90h : define HSER_BAUD 9600
define HSER_TXSTA 20h : define OSC 4 : rts var porta.2:cts var portd.3
led var portd.2 : rx var portc.7 : OK var word : input rts : output cts
output led : ADCON1=15 : low led : low cts
main:
while rts = 1
high cts : SERIN rx,N9600,OK
if ok = "4" then
high led
endif
wend
low cts
goto main

Why are you using the RTS & CTS lines? Just curious...I've never used them, never had a need, kinda complicates thing unneccesarily. Try this whole thing without the RTS and CTS lines. Set Windows to 'Handshaking-None' in the serial properties. See what happens then. Again, you're breaking it down, starting super-simple, then working your way up.

Another thing, you're using SERIN with HSER defines. Why?

And SERIN @ 4mhz isn't exactly reliable. Switch down to 2400 and see what happens.

You're also not using a MAX232. It should work with a PC using the inverted serial modes, but the voltage levels might not be high enough. You might end up using one. It's always worked for me, but you never know.

Incidentally, I got over 100 threads here on this site alone all about serial interfacing, PC's, visual basic, hyperterminal, etc.

mbw123
- 31st March 2007, 01:55
OK, thanks for the response.

I tried it without the RTS / CTS lines but it still didn't work.

Sorry about the HSER defines, I used them before and I forgot to get rid of them. Even without them and switching the baud rates, though, it still doesn't work.

"You're also not using a MAX232. It should work with a PC using the inverted serial modes, but the voltage levels might not be high enough. You might end up using one. It's always worked for me, but you never know."
Yeah, I may end up with a MAX232. I just want to avoid it if at all possible.

I really don't know where to go from here. Any help or suggestions?

Thanks.

-Mike

skimask
- 31st March 2007, 02:06
Did you switch to 2400 baud?
Did you read the manual where it says that 9600 baud might not be possible at 4mhz?

mbw123
- 31st March 2007, 02:18
Yeah, I switched it to 2400 baud rate and turned off the handshake. Maybe the visual basic code is wrong or the code on the PIC should be "if OK = 4" instead of 4 being in quotation marks. Or am I missing something that's staring me in the face?

Thanks for the help.

-Mike

skimask
- 31st March 2007, 02:24
Yeah, I switched it to 2400 baud rate and turned off the handshake. Maybe the visual basic code is wrong or the code on the PIC should be "if OK = 4" instead of 4 being in quotation marks. Or am I missing something that's staring me in the face?

Thanks for the help.

-Mike

Try this. It echo's everything you type on hyperterminal, but echo the same character 3 times and toggle the led every time you hit a key, that way you know it's really working..

Include "modedefs.bas"
DEFINE OSC 4
rx var portc.7 : output rx : tx var portc.6 : input tx
led var portd.2 : output led : serialdata var byte : ADCON1 = $F
main:
toggle led 'just show the program is running
serin rx , N2400 , serialdata
serout tx , n2400 , [ serialdata , serialdata , serialdata ]
goto main


Once again, you break it down, make it super simple, then build it back up...

sougata
- 31st March 2007, 13:55
Hi,

Here is a portion from the PBP manual


Since the serial reception is done in hardware, it is not possible to set
the levels to an inverted state to eliminate an RS-232 driver. Therefore
a suitable driver should be used with HSERIN.


Any idea what this means ? I don't know or may be I do :D

Using the hardware pins with a software serial (inverted) works but makes no sense as anyway you don't use the hardware peripheral. So time to spend on MAX or be happy with soft serial.

skimask
- 31st March 2007, 16:06
Hi,

Here is a portion from the PBP manual


Any idea what this means ? I don't know or may be I do :D

Using the hardware pins with a software serial (inverted) works but makes no sense as anyway you don't use the hardware peripheral. So time to spend on MAX or be happy with soft serial.

Sure ya do. HSERIN/OUT uses the UART, which is monitored by PBP for input/output. So you need the correct RS232 voltages...