18f24K40

Discuss PIC and electronic related things

Moderators: David Barker, Jerry Messina

backpacker69
Posts: 18
Joined: Sun Feb 03, 2013 2:26 am
Location: Salmon, Idaho

18f24K40

Post by backpacker69 » Sat Jun 10, 2017 2:20 pm

this is a new chip?? but can SF / pickit2 even deal with it. I see no modules etc such as setalldigital. Am I digging a hole and no way out.
This pic for cost is only $1.50 from Mouser. Only reason I am considering using this pic.
Might run and use the 18f2221 but??
I have no need for lots of bells n whistles just a pic w/ HLVD and limited ram
I miss backpacking the Sierras

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

Re: 18f24K40

Post by Jerry Messina » Sat Jun 10, 2017 3:30 pm

You can get it to work with SF but it'll take downloading mplabx from microchip and doing a little swapping of files.
You can get the device files here (along with an updated SetAllDigital), but you need the assembler from mplabx, which is a huge download just to get mpasmx.

See the following pages for more info if you're interested:
SetDigitalIO
SystemConvert
mpasmx_5.71_device_files.zip

As far as getting it to work with a pickit2 unfortunately I think you're out of luck there. I don't know of any updated files for that. It does work with the pickit3, but you'll need to use all the mplabx programs/utilities and none of the old standalone apps to program it.

All this is a lot of work, and it's hard to recommend doing it unless you're comfortable with messing with all the files.
Personally I'd recommend an 18F2xK22. They're not quite as cheap as the K40, but they're supported by all the existing tools.
They do have a lot of 'bells and whistles' but newer chips tend to be cheaper than the older ones.

backpacker69
Posts: 18
Joined: Sun Feb 03, 2013 2:26 am
Location: Salmon, Idaho

Re: 18f24K40

Post by backpacker69 » Sat Jun 10, 2017 7:14 pm

Thanks Jerry
I have several 18f24K22 that work but seem slow. Why I have no idea. Checked the osscilator setting etc. he nly way I can get it running half way decent is to use INTOSC8.bas. The InternalOscillator.bas won't work, or at least I don't see the device listed in the module.
Will proceed with the 18f24K22 or the 18lf2221.
I miss backpacking the Sierras

backpacker69
Posts: 18
Joined: Sun Feb 03, 2013 2:26 am
Location: Salmon, Idaho

Re: 18f24K40

Post by backpacker69 » Sat Jun 10, 2017 8:04 pm

It pays to shop
the 18f24K22 at Mouser = $2.50 Digi-key = $2.14
now just need to order and write code for it. Maybe the SetDigitalIO.bas module will work better??
I miss backpacking the Sierras

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

Re: 18f24K40

Post by Jerry Messina » Sat Jun 10, 2017 9:17 pm

That chip will run up to 64MHz using the internal osc, so it's plenty fast.

If you post your copy of InternalOscillator.bas I'll take a look and make sure it'll support the 2xK22 for you. I think that was an older file, maybe from Digital DIY?

The SetDigitalIO page has updated files for the SetAllDigital routine, but that won't do anything for the clock speed. That's a separate issue.

bitfogav
Registered User
Registered User
Posts: 169
Joined: Sat Oct 09, 2010 1:39 pm
Location: United Kingdom

Re: 18f24K40

Post by bitfogav » Sat Jun 10, 2017 11:20 pm

If it's any help then there is a good article on the wiki which covers how to set the internal OSC, the article is for a 18F25K22 but should be the same for the 18F24K22.

http://www.sfcompiler.co.uk/wiki/pmwiki ... hSwordfish

backpacker69
Posts: 18
Joined: Sun Feb 03, 2013 2:26 am
Location: Salmon, Idaho

Re: 18f24K40

Post by backpacker69 » Sun Jun 11, 2017 12:17 pm

Thanks guys, the link that bitfogav posted a link to shed some light on what and why my project using a K pic seems slow.
Will go back and review what I have as well as the setalldigital.bas.
Yes the setalldigital was from Diy. but looking over the code it has the 24K22

Code: Select all

  // 18FxxK20 family (no LF versions)
  #elseif _device in (18F23K20, 18F24K20, 18F25K20, 18F26K20, 18F43K20, 18F44K20, 18F45K20, 18F46K20)
    ANSEL = $00
    ANSELH = $00
    CM1CON0 = 0
    CM2CON0 = 0
    SLRCON = _SLR               // added V1.8
    
I miss backpacking the Sierras

backpacker69
Posts: 18
Joined: Sun Feb 03, 2013 2:26 am
Location: Salmon, Idaho

Re: 18f24K40

Post by backpacker69 » Sun Jun 11, 2017 12:44 pm

now I an't get it to compile. I screwed something up??

Code: Select all

DEVICE = 18F24K22      
   CLOCK = 8            '8MHz
   CONFIG FOSC = INTIO7   'internal oscillator
   OSCCON = %01011100     ' how fast to run 
#option SWORDFISH_SE = true
INCLUDE "SetDigitalIO.bas"
INCLUDE "Utils.bas"  

 DIM led AS PORT b.0
 
 WHILE true
       led = 1
       DELAYMS(1000)
       TOGGLE (led)
       DELAYMS(500)
 WEND      
I miss backpacking the Sierras

bitfogav
Registered User
Registered User
Posts: 169
Joined: Sat Oct 09, 2010 1:39 pm
Location: United Kingdom

Re: 18f24K40

Post by bitfogav » Sun Jun 11, 2017 1:33 pm

The SetDigitalIO library has been updated since the DIY version which is compatible with the 18F24K22 or the K22 series of mcu's..
You need to download the latest version and put that in the same folder as your programme or put it in the Swordfish userLibrary folder.

http://www.sfcompiler.co.uk/wiki/pmwiki ... tDigitalIO


As for your code you need to change it to this: (note I've not tested the code, but it compiles).

Code: Select all

Device = 18F24K22      
   Clock = 8            '8MHz
   Config FOSC = INTIO7   'internal oscillator   

#option SWORDFISH_SE = true
Include "SetDigitalIO.bas"
Include "Utils.bas"  

 Dim led As Portb.0              ' <- changed this for correct port
 
 OSCCON = %01100100          ' <- moved this here and changed this for 8Mhz Int clock
 
 While true
       led = 1
       DelayMS(1000)
       Toggle (led)
       DelayMS(500)
 Wend 

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

Re: 18f24K40

Post by Jerry Messina » Sun Jun 11, 2017 1:57 pm

You're mixing up the order of executable statements, includes, and variable declares.
Some of those have to be done before any executable statements.

1). make sure you're using the SetDigitalIO file from here:
http://www.sfcompiler.co.uk/wiki/wiki-u ... IO_v22.zip

inside the zip file you'll find SetDigitalIO.bas
put it in your user library folder

when you go to use it you must add a 'SetAllDigital' statement

2). I've attached a zip file with an updated version of "InternalOscillator.bas" that supports the K22
inside the zip you'll find "intosc.bas" (I've renamed the file as "intosc.bas" so it won't overwrite your old original DDIY one).

put it in your user library folder

(NOTE: the zip file has been updated and moved to http://www.sfcompiler.co.uk/wiki/pmwiki ... ser.IntOSC)

to use it all you have to do is add

Code: Select all

include "intosc.bas"
to the start of your code

Unlike setalldigital, it'll add all the code to setup the osc automatically.

3). try this test code:

Code: Select all

DEVICE = 18F24K22     
CLOCK = 8            '8MHz

INCLUDE "intosc.bas"
INCLUDE "SetDigitalIO.bas"

 DIM led AS PORTb.0

 SetAllDigital()

output(led)
  
 WHILE true
       TOGGLE (led)
       DELAYMS(1000)
 WEND    
Last edited by Jerry Messina on Sat Jun 17, 2017 3:15 pm, edited 1 time in total.
Reason: remove zip file... now on wiki IntOSC page

backpacker69
Posts: 18
Joined: Sun Feb 03, 2013 2:26 am
Location: Salmon, Idaho

Re: 18f24K40

Post by backpacker69 » Sun Jun 11, 2017 5:47 pm

Thanks, I thought I didn't have the INCLUDE modules in the right folder but after looking at your example I see my mistake.
Now to order the 10 chips SMD and one in a dip for testing my code for proper functioning HLVD etc.
was slightly worried why it wouldn't compile. Need to make some changes to the code for the 18f22k20 boards I have. Could be why it starts slow.
I miss backpacking the Sierras

backpacker69
Posts: 18
Joined: Sun Feb 03, 2013 2:26 am
Location: Salmon, Idaho

Re: 18f24K40

Post by backpacker69 » Sun Jun 11, 2017 8:54 pm

Looks like a slight change in part number. Instead of the 18f24K22 going with the 18f24K20. Cheaper at mouser for 10 units plus I happen to have several of these puppies on hand.
Onward to locating white leds and writing code to include the HLVD
I miss backpacking the Sierras

backpacker69
Posts: 18
Joined: Sun Feb 03, 2013 2:26 am
Location: Salmon, Idaho

Re: 18f24K40

Post by backpacker69 » Mon Jun 12, 2017 12:37 am

got the "simple" code done involving the HLVD section but got to wondering if the HLVDCON value be changed using a sub route. Is what I am contemplating is having the LOW voltage detect LED0 blink at a faster rate as the battery goes lower. In this code (haven't calibrated the pickit2 voltage but the led0 starts blinking at 3.1 volts).
I could use the external voltage input but pcboards are in the mail.
Going to experiment using a subroutine to change the HLVDCON value

Code: Select all

 DEVICE = 18F24K20    
CLOCK = 8            '8MHz

INCLUDE "intosc.bas"
INCLUDE "SetDigitalIO.bas"
INCLUDE "Utils.bas"
DIM led0 AS PORTb.0
dim led1 as portb.1

OUTPUT(led0)
output (led1)  
hlvdcon= %00011100
pir2.bits(2)=0
pie2.bits(2)=1
intcon.bits(7)=1
led0= 0 
SetAllDigital()


  
 WHILE true
 if pir2.bits(2)=1 then // check hlvd interrupt
    led0 = 0
    delayms(500)
    led0=1
    delayms(500)
    pir2.bits(2) =0  //clr interrupt
    else
    led0=1
    endif
    toggle(led1)
             DELAYMS(1000)
 WEND         
I miss backpacking the Sierras

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

Re: 18f24K40

Post by Jerry Messina » Mon Jun 12, 2017 9:39 am

Two suggestions...

Since you're polling the HLVDIF and not using interrupts, don't mess with the interrupt enable bits.
You can still poll the PIR flags to see if they get set.

Secondly you want to call SetAllDigital before you start using the IO pins.

Code: Select all

SetAllDigital()     <<<< put this first
OUTPUT(led0)
output (led1) 
hlvdcon= %00011100
pir2.bits(2)=0
pie2.bits(2)=1       <<<<< remove this
intcon.bits(7)=1    <<<<<< and this
led0= 0
I could use the external voltage input
When you use the external HLVDIN pin you can't change the trip point. In this mode the voltage at the pin is compared to a fixed 1.2V reference. To change it you have to use external resistors to divide the input voltage.

backpacker69
Posts: 18
Joined: Sun Feb 03, 2013 2:26 am
Location: Salmon, Idaho

Re: 18f24K40

Post by backpacker69 » Mon Jun 12, 2017 2:34 pm

Thanks Jerry.
Will make suggested changes
I miss backpacking the Sierras

Post Reply