PDA

View Full Version : HSERIN2 not working on PIC18F97J60 ???



mikebar
- 26th April 2008, 12:30
Good morning gurus.

I'm experiencing a problem with HSERIN2 command using Pic18F97J60 (100 pin version)
A simple code like this, does not receive any character, but the sending of characters it work by the use of HSEROUT2:



Include "modedefs.bas" 'Inline compiler's pre-defined constants
define OSC 25 'Set the Crystal Clock speed
DEFINE HSER2_RCSTA 90h 'Set the RX Register for the Serial Port 2
DEFINE HSER2_TXSTA 20h 'Set the Serial Port2 TX enabled
DEFINE HSER2_BAUD 9600 'Set baud rate for Serial Port 2
DEFINE HSER2_SPBRG 40 'Set SPBRG directly ((((1000000*25)/9600)+32)/64)-1

define HSER1_CLROERR 1 'No Error Overflow on Serial Port 1
define HSER2_CLROERR 1 'No Error Overflow on Serial Port 2
ADCON0 = 0 'AD converter module 0 = disabled
ADCON1 = %00001111 'Set all analog pins to digital
ADCON2 = %00000000
CMCON = %00000111 'Comparator = disabled
INTCON = %00111000
INTCON2 = %10000000
INTCON3 = %00000000
RCON.7 = %00000000
T0CON = %00000000 'Timer0 disabled (see page 163)
T1CON = %00000000 'Timer1 disabled (see page 167)
T2CON = %00000000 'Timer2 disabled (see page 173)
T3CON = %00000000 'Timer3 disabled (see page 175)
T4CON = %00000000 'Timer4 disabled (see page 179)
CCP4CON = %00000000 'Capture/Compare/PWM 4 disabled (see page 181)
CCP5CON = %00000000 'Capture/Compare/PWM 5 disabled (see page 181)
CCP1CON = %00000000 'Capture/Compare/PWM 1 disabled (see page 189)
CCP2CON = %00000000 'Capture/Compare/PWM 2 disabled (see page 181)
CCP3CON = %00000000 'Capture/Compare/PWM 3 disabled (see page 181)
EECON2.5 = 0 'Ethernet Module Disabled


SSP1CON1.5 = 0 'Disable SPI 1 and set pins as GPIO
SSP2CON1.5 = 0 'Disable SPI 2 and set pins as GPIO


BAUDCON1 = %00001000 'BRG16=1; See page 304
'BAUDCON2 = %00001000

'Serial Port 1 speed setting:
SPBRGH1 = $02 '9600 bps value for serial port =650=$028A
SPBRG1 = $8A
TXSTA1 = %00100100 'Tx1 enabled,8bit,Async, see page 302
RCSTA1 = %10010000 'Rx1 enabled,8 bit,Async,No frame&overfl.err

'Port D features setting:
PSPCON.4 = 0 'Port D set to be used as GPIO

TRISG.0 = 0
TRISG.1 = 0 'PortG1 = Output (External RS232-TX2)
TRISG.2 = 1 'PortG2 = Input (External RS232-RX2)
TRISG.3 = 0
TRISG.4 = 1
TRISG.5 = 1
TRISG.6 = 1
TRISG.7 = 1
LATG = 0
syncByte var byte

MainLoop:
LED_SysAlive = ~ Led_SysAlive 'Blink a led

HSERIN2 1000, MainLoop,[syncByte] 'Does not work...
select case syncByte
case "S"
HSERIN2 250, MainLoop, [syncByte]
HSEROUT2 ["synByte=", #syncByte, 13,10]

'case = "A"
' hserin2 250, Main, [DEC syncByte]
' HSEROUT2 ["Case A=", #syncByte, 13,10]
'
'case = "B"
case else
HSEROUT2 ["syncByte=", #syncByte, 13,10]
end select
LED_CPUOverT = 0

Goto MainLoop


This code does not pass neighter the first instruction HSERIN2.
The LED just blink, I saw with a digital scope that data are on to the right pin... and nothing happen.

BUT:
removing the HSERIN2 line and putting instead the line with the Serin2:


SerIn2 PortG.2,84,1000,MainLoop,[syncByte] 'It work !!!


IT WORK!

Why the hardware serial port 2 does not work with the HSERIN2 ?
Maybe I've missed some register's settings or initialized in a bad sequence?

Thank you very much.

mister_e
- 26th April 2008, 20:34
Since you can transmit, your baudrate setting has to be right. i think it's more a library mistake...


LOC OBJECT CODE LINE SOURCE TEXT
VALUE

00000C 6E01 02333 movwf R0
00000E 500E 02334 movf R6 + 1, W
000010 6E02 02335 movwf R0 + 1
000012 6A03 02336 clrf R1 ; Start off inner loop counter at zero
000014 6A04 02337 clrf R1 + 1
02338 hserin2toloop CLRWDT?NOP ; 1 (8, 16) Keep Watchdog timer clear
M ifndef NO_CLRWDT
000016 0004 M clrwdt
M else
M nop
M endif
000018 BA9E 02339 btfsc PIR, RCIF ; 2 Check for char
00001A D00C 02340 bra HSERIN2X ; We're started
00001C 6AE8 02341 clrf WREG ; 1 For subtracts
00001E 0603 02342 decf R1, F ; 1
000020 5A04 02343 subwfb R1 + 1, F ; 1
000022 E2F9 02344 bc hserin2toloop ; 2 / 1 End of inner loop
000024 0601 02345 decf R0, F ; 1
000026 5A02 02346 subwfb R0 + 1, F ; 1
000028 E307 02347 bnc hserin2done ; 1 Timed out (C clear)
00002A 0E0C 02348 movlw low (HSERIN2TO_DELAY) ; 1 Reset inside loop counter
00002C 6E03 02349 movwf R1 ; 1
00002E 0E03 02350 movlw low ((HSERIN2TO_DELAY) >> 8) ; 1
000030 6E04 02351 movwf R1 + 1 ; 1
000032 D7F1 02352 bra hserin2toloop ; 2 End of outer loop

To me that line in red should be

BTFSC PIR3,RC2IF

How to change it... open PBPPIC18.lib file, go to line 2339


hserin2toloop CLRWDT?NOP ; 1 (8, 16) Keep Watchdog timer clear
btfsc PIR, RCIF ; 2 Check for char
bra HSERIN2X ; We're started
clrf WREG ; 1 For subtracts

and change this line to

BTFSC PIR3,RC2IF

Save the file and try again.. finger crossed... ;)

The problem (if it's the problem) also seems to be in PBPPI18L.lib @ line 2620

The way it was before, it checked for a character... but on USART1

mister_e
- 27th April 2008, 03:49
Deuh... and now it seems i've missed the 2.50 Patch :D


Software patch for PICBASIC PRO™ version 2.50.

* Adds support for PIC18F6628, 66J11, 66J16, 6723, 67J11, 8628, 86J11, 86J16, 8723 and 87J11.
* Adds Microchip USB 1.2 fixes for PIC18.
* Fixes USB for PIC18F87J50 series parts.
* Sets Adcin default clock to rc for PIC16F882, 883, 884, 886 and 887.
* Fixes Hpwm pin for PIC12F615, PIC12HV615, PIC16F616, 685, 690, 785, PIC16HV616 and 785.
* Fixes Hserin2 timeout for PIC18.
* Fixes I2C_SLOW Define for PIC18.
* Fixes operator precedence in If..Then for long PIC18 (be safe - use parenthesis.)
* Fixes Lookup2 for non-long PIC18.
http://www.melabs.com/support/patches.htm

mikebar
- 3rd May 2008, 02:10
Thank you very much, mister-e.
I've tried your solution first and then worked perfectly.
Only after this I've tried the upgrade from Melabs...

Thank you again.

mister_e
- 3rd May 2008, 02:12
GRRRRRRRRRRREAT! Thanks for the feedback.

mikebar
- 4th May 2008, 08:43
Hello guys, I'm back with another issue related to PIC18F97J60 and serial ports.
Even if the good Mister-e pointed out and actually solved the issue of HSERIN2 not working at all for that chip, seems that there is something still weird there.

Explanation:
- In order to understand if there is a byte into the port1 buffer, I poll PIR1.5 byte;
- if found it set to 1, it means there is a character. Then start HSERIN. I don't care the first character since there are 3 characters as preamble;
- If a character is found, the do some process and write outside with HSEROUT
- If no data, I'll check the register PIR3.5 to know if a character is in the buffer for the port 2.
- the same as before, if there is one immediately start HSERIN2 then some process and write outside with HSEROUT2.

Well, anything is working BUT the code that deal with HSERIN, it work only ONCE. I've tried MANY times even with a digital scope. Data are really coming to the Port 1 but the PIR1.5 is everytime 0.
I've figured out that maybe need to toggle the CREN bit. Nothing to do.
Instead the bit PIR3.5 is still working very well and all the time.
I've also putted a LED (named LED_48VFault in the code) to see WHEN the code after the checking of PIR1.5 is executed.
Since the high timeout on HSERIN (200ms) I'm able to see it flashing when it execute.
The problem is that it execute just ONCE. The first time that I send some command to the Port1, it flash and anything is working. Since the second time, no more working but the Port2 it work regularly and as expected.

Below is the code:



Include "modedefs.bas" 'Inline compiler's pre-defined constants
define OSC 25 'Set the Crystal Clock speed

define HSER_CLROERR 1 'No Error Overflow on Serial Port 1
define HSER2_CLROERR 1 'No Error Overflow on Serial Port 2

ADCON0 = 0 'AD converter module 0 = disabled
ADCON1 = %00001111 'Set all analog pins to digital
ADCON2 = %00000000
CMCON = %00000111 'Comparator = disabled
INTCON = %00111000
INTCON2 = %10000000
INTCON3 = %00000000
RCON.7 = %00000000
T0CON = %00000000 'Timer0 disabled (see page 163)
T1CON = %00000000 'Timer1 disabled (see page 167)
T2CON = %00000000 'Timer2 disabled (see page 173)
T3CON = %00000000 'Timer3 disabled (see page 175)
T4CON = %00000000 'Timer4 disabled (see page 179)
CCP4CON = %00000000 'Capture/Compare/PWM 4 disabled (see page 181)
CCP5CON = %00000000 'Capture/Compare/PWM 5 disabled (see page 181)
CCP1CON = %00000000 'Capture/Compare/PWM 1 disabled (see page 189)
CCP2CON = %00000000 'Capture/Compare/PWM 2 disabled (see page 181)
CCP3CON = %00000000 'Capture/Compare/PWM 3 disabled (see page 181)
EECON2.5 = 0 'Ethernet Module Disabled


SSP1CON1.5 = 0 'Disable SPI 1 and set pins as GPIO
SSP2CON1.5 = 0 'Disable SPI 2 and set pins as GPIO

BAUDCON1 = %00001000 'BRG16=1; See page 304
BAUDCON2 = %00001000

'Serial Port 1 speed setting:
SPBRGH1 = $00 '115200 bps for serial port 1 =53 ($35)
SPBRG1 = $35

'Serial Port 2 speed setting:
SPBRGH2 = $00 '115200 bps for serial port 2 =53 ($35)
SPBRG2 = $35

TXSTA1 = %00100100 'Tx1 enabled,8bit,Async, see page 302
RCSTA1 = %10010000 'Rx1 enabled,8 bit,Async,No frame&overfl.err
'See page 303

TXSTA2 = %00100100 'Tx2 enabled,8bit,Async,Hi-speed see page 302
RCSTA2 = %10010000 'Rx2 enabled,8 bit,Async,Hi-Speed
'No frame&overfl.err (See page 303)


'Port D features setting:
PSPCON.4 = 0 'Port D set to be used as GPIO

TRISG.0 = 0
TRISG.1 = 0 'PortG1 = Output (External RS232-TX2)
TRISG.2 = 1 'PortG2 = Input (External RS232-RX2)
TRISG.3 = 0
TRISG.4 = 1
TRISG.5 = 1
TRISG.6 = 1
TRISG.7 = 1
LATG = 0

syncByte var byte
ADDRByte var byte
CmdType var byte
CmdID var byte
preamble var byte
PortAddr var byte
LED_SysAlive var PORTJ.0
LED_48VFault var PORTJ.1

MainLoop:
LED_SysAlive = ~ Led_SysAlive 'Blink a led as fast is possible
Gosub ReadPort1
Gosub ReadPort2
Goto MainLoop

ReadPort1:
'The following lines are commented because do not produce any visible results.
'if RCSTA1.1=1 then
'RCSTA1.4=0
'RCSTA1.4=1
'endif
'RCSTA1 = %10010000


IF PIR1.5 = 1 then 'Data ready on Port1:
PortAddr = 1
preamble1:
LED_48VFault = 1
hserin 200, NoData1,[syncByte]
if syncByte = "-" then
goto preamble1
endif
IF syncByte = "U" then
hserin 200, NoData1,[ADDRByte] 'Expect the address of the Unit
IF ADDRByte = 1 Then
'Data stream start coming now to this unit
hserin 200, NoData1,[CmdType, CmdID, preamble]
Select Case CmdID
Case = 1
'Do something
Gosub Answer
Case = 2
'Do something else
Gosub Answer
End Select
End IF
End IF
End IF

NoData1:
syncByte = 0
CmdType = 0
LED_48VFault = 0
Return


ReadPort2:
IF PIR3.5 = 1 then 'Data ready on Port2:
PortAddr = 2
preamble2:
hserin2 200, NoData2,[syncByte]
if syncByte = "-" then
goto preamble2
endif
IF syncByte = "U" then
hserin2 200, NoData2,[ADDRByte] 'Expect the address of the Unit
IF ADDRByte = 1 Then
'Data stream start coming now to this unit
hserin2 200, NoData2,[CmdType, CmdID, preamble]
Select Case CmdID
Case = 1
'Do something
Gosub Answer
Case = 2
'Do something else
Gosub Answer
End Select
End IF
End IF
End IF

NoData2:
syncByte = 0
CmdType = 0
Return

Answer:
IF PortAddr = 1 Then
HSEROUT ["U", 1, CmdType, CmdID]
Else
HSEROUT2 ["U", 1, CmdType, CmdID]
End IF
Return


Some clue?

Thank you very much to all

mister_e
- 4th May 2008, 10:09
I haven't go really deep in your code, but usually when you deal with HSERIN(2)/HSEROUT(2), you must use the DEFINEs instead of writing to the PIC registers, unless PBP will assume the default 2400 bauds.

mikebar
- 4th May 2008, 10:23
I haven't go really deep in your code, but usually when you deal with HSERIN(2)/HSEROUT(2), you must use the DEFINEs instead of writing to the PIC registers, unless PBP will assume the default 2400 bauds.

Ok, thank you for the suggestion.

I'll try right now.

mikebar
- 4th May 2008, 16:20
Another sleepless night.

Nothing to do Mister-E. Still the same behaviour even with your suggestions.

Some hint more:
In the segment of code where I'm talking about "Do something" and "Do something else", really I'm using the instructions I2CRead and I2CWrite.

Seems that if I call the sequence:
HSERIN
HSEROUT
I2CREAD
then the HSERIN does not work anymore

Again,
If I remove the checking of the PIR1.5 bit, EVERYTHING IS WORKING !!!!!!
As per the above code that DOES NOT WORK:

ReadPort1:
....
IF PIR1.5 = 1 Then
HSERIN 200, NoData1, [syncByte]
.....
.....
EndIF

If I remove the line IF PIR1.5 = 1 (and his EndIF, of course) even using:
HSERIN 10,NoData1,[syncByte] 'ALL IS WORKING FINE, but I miss 10ms each time !!!

How this is possible?


Again:
I'm using Tibbo EM200 that suppose to talk with the serial port 1. His outputs are at CMOS level (HIGH when idle) and I'm using HSERIN. I should consider something in this case?

Again:
I've tried SERIN2. Much worste.
I used:
SERIN2 PortC.7, 16390, 200, NoData1, [syncByte] 'Inverted mode 38400
and even
SERIN2 PortC.7, 6, 200, NoData1, [syncByte] 'True mode 38400
In both cases, the program pass the checking about PIR1.5 but then is supposed to go to NoData1 in case of no data in 200ms. Well, I don't know where it go, since the LED_48VFault is supposed to be switched off when the program go to NoData1. Instead it still remain switched ON... and the remaining part of the program is still working!!!

Any idea?
Should I hire an exorcist?

Thank you.