Author Topic: PIC programming advice  (Read 3919 times)

0 Members and 1 Guest are viewing this topic.

Offline step_s

  • Regular Contributor
  • *
  • Posts: 138
  • Country: dk
PIC programming advice
« on: October 31, 2016, 01:01:15 pm »
Hi guys.
It seems that I forget most of my programming knowledge every time I have gone through an exam, so wanted to return to it a little.
What I need now is some advice for a very reliable PIC setup.
The point is that I wanted to use a PIC16F1574 to run a buck converter, as well as have a capacitive touch sensor, and some indicator LEDs for a simple powerbank for LiPO batteries.
The advice I'm looking for is on HOW to program the buck converter, touch and LEDs so that the buck converter "refresh rate" isn't affected by any of the other things.
Have thought about making timer events to ensure the refresh rates, but any advice is welcome :)
 

Offline zzattack

  • Regular Contributor
  • *
  • Posts: 63
  • Country: nl
Re: PIC programming advice
« Reply #1 on: October 31, 2016, 02:27:21 pm »
Without more detail, in general you'll want to look at performing the 'refresh' in an interrupt originating from a timer, and don't service anything else in the interrupt routine but instead do that from the main loop.
 

Offline Ian.M

  • Super Contributor
  • ***
  • Posts: 8144
Re: PIC programming advice
« Reply #2 on: October 31, 2016, 02:53:00 pm »
That's probably not the best PIC for the job as its PWM modules seem to be optimised for motor control, whereas for a buck converter, you want one of Microchip's ECCP modules with PWM auto-shutdown capability.   Given such a PIC, you simply configure its PWM module to drive the pass transistor with a current sensor feeding a comparator input to control its auto-shutdown feature to provide fast acting cycle by cycle current limiting.  You can then close the loop by sampling the output using the ADC and correcting the duty cycle using a carefully optimised integer PID algorithm.     The PWM would run autonomously, with the output ADC sampling and duty cycle correction done in its timer ISR to synchronise it to the PWM so it doesn't 'beat' with the residual switching noise on the output.

The UI can then simply run in a polling loop as the main program.
« Last Edit: November 01, 2016, 05:11:00 am by Ian.M »
 
The following users thanked this post: step_s

Offline step_s

  • Regular Contributor
  • *
  • Posts: 138
  • Country: dk
Re: PIC programming advice
« Reply #3 on: November 01, 2016, 03:02:17 am »
So how to prioritize the PWM modules with timer interrupts? Just a coarse example :)

Sure the ECCP would have some more functions. Maybe I should check that out. Although the current limit sensing etc can still be done quite simple over the transistor, although this would not be very reliable with a standard PWM module? Would require some more programming most likely. Ofcourse the entire point is to NOT have the converter hang at any point. What I'm a little afraid of, is the MCU hanging and causing spikes on the output, potentially frying the connected unit.
 

Offline Ian.M

  • Super Contributor
  • ***
  • Posts: 8144
Re: PIC programming advice
« Reply #4 on: November 01, 2016, 05:32:51 am »
Doing software current limiting, although possible, is another constraint on a hard realtime design.  I presume that your inductor will be running at a peak current of up to 80% or 90% of its saturation current (as significantly less means you've over-specified it) which means you don't have a lot of time to get that PWM shut down before the chopper transissor gets badly overloaded.   ECCP module PWM autoshutdown would implement that critical over-current trip in hardware, can react in as little as one clock cycle, can run in auto-restart mode to provide continuous current limiting (useful for starting up into capacitive loads without tripping), and above all, reacts autonomously even if a bug has hung your code.  The only issue is with debugging: peripheral freeze + a critical control loop inside the MCU is *NOT* a good combo unless you tack on external limiting for the duration of development with a debugger.

Of course the entire point is to NOT have the converter hang at any point. What I'm a little afraid of, is the MCU hanging and causing spikes on the output, potentially frying the connected unit.

TL431+TRIAC crowbar.
 

Offline step_s

  • Regular Contributor
  • *
  • Posts: 138
  • Country: dk
Re: PIC programming advice
« Reply #5 on: November 04, 2016, 02:14:50 pm »
I see, adding the crowbar would help with tripping the overcurrent auto shutdown.
I will have a look at suitable PIC's with ECCP modules, and return when the time comes! :)
Thanks for the help as always Ian.
 

Offline Ian.M

  • Super Contributor
  • ***
  • Posts: 8144
Re: PIC programming advice
« Reply #6 on: November 04, 2016, 03:31:52 pm »
The purpose of the crowbar is to blow the fuse, while holding the output voltage to a safe level!

I see, adding the crowbar would help with tripping the overcurrent auto shutdown.
If its lost regulation, you cant be certain that the over-current protection control loop is going to function, unless its purely implemented in hardware, its set-point is not soft-configurable, it uses a separate switching transistor and it doesn't share a reference with the voltage control loop.

Therefore you need a beefy TRIAC (cant use a thyristor without an extra transistor to invert the gate drive) that will survive the worst case overload for long enough for the fuse to blow.   If the over-current loop is still working, it will trip before the fuse does, and there is a possibility to restart after the output voltage has fallen low enough to let the TRIAC drop out.
 

Offline step_s

  • Regular Contributor
  • *
  • Posts: 138
  • Country: dk
Re: PIC programming advice
« Reply #7 on: November 06, 2016, 01:37:24 pm »
What about using a simple op-amp setup that would directly shutdown the chip instead? In case of the circuit hanging, overcurrent, or over voltage? Restarting the chip should kick it back in to working conditions, and an analog component would surely be fast enough?
I'm looking at the PIC's with the ECCP modules, and they tend to be priced a lot higher, and lack regular PWM. The search function on microchips page is not really great in this regard :/
 

Offline Ian.M

  • Super Contributor
  • ***
  • Posts: 8144
Re: PIC programming advice
« Reply #8 on: November 06, 2016, 06:03:41 pm »
Errrr..... What? ? ? ???

The cheapest PIC with autoshutdown is the PIC12F1572 - $0.43 (5K pricing)
It has autoshutdown via its CWG moduloe that can be applied to the output of one of its 16 bit PWM modules.

Alternatively for around $1 look at the PIC16F1825/7/9 all of which have two CCP modules + 2 ECCP modules, so that's 4 independent duty cycle 10 bit PWMs, two with autoshutdown.   
 

Offline JPortici

  • Super Contributor
  • ***
  • Posts: 2560
  • Country: it
Re: PIC programming advice
« Reply #9 on: November 06, 2016, 06:52:00 pm »
http://www.microchip.com/maps/microcontroller.aspx
kinda agree on the search on microchip, but actually i don't.
anyway in the MAPS you have more data and filters available that usually isn't in the regular product search

(for example, did you know that there were pic16 with 2 ADCs for a long time?)
 

Offline Ian.M

  • Super Contributor
  • ***
  • Posts: 8144
Re: PIC programming advice
« Reply #10 on: November 06, 2016, 08:31:41 pm »
MAPS is nearly ****ing useless unless you already know what you are looking for!  |O

Don't go to MAPS.  Instead, dump the product selector table for all FLASH MCUs to a spreadsheet CSV file (it has an export facility) after doing show all products and view all columns then sort and filter it yourself. http://www.microchip.com/ParamChartSearch/chart.aspx?branchID=1005

For all the 8 bit PICs I further filter it by cross-referencing the device names it against a list of devices derived from the  PIC MPASM includes created by searching the MPASMX folder for *.inc containing a specific SFR or bit name for the version of the peipheral I must have, using https://www.mythicsoft.com/filelocatorlite that can export the search results to the clipboard so I can directly post it into Excel.

For 16 or 32 bit PICs you'd have to use the XC16 or XC32 device specific headers instead.

CAUTION: The underlying Microchip product database behind MAPS and the product selectors is corrupt. After filtering, ALWAYS verify that essential peripherals for your application are present and usable using ALL of the datasheet, errata and the include file BEFORE ordering!
« Last Edit: November 06, 2016, 08:56:55 pm by Ian.M »
 

Offline step_s

  • Regular Contributor
  • *
  • Posts: 138
  • Country: dk
Re: PIC programming advice
« Reply #11 on: November 06, 2016, 08:50:40 pm »
Cheers on the suggestions.
As Ian says, the maps is a bit confusing and it seems to leave out some suggestions, but it's quite helpful :)

The normal search doesn't have the filter option for ECCP, neither does it say in the exported file :/

The PIC16F1824/5 is the one that shows up when I search for 2 ECCP and 2 CCP with 14 pins. Cheap as well!
Looks like I will test these out then!

The idea I meantioned before was a hardware "switch", from a dual comparator, where a too high current or voltage would force the Clear pin low on the IC, to shut it down. Now that you have shown me the other PIC's, it's almost stupid to do that :P
 

Offline step_s

  • Regular Contributor
  • *
  • Posts: 138
  • Country: dk
Re: PIC programming advice
« Reply #12 on: November 07, 2016, 02:36:47 am »
There you go opening a whole new can of worms. . . xD
From the Microchip document on NCO that would be very useful. High frequency and high resolution, even on lower speed MUC's. Although I have no idea on how to do this at the moment. . Might be worth looking into.

Was looking more for a 14pin chip, so something like PIC16F18326? How would I start on doing this?
 

Offline Ian.M

  • Super Contributor
  • ***
  • Posts: 8144
Re: PIC programming advice
« Reply #13 on: November 07, 2016, 12:59:16 pm »
Going back to the problem of finding PICs for a particular application, here's my offline Excel 97 copy of the product selector data, with extra columns to link to individual product pages, to select from a shortlist, and to allow selection on legacy tool support.  I've also added a column showing the total number of CCP + ECCP modules.   I generally update my copy of this annually, by creating a new sheet, pasting in the whole CSV file from the Microchip page, then adding the extra columns, formulae and formatting from the previous one.  When its all working I can delete the previous sheet.
 

Offline Ian.M

  • Super Contributor
  • ***
  • Posts: 8144
Re: PIC programming advice
« Reply #14 on: November 07, 2016, 04:19:00 pm »
Even on older PICs with the 'classic' ECCP module, it would be possible to implement the voltage control loop in hardware.   On chips with two comparators its possible to select either comparator output being high as the auto-shutdown control signal.  One comparator would monitor the switching transistor current and the other could be used for the voltage control loop to force cycle skipping using the auto-restart feature.  The current reference could come from the coarse 4 bit DAC of the comparator reference divider, and the voltage reference could be supplied by another PWM module + an external passive low-pass filter.   The result would be bang-bang control - i.e. it would run at whatever duty cycle had been programmed till the voltage threshold was reached then skip cycles,  To minimise LF output ripple, a software control loop would keep trimming the duty cycle to try to avoid too many cycles being skipped due to overvoltage, (detected by gating Timer 1 off the comparator output, and checking its count in a postscaled Timer 2 ISR)   This would also let you run the PWM much faster as you wouldn't need as much resolution.  7 bit resolution would probably be sufficient, allowing a PWM frequency of over 150KHz with 20MHz Fosc.
 

Offline step_s

  • Regular Contributor
  • *
  • Posts: 138
  • Country: dk
Re: PIC programming advice
« Reply #15 on: November 09, 2016, 04:41:16 pm »
@Ian and AcHmed99
So reading the TB3103 document, it seems pretty simple, as long as you have the high resolution DAC. I was looking at the 14 pin version PIC16F17005. I understand that the 4 and even 5bit resolution might be a bit coarse for the voltage regulation but wouldn't the 8bit resolution be enough to drive a 5V output well within USB ratings? (Document says you need 9bit resolution)
A little fast calculation tells me that 8bit should make it able to register down to 0.02V of resolution?

Also thanks for the document Ian :)
 

Offline step_s

  • Regular Contributor
  • *
  • Posts: 138
  • Country: dk
Re: PIC programming advice
« Reply #16 on: November 11, 2016, 01:54:19 pm »
Wouldn't the internal voltage reference work then? Since it's at 1.024V.
 

Offline Ian.M

  • Super Contributor
  • ***
  • Posts: 8144
Re: PIC programming advice
« Reply #17 on: November 11, 2016, 02:15:40 pm »
Microchip MCU internal references *SUCK* (golf balls through a hose grade suckitude)  Check the datasheet for its tolerance and you'll see why.   
 

Offline step_s

  • Regular Contributor
  • *
  • Posts: 138
  • Country: dk
Re: PIC programming advice
« Reply #18 on: November 11, 2016, 05:18:07 pm »
@Ian
Ye, I have noticed the tolerance before, and it seems to float pretty badly. .

@AcHmed
Ye, 20mV was what I calculated myself, but that is more than plenty :)
The plan was to use a resistive divider, since that would also work for higher voltages.
Cheers on the schematics, they help out a lot!
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf