SERIN2 still hangs trying to fill a WORD variable
Thanks Joe S.
It will compile but it hangs up like the other efforts.
I have considered a "resistor problem" and but only 1k receive and 1k transmit resistors work at all (the byte in, byte out test). That also agrees with the manual's recommendation.
I cannot remember being this frustrated in at least 20 years!
Why does it work so easily in a BS2 but seems impossible in a PIC?
Thank you again for your suggestion. I will need all the help I can get.
Bob
Got it to work. Confusion with DEC has abated.
Joe S. and mackrackit,
Background: I had written a program with PBCC (PowerBASIC Console Compiler for WinXP) that:
1) Waits for the PIC to send a "0" to the PC, then
2) the PC sends 12 consecutive 16-bit words to the PIC expecting the PIC to receive them and do other things with them.
I had never been able to get the PIC to receive them. It would always just "hang up" at the SERIN2 statement.
I went back to basics and worked to send a set of characters (numeric) to the PIC via a PC terminal program and had the PIC send them back. I (finally) got it to work just fine:
My TX-RX code for this simple test is:
'--------------------------------------------------------
INCLUDE "MODEDEFS.BAS"
ADCON1 = 7 'set analog pins to digital
TRISA = 1 'set port A to inputs
TRISB = 0 'set port B to outputs
TRISC = 0 'make it an OUTPUT for serial comm on it's pins, even though
'RX is sort of an INPUT.
DEFINE OSC 20
MD VAR WORD
LedPin VAR PORTC.3
RXData VAR WORD
RXPin VAR PORTC.5
TXPin VAR PORTC.4
MD = 16780 '16780 = 2400 driven, 8 bit, 1 stop, no parity, inverted
main:
SERIN2 RXpin, MD, [DEC RXdata] 'waits for a character then moves on
SEROUT2 TXpin, MD, ["I am sending it back -> ", DEC RXdata, 10, 13]
HIGH LedPin 'hook LED to Pin 14 = RC3
PAUSE 20 'a flash of the LED means we got
LOW Ledpin 'this far OK
PAUSE 20
GOTO main
'--------------------------------------------------------
I got this to work with Hyperterm and with a simple terminal program I wrote in PBCC (PowerBASIC Console Compiler for WinXP).
I used a Serial Port Monitor to look at all the bytes going and coming.
What I learned:
1) I need a 1k resistor in the TX line and another 1k resistor in the RX line (I am not using an RS232 converter). Gee, this is exactly what the PBP manual said to do! Other references to a 22k or 10k resistor in the RX line were not good for me. What a wild goose chase that was. It really masked the problem for a long time.
2) The PC transmit end must be set for 8-bit whenever the DEC qualifier is used for variables within the SERIN2 statement. A transmitted 7-bit byte will not do (even though a 7-bit byte can still represents an ascii number).
3) When I use the qualifer DEC, SERIN2 will accept ONLY NUMERIC ascii characters, and it will keep accepting them until any NON-numeric character is received. THIS IS AS IT HAS TO BE. I turns out that my PBCC program had been sending a space character prior to the string of numeric characters. Then DEC caused that variable to be bypassed and nothing was stuffed into that variable at all.
4) The character that tells SERIN2 (with DEC) that the string of numeric characters has ended can be any non-numeric character. It does not have to be a CR.
5) When testing the TX-RX code, remember to send NUMERIC keystrokes in addition to alpha keystrokes. Afterall, wasn't it NUMERIC values I was trying to stuff in there? How could I have missed that one?
6) Breaking my 16-bit value into two bytes (high and low) then sending them to the PIC while SERIN2 uses the DEC qualifier will not work either! That technique must need some other qualifier (or no qualifier ... will have to try that again later).
7) If I set the baud rate mode via a variable, and that mode number is larger than a single byte's worth (like 16780) , then that variable better be defined as a WORD, not as a BYTE. Yet another noobie mistake!
8) SERIN2 is UNFORGIVING and will tell you nothing if:
* DEC is used and if
* Non-numeric STRING characters are sent, or
* If a non-numeric string character is sent
before the numeric string characters are sent.
9) SERIN2 with the DEC qualifier will accept lots of numeric characters, but if they represent a number higher than 65535 (max 16-bit word), the numbers that are stuffed into the variable will not be accurate.
10) While one would think that a Receive pin should be set as an INPUT, no, set both TX and RX pins as outputs. Learned that one by luck.
Now I can even get my 12 word transmission to work with the PIC. Hooray! Now I will move on to making the PIC program do more than receive the needed values. Progress at last!
It has been a painful learning experience. But I am stronger for it, I suppose.
Once I understood it, the PBP manual was right afterall. Ha! How dumb could I be?
Many thanks for your suggestions. One of the reasons I did this long post was to (hopefully) help other beginners navigate around some of these TX/RX and SERIN2 DEC traps.
Best regards to all,
Bob
Correction on resistor values TX and RX
I looked at the resistors that I used successfully. My previous post was in error with regard to the resistor value used in the TX line from the PIC. I used a 10k, not a 1k. I don't know if a 1k will work in the TX line, but I know the 10k works.
I still use the 1k in the RX line to the PIC, though.
I am next going to install an RS232 converter to avoid all this resistor roulette!
Best regards to all,
Bob