(re)programming a PIC over USB

For users of Matrix Multimedia Electronics software to discuss projects, circuits and any other electronics concepts.

Moderators: Benj, Mods

Post Reply
Lariekoek
Posts: 20
Joined: Sun Jun 10, 2018 1:24 pm
Contact:

(re)programming a PIC over USB

Post by Lariekoek »

Hello

As a starting electronics teacher i'm trying to develop some hardware on which my students can learn to program. The device i'm working on atm is almost finished, but I would really like to implement a USB port. That way the students can easily upload and test their code on the device without the need of an (expensive) PICkit.

If I understand correctly I need a PIC µcontroller with USB peripheral (duh) and support of flash self-write (self-programming).
Question 1: Does someone have experience with a 8-bit, 20 or 28 pin PIC with int. osc. that supports this? The filters on RS-online don't allow searching on these criteria.

I already stumbled upon "Tiny PIC bootloader", but if I'm not mistaking this one only works with the rather old 18F4455 using USB.
I've never worked with bootloaders before, and i'm trying to read myself into it but can't seem to find the right relevant information.
Question 2: Could someone point me in the right direction? Ideally a ready-to-use bootloader, or some guide on how to modify an existing one to my needs.

Question 3: I suppose that when using a bootloader, in Flowcode you can't just select the chip as a target anymore? Or is that assumption wrong? I myself learned to program in Flowcode using the "Formula Flowcode Buggy", and to program that we had to select the buggy as target instead of the 18F4455 that was used on the buggy.

Thanks, all help is much appreciated!

kersing
Valued Contributor
Valued Contributor
Posts: 1973
Joined: Wed Aug 27, 2008 10:31 pm
Location: Netherlands
Contact:

Re: (re)programming a PIC over USB

Post by kersing »

To search for chips you can use the Microchip parametric search.

With the right parameters you should find the 16F1459 which meets the requirements. A quick google for USB boot loaders suggests Microchip provides code for one (HID boot loader).
To use the result with Flowcode you will need to provide a start offset for the code. This can be done in several ways, creating a custom batch file is one, creating a custom chip definition would be another.
“Integrity is doing the right thing, even when no one is watching.”

― C.S. Lewis

Lariekoek
Posts: 20
Joined: Sun Jun 10, 2018 1:24 pm
Contact:

Re: (re)programming a PIC over USB

Post by Lariekoek »

Thanks for the quick reply, kersing!

Using the parametric search (I feel silly for not knowing this, thx!) I found a chip that's perfect for my application (the PIC18F27J53).
I found the Microchip bootloaders inside the "Microchip Libraries for Applications". There are USB bootloaders listed for a few PIC controllers, including the PIC18F47J53. This chip belongs to the family of the chip I selected, so with a few modifications this bootloader should do the trick.

These bootloaders are intended to be built with the XC8 compiler. I already found a forum post describing how to make Flowcode use the XC8 compiler by creating a batch file and make flowcode use that in the "compiler" settings. But in order to use this compiler with the bootloader, the "Codeoffset" and "ROM ranges" must be changed. I know how to do this in MPLAB, but how do I do this in Flowcode? I suppose by adding some parameters in the "Compiler" setting window?

User avatar
Steve001
Valued Contributor
Valued Contributor
Posts: 1163
Joined: Wed Dec 31, 2008 3:37 pm
Contact:

Re: (re)programming a PIC over USB

Post by Steve001 »

Hello,

Just a suggestion have you seen the ECIO's ?

https://www.matrixtsl.com/other/ecio/

Steve
Success always occurs in private and failure in full view.

Lariekoek
Posts: 20
Joined: Sun Jun 10, 2018 1:24 pm
Contact:

Re: (re)programming a PIC over USB

Post by Lariekoek »

I'm very familiar with the ECIO's, but for my application they are way to expensive, too big and a bit overkill :)

I decided to go for the PIC18F25K50 and Johnny's "USB PIC bootloader".

A bit off-topic, but i'm currently figuring out how to implement a Li-ion battery + BMS. If someone has tips & tricks on this subject, please feel free to share :)

User avatar
Steve001
Valued Contributor
Valued Contributor
Posts: 1163
Joined: Wed Dec 31, 2008 3:37 pm
Contact:

Re: (re)programming a PIC over USB

Post by Steve001 »

Li-ion battery - ooo the "enemy" :lol: Not familiar with this technology I am more industrial lead acid's and Nicad's
Sorry cannot help you much with Li-ion

What do you want with the Battery Monitor System , i may be able to assist baised on industrial lead acid's and Nicad's but i do know Li-ion can have them built into the battery.

microchip and liner technology (Analog Devices) have various IC's dedicated to this subject

https://www.microchip.com/Developmentto ... MCP73833EV

https://www.microchip.com/wwwproducts/en/en027785

https://www.analog.com/en/search.html?q ... %20monitor

Steve
Success always occurs in private and failure in full view.

User avatar
Benj
Matrix Staff
Posts: 15056
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Contact:

Re: (re)programming a PIC over USB

Post by Benj »

For single cell batteries I've had good results with these, though I was using Li-Po type cells they should be compatible with both Li-ion and Li-Po.

https://uk.farnell.com/w/c/semiconducto ... rt=P_PRICE
Battery.jpg
Battery.jpg (20.71 KiB) Viewed 427 times

Lariekoek
Posts: 20
Joined: Sun Jun 10, 2018 1:24 pm
Contact:

Re: (re)programming a PIC over USB

Post by Lariekoek »

@Benj @Steve001 Thanks for the reply, I decided to go for the MCP73832T and it's working like a charm :) Only thing is, the STAT led stays on after charging is done. But that may be because I'm testing with Li-Ion battery's that are over 8yrs old.

Back on-topic: I managed to get the system working with Johnny's "USB PIC bootloader" (hackaday.io) and its working wonderfull. But I need to set a codeoffset of 0x2000 and I can't find out how to do this in Flowcode. Is this possible and if it is, how?

Lariekoek
Posts: 20
Joined: Sun Jun 10, 2018 1:24 pm
Contact:

Re: (re)programming a PIC over USB

Post by Lariekoek »

I think I got it by making this .bat file for the compiler.

Code: Select all

@REM $(chip) "$(outdir)" "$(target)"
@PUSHD %~2
"C:\Program Files\Microchip\xc8\v2.20\bin\xc8-cc.exe" -mcpu=%~1 "%~3.c" -w=359,1273,1388 -fshort-float -fshort-double -std=c90 -Os -mcodeoffset=0x2000
@POPD
@IF %ERRORLEVEL% NEQ 0 GOTO Error
@ECHO.
@ECHO Compilation successful!
@EXIT 0
:Error
@ECHO.
@ECHO Error returned from [xc8.exe]
@EXIT 1
the generated hex files are now accepted and running. Something is still off with the oscillator settings tho, my 'blink' program of 1 sec is IRL 50 sec...

kersing
Valued Contributor
Valued Contributor
Posts: 1973
Joined: Wed Aug 27, 2008 10:31 pm
Location: Netherlands
Contact:

Re: (re)programming a PIC over USB

Post by kersing »

Keep in mind the boot loader sets the oscillator and you might not be able to change those settings in your code. Try matching the boot loader settings in your program.
“Integrity is doing the right thing, even when no one is watching.”

― C.S. Lewis

Lariekoek
Posts: 20
Joined: Sun Jun 10, 2018 1:24 pm
Contact:

Re: (re)programming a PIC over USB

Post by Lariekoek »

I configured the bootloader to use internal clock and was aiming to get 16MHz, instead it uses 1MHz.
From datasheet:
FREQUENCY SELECT BITS (IRCF)
The HFINTOSC (16 MHz) outputs to a divide circuit
that provides frequencies of 16 MHz to 31.25 kHz.
These divide circuit frequencies, along with the
31.25 kHz INTRC output, are multiplexed to provide a
single INTOSC clock output (see Figure 3-1). The
IRCF<2:0> bits of the OSCCON register and the
INTSRC bit of the OSCCON2 register select the output
frequency of the internal oscillators. One of eight
frequencies can be selected via software:
• 16 MHz
• 8 MHz
• 4 MHz
• 2 MHz
• 1 MHz (Default after Reset)
• 500 kHz
• 250 kHz
• 31 kHz (INTRC or HFINTOSC)
I'm not super familiar with setting registers but I think the IRCF bits are not set in the config words? So I need to set those bits in my code to select the 16MHz clock?

kersing
Valued Contributor
Valued Contributor
Posts: 1973
Joined: Wed Aug 27, 2008 10:31 pm
Location: Netherlands
Contact:

Re: (re)programming a PIC over USB

Post by kersing »

USB with 1 MHz clock? That does not sound like a setting that would work (from memory).
“Integrity is doing the right thing, even when no one is watching.”

― C.S. Lewis

Lariekoek
Posts: 20
Joined: Sun Jun 10, 2018 1:24 pm
Contact:

Re: (re)programming a PIC over USB

Post by Lariekoek »

In the bootloader I find
OSCCONbits.IRCF = 7
and thus selecting the 16MHz internal clock. From there on the USB clock module gets the necessary 48MHz clock. But when running the user code, it resets back to using the 1MHz clock.

It's a bummer that the IRCF bits are software controlled, for ease of use I'd like the controller to use 16MHz as default without extra code. Guess I'll use a 16MHz Xtal than.

kersing
Valued Contributor
Valued Contributor
Posts: 1973
Joined: Wed Aug 27, 2008 10:31 pm
Location: Netherlands
Contact:

Re: (re)programming a PIC over USB

Post by kersing »

A bummer? Having software control of the oscillator is a feature. At allows you maximum flexibility.

What are the oscillator settings in your flowcode project?
“Integrity is doing the right thing, even when no one is watching.”

― C.S. Lewis

Lariekoek
Posts: 20
Joined: Sun Jun 10, 2018 1:24 pm
Contact:

Re: (re)programming a PIC over USB

Post by Lariekoek »

Hmm you're right actually, and with the help of the IntOscHelper it's easy to set the right frequency. I'll leave it using the internal osc, I don't need the high accuracy of an Xtal and this way I have 2 extra free pins. Thx for making me realize :)

Flowcode settings are ok, everything working now.

User avatar
Steve001
Valued Contributor
Valued Contributor
Posts: 1163
Joined: Wed Dec 31, 2008 3:37 pm
Contact:

Re: (re)programming a PIC over USB

Post by Steve001 »

Hi Lariekoek,
Lariekoek wrote: But that may be because I'm testing with Li-Ion battery's that are over 8yrs old.
Not sure what batteries you are using, but some of the flange pack items that RS sell have a shelf life of 6 Months before requiring a recharge.
They also have a 2 - 3 year life or 500 cycles which occurs first.

You doing well if a 8 year old battery accepted charge, did it work also ?

Steve
Success always occurs in private and failure in full view.

Lariekoek
Posts: 20
Joined: Sun Jun 10, 2018 1:24 pm
Contact:

Re: (re)programming a PIC over USB

Post by Lariekoek »

Atm I'm testing with a battery from an old third party wireless PS3 controller. It was dead at first, but accepted charge from the circuit and charged fine. My circuit isn't drawing much current and only tested for short periods of time. I have no idea how much charge it's holding, but for testing purposes it's doing the trick :)

Post Reply