PDA

View Full Version : SERIN frustration



Ellenjay
- 19th November 2014, 21:37
Trying to understand serial communications..
The aim is to load a large number of 5 digit numbers into a serial eeprom from a PC.
Step 1 is to master serial comms from PC.

Using serial communicator (Microcode Studio tool) I send "1234", BUT LCD displays "Received = 3846"

Any advice would be most welcome.

My code..

'**********************************
@ device PIC16F877a , hs_osc, wdt_on, lvp_off, protect_off
CLEAR
DEFINE OSC 8

DEFINE LCD_DREG PORTB ' Set LCD Data port
DEFINE LCD_BITS 4 ' Set LCD bus size (4 or 8 bits)
DEFINE LCD_DBIT 4 ' Set starting Data bit (0 or 4) if 4-bit bus
DEFINE LCD_RSREG PORTB ' Set LCD Register Select port
DEFINE LCD_RSBIT 2
DEFINE LCD_EREG PORTB ' Set LCD Enable port
DEFINE LCD_EBIT 3 ' Set LCD Enable bit

PortA = %00000000 'all outputs low
PortB = %00000000 'all outputs low
TrisA = %00000000 'All as outputs
TrisB = %00000000 ' B.0 as input all others as outs
TrisC = %11111111

include"modedefs.bas" ' includes aliases for modes
W0 var word
B0 Var byte
B1 var byte

LCDOUT 254, 1 'Clear Screen
pause 100
LCDOUT 254, 1 'Clear Screen
LCDOUT "W0 = " , dec W0
pause 1000
LCDOUT 254, 1 'Clear Screen
mainloop:

serin PortC.7,T2400,[] ,w0.lowbyte ,w0.highbyte

LCDOUT 254, 1 'Clear Screen
LCDOUT "Received =" , #W0
pause 1500

goto mainloop

end
'*******************************************

Ellenjay
- 19th November 2014, 21:45
Trying to understand serial communications..
The aim is to load a large number of 5 digit numbers into a serial eeprom from a PC.
Step 1 is to master serial comms from PC.

Using serial communicator (Microcode Studio tool) I send "1234", BUT LCD displays "Received = 3846"

Any advice would be most welcome.

My code..

'**********************************
@ device PIC16F877a , hs_osc, wdt_on, lvp_off, protect_off
CLEAR
DEFINE OSC 8

DEFINE LCD_DREG PORTB ' Set LCD Data port
DEFINE LCD_BITS 4 ' Set LCD bus size (4 or 8 bits)
DEFINE LCD_DBIT 4 ' Set starting Data bit (0 or 4) if 4-bit bus
DEFINE LCD_RSREG PORTB ' Set LCD Register Select port
DEFINE LCD_RSBIT 2
DEFINE LCD_EREG PORTB ' Set LCD Enable port
DEFINE LCD_EBIT 3 ' Set LCD Enable bit

PortA = %00000000 'all outputs low
PortB = %00000000 'all outputs low
TrisA = %00000000 'All as outputs
TrisB = %00000000 ' B.0 as input all others as outs
TrisC = %11111111

include"modedefs.bas" ' includes aliases for modes
W0 var word
B0 Var byte
B1 var byte

LCDOUT 254, 1 'Clear Screen
pause 100
LCDOUT 254, 1 'Clear Screen
LCDOUT "W0 = " , dec W0
pause 1000
LCDOUT 254, 1 'Clear Screen
mainloop:

serin PortC.7,T2400,[] ,w0.lowbyte ,w0.highbyte

LCDOUT 254, 1 'Clear Screen
LCDOUT "Received =" , #W0
pause 1500

goto mainloop

end
'*******************************************

Ellenjay
- 19th November 2014, 22:04
CORRECTION .. had terminal set to 1200..

set terminal to 2400 - No Parity - Byte Size = 8 - No Stop Bit
NOW displays "12849"

pedja089
- 19th November 2014, 22:59
http://www.picbasic.co.uk/forum/showthread.php?t=19557

richard
- 20th November 2014, 00:52
Using serial communicator (Microcode Studio tool) I send "1234", BUT LCD displays "Received = 3846"

will actually send [0x31,0x32,0x33,0x34,0x0d,0x0a] ascii 1,2,3,4,cr,lf



how is your pc serial port connected to your pic ?

Amoque
- 20th November 2014, 01:38
I think Richard is correct. This from the manual:

SERIN begins storing data in the variables associated with each Item. If the variable name is used alone, the value of the received ASCII character is stored in the variable.

Meaning, in my mind, that you are actually receiving "3" "8" "4" "6" rather than 3846. If the LCD character codes are different than the ASCII character codes...

The manual goes on to say:

If variable is preceded by a pound sign ( # ), SERIN converts a decimal value in ASCII and stores the result in that variable.

This seems to indicate you will receive 3, 8, 4, 6 if you modify your statement. You can receive your data as byte array, and then use multiplication to expand the value into a word variable.

Editing to include this, a snippet from a recent project of mine that accepts a serial command in the format: CC*HR:MN to reset the time via serial input.

IF (CO[0] ="T") AND (CO[1]="I") THEN
REG_VAL[2] = (CO[3]-48) * 10 + (CO[4]-48)
REG_VAL[1] = (CO[6]-48) * 10 + (CO[7]-48)
GOSUB CLK_WRITE

CO[3] and CO[4] are the hour value of the time - I had to subtract 48 from the serial input to convert from the string value to the numeric value and multiply. I suspect you will need to do something similar.

Archangel
- 20th November 2014, 05:37
Is this what you want?


mainloop:
serin2 porta.1,84, [STR buffer\5]

pause 200

serout2 PortA.0,84,[ "Received = ",STR buffer\5]
pause 500

goto mainloop

it works using debug a little faster too

Amoque
- 20th November 2014, 12:32
I think, Archangel, that it exactly the opposite of what he wants - though your code demonstrates the issue perfectly. The OP, it seems from my read, is transmitting the value 3846, but receiving the ASCII character codes of the digits, rather than the single numeric value he desires. Rereading my own post, I was not as clear as I'd wish to be in explaining this - and how to recover the actual value. Your use of STR demonstrates more clearly that it is actually 5 STRing characters received, rather than 1 numeric value.

Archangel
- 20th November 2014, 18:04
Hi Amoque,
If he wants the Decimal number he will run into a problem at 65535 as that completely fills a WORD variable, requiring the use of a LONG variable.
His OP didn't indicate this need, at least that's how I read it. Still ASCII can be the medium for transport, doing the MATH (dirty 4 letter word) after receiving the number. LCD isn't going to display actual numbers as the first 47 are commands and symbols dating back to RTTY for which ASCII was invented, and that is what LCDs display (ASCII)