Quantcast
Channel: MEL PICBASIC Forum
Viewing all articles
Browse latest Browse all 4787

loosing some bytes in HSERIN

$
0
0
Hello there,


I want to do serial communicate with a digital scale. 9600,8,E,1. Scale accepts protocols like this:
[Address (1byte)] + [Command (1byte)] + {Optional Data+ ... if exists} + [Checksum(1byte)]. including a short delay between each segment of protocol.


The scale would response like this:
[Address (1byte)] + [Command(1byte)] + {Optional Data+ ... Echos if already have been received by scale} + Data1+ Data2+ Data3+ Data4+[Checksum]. including a short delay between each segment of protocol.

Each protocol is completed with an easy to calculate XOR checksum. The end of the protocol is detected using a timeout control (less than 4.5 msec).

Whit this below code, transmiting from MCU toward the scale is perfect, but MCU looses some bytes of scale's responses. For instance: it looses the address and command and strangely any byte which is (zero) "00" .
Code:

INCLUDE "DT_INTS-14.bas"
INCLUDE "ReEnterPBP.bas"
;---------------------------------------------------------------------------------------------------
RCSTA = $D8
TXSTA = $65
SPBRG = 25 


;---------------------------------------------------------------------------------------------------
DEFINE HSER_BITS 9
DEFINE HSER_EVEN 1
DEFINE HSER_CLROERR 1 ' Clear overflow automatically
;---------------------------------------------------------------------------------------------------
#CONFIG
  __config  _HS_OSC & _WDT_ON & _PWRTE_OFF & _BODEN_OFF & _LVP_OFF & _CPD_OFF & _WRT_OFF & _DEBUG_OFF & _CP_OFF
#ENDCONFIG
;---------------------------------------------------------------------------------------------------
ASM
INT_LIST macro ;IntSource, Label,    Type, ResetFlag?
    INT_Handler RX_INT,  _GetChar,  PBP, yes
    endm
    INT_CREATE
ENDASM
@    INT_ENABLE  RX_INT   
;---------------------------------------------------------------------------------------------------
define OSC 4
DEFINE LCD_DREG PORTB
DEFINE LCD_DBIT 4
DEFINE LCD_RSREG PORTB
DEFINE LCD_RSBIT 3     
DEFINE LCD_EREG PORTB
DEFINE LCD_EBIT 2       
DEFINE LCD_BITS 4
DEFINE LCD_LINES 4
DEFINE LCD_COMMANDUS 2000
DEFINE LCD_DATAUS 50
ADCON1=7
TRISA=255
TRISB=0
TRISC=%10000000
clear
leD var PORTC.2
;---------------------------------------------------------------------------------------------------
a0 var byte
a1 var byte
a2 var byte
;---------------------------------------------------------------------------------------------------
Address var byte
Address = $40
Chksum var byte
Comand var byte
Comand2 var byte
Key0 var PORTA.0
Key1 var PORTA.1
Key2 var PORTA.2
;---------------------------------------------------------------------------------------------------
wsave  VAR BYTE    $70    SYSTEM
;---------------------------------------------------------------------------------------------------
n var byte[16]
i var byte
;---------------------------------------------------------------------------------------------------
lcdout $fe,1
lcdout $fe,2,"Scale DT-INTS"
pause 2200
lcdout $fe,1
'---------------------------------------------------------------------------------------------------
scan:
lcdout $fe,2,hex2 n(0),"-",HEX2 n(1),"-", HEX2 n(2),"-",hex2 n(3)
lcdout $fe,192,hex2 n(4),"-",HEX2 n(5),"-", HEX2 n(6),"-",hex2 n(7),"-s",hex2 Chksum
'---------------------------------------------------------------------------------------------------
button key0,1,0,255,a0,1,K0
button key1,1,0,255,a1,1,K1
button key2,1,0,255,a2,1,K2
;---------------------------------------------------------------------------------------------------
goto scan
'---------------------------------------------------------------------------------------------------
'---------------------------------------------------------------------------------------------------
K0:
lcdout $fe,1
for i=0 to 15
n(i)=0
next i
i=0
chksum= $02
comand= $42


high led
hserout [address]
PAuse 2
hserout [comand]
PAuse 2
hserout [chksum]
pause 2
low  led
;---------------------------------------------------------------------------------------------------
goto scan
'---------------------------------------------------------------------------------------------------
'---------------------------------------------------------------------------------------------------
K1:
lcdout $fe,1
for i=0 to 15
n(i)=0
next i
i=0
chksum= $4C
Comand= $44
Comand2=$48
high led
hserout [address]
PAuse 2
hserout [Comand]
PAuse 2
hserout [Comand2]
PAuse 2
hserout [chksum]
pause 2
low  led
;---------------------------------------------------------------------------------------------------
goto scan
'---------------------------------------------------------------------------------------------------
'---------------------------------------------------------------------------------------------------
K2:
lcdout $fe,1
for i=0 to 15
n(i)=0
next i
i=0
chksum= $06
Comand= $46
high led
hserout [address]
PAuse 2
hserout [comand]
PAuse 2
hserout [chksum]
pause 2
low  led
;---------------------------------------------------------------------------------------------------
goto scan
'---------------------------------------------------------------------------------------------------
GetChar:
Hserin [n(i)]
i=i+1
if i>=15 then
i=0
endif
@ INT_RETURN

from commands scale reference book: depends on the commands the response times are different from maximum 5 msec until 250 msec. . there is a table for response time in scale's manual.

Even I have tried this code (without DT-ints). still loosing some bytes of response. specially if a byte be zero, it would never detect.
Code:


RCSTA = $D8
TXSTA = $65
SPBRG = 25 
;---------------------------------------------------------------------------------------------------
DEFINE HSER_BITS 9
DEFINE HSER_EVEN 1
DEFINE HSER_CLROERR 1 ' Clear overflow automatically
;---------------------------------------------------------------------------------------------------
#CONFIG
  __config  _HS_OSC & _WDT_ON & _PWRTE_OFF & _BODEN_OFF & _LVP_OFF & _CPD_OFF & _WRT_OFF & _DEBUG_OFF & _CP_OFF
#ENDCONFIG
;---------------------------------------------------------------------------------------------------
;---------------------------------------------------------------------------------------------------
define OSC 4
DEFINE LCD_DREG PORTB
DEFINE LCD_DBIT 4
DEFINE LCD_RSREG PORTB
DEFINE LCD_RSBIT 3     
DEFINE LCD_EREG PORTB
DEFINE LCD_EBIT 2     
DEFINE LCD_BITS 4
DEFINE LCD_LINES 4
DEFINE LCD_COMMANDUS 2000
DEFINE LCD_DATAUS 50
ADCON1=7
TRISA=255
TRISB=0
TRISC=%10000000
clear
leD var PORTC.2
;---------------------------------------------------------------------------------------------------
a0 var byte
a1 var byte
a2 var byte
;---------------------------------------------------------------------------------------------------
Address var byte
Address = $40
Chksum var byte
Comand var byte
Comand2 var byte
Key0 var PORTA.0
Key1 var PORTA.1
Key2 var PORTA.2
Key3 var PORTA.3
Key4 var PORTA.4
Key5 var PORTA.5
;---------------------------------------------------------------------------------------------------
;---------------------------------------------------------------------------------------------------
n var byte[16]
i var byte
;---------------------------------------------------------------------------------------------------


lcdout $fe,1
lcdout $fe,2,"Scale Test2B"
pause 2200
lcdout $fe,1
'---------------------------------------------------------------------------------------------------
scan:
lcdout $fe,2,hex2 n(0),"-",HEX2 n(1),"-", HEX2 n(2),"-",hex2 n(3)
lcdout $fe,192,hex2 n(4),"-",HEX2 n(5),"-", HEX2 n(6),"-",hex2 n(7),"-C",hex2 Chksum
'---------------------------------------------------------------------------------------------------
button key0,1,0,255,a0,1,K0
button key1,1,0,255,a1,1,K1
button key2,1,0,255,a2,1,K2
;---------------------------------------------------------------------------------------------------
goto scan
'---------------------------------------------------------------------------------------------------
'---------------------------------------------------------------------------------------------------
K0:
lcdout $fe,1
for i=0 to 15
n(i)=0
next i
i=0
chksum= $02
comand= $42


high led
hserout [address]
PAuse 2
hserout [comand]
PAuse 2
hserout [chksum]
pause 1


for i=0 to 15
Hserin 4,start,[n(i)]
start:
next i
low  led


;---------------------------------------------------------------------------------------------------
goto scan
'---------------------------------------------------------------------------------------------------
'---------------------------------------------------------------------------------------------------
K1:
lcdout $fe,1
for i=0 to 15
n(i)=0
next i
i=0
chksum= $4C
Comand= $44
Comand2=$48
high led
hserout [address]
PAuse 2
hserout [Comand]
PAuse 2
hserout [Comand2]
PAuse 2
hserout [chksum]
pause 1


for i=0 to 15
Hserin [n(i)]
next i
low  led
;---------------------------------------------------------------------------------------------------
goto scan
'---------------------------------------------------------------------------------------------------
'---------------------------------------------------------------------------------------------------
K2:
lcdout $fe,1
for i=0 to 15
n(i)=0
next i
i=0
chksum= $06
Comand= $46
high led
hserout [address]
PAuse 2
hserout [comand]
PAuse 2
hserout [chksum]
pause 1


for i=0 to 15
Hserin [n(i)]
next i
low  led
;---------------------------------------------------------------------------------------------------
goto scan
'---------------------------------------------------------------------------------------------------

Would you please let me know where is the problem.

Thank you very much in advance and regards,

Andy

Viewing all articles
Browse latest Browse all 4787