PDA

View Full Version : Serial Communication with PIC16F877A



mcbeasleyjr
- 26th May 2009, 22:19
Hello again to all... I am testing pieces of code that I have put together from various sources. I am trying to get a communications link set up between my PIC16F877A and my PC. I am using the serial communicator that comes with MicroCode Studio Plus. If I understand what I'm trying to do correctly, the PIC should wait for a message from the PC. If it doesn't receive this message, it will timeout and is supposed to display "nada". However, when I don't send anything to the PIC, it returns 4 wierd looking symbols. Also, if I try to transmit something to PIC, it doesn't return anything at all. Any helpful advice would be greatly appreciated.



DEFINE OSC 20

INCLUDE "MODEDEFS.BAS"
DEFINE HSER_BAUD 9600 ' HSER baud rate
DEFINE HSER_SPBRG 129 ' HSER SPBRG init
DEFINE HSER_RCSTA 90h ' HSER receive status init
DEFINE HSER_TXSTA 24h ' HSER transmit status init high speed
'DEFINE HSER_EVEN 1 ' Use only if even parity is desired
'DEFINE HSER_ODD 1 ' Use only if odd parity is desired
DEFINE HSER_BITS 9 ' Use for 8 bits ''+ parity
DEFINE HSER_CLROERR 1 ' Automatically clear HSERIN overflow errors

TRISC=%10000000

TimeDate VAR BYTE[6]

Main:
HSERIN 5000,TimeOut,[WAIT("A"),STR TimeDate\6] ' A369621
HSEROUT ["Rcvd string: ", STR TimeDate\6,13,10]
GOTO Main

TimeOut:
HSEROUT ["Nada",13,10]
GOTO Main


Port Settings:
COM1
9600 Baud
No Parity
Byte Size is 8
1 Stop Bit

mackrackit
- 26th May 2009, 22:57
Do you have a MAX232 or similar between the PIC and PC?

If you do not have one, try SERIN2/SEROUT2 using inverted mode.

mcbeasleyjr
- 26th May 2009, 23:01
Yes I have a MAX232 on the ICD I've built... it should also work as my serial communicator for this purpose as well... This is the number on the chip:

69CT01M
MAX232IN

TTL to CMOS converter...

any ideas?

Darrel Taylor
- 26th May 2009, 23:10
Serial data to a PC should always be 8 bits.

DEFINE HSER_BITS 9 ; won't work
<br>

mackrackit
- 26th May 2009, 23:12
Missed this the first time I looked at it.
DEFINE HSER_BITS 9
Scratch the above line

mcbeasleyjr
- 26th May 2009, 23:17
I have scratched the line as you suggested... however, it still isn't working. If I try to transmit the example in my code, which is A369621... I don't receive anything back at all. Also, if I don't transmit anything and the USART times out, I receive this: ¬ºê*ë... any other ideas?

Do I have to set PORTC as this:????

TRISC=%11000000

I'm a little confused on the correct settings to get the USART to function properly.

mackrackit
- 27th May 2009, 00:19
Maybe this will help.
http://www.melabs.com/resources/samples/pbp/usart.bas

mcbeasleyjr
- 27th May 2009, 00:25
I copy and pasted the code you provided me but had no luck with it...

mackrackit
- 27th May 2009, 00:32
Can you post your schematic and/or a picture of your setup. I think it is down to a hardware problem now.

Caps on the 232?
Cable is straight through?
Usb to serial converter?
Real serial port?
Port on PC is known to be good?

Hold one hand in the air while touching you big toe? :)

Could be anything now.

mcbeasleyjr
- 27th May 2009, 00:37
Can you post your schematic and/or a picture of your setup. I think it is down to a hardware problem now.

Caps on the 232?
Cable is straight through?
Usb to serial converter?
Real serial port?
Port on PC is known to be good?

Hold one hand in the air while touching you big toe? :)

Could be anything now.

Well... you'd have to give me a while to get a schematic drawn up. There are 4 caps on the 232. The cable is a regular serial cable, male on one end female on the other. I don't have a USB to serial converter although using that while trying to debug does work. It is a serial port on the back of my computer so I'm assuming that it is a real port. This is first time I've really tried using the serial port so as far as knowing whether its good or not I don't know, nor do I know how to check. Device manager does say that device is working properly with no problems. Lol, and not sure I want to try that one or not. Probably fall flat on my face. :D

mackrackit
- 27th May 2009, 00:46
Make sure the cable is not for a modem. Pin 2 on one end goes to Pin 2 on the other, the same for pin 3.

Test PC serial port by jumping PINs 2 and 3 with a 10K and use a terminal to see if you get an echo.

mcbeasleyjr
- 27th May 2009, 01:24
Make sure the cable is not for a modem. Pin 2 on one end goes to Pin 2 on the other, the same for pin 3.

Test PC serial port by jumping PINs 2 and 3 with a 10K and use a terminal to see if you get an echo.

I tested my serial port and sent 5 succesfully in the loopback... what else can i try?

mcbeasleyjr
- 27th May 2009, 02:28
I am attaching a bitmap of my schematic for my ICD and ICP. I am using the ICD for the serial communication.

Archangel
- 27th May 2009, 06:27
Make sure the cable is not for a modem. Pin 2 on one end goes to Pin 2 on the other, the same for pin 3.

Did you test as Mackrackit suggested above? Many serial appearing cables are actually laplink cables, or nul modem cables and the Tx and Rx pins are crossed so two computers can be linked directly.

mcbeasleyjr
- 27th May 2009, 21:34
Did you test as Mackrackit suggested above? Many serial appearing cables are actually laplink cables, or nul modem cables and the Tx and Rx pins are crossed so two computers can be linked directly.

Yes I tested it... I successfully sent a message through my cable and echoed it back by shorting pin 2 to pin 3.... any other ideas?

Added:

Is there anyway to tell if my cable is crossed as you suggest?... I know I got the echo by shorting pin 2 to pin 3 but I would still get the echo if the pins were crossed...

Resolved:

Thank you guys for your help. After testing my cable for continuity I realized that it is crossed. Going to get a new cable and hopefully that fixes the problem.

mackrackit
- 28th May 2009, 01:15
Just reverse the connection at the device end, swap pins 2 and 3.
Or cut the cable...

Archangel
- 28th May 2009, 01:38
Just reverse the connection at the device end, swap pins 2 and 3.
Or cut the cable...
Or if you have a null modem adapter use it to swap back . . .