(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: 23
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: 2045
Joined: Wed Aug 27, 2008 10:31 pm
Location: Netherlands
Has thanked: 553 times
Been thanked: 1081 times
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: 23
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: 1189
Joined: Wed Dec 31, 2008 3:37 pm
Has thanked: 460 times
Been thanked: 523 times
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: 23
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: 1189
Joined: Wed Dec 31, 2008 3:37 pm
Has thanked: 460 times
Been thanked: 523 times
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: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
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 33081 times

Lariekoek
Posts: 23
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: 23
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: 2045
Joined: Wed Aug 27, 2008 10:31 pm
Location: Netherlands
Has thanked: 553 times
Been thanked: 1081 times
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: 23
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: 2045
Joined: Wed Aug 27, 2008 10:31 pm
Location: Netherlands
Has thanked: 553 times
Been thanked: 1081 times
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: 23
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: 2045
Joined: Wed Aug 27, 2008 10:31 pm
Location: Netherlands
Has thanked: 553 times
Been thanked: 1081 times
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: 23
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: 1189
Joined: Wed Dec 31, 2008 3:37 pm
Has thanked: 460 times
Been thanked: 523 times
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: 23
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 :)

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

Re: (re)programming a PIC over USB

Post by Lariekoek »

It's a battery rated at 570mAh and I can confirm it has about 500mAh left (rough calculation, but should be close to it). Not bad :)

User avatar
Steve001
Valued Contributor
Valued Contributor
Posts: 1189
Joined: Wed Dec 31, 2008 3:37 pm
Has thanked: 460 times
Been thanked: 523 times
Contact:

Re: (re)programming a PIC over USB

Post by Steve001 »

Hi Lariekoek,

[/quote]=Lariekoek :-
It's a battery rated at 570mAh and I can confirm it has about 500mAh left (rough calculation, but should be close to it). Not bad :)
[/quote]

I am not certain, but based on Industrial Nicad's & Lead Acid batteries.
Batteries are deemed end of life when the amp hour capacity reaches 65% = 370 mAh in your case so your battery around 87 % as you say not bad

A couple of things that catch people out batteries have a mAh or Ah capacity

Example

A 30 V battery 8 Ah

so you think 1 Amp for 8 hours (which is a good rule of thumb to start with)

1. Ohms law also plays a part here too, as the battery discharges sometimes dependant on the Load type the current increases and discharges the battery faster.
2. so at your 1 Amp discharge the battery has a terminal voltage of 33.15 V DC (fully charged) as the battery discharges to 21 V DC the battery will still supply your 1 Amp load current but at this voltage will the equipment still function ?

Hope this makes sense and sorry for jumping off topic a bit.

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

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

Re: (re)programming a PIC over USB

Post by Lariekoek »

Hey Steve

Thx for the info, didn't knew about the 65% rule!

I have a 3,7V (fully charged 4.2V) 570mAh battery and tested by running full load (roughly 500mA) until the µC started to brown-out (about 1 hour). Brown-out occurred when the battery voltage dropped to 3,52V. Could probably make it run +-20min longer by adjusting BOR voltage.

User avatar
Steve001
Valued Contributor
Valued Contributor
Posts: 1189
Joined: Wed Dec 31, 2008 3:37 pm
Has thanked: 460 times
Been thanked: 523 times
Contact:

Re: (re)programming a PIC over USB

Post by Steve001 »

Hi Lariekoek,

something else you can look at to get a bit more from your battery.

A DCDC Setup convertor this has a minimum voltage of 1.5 V
Be Sure to check that if you use this you don't go bellow the minimum recommended volts per cell for your battery

see Example 8.2.1 & 2 on page 10

https://uk.rs-online.com/web/c/semicond ... erm=LM2621

I used one with my Super Capacitor Backup supply that is one here seams to work ok
The IC is TINY !!

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

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

Re: (re)programming a PIC over USB

Post by Lariekoek »

Hey Steve

I thought about using a boost convertor but decided not to because the benefits (longer use on one charge) do not outweigh the added costs and soldering work for my application (students with little soldering skills have to be able to assemble the device). Also, the pcb is getting quite full already because of the use of easy-soldered components (smallest footprint 1206). Thanks for the input tho! :)

User avatar
Steve001
Valued Contributor
Valued Contributor
Posts: 1189
Joined: Wed Dec 31, 2008 3:37 pm
Has thanked: 460 times
Been thanked: 523 times
Contact:

Re: (re)programming a PIC over USB

Post by Steve001 »

Hi Lariekoek,

No problems glad i could help

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

Post Reply