PDA

View Full Version : strange problem with HSERIN



Beginner
- 2nd September 2010, 07:54
I'm trying to transfer a working program from a 16f887 to a 18f85j11.
I'm working with PBP 2.60.

The program works well on the 16f887, but i'm having a problem with the serial communication on the 18f85j11.

The program seems to hang, or get really slow when I insert the HSerin command into the program. Even when the program NEVER jumps to the RS232 Label the program is really slow. When I comment out the Hserin (and still don't jump to the rs232 label) , the program works nicely.

What can the problem be?

Thanks in advance.






RS232:
DIRECTION = 0 ' send or receive bit
HSerin 1,GO,[WAIT("$"),BUFF]
IF BUFF = ADRESS Then
OK = 1
For I = 1 TO AANTAL
HSerin [DATI]
BUFFER(I) = DATI
Next
EndIF

Bruce
- 2nd September 2010, 11:20
If you comment out the entire routine, or never jump to it, it should have zero effect on anything. If your code falls-through into the routine, then it may, but it's tough to help diagnose without seeing all your code, and knowing what you have set for config options, hardware, etc.

The more info you post, the more likely you are to get help.

Beginner
- 2nd September 2010, 12:15
Well, thats the strange thing about this.

It does not matter if I jump to the routine or not.
It only matters if I comment out the HSERIN command.

Can It be something with the initialisation of the chip and the USART?

Bruce
- 2nd September 2010, 12:40
That is strange. Can you post more information as mentioned above?

malc-c
- 2nd September 2010, 12:43
I had issues with serial comms when moving code from an 16F877A to an 18F4580. Turns out it was due to the crystal. I had been using a 20Mhz with an OSC 48 defined - when used with the 18F the program would still run but serial comms at 115200 was garbage - following advice from forum members I changed the xtal to 12Mhz and ran with the HS_PLL defined and same OSC defined and that worked for me.

As Bruce stated, post up your complete code or at least the defines and what Xtal you are using

Beginner
- 2nd September 2010, 12:51
I will post the code tomorrow morning, I do not have it available where I'm at the moment.

Beginner
- 3rd September 2010, 07:39
This is the program code. the hserin2 is only used. I'm using a 20Mhz OSC.

the tx_enable is connected to TG0 (pin5)
the rx2 is connected to RG2 (pin7)
the tx2 is connected to RG1 (pin6)




'PIC18F85J11 mainboard grafisch display

DEFINE HSER_TXSTA 24H
DEFINE HSER_RCSTA 90H
DEFINE HSER_SPBRG 4
DEFINE HSER_CLROERR 1

DEFINE HSER2_TXSTA 24H
DEFINE HSER2_RCSTA 90H
DEFINE HSER2_SPBRG 4
DEFINE HSER2_CLROERR 1

'DEFINE ADC_BITS 8
'DEFINE ADC_CLOCK 3
'DEFINE ADC_SAMPLEUS 50

DEFINE OSC 20

DEFINE LCD_DREG PORTE
DEFINE LCD_DBIT 0
DEFINE LCD_RSREG PORTF
DEFINE LCD_RSBIT 4
DEFINE LCD_EREG PORTG
DEFINE LCD_EBIT 4
DEFINE LCD_BITS 8
DEFINE LCD_LINES 2
DEFINE LCD_COMMANDUS 2000
DEFINE LCD_DATAUS 50


ADCON1 = 15
ADCON0 = 0
CMCON = 7
T0CON = %11000011
INTCON = %10100000
INTCON2 = %00000100

TRISA = %10111111
TRISB = %00000001
TRISC = %10000000
TRISD = %01000000
TRISE = %00000000
TRISF = %00000110
TRISG = %00000000
TRISH = %10110100
TRISJ = %00000001

INP_OUTP21 VAR PORTA.0
INP_OUTP22 VAR PORTA.1
INP_OUTP23 VAR PORTA.2
INP_OUTP24 VAR PORTA.3
INP_OUTP25 VAR PORTA.4
INP_OUTP26 VAR PORTA.5
XTAL_0 VAR PORTA.6
XTAL_1 VAR PORTA.7

INP_OUTP27 VAR PORTB.0
IR_DATA VAR PORTB.1
INP_OUTP28 VAR PORTB.2
INP_OUTP29 VAR PORTB.3
INP_OUTP30 VAR PORTB.4
INP_OUTP31 VAR PORTB.5
PRG_CLK VAR PORTB.6
PRG_DTA VAR PORTB.7

INP_OUTP32 VAR PORTC.0
RES_01 VAR PORTC.1
RES_02 VAR PORTC.2
SCL_ROM VAR PORTC.3
SDA_ROM VAR PORTC.4
RES_03 VAR PORTC.5
RF_TX VAR PORTC.6
RF_RX VAR PORTC.7

RES_04 VAR PORTD.0
RES_05 VAR PORTD.1
RES_06 VAR PORTD.2
RES_07 VAR PORTD.3
INP_OUTP01 VAR PORTD.4
INP_OUTP02 VAR PORTD.5
INP_OUTP03 VAR PORTD.6
INP_OUTP04 VAR PORTD.7

D0_DISPLAY VAR PORTE.0
D1_DISPLAY VAR PORTE.1
D2_DISPLAY VAR PORTE.2
D3_DISPLAY VAR PORTE.3
D4_DISPLAY VAR PORTE.4
D5_DISPLAY VAR PORTE.5
D6_DISPLAY VAR PORTE.6
D7_DISPLAY VAR PORTE.7

DISPL_EN1 VAR PORTF.1
DISPL_EN2 VAR PORTF.2
RES_08 VAR PORTF.3
DISPL_RS VAR PORTF.4
RES_09 VAR PORTF.5
RES_10 VAR PORTF.6
RES_11 VAR PORTF.7

TX_ENABLE VAR PORTG.0
TX_RS485 VAR PORTG.1
RX_RS485 VAR PORTG.2
RES_12 VAR PORTG.3
DISPL_EN VAR PORTG.4

INP_OUTP05 VAR PORTH.0
INP_OUTP06 VAR PORTH.1
INP_OUTP07 VAR PORTH.2
INP_OUTP08 VAR PORTH.3
INP_OUTP09 VAR PORTH.4
INP_OUTP10 VAR PORTH.5
INP_OUTP11 VAR PORTH.6
INP_OUTP12 VAR PORTH.7

INP_OUTP13 VAR PORTJ.0
INP_OUTP14 VAR PORTJ.1
INP_OUTP15 VAR PORTJ.2
INP_OUTP16 VAR PORTJ.3
INP_OUTP17 VAR PORTJ.4
INP_OUTP18 VAR PORTJ.5
INP_OUTP19 VAR PORTJ.6
INP_OUTP20 VAR PORTJ.7

I VAR BYTE
K VAR BYTE
TELLER VAR BYTE
ADRESS VAR BYTE
AANTAL VAR BYTE
LCDBUFI VAR BYTE [6]
LCDBUFO VAR BYTE [6]
BUFFERI VAR BYTE [6]
BUFFERO VAR BYTE [6]
STARTCHAR VAR BYTE
DATO VAR BYTE
DATI VAR BYTE
BUFF VAR BYTE
OK VAR BIT


Pause 500

Clear
FLAGS = 0

LCDOut $FE,$1, "DISPLAY"
LCDOut $FE,$C0, "TEST"

Pause 500

TX_ENABLE = 1
STARTCHAR = "$"


BEGIN:
For K = 240 TO 240
teller = teller + 1
ADRESS = K
AANTAL = 1
buffero(K) = teller
GoSub WEGSCHRIJVEN
PauseUs 200
ADRESS = K
AANTAL = 1
GoSub OPHALEN
IF OK = 1 Then
lcdbufO(K) = bufferi(K)
LCDBUFI(K) = BUFFERO(K)
GoSub LCD
OK = 0
GoTo HIER1
EndIF
LCDBUFO(K) = 0
LCDBUFI(K) = 0
GoSub LCD
HIER1: Next

GoTo BEGIN

'SUBROUTINES

WEGSCHRIJVEN:

DATO = STARTCHAR : GoSub CHAROUT
DATO = ADRESS : GoSub CHAROUT
DATO = "W" : GoSub CHAROUT
For I = 1 TO AANTAL
DATO = BUFFERO(I) : GoSub CHAROUT
Next
Return

OPHALEN:
OK = 0
DATO = STARTCHAR : GoSub CHAROUT
DATO = ADRESS : GoSub CHAROUT
DATO = "R" : GoSub CHAROUT
PauseUs 100
TX_ENABLE = 0
HSerin2 1,GO,[WAIT("$"),BUFF]
IF BUFF = ADRESS Then
OK = 1
For I = 1 TO AANTAL
HSerin2 [DATI]
BUFFERi(I) = DATI
Next
EndIF
GO:
PauseUs 50
TX_ENABLE = 1
Return
CHAROUT:
TX_ENABLE = 1
HSerout2 [DATO]
Return


LCD:

GoSub USE_LCD1
LCDOut $FE,$80, "ADRES 1 2"
LCDOut $FE,$94, " 3 4 5"
LCDOut $FE,$C0, "DATA ",HEX LCDBUFO(1)," ",HEX LCDBUFO(2)
LCDOut $FE,$D4, HEX LCDBUFO(3)," ",LCDBUFO(4)," ",LCDBUFO(5)
GoSub USE_LCD2
LCDOut $FE,$80, "DATA ",HEX LCDBUFI(1)," ",HEX LCDBUFI(2)
LCDOut $FE,$94, HEX LCDBUFI(3)," ",LCDBUFI(4)," ",LCDBUFI(5)

Return

USE_LCD1:
Input DISPL_EN1
Low DISPL_EN2
Return

USE_LCD2:
Low DISPL_EN1
Input DISPL_EN2
Return

USE_BothLCD:
Input DISPL_EN1
Input DISPL_EN2
Return

End

mackrackit
- 3rd September 2010, 10:28
This is the program code. the hserin2 is only used. I'm using a 20Mhz OSC.
HSERIN2 is only used for chips with two hardware serial ports/

Beginner
- 3rd September 2010, 12:37
HSERIN2 is only used for chips with two hardware serial ports/

The 18f85j11 has 2 serial ports. 1 EUSART and 1 AUSART. I should be able to use this command.

mackrackit
- 3rd September 2010, 13:03
OOPPSS

Maybe this???

The pins of the AUSART module are multiplexed with
the functions of PORTG (RG1/TX2/CK2 and
RG2/RX2/DT2, respectively). In order to configure
these pins as an AUSART:
• bit SPEN (RCSTA2<7>) must be set (= 1)
• bit TRISG<2> must be set (= 1)
• bit TRISG<1> must be cleared (= 0) for
Asynchronous and Synchronous Master modes

Archangel
- 4th September 2010, 02:59
Hi Beginner,
One thing to check is the port's idle state. HSER* requires the port to idle HIGH so you will need a pull up resistor or the port will likely hang the program.

malc-c
- 4th September 2010, 10:32
One thing to check is the port's idle state. HSER* requires the port to idle HIGH so you will need a pull up resistor or the port will likely hang the program.

Joe,

Thanks for the heads up on that, but most of the samples I've come across have something like


IF RCIF THEN GOSUB xxxxx
or
IF RCIF=1 THEN GOSUB xxxxx


If the pins are tied high then this will cause the code to constantly loop to the sections that deal with the comms. would it not be better to tie the pins to ground via resistors ?

Bruce
- 5th September 2010, 00:46
The USART RX pin should definitely idle high, but this routine has some major issues;


BEGIN:
For K = 240 TO 240 ' <-- BIG problem here
teller = teller + 1
ADRESS = K
AANTAL = 1
buffero(K) = teller ' <-- there is no buffero(240)
GoSub WEGSCHRIJVEN
PauseUs 200
ADRESS = K
AANTAL = 1
GoSub OPHALEN
IF OK = 1 Then
lcdbufO(K) = bufferi(K) ' <-- same issue here
LCDBUFI(K) = BUFFERO(K) ' <-- and here
GoSub LCD
OK = 0
GoTo HIER1
EndIF
LCDBUFO(K) = 0
LCDBUFI(K) = 0
GoSub LCD
HIER1: Next
K = 240, but your largest array variables are only 6 bytes. I.E. buffero(0) to buffero(5) would be the last byte available in your buffero array.

malc-c
- 5th September 2010, 10:58
Uhmm... just looked at the schematic in the EasyPIC5 manual, the RX line is connected direct to the R1Out pin on the Max232 - the TX pin to the T1in pin which is tied to Vcc via 1K resistor. I assume the MAX232 will handle the logic level of the RX pin ?

Bruce
- 5th September 2010, 15:20
I assume the MAX232 will handle the logic level of the RX pin ?
Yes it will. The MAX232 has an internal pull-down on the R1in side of the inverter http://www.rentron.com/Files/bMAX232.gif

Beginner
- 6th September 2010, 07:25
The USART RX pin should definitely idle high, but this routine has some major issues;


BEGIN:
For K = 240 TO 240 ' <-- BIG problem here
teller = teller + 1
ADRESS = K
AANTAL = 1
buffero(K) = teller ' <-- there is no buffero(240)
GoSub WEGSCHRIJVEN
PauseUs 200
ADRESS = K
AANTAL = 1
GoSub OPHALEN
IF OK = 1 Then
lcdbufO(K) = bufferi(K) ' <-- same issue here
LCDBUFI(K) = BUFFERO(K) ' <-- and here
GoSub LCD
OK = 0
GoTo HIER1
EndIF
LCDBUFO(K) = 0
LCDBUFI(K) = 0
GoSub LCD
HIER1: Next
K = 240, but your largest array variables are only 6 bytes. I.E. buffero(0) to buffero(5) would be the last byte available in your buffero array.

I tried to change the For K = 240 TO 240 to For K = 0 TO 5 , but it does not make any difference.

The chip still keeps running VERY slow (like 25 seconds startup time) when I'm using the HSERIN command.

---------------------

The RS232 schematic is ok, I'm using the same for other PCB's with 16f887 and 16f882 chips where it works fine.
----------------------

Also changing the bit TRISG<2> = 1 does not solve this problem.

Any more suggestions?

mat janssen
- 8th September 2010, 17:53
I did some tests with that chip and also using a rs485 chip to test. You talked about rs232 and a direction (TX_ENABLE) and when I look at the code I think it's RS485, but that is not an issue.
I also discovered that indeed the chip will not startup when using HSERIN and the RX pin of the uart is high.
I put a 10K resistor from the RX pin of the picchip to ground and now the program is starting up normaly.
I hope it will help you.