PDA

View Full Version : serial communication problem with PIC18F25K22



harryweb
- 7th April 2011, 15:48
HI,

I have a problem with 2 18F25K22 running at 12x4=48 MHz

@ 19200 Bauds
The first one (named:Net-master) sends every 0.5sec "0?" + 0A from portc.4 to the second one (named: NetATT) portb.7, using hardware usart 2

NetATT, then sends (with TX2 Hardware) how many bytes received, what bytes,how many times it received something and the answer of the question "?0", in this case "625WAYX".

It works fine in the beginning but after a few transmission (time = 26 + 1 because 0 first) the counter of "how many times it received something" seems to have a problem and restart at 0 :confused:
It will work again for few transmissions and at time = 26 it start to freeze.
(see attached file taken from my PICKIT serial analyser: PSA.TXT)

NETATT:


'************************************************* ***************
'* Name : netatt-test.pbp *
'* Author : [select VIEW...EDITOR OPTIONS] *
'* Notice : Copyright (c) 2011 [select VIEW...EDITOR OPTIONS] *
'* : All Rights Reserved *
'* Date : 28/02/2011 *
'* Version : 1.0 *
'* Notes : *
'* : *
'************************************************* ***************


Include "modedefs.bas"
define OSC 48

' - Memorisation des données EEPROM à la programmation
' à partir de l'addresse 1
EEPROM 1,[24,41,36,21,29,80]
' 1, H, Y, T, E, M, _
' à partir de l'adresse 16
' 17,18,19,20
EEPROM 16,[87,65,89,88]
' 16, W ,A ,Y, X
'-----------------------------------------------------------------------
DEFINE ADC_BITS 8 ' ADCIN resolution (Bits)
DEFINE ADC_CLOCK 7 ' ADC clock source (Frc)
DEFINE ADC_SAMPLEUS 11 ' ADC sampling time (uSec)


' configuration USART2 portb.6 (communication to net-master)
RCSTA2 = $90 ' Enable serial port & continuous receive
TXSTA2 = $20 ' Enable transmit, BRGH = 0
SPBRG2 = 38 ' 19200 Baud @ 48MHz, 0,16%


' configuration USART1 (external communication with EM1206 TIBBO)
RCSTA1 = $90 ' Enable serial port & continuous receive
TXSTA1 = $20 ' Enable transmit, BRGH = 0
SPBRG1 = 77 ' 9600 Baud @ 48MHz, 0,16%

ANSELA = 0
ANSELB = 0
ANSELC = 16 'analogue sur portc.4


TRISA = %10001111
TRISB = %10000001
TRISC = %10010000

PORTA = 0
PORTB = 0
PORTC = 0

led var PORTA.5
moins var PORTA.0
plus VAR PORTA.1
x var byte
ldcnt var byte 'clignotement de la led
ldcnt2 var word 'idem
voie var byte
nv1 var byte
nv2 var byte
nv3 var byte
nv4 var byte
what var byte
way var byte
remote var bit
att var word
vmax var WORD
attn var byte
val var byte
GP2 VAR BYTE ' GP variable
BytesIn2 VAR BYTE[10] ' Up to 10 bytes
ByteCnt2 VAR BYTE ' Index pointer
EOM CON 10 ' EOM = "End Of Msg" terminating character
OERR2 VAR RCSTA2.1 ' Alias USART over-run bit
CREN2 VAR RCSTA2.4 ' Alias USART continuous receive enable bit
RCIF2 VAR PIR3.5 ' Alias USART received character interrupt flag bit
RCIF1 VAR PIR1.5
GP1 VAR BYTE ' GP variable
BytesIn1 VAR BYTE[80] ' Up to 80 bytes
ByteCnt1 VAR BYTE ' Index pointer
OERR1 VAR RCSTA1.1 ' Alias USART over-run bit
CREN1 VAR RCSTA1.4 ' Alias USART continuous receive enable bit
d1 var byte
d2 var byte
d3 var byte
num1 var byte
num2 var byte
num3 var byte
num4 var byte
num5 var byte
num6 var byte
tx var portc.3 'signale un chgt a envoyer

pause 10
clear

'************************************************* ****
'Détermine la valeur max de l'att et positionne au max
'************************************************* ****

if portb.0 = 0 then
vmax = 935
ldcnt = 200 'led clignote à environ 1sec
else
vmax = 625
ldcnt = 100 'led clignote à environ 1/2sec
endif
att = vmax
gosub out 'envoi vers l'atténuateur

'********************************
'Détermine la voie du PIC (0-3)
'********************************

ADCIn 16,way

IF way < 30 then
way = 48 'voie 0 pour 470R
goto mainloop
endif

IF way < 60 then
way = 49 'voie 1 pour 2K2
goto mainloop
endif

IF way < 90 then
way = 50 'voie 2 pour 3K9
goto mainloop
endif

way = 51 'voie 3 pour 10K




'**************** programme principal ***************************

mainloop:

IF PORTA.2 = 1 then ' inter en remote ?
remote = 1
else
remote = 0
endif


IF RCIF2 then 'reception from master PIC ?
gosub RX2
endif

goto mainloop

'================================================= ===================





'********* reception with USART2 (from netMaster PIC) ****
RX2:
IF RCIF2 THEN ' If RCIF2=1 there's a new character in RCREG
BytesIn2[ByteCnt2]=RCREG2 ' Yes. Then store character in array
IF BytesIn2[ByteCnt2]=10 THEN OutMsg2 ' LF (0A)
ByteCnt2=ByteCnt2+1 ' Increment element index pointer
ENDIF
IF !OERR2 THEN RX2 ' If NOT over-run, then re-cycle
CREN2 = 0 ' Yes. Over-run condition is present, so clear it
CREN2 = 1 ' Re-enable & clear over-run condition
HSEROUT2 ["Over-run reset",13,10]
return

OutMsg2:
HSEROUT2 [13,10,DEC ByteCnt2," received bytes",13,10]
HSEROUT2 [STR BytesIn2\ByteCnt2,13,10] ' Show each character
hserout2 ["time:",dec x,10]
x = x + 1

IF BytesIn2[0] = 63 and Bytesin2[1] = way THEN
gosub sendata '?voie
gosub reset2
goto mainloop
endif

hserout2 ["?"]
gosub reset2
goto mainloop


reset2:
FOR GP2=0 TO ByteCnt2 ' Clear array bytes 0 to ByteCnt
BytesIn2[GP2]=0
NEXT GP2
ByteCnt2=0 ' Reset index pointer back to first element
WHILE RCIF2 ' Trash any left over characters in RCREG buffer
GP2=RCREG2 ' after EOM is received
WEND
RETURN


' *******************************
' Fixe l'atténuateur
' *******************************
out:
attn = att / 10 ' max 935 ==> 93dB + positionnement du 0.5dB
IF attn * 10 <> att then
HIGH PORTC.1 ' 0.5 dB
ELSE
LOW PORTC.1
ENDIF

IF attn < 63 then 'conversion vers 0-128
IF attn < 31 THEN
val = attn & 31
ELSE
val = ((attn - 31) & 31) + 32
ENDIF
else
val = ((attn - 62) & 31) + 96
endif

IF val.0 = 1 THEN '1
HIGH PORTB.5
ELSE
LOW PORTB.5
ENDIF
IF val.1 = 1 THEN '2
HIGH PORTB.2
ELSE
LOW PORTB.2
ENDIF
IF val.2 = 1 THEN '4
HIGH PORTB.3
ELSE
LOW PORTB.3
ENDIF
IF val.3 = 1 THEN '8
HIGH PORTC.0
ELSE
LOW PORTC.0
ENDIF
IF val.4 = 1 THEN '16
HIGH PORTB.1
ELSE
LOW PORTB.1
ENDIF
IF val.5 = 1 THEN '31
HIGH PORTC.2
ELSE
LOW PORTC.2
ENDIF
IF val.6 = 1 then '2éme 31
high PORTB.4
else
low PORTB.4
endif


RETURN

sendata:
toggle led
read 16,nv1 'nom 1ere lettre
read 17,nv2 'nom 2éme lettre
read 18,nv3 '....
read 19,nv4 '....

if att < 10 then
hserout2 ["00",dec att,nv1,nv2,nv3,nv4,10]
return
endif

if att < 100 then
hserout2 ["0",dec att,nv1,nv2,nv3,nv4,10]
return
endif

hserout2 [dec att,nv1,nv2,nv3,nv4,10] 'val de l'att + nom de voie

NETMASTER:


'************************************************* ***************
'* Name : net-master-test.pbp *
'* Author : [select VIEW...EDITOR OPTIONS] *
'* Notice : Copyright (c) 2011 [select VIEW...EDITOR OPTIONS] *
'* : All Rights Reserved *
'* Date : 28/02/2011 *
'* Version : 1.0 *
'* Notes : *
'* : *
'************************************************* ***************


Include "modedefs.bas"

DEFINE OSC 48

'-----------------------------------------------------------------------
' configuration USART2 (communication avec FAV)
RCSTA2 = $90 ' Enable serial port & continuous receive
TXSTA2 = $20 ' Enable transmit, BRGH = 0
SPBRG2 = 77 ' 9600 Baud @ 48MHz, 0,16%

' configuration USART1 (communication RS232 externe)
RCSTA1 = $90 ' Enable serial port & continuous receive
TXSTA1 = $20 ' Enable transmit, BRGH = 0
SPBRG1 = 77 ' 9600 Baud @ 48MHz, 0,16%

ANSELA = 0 ' analog off
ANSELB = 0 ' analog off
ANSELC = 0 ' analog off

TRISA = %00000100
TRISB = %10001000
TRISC = %10100100

PORTA = 0
PORTB = 0
PORTC = 0

d1 var byte
d2 var byte
d3 var byte
c1 var byte
c2 var byte
c3 var byte
n1 var byte
n2 var byte
n3 var byte
n4 var byte
c11 var byte
c12 var byte
c13 var byte
n11 var byte
n12 var byte
n13 var byte
n14 var byte
c21 var byte
c22 var byte
c23 var byte
n21 var byte
n22 var byte
n23 var byte
n24 var byte
c31 var byte
c32 var byte
c33 var byte
n31 var byte
n32 var byte
n33 var byte
n34 var byte
dgt1 var byte
dgt2 var byte
dgt3 var byte
voie var byte
led var PORTA.5
raffr var byte ' raffraichissement
value var byte
remote var bit
x var byte
GP2 VAR BYTE ' GP variable
BytesIn2 VAR BYTE[80] ' Up to 80 bytes
ByteCnt2 VAR BYTE ' Index pointer
EOM CON 10 ' EOM = "End Of Msg" terminating character
OERR2 VAR RCSTA2.1 ' Alias USART over-run bit
CREN2 VAR RCSTA2.4 ' Alias USART continuous receive enable bit
RCIF2 VAR PIR3.5 ' Alias USART received character interrupt flag bit
RCIF1 VAR PIR1.5
GP1 VAR BYTE ' GP variable
BytesIn1 VAR BYTE[80] ' Up to 80 bytes
ByteCnt1 VAR BYTE ' Index pointer
OERR1 VAR RCSTA1.1 ' Alias USART over-run bit
CREN1 VAR RCSTA1.4 ' Alias USART continuous receive enable bit


depart:
clear
high led
portc.4 = 1 ' serial to netatt
PAUSE 1000

' demande la position au démarrage
position:
portc.4 = 1
serout2 PORTC.4,32,["?0",10]
toggle led
pause 500
goto position

I can't use other than serout2 for this one because all usart ports are also used for other application.

What is wrong ?

Regards
Herve

Bruce
- 8th April 2011, 00:00
For me - it's hard to follow your code with the schematic you posted. Can you get into a bit more detail on what's physically connected & what isn't working right?

harryweb
- 8th April 2011, 08:39
HI Bruce,

OK, I will try to explain more.

here are simplified shematic and code


'************************************************* ***************
'* Name : net-master-test.pbp *
'************************************************* ***************


Include "modedefs.bas"

DEFINE OSC 48

'-----------------------------------------------------------------------
' configuration USART2 (communication avec FAV)
RCSTA2 = $90 ' Enable serial port & continuous receive
TXSTA2 = $20 ' Enable transmit, BRGH = 0
SPBRG2 = 77 ' 9600 Baud @ 48MHz, 0,16%

' configuration USART1 (communication RS232 externe)
RCSTA1 = $90 ' Enable serial port & continuous receive
TXSTA1 = $20 ' Enable transmit, BRGH = 0
SPBRG1 = 77 ' 9600 Baud @ 48MHz, 0,16%

ANSELA = 0 ' analog off
ANSELB = 0 ' analog off
ANSELC = 0 ' analog off

TRISA = %00000100
TRISB = %10001000
TRISC = %10100100

PORTA = 0
PORTB = 0
PORTC = 0

led var PORTA.5
GP2 VAR BYTE ' GP variable
BytesIn2 VAR BYTE[80] ' Up to 80 bytes
ByteCnt2 VAR BYTE ' Index pointer
EOM CON 10 ' EOM = "End Of Msg" terminating character
OERR2 VAR RCSTA2.1 ' Alias USART over-run bit
CREN2 VAR RCSTA2.4 ' Alias USART continuous receive enable bit
RCIF2 VAR PIR3.5 ' Alias USART received character interrupt flag bit
RCIF1 VAR PIR1.5
GP1 VAR BYTE ' GP variable
BytesIn1 VAR BYTE[80] ' Up to 80 bytes
ByteCnt1 VAR BYTE ' Index pointer
OERR1 VAR RCSTA1.1 ' Alias USART over-run bit
CREN1 VAR RCSTA1.4 ' Alias USART continuous receive enable bit


depart:
clear
high led
portc.4 = 1 ' serial to netatt
PAUSE 1000

' ask for position
position:
portc.4 = 1
serout2 PORTC.4,32,["?0",10]
toggle led
pause 500
goto position


'************************************************* ***************
'* Name : netatt-test.pbp *
'************************************************* ***************


Include "modedefs.bas"
define OSC 48

' - Memorisation des données EEPROM à la programmation
' à partir de l'addresse 1
EEPROM 1,[24,41,36,21,29,80]
' 1, H, Y, T, E, M, _
' à partir de l'adresse 16
' 17,18,19,20
EEPROM 16,[87,65,89,88]
' 16, W ,A ,Y, X
'-----------------------------------------------------------------------
DEFINE ADC_BITS 8 ' ADCIN resolution (Bits)
DEFINE ADC_CLOCK 7 ' ADC clock source (Frc)
DEFINE ADC_SAMPLEUS 11 ' ADC sampling time (uSec)


' configuration USART2 portb.6 (communication to net-master)
RCSTA2 = $90 ' Enable serial port & continuous receive
TXSTA2 = $20 ' Enable transmit, BRGH = 0
SPBRG2 = 38 ' 19200 Baud @ 48MHz, 0,16%


' configuration USART1 (external communication with EM1206 TIBBO)
RCSTA1 = $90 ' Enable serial port & continuous receive
TXSTA1 = $20 ' Enable transmit, BRGH = 0
SPBRG1 = 77 ' 9600 Baud @ 48MHz, 0,16%

ANSELA = 0
ANSELB = 0
ANSELC = 16 'analog on portc.4


TRISA = %10001111
TRISB = %10000001
TRISC = %10010000

PORTA = 0
PORTB = 0
PORTC = 0

led var PORTA.5
x var byte
voie var byte
nv1 var byte
nv2 var byte
nv3 var byte
nv4 var byte
what var byte
way var byte
vmax var WORD
att var word
GP2 VAR BYTE ' GP variable
BytesIn2 VAR BYTE[10] ' Up to 10 bytes
ByteCnt2 VAR BYTE ' Index pointer
EOM CON 10 ' EOM = "End Of Msg" terminating character
OERR2 VAR RCSTA2.1 ' Alias USART over-run bit
CREN2 VAR RCSTA2.4 ' Alias USART continuous receive enable bit
RCIF2 VAR PIR3.5 ' Alias USART received character interrupt flag bit
RCIF1 VAR PIR1.5
GP1 VAR BYTE ' GP variable
BytesIn1 VAR BYTE[80] ' Up to 80 bytes
ByteCnt1 VAR BYTE ' Index pointer
OERR1 VAR RCSTA1.1 ' Alias USART over-run bit
CREN1 VAR RCSTA1.4 ' Alias USART continuous receive enable bit

pause 10
clear


'********************************
' What is my way ? (0-3)
'********************************

ADCIn 16,way

IF way < 30 then
way = 48 'way 0 for 470R
goto mainloop
endif

IF way < 60 then
way = 49 'way 1 for 2K2
goto mainloop
endif

IF way < 90 then
way = 50 'way 2 for 3K9
goto mainloop
endif

way = 51 'way 3 for 10K


'*******************************************
mainloop:
IF RCIF2 then 'reception from master PIC ?
gosub RX2
endif

goto mainloop

'********* reception with USART2 (from netMaster PIC) ****
RX2:
IF RCIF2 THEN ' If RCIF2=1 there's a new character in RCREG
BytesIn2[ByteCnt2]=RCREG2 ' Yes. Then store character in array
IF BytesIn2[ByteCnt2]=10 THEN OutMsg2 ' LF (0A)
ByteCnt2=ByteCnt2+1 ' Increment element index pointer
ENDIF
IF !OERR2 THEN RX2 ' If NOT over-run, then re-cycle
CREN2 = 0 ' Yes. Over-run condition is present, so clear it
CREN2 = 1 ' Re-enable & clear over-run condition
HSEROUT2 ["Over-run reset",13,10]
return

OutMsg2:
HSEROUT2 [13,10,DEC ByteCnt2," received bytes",13,10]
HSEROUT2 [STR BytesIn2\ByteCnt2,13,10] ' Show each character
hserout2 ["time:",dec x,10]

x = x + 1

IF BytesIn2[0] = 63 and Bytesin2[1] = way THEN '?way
gosub sendata
gosub reset2
goto mainloop
endif

hserout2 ["?"]
gosub reset2
goto mainloop


reset2:
FOR GP2=0 TO ByteCnt2 ' Clear array bytes 0 to ByteCnt
BytesIn2[GP2]=0
NEXT GP2
ByteCnt2=0 ' Reset index pointer back to first element
WHILE RCIF2 ' Trash any left over characters in RCREG buffer
GP2=RCREG2 ' after EOM is received
WEND
RETURN

sendata:
toggle led
read 16,nv1 'first letter
read 17,nv2 'second
read 18,nv3 '....
read 19,nv4 '....

if att < 10 then
hserout2 ["00",dec att,nv1,nv2,nv3,nv4,10]
return
endif

if att < 100 then
hserout2 ["0",dec att,nv1,nv2,nv3,nv4,10]
return
endif

hserout2 [dec att,nv1,nv2,nv3,nv4,10] 'att value + name
return


First PIC (U5) is sending every 500mSec : serout2 PORTC.4,32,["?0",10]
to my second PIC (U1). That's working fine

After receiving this, my second PIC must answer:
-How many bytes it received
-showing each character
-How many times it received something
-A string (always the same in this case)

Please see below what I see




'taken with PICKIT serial analyser

2 received bytes 'beginning working fine (problem is below)
?0
time:0
625WAYX

2 received bytes
?0
time:1
625WAYX

2 received bytes
?0
time:2
625WAYX

2 received bytes
?0
time:3
625WAYX

2 received bytes
?0
time:4
625WAYX

2 received bytes
?0
time:5
625WAYX

2 received bytes
?0
time:6
625WAYX

2 received bytes
?0
time:7
625WAYX

2 received bytes
?0
time:8
625WAYX

2 received bytes
?0
time:9
625WAYX

2 received bytes
?0
time:10
625WAYX

2 received bytes
?0
time:11
625WAYX

2 received bytes
?0
time:12
625WAYX

2 received bytes
?0
time:13
625WAYX

2 received bytes
?0
time:14
625WAYX

2 received bytes
?0
time:15
625WAYX

2 received bytes
?0
time:16
625WAYX

2 received bytes
?0
time:17
625WAYX

2 received bytes
?0
time:18
625WAYX

2 received bytes
?0
time:19
625WAYX

2 received bytes
?0
time:20
625WAYX

2 received bytes
?0
time:21
625WAYX

2 received bytes
?0
time:22
625WAYX

2 received bytes
?0
time:23
625WAYX

2 received bytes
?0
time:24
625WAYX

2 received bytes
?0
time:25
625WAYX

2 received bytes
?0
time:2? ' Time must be 26 but it sends 2? and didn't send 625WAYX
2 received bytes ' Restarting ? what's wrong ?
?0
time:0 ' It seems that my counter time is restarting... Why ?
625WAYX

2 received bytes ' After this problem, Now it's working again...
?0 ' other problem below (time = 28)
time:1
625WAYX

2 received bytes
?0
time:2
625WAYX

2 received bytes
?0
time:3
625WAYX

2 received bytes
?0
time:4
625WAYX

2 received bytes
?0
time:5
625WAYX

2 received bytes
?0
time:6
625WAYX

2 received bytes
?0
time:7
625WAYX

2 received bytes
?0
time:8
625WAYX

2 received bytes
?0
time:9
625WAYX

2 received bytes
?0
time:10
625WAYX

2 received bytes
?0
time:11
625WAYX

2 received bytes
?0
time:12
625WAYX

2 received bytes
?0
time:13
625WAYX

2 received bytes
?0
time:14
625WAYX

2 received bytes
?0
time:15
625WAYX

2 received bytes
?0
time:16
625WAYX

2 received bytes
?0
time:17
625WAYX

2 received bytes
?0
time:18
625WAYX

2 received bytes
?0
time:19
625WAYX

2 received bytes
?0
time:20
625WAYX

2 received bytes
?0
time:21
625WAYX

2 received bytes
?0
time:22
625WAYX

2 received bytes
?0
time:23
625WAYX

2 received bytes
?0
time:24
625WAYX

2 received bytes
?0
time:25
625WAYX

2 received bytes
?0
time:26
625WAYX

2 received bytes
?0
time:27
5WAYX 'not the good answer !!!

2 received bytes
?0
time:8 'Why my counter = 8 ? It must be 28 ?
WAYX 'not the good answer !!!

received bytes 'Now there is no answer
?
time:
'no more data the pic seems sleeping ?

Any idea ? I'm working hard on it..

harryweb
- 8th April 2011, 10:26
The problem is now resolved.
Hardware
Bad 100nF capacitor !!!

Thank you