PDA

View Full Version : SEROUT2 broken on 18F4620 ?



mr.sneezy
- 29th May 2010, 13:08
My kicked Black Cat is back...
I ported my BMP085 test code from a 18F1220 to a 18F4620, and after reconfiguring the config directives, and checking port pins correct etc etc I expected my code to run. Well it did, except I can not get SEROUT2 to work on the 18F4620.
It's like the PBP SEROUT2 command is broken for this PIC.

I've changed pins three times now, and my 'I'm Alive' LED will flash away perfectly normally on it's own pin OR on the pin that I tried out SEROUT2 on (I swapped the SEROUT pin and LED pin in my code). So the pin is fine because it can drive a LED, but I get no serial data out of it when used for SEROUT2 instead. Weird hey.

Anybody know if there are any bugs with the 18F4620 and SEROUT2 ?

I'd have thought not, but before a spend a day trying to work out why, I thought I'd ask here to save my small amount of remaining sanity.

I also checked my OSC clock speed by measuring 500mS LED pulses on my CRO, yep it's spot on time. I also put a 18F1220 back on my test board, and yes SEROUT2 to my serial LCD works fine, so my Serial LCD is still OK.

I need a few Panadols and a lay down,
Martin

The first part of my code up to just after the first SEROUT2's in included below.


'Config Directive settings for MPASM (fuses) for 18F4620

@ __CONFIG _CONFIG1H, _IESO_OFF_1H & _FCMEN_OFF_1H & _OSC_INTIO7_1H
@ __CONFIG _CONFIG2L, _PWRT_ON_2L & _BOREN_OFF_2L
@ __CONFIG _CONFIG2H, _WDT_OFF_2H & _WDTPS_32_2H
@ __CONFIG _CONFIG3H, _MCLRE_OFF_3H & _PBADEN_OFF_3H
@ __CONFIG _CONFIG4L, _DEBUG_OFF_4L & _LVP_OFF_4L & _STVREN_OFF_4L
@ __CONFIG _CONFIG5L, _CP0_OFF_5L & _CP1_OFF_5L & _CP2_OFF_5L & _CP3_OFF_5L
@ __CONFIG _CONFIG5H, _CPB_OFF_5H & _CPD_OFF_5H
@ __CONFIG _CONFIG6L, _WRT0_OFF_6L & _WRT1_OFF_6L & _WRT2_OFF_6L & _WRT3_OFF_6L
@ __CONFIG _CONFIG6H, _WRTC_OFF_6H & _WRTB_OFF_6H & _WRTD_OFF_6H
@ __CONFIG _CONFIG7L, _EBTR0_OFF_7L & _EBTR1_OFF_7L & _EBTR2_OFF_7L & _EBTR3_OFF_7L
@ __CONFIG _CONFIG7H, _EBTRB_OFF_7H

DEFINE OSC 8 '8Mhz clock used.

' Define some constants if needed

' Software Defines (variables and pins)
Cal_table var word[11] '11 word array to store calibration data
lUpres var long 'Long variable for Uncompensated Pressure
lPres var Long 'Long variable for Compensated Pressure
lTemp_Var1 Var long 'Long temporary variable
X1 var Long
X2 var Long
X3 var Long
B3 var long
B4 var long
B5 var long
B6 var Long
B7 var long

lAC1 var Long 'Long variables for cal values actually negative in my sensor
lAC2 var long
lAC3 var Long
lAC4 var Long
lMB var Long
lMC var Long


bTemp_Var1 var byte 'Byte temp variable
wTemp_Var1 var Word 'Word temp variable
wTemp_Var2 var word 'Word temp variable
lUTemp var long 'Uncompensated temp reading

lCTemp var Long 'Compensated temperature x10 (1/10th of C)
' UTemp var word 'Uncompensated temp reading
' UPres var word 'uncompensated pressure reading
i2c_Reg var Byte 'variable for target i2c register address

CPIN var PortB.0 ' I2C clock pin
DPIN var PortB.1 ' I2C data pin
SO Var PortD.0 'Serial out pin
LED var PortA.1 'Indicator LED, via 500ohm to +3.3V

'Alias's for calibration data in the sensor to match the Bosch parameter list names
AC1 var Cal_table[0] '
AC2 var Cal_table[1] 'BMP085 has 11 16bit values stored in EEPROM
AC3 var Cal_table[2] 'First byte is at $AA last at $BF, two bytes per cal value
AC4 var Cal_table[3] 'Lowbyte is MSB (e.g $AA), Highbyte is LSB (e.g. $AB)
AC5 var Cal_table[4] 'some values are signed (not AC4, AC5, AC6)
AC6 var Cal_table[5]
B1 var Cal_table[6]
B2 var Cal_table[7]
MB var Cal_table[8]
MC var Cal_table[9]
MD var Cal_table[10]

' Initialise Processor - check for each PIC type
' -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
ADCON1 = %00001111 'Turn off all AD's
' OSCCON = %01100111 'set INTRC to 4 MHZ
OSCCON = %01110111 'set INTRC to 8 MHZ
' OSCTUNE = 0 'OSC trim set to Null

' Include "modedefs.bas" ' Include serial modes

' Set initial state of port pins as Input or Output

TRISA = %11111100 'Input(0 = output, 1 = Input)
TRISB = %11111100 '
TRISC = %11111111
TRISD = %11111111
TRISE = %11111111

' PIC initialization code
Low So 'Must start low, or you get rubbish on the LCD at PIC boot up.
Low LED 'LED on
pause 500 'led on for x seconds
High LED 'LED off
Pause 500
Low LED 'LED on
pause 500 'led on for x seconds
High LED 'LED off

Serout2 SO,16780,[$FE,$01] ' Clear LCD & home LCD cursor.
pause 10 ' wait for LCD to catch up
Serout2 SO,16780,[" FrSky Vario "] ' Serial print
Serout2 SO,16780,[$FE,$C0] ' Shift cursor to line2
Serout2 SO,16780,[" Development Jig "] ' Serial print
Pause 5000 'wait three seconds

Kamikaze47
- 29th May 2010, 13:41
Might not make a difference, but try adding:

CMCON=%00000111

Acetronics2
- 29th May 2010, 13:44
Hi, Sneezy

your :eek: error is here :



TRISE = %11111111


see datasheet @ PORTD Chapter ( $9.4 ... I think )

Alain

Kamikaze47
- 29th May 2010, 13:58
Nicely spotted Acetronics.

Changing to TRISE=%00000111 should do the trick. (Or just remove the line altogether since %0000111 is it's power on / reset value anyway.)

mr.sneezy
- 30th May 2010, 01:28
OK, I'll give that a bash shortly (when my first morning coffee has taken effect).

In my defense though, I originally had the Serout2 working on PortA.0 and it wouldn't work there, and I did have the TRIS bits set for Outputs at that time. After all, the Serout2 code was working fine on the 18F1220 and was on PortA.0.

After that (portA) I tried PortE.0 and PortD.0, but didn't swap the TRIS bits, that how the code was left.
Doesn't PBP force the pin to an output anyway even if not set in the TRIS statement ?

Talk to you soon.

sinoteq
- 30th May 2010, 02:14
Doesn't PBP force the pin to an output anyway even if not set in the TRIS statement ?


Normally PBP takes care of it but you enabled Parallel Slave Port by using TRISE and a few extra "1" By doing this you kill the PortD function of the PortD port and make it to a 8 bit super special data bus and PBP have absolutly no idea.

It is really good to make your own TRIS statements you were just a bit unlucky :)

mr.sneezy
- 30th May 2010, 06:56
OK, I've spent about three hours on this now, and I have made some progress, and no, all the suggestions above didn't turn out to be the cause, I tried all the Tris changes suggested, then rem'd them all out as well.
However I have not pin-pointed the exact problem yet... (and my caffine level has dropped to 'critical', so I must take a break)

I stripped my code down to nothing but my configs, a LED flash and a couple of Serout2 text strings, and put the Serout2 back on PortA. Still it failed to Serout...

Then I stripped out my configs, and unremarked the defaults in PBP's 18F4620 inc file (made changes to OSC and MCLRE to suit me). And.... Serout2 came back to life.

I then put my configs back, and rem'd out the INC files entries. Serout2 dies (LED still flashes correctly).
So unless I'm being tricked somehow, by me being very pedantic with my list of configs I've broken something very subtle somehow.

Here is my configs, and the PBP defaults. If anyone knows which of my configs could hurt PBP compiler please let me know.... To try each config in turn randomly will take ages.


'My Config Directive settings for MPASM (fuses) for 18F4620 (that don't work with Serout2)

@ __CONFIG _CONFIG1H, _IESO_OFF_1H & _FCMEN_OFF_1H & _OSC_INTIO7_1H
@ __CONFIG _CONFIG2L, _PWRT_ON_2L & _BOREN_OFF_2L
@ __CONFIG _CONFIG2H, _WDT_OFF_2H & _WDTPS_32_2H
@ __CONFIG _CONFIG3H, _MCLRE_OFF_3H & _PBADEN_OFF_3H
@ __CONFIG _CONFIG4L, _DEBUG_OFF_4L & _LVP_OFF_4L & _STVREN_OFF_4L
@ __CONFIG _CONFIG5L, _CP0_OFF_5L & _CP1_OFF_5L & _CP2_OFF_5L & _CP3_OFF_5L
@ __CONFIG _CONFIG5H, _CPB_OFF_5H & _CPD_OFF_5H
@ __CONFIG _CONFIG6L, _WRT0_OFF_6L & _WRT1_OFF_6L & _WRT2_OFF_6L & _WRT3_OFF_6L
@ __CONFIG _CONFIG6H, _WRTC_OFF_6H & _WRTB_OFF_6H & _WRTD_OFF_6H
@ __CONFIG _CONFIG7L, _EBTR0_OFF_7L & _EBTR1_OFF_7L & _EBTR2_OFF_7L & _EBTR3_OFF_7L
@ __CONFIG _CONFIG7H, _EBTRB_OFF_7H


PBP's configs, that do work.

INCLUDE "P18F4620.INC" ; MPASM Header
__CONFIG _CONFIG1H, _OSC_INTIO7_1H & _FCMEN_OFF_1H & _IESO_OFF_1H
__CONFIG _CONFIG2H, _WDT_ON_2H & _WDTPS_512_2H
__CONFIG _CONFIG3H, _CCP2MX_PORTC_3H & _PBADEN_OFF_3H & _LPT1OSC_OFF_3H

& _MCLRE_OFF_3H
__CONFIG _CONFIG4L, _STVREN_ON_4L & _LVP_OFF_4L & XINST_OFF_4L

My code stripped down

' -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
'Config Directive settings for MPASM (fuses) for 18F4620

@ __CONFIG _CONFIG1H, _IESO_OFF_1H & _FCMEN_OFF_1H & _OSC_INTIO7_1H
@ __CONFIG _CONFIG2L, _PWRT_ON_2L & _BOREN_OFF_2L
@ __CONFIG _CONFIG2H, _WDT_OFF_2H & _WDTPS_32_2H
@ __CONFIG _CONFIG3H, _MCLRE_OFF_3H & _PBADEN_OFF_3H
@ __CONFIG _CONFIG4L, _DEBUG_OFF_4L & _LVP_OFF_4L & _STVREN_OFF_4L
@ __CONFIG _CONFIG5L, _CP0_OFF_5L & _CP1_OFF_5L & _CP2_OFF_5L & _CP3_OFF_5L
@ __CONFIG _CONFIG5H, _CPB_OFF_5H & _CPD_OFF_5H
@ __CONFIG _CONFIG6L, _WRT0_OFF_6L & _WRT1_OFF_6L & _WRT2_OFF_6L & _WRT3_OFF_6L
@ __CONFIG _CONFIG6H, _WRTC_OFF_6H & _WRTB_OFF_6H & _WRTD_OFF_6H
@ __CONFIG _CONFIG7L, _EBTR0_OFF_7L & _EBTR1_OFF_7L & _EBTR2_OFF_7L & _EBTR3_OFF_7L
@ __CONFIG _CONFIG7H, _EBTRB_OFF_7H

' DEFINE OSC 8 '8Mhz clock used.

SO Var PortA.2 'Serial out pin
LED var PortA.1 'Indicator LED, via 500ohm to +3.3V


' Initialise Processor - check for each PIC type
' -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
' ADCON1 = %00001111 'Turn off all AD's
OSCCON = %01100111 'set INTRC to 4 MHZ
' OSCCON = %01110111 'set INTRC to 8 MHZ
' OSCTUNE = 0 'OSC trim set to Null
' Set initial state of port pins as Input or Output

' TRISA = %11111100 'Input(0 = output, 1 = Input)
' TRISB = %11111100 '
' TRISC = %11111111
' TRISD = %11111110
' TRISE = %11111111

' PIC initialization code
' Low So 'Must start low, or you get rubbish on the LCD at PIC boot up.
' Gosub Alive
Low LED 'LED on
pause 500 'led on for x seconds
High LED 'LED off
Pause 500
Low LED 'LED on
pause 500 'led on for x seconds
High LED 'LED off

Serout2 SO,16780,[$FE,$01] ' Clear LCD & home LCD cursor.
pause 10 ' wait for LCD to catch up
Serout2 SO,16780,[" FrSky Vario "] ' Serial print
Serout2 SO,16780,[$FE,$C0] ' Shift cursor to line2
Serout2 SO,16780,[" Development Jig "] ' Serial print
Pause 5000 'wait three seconds

End

Kamikaze47
- 30th May 2010, 07:06
If the default configs work, is there any reason you cant just use them? If there are any configs that you definitely need to change from default, then simply change them one at a time so you know if one of them breaks it.

mr.sneezy
- 30th May 2010, 12:06
I like to know whats going on, so I did it the hard way...

This config line kills Serout2 functionality.
@ __CONFIG _CONFIG4L, _DEBUG_OFF_4L & _LVP_OFF_4L & _STVREN_OFF_4L

This line restores it.
@ __CONFIG _CONFIG4L, _STVREN_ON_4L & _LVP_OFF_4L & _XINST_OFF_4L

Drilling down to individual config parameters, if I add this one to my original list Serout2 works again.
& _XINST_OFF_4L

The datasheet refers to this as an Extended Instruction set enabling bit. I read a bit about it, and it's use is way beyond my comprehension.

What the ?
And why does it just break the Serout2 command and not everything else in PBP too ?

It's days like these I really wonder why we bothered coming out of caves and inverting the wheel anyway, it would have been much less pain just staying in a nice warm cave :confused:

Acetronics2
- 30th May 2010, 12:53
Hi, Sneezy

Here's the reason ...

PBP defaults :


INCLUDE "P18F4520.INC" ; MPASM Header
;__CONFIG _CONFIG1H, _OSC_HS_1H & _FCMEN_OFF_1H & _IESO_OFF_1H
;__CONFIG _CONFIG2H, _WDT_ON_2H & _WDTPS_512_2H
;__CONFIG _CONFIG3H, _CCP2MX_PORTC_3H & _PBADEN_OFF_3H & _LPT1OSC_OFF_3H & _MCLRE_ON_3H
;__CONFIG _CONFIG4L, _STVREN_ON_4L & _LVP_OFF_4L & _XINST_OFF_4L


so, "PBP" is supposed to clear this config bit ... but you remmed those lines :

BUT ...

MPASM defaults it to OFF ...

Sooooo, What happend ??? how did it turn on by itself ??? :rolleyes:

Conclusion ... do not forget to explicitly turn OFF XTINST in your config.

Further explanations can be found in Datasheet $24 ( a bit over my own head ...I admit ! )


Alain

mr.sneezy
- 30th May 2010, 13:25
Thanks Alain,
BTW, do you think I've just had a run of strangely bad luck with my programming, or does it come this hard for you too ?

(By bad luck I mean the fact that this config only broke Serout2 in my code but apparently nothing else)

Acetronics2
- 30th May 2010, 13:58
BTW, do you think I've just had a run of strangely bad luck with my programming, or does it come this hard for you too ?

(By bad luck I mean the fact that this config only broke Serout2 in my code but apparently nothing else)

...
The existence of extra-terrestrians hasn't been proved nor denied ...;)

SO, ... I do believe you were unlucky, trying desesperatly some " uncontrolled " receipes :D

Sometimes I have surprises too ... just remember what to do not to have to face it again ...


BUT I never could explain I dreamed of situations that I faced several years LATER ...
There are still great mysteries in our lives ...

And I do think it's much better like that !!!

Seriously ... someone like Darrel, who learnt the internals of PBP, maybe could explain what happens when XTENDED commands are enabled.

I Personnaly do not have time to, and my Pics use being Hobby dedicated ... I prefer learning other things ... or build toys for my Daughters.

Alain

Bruce
- 30th May 2010, 18:56
PBP places all of its system variables in ACCESS RAM on the 18F type. When you have the extended instruction set enabled, this changes how all PBP system variables in the range of 00h to 5Fh in ACCESS RAM are accessed.

With extended instruction set enabled, FSR2 is used as the pointer to an area in RAM, and the address embedded in the instruction itself, that is trying to write to ACCESS RAM from 00h to 5Fh, is the literal "offset" into the RAM area pointed to by the value in FSR2.

So ACCESS RAM from 00h to 5Fh is no longer directly accessible without using FSR2 as the base address pointer.

Example:

Say a PBP system variable R1 is in ACCESS RAM at location 2. Now say FSR2 has a value of 2. When PBP tries to directly load a value into R1, it actually gets placed in location 4. This happens because location 2 which would be the DIRECT address to R1 in ACCESS RAM is used as the literal "offset" and added to the value in FSR2 to get the final RAM address.

Just clearing FSR2 somewhere in your program just isn't going to work. Several PBP library routines and a TON of PBP macros use FSR2.

Also - it's a good idea to keep the _XINST_OFF_4L config option in there.

All config options are just numbers that get ANDed together by the assembler. If you leave one out, like _XINST_OFF_4L, which includes a value of BFh that gets ANDed with other config options for CONFIG4L, you end up with _XINST_ON_4L by default.

Here's an example;

These are pre-defined CONFIG4L options in your P18F4620.INC file.


;----- CONFIG4L Options --------------------------------------------------
_STVREN_OFF_4L EQU H'FE' ; Disabled
_STVREN_ON_4L EQU H'FF' ; Enabled
_LVP_OFF_4L EQU H'FB' ; Disabled
_LVP_ON_4L EQU H'FF' ; Enabled
_XINST_OFF_4L EQU H'BF' ; Disabled
_XINST_ON_4L EQU H'FF' ; Enabled
_DEBUG_ON_4L EQU H'7F' ; Enabled
_DEBUG_OFF_4L EQU H'FF' ; Disabled

Note that XINST is bit # 6 in CONFIG4L. A value of 1 in bit position # 6 = enabled.

Take any value "ON or OFF" above for STVREN, LVP, and DEBUG. AND them together, then look at the result. The value you end up with after ANDing any 3 options is the value that gets placed in CONFIG4L.

Leaving XINST "on or off" out leaves bit # 6 set, and extended instruction mode enabled.

The same goes for all other config registers. If you leave one option out, you may not be getting all config settings like you might expect - due to the AND result of all the ones you 'did' include.

mr.sneezy
- 31st May 2010, 01:03
Much appreciated, as it is a full answer to the question.

To reiterate though for my benefit, and maybe others at my low level of understanding.

If I use assembly style configs, either in my code or the PBP PIC.inc defaults, if I need to use a non PIC default config setting I should also add all other config values associated with that particular register, to make sure they don't get changed from what I expect ?

Bruce
- 31st May 2010, 01:57
Yes. It's a good idea to include all config options.