Compiler issues with 18F25K22 - SOLVED!

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

Post Reply
matherp
Registered User
Registered User
Posts: 31
Joined: Tue May 31, 2011 2:59 pm
Location: Cambridge

Compiler issues with 18F25K22 - SOLVED!

Post by matherp » Mon Jun 06, 2011 5:15 pm

I am currently evaluating Swordfish and have successfully run most of the sample programs on a 18F4520. However my target environment for most projects will be the 18F25K22.

I have implemented the BAS and INC files provided by Miles from Ciseco. I have analysed these in detail and haven't found any errors or issues. Using these some of the Swordfish samples work and others don't.

In order to get some of the samples to compile I have added some 18F25K22 code to SetAllDigital in utils.bat as follows:

#elseif _device in (18F25K22)
ANSELA=0
ANSELB=0
ANSELC=0

I can program the 18F25K22 using either a PicKit or by installing the DS30 bootloader. Some samples work when programmed with one and not the other and visa-versa.

The attached is the i2c sample program. In this case it works fine when programmed with the DS30 but doesn't work when exactly the same hex file is programmed with the PicKit. The same thing happens with the softwareI2C sample program. In both cases the scope shows that when programmed with the Pickit there is no activity at all on the SDA and SCL lines. The USART output works perfectly.

In trying to analyse what is happening in this and other examples, one possibility seems to be that some key registers in the 18F25K22 such as the ANSEL registers are not in the access bank but need an explicit bank select and subsequent deselect. However this is only supposition.

I would appreciate any help on this as this is a showstopper for me in terms of purchasing the full licence

Thanks

Peter

Code: Select all

// if device and clock are omitted, then the compiler defaults to 
// 18F452 @ 20MHz - they are just used here for clarity...
Device = 18F25K22
Clock = 64

// import libraries...
Include "I2C.bas"
Include "usart.bas"
Include "utils.bas"

// target 24LC128 I2C EEPROM device...
Const I2C_EEPROM = $A0 

// local variables...
Dim
   Value As Byte,
   Address As Word
   
// program start...
SetAllDigital()
Address = 55
I2C.Initialize

// write some data...
USART.SetBaudrate(br19200)
USART.Write("Start", 13, 10)
I2C.Start                          
I2C.WriteByte(I2C_EEPROM)              
I2C.WriteByte(Address)
'I2C.WriteByte(Address.Byte0) 
I2C.WriteByte("Z")                 
I2C.Stop 
USART.Write("Sent", 13, 10)

// allow external EEPROM to write data...
DelayMS(40) 

// read the data back...
I2C.Start
I2C.WriteByte(I2C_EEPROM)
I2C.WriteByte(Address)
'I2C.WriteByte(Address.Byte0) 
I2C.Restart                         
I2C.WriteByte(I2C_EEPROM + 1)
Value = I2C.ReadByte
I2C.Acknowledge(I2C_NOT_ACKNOWLEDGE)
I2C.Stop


// output the result
USART.Write("Value = ", Value, 13, 10)
:D
Last edited by matherp on Tue Jun 07, 2011 10:44 am, edited 1 time in total.

matherp
Registered User
Registered User
Posts: 31
Joined: Tue May 31, 2011 2:59 pm
Location: Cambridge

Post by matherp » Mon Jun 06, 2011 5:53 pm

One more example:

The following works perfectly when programmed with the DS30 bootloader

Code: Select all

Device = 18F25K22
Clock = 64

Include "usart.bas"
Include "ADC.bas"
Include "convert.bas"
          

Function ADInAsVolt() As Word
   result = ADC.Read(0)
End Function

// sampled AD value...
Dim ADVal As Word
Dim s As String(7)
// initialise and clear LCD...
SetBaudrate(br19200)
SetAcqTime(24)
SetConvTime(FOSC_64)

DelayMS (500)


// main program loop...
While true
   ADVal = ADInAsVolt
   s=DecToStr(ADVal)
   USART.Write("DC Volts = ",DecToStr(ADVal),13,10)
   DelayMS(1000)
Wend
But the much simpler example:

Code: Select all

Device = 18F25K22
Clock = 64

// import usart and conversion modules...
Include "usart.bas"
Include "convert.bas"
Dim ADVal As Word
SetBaudrate(br19200)
ADVal=12345

USART.Write("Decimal  : ", DecToStr(ADVal), 13, 10)  // binary number
using the same DecToSTR routine outputs "Decimal : 0"

However if programmed with the PicKit both work properly

Jerry Messina
Swordfish Developer
Posts: 1473
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Post by Jerry Messina » Mon Jun 06, 2011 6:25 pm

Are you using the SE version of the compiler? If so, it's a couple of versions behind the released one, and it has issues with the K22 that have been fixed in the latest full version.

I can't vouch for the include files from Miles, but when using the ones produced by the SF SystemConvert utility and the latest MPLAB, the full version of SF produces the correct code for accessing the ANSEL registers on the 25K22.

Try adding

Code: Select all

    BSR = 15
    ANSELA = $00
    ANSELB = $00
    ANSELC = $00
    BSR = 0
to utils.bas. With any luck, that'll help.

Also, the 25K22 has a relatively new feature, and that's slew-rate control on the port pins. This is controlled by the SLRCON register, and by default the chip powers up with the pins set for "slow". It's a long shot, but perhaps try setting SLRCON = 0

Update: and you'll probably have to add similar code to get the ADC working again... ADC.bas doesn't have code to setup a 25K22

matherp
Registered User
Registered User
Posts: 31
Joined: Tue May 31, 2011 2:59 pm
Location: Cambridge

Post by matherp » Mon Jun 06, 2011 6:42 pm

Jerry

Thanks for the fast response

The change to utils has solved the first example i2c problem but not the one in the second post. In this one it looks like the bank select is pointing somewhere other than the string when then DexToStr is executed as depending on what has been run in the PIC previously it converts whatever is in some different bit of RAM at the time.

I am using SE as I am evaluating the product> The release is 2.2.0.8 which was released June 2nd 2011.

Any more ideas?

Thanks again

Peter

Jerry Messina
Swordfish Developer
Posts: 1473
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Post by Jerry Messina » Mon Jun 06, 2011 6:45 pm

Peter -

Didn't realize SE had been updated too. Let me take a look...

Jerry Messina
Swordfish Developer
Posts: 1473
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Post by Jerry Messina » Mon Jun 06, 2011 7:45 pm

Bootloaders can(do) change some of the pic registers, and might not change them back. I'd have to look at DS30Loader, but with the Mchip one the following works...

First, copy the file SwordfishSE\Library\System.bas to SwordfishSE\UserLibrary

This copy will over-ride the one in the Library folder, and let you make changes without mucking up the original.

Edit the new SwordfishSE\UserLibrary\System.bas file, and at the very end add the following code

Code: Select all

//
// SF startup initialization
// note: these initializations are normally not required, but they can be 
// very important if running under a bootloader or you reset using a 'goto 0'
// initialization is enabled by default (since it shouldn't hurt anything),
// but you can disable this initialization by setting the option false.
// 
#option SYSTEM_SET_REGISTER_DEFAULTS = true

public inline sub SetRegisterDefaults()
    // v1.3 - init upper tableptr reg for 'const' data access
    TBLPTRU = $00
    
    // v1.4 - init bank select, pclat, and stkptr registers
    BSR = $00

    // these are probably fine, but they're very important, so just in case...
    PCLATU = $00
    PCLATH = $00
    STKPTR = $00
end sub

#if (SYSTEM_SET_REGISTER_DEFAULTS)
SetRegisterDefaults()
#endif


Now, just to make sure it's the first thing executed, add an "include" to your main program

Code: Select all

Device = 18F25K22
Clock = 64

// include updated system library
include "system.bas"

// import usart and conversion modules...
Include "usart.bas"
Include "convert.bas"
Dim ADVal As Word
SetBaudrate(br19200)
ADVal=12345

USART.Write("Decimal  : ", DecToStr(ADVal), 13, 10)  // binary number 
See if that helps.

The nice thing about SF is that you get access to all these routines, so you can change them as required.

matherp
Registered User
Registered User
Posts: 31
Joined: Tue May 31, 2011 2:59 pm
Location: Cambridge

Post by matherp » Mon Jun 06, 2011 8:07 pm

Jerry

That seems to solve that one - again many thanks!

Tomorrow I'll go through all of the sample programs and check them and report back

best regards

Peter

matherp
Registered User
Registered User
Posts: 31
Joined: Tue May 31, 2011 2:59 pm
Location: Cambridge

18F25K22 - Solved

Post by matherp » Tue Jun 07, 2011 10:44 am

Thanks to Jerry I can confirm that I have successfully run the Swordfish sample programs on the PIC18F25K22 at 64Mhz with an external crystal using both a PicKit2 or DS30 bootloader to program the PIC.

These tests have included:
All timers with or without interrupts
Software and Hardware I2C
Software and Hardware USART
ADC
PWM
Onewire (DS18B20)
Internal EEPROM

I haven't tested SPI but have no reason to believe it won't work

To summarise the fixes needed:
utils.bas must be edited to include the 18F25K22 in SetAllDigital and this must be called before using any hardware ports in modules such as I2C and USART.

System.bas must be edited to provide additional initialisation if using the DS30 bootloader

Thanks again Jerry

Best regards

Peter

Jerry Messina
Swordfish Developer
Posts: 1473
Joined: Fri Jan 30, 2009 6:27 pm
Location: US

Post by Jerry Messina » Tue Jun 07, 2011 11:21 am

Glad you've got everything working so far, Peter.

A little word of warning: one thing David just pointed out to me is that the SE version has no provisions for setting the Bank Select Register, hence the single bank limit. When using SE, you could run into issues with devices that have SFR's that are located in the non-ACCESS area of the upper bank (and there are a few of these, not just the 25K22). For these chips, you'll have to manage setting/clearing the BSR by hand when accessing these registers.

Perhaps that should have been obvious, but that little detail escaped me.

Post Reply