ISR for ADC's

Coding and general discussion relating to user created compiler modules

Moderators: David Barker, Jerry Messina

Post Reply
richardb
Posts: 306
Joined: Tue Oct 03, 2006 8:54 pm

ISR for ADC's

Post by richardb » Thu Feb 11, 2016 11:07 am

HI, I was wondering if anyone's done an ISR to read 6 ADC values and store the results in globals.
I did try searching but didn't get anywhere as the term interrupts is "too common"!?

I'm have built a power supply using a pic26k80 and 3 pwm outputs but I need more time for computation and the waiting for the adc's takes up a big portion of my cycle time.

Any suggestions?

Rich
Hmmm..

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

Re: ISR for ADC's

Post by Jerry Messina » Thu Feb 11, 2016 12:45 pm

You'll likely have to tweak the standard ADC.bas module. If you have everything set properly you should be able to get an ADC reading every 16us or so (plus a bit of software overhead),
but that depends on the source impedance of what you're connecting the ADC to and the clock freq you're running at.

If you need to wait for all 6 readings then that'd be 6 x 16us = 96us.

Many times interrupts don't really speed things up... they just add add'tl overhead and actually make things slower.
How fast do you need it, and how are you arranging things now? Is there something in your main loop that can't wait the 16us or 100us?

User avatar
David Barker
Swordfish Developer
Posts: 1214
Joined: Tue Oct 03, 2006 7:01 pm
Location: Saltburn by the Sea, UK
Contact:

Re: ISR for ADC's

Post by David Barker » Thu Feb 11, 2016 2:12 pm

Top tip...

You also have to be very careful using interrupts when "writing to globals", particularly if the data is greater than 8 bits (1 byte). For example, your main program may be reading the first byte of a 16 bit value then an ISR triggers. The word variable gets changed, you return to the main program and read the second byte - see what's happened here? You have read a single variable that represents two separate events. Then Jerry or I see a post with "erroneous results when using ISR" ;-)

If you want to use an ISR to do the ADC reading and then store the values, make sure you halt the ISR before reading data in your main program. This assumes timing or speed isn't really an issue. If it is, you need to get creative and tread carefully...

richardb
Posts: 306
Joined: Tue Oct 03, 2006 8:54 pm

Re: ISR for ADC's

Post by richardb » Fri Feb 12, 2016 2:12 pm

my thinking was that, at the moment I cant do any calculations until the adc has finished so I waste over 100 uS. for every calculation cycle.

The problem I'm having is that my load is very dynamic and I need the voltage to be more stable than it currently is. It doesn't help that the PWM resolution is only 320 so each step change in the output is relatively large approx. 0.1V per count.
so I need to dither the pwm values.

as for globals I was thinking I could just have a flag that main sets just before reading so that the ISR checks before updating, but I haven't thought about this too much. I was hoping that someone had already looked at this :)
Hmmm..

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

Re: ISR for ADC's

Post by Jerry Messina » Fri Feb 12, 2016 6:02 pm

a few questions then:

- what total cycle time (read adc + calculations) are you trying to target?
- how long do the calculations take?
- do you have to get all 6 adc readings at once or can you read a few channels, do some calcs, read more channels, do next calcs, etc?

richardb
Posts: 306
Joined: Tue Oct 03, 2006 8:54 pm

Re: ISR for ADC's

Post by richardb » Mon Feb 15, 2016 8:33 am

what total cycle time (read adc + calculations) are you trying to target?
slightly embarrassingly I don't have that information to hand. but it was approximately 300us cycle time of which adc reading was over 100us, which was why I was hoping to overlap the calcs with the acquisition times.
do you have to get all 6 adc readings at once or can you read a few channels, do some calcs, read more channels, do next calcs, etc?
I have 3 psu channels each with a current and voltage signal feedback. the load is unusual in that it both sinks and sources current on one of the rails.

I read and use each signal independently. there are some other quirks with the current sensing chip that makes more work out of things as it gives a high current signal if the voltage is below 2.7V :evil:

I'm starting to think my biggest problem is actually the PWM resolution. I have to run at 200kHz which gives a resolution of 320 and I'm working at 36V which leaves me with only ~0.1V per step change so if my error algorithm oversteps then its 300us before I can change it again, which is 60cycles of the PSU.

this was why i have tried dithering the pwm with some sucsess but there are issues with this as i need to make sure i dither the pwm for the same amounts of time.

Maybe I could use a timer or counter? to regularly change the pwm values to give better apparent resolution? what do you think.
Hmmm..

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

Re: ISR for ADC's

Post by Jerry Messina » Mon Feb 15, 2016 12:08 pm

but it was approximately 300us cycle time of which adc reading was over 100us, which was why I was hoping to overlap the calcs with the acquisition times
You could probably get this a little better by splitting "readADC" up into a number of individual steps and using a state machine to interleave some of the calcs, but right now even if you can hide all of the ADC conversion time you still have ~200us loop time. Maybe you'd have better luck trying to optimize the calculations themselves. What does the math look like?
Maybe I could use a timer or counter? to regularly change the pwm values to give better apparent resolution?
I suppose that might work, but you're still going to have errors since you don't get any real feedback until 300us later.

richardb
Posts: 306
Joined: Tue Oct 03, 2006 8:54 pm

Re: ISR for ADC's

Post by richardb » Mon Feb 15, 2016 2:06 pm

the maths at the moment isn't that complicated.

something like the following but with extra limit testing

Code: Select all

error =(vmeasured-vref)/gain

pwm = pwm- error
the problem with this, is that there is no compensation for the rate of change of voltage.

what I have just tried is just running with only one channel, so 3 times the update rate and it made almost no difference to the ripple :( so the algorithm needs improving rather than just update rate.

I am feeling I will have to redesign the board with a more analogue control system. I may try hacking an error amp between the voltage input and the adc...

thanks for the help BTW
Hmmm..

SHughes_Fusion
Posts: 219
Joined: Wed Sep 11, 2013 1:27 pm
Location: Chesterfield

Re: ISR for ADC's

Post by SHughes_Fusion » Fri Feb 19, 2016 10:19 am

Daft question, but have you considered using an LED driver chip to generate your PWM control signal? NXP for example do some I2C controlled ones with fairly high PWM resolutions and I've also seen some which offer things like staggered outputs and PWM dithering. It might add a couple of quid to your BOM but would simplify the software a heck of a lot.

Otherwise, could you use a flag-based system to avoid global clash? If I'm reading you correctly your 6 ADCs come in two pairs so you could have three booleans which the ISR checks, plus a variable local to the ISR which keeps track of which channel it is currently converting.

Your calculation code would start off by starting the ADC and setting the flag for the first pair and waiting until the ISR resets the flag.

You'd then start the ADC again and set the flag for the second pair and start calculations on the first pair of results.

Once you are done, wait for the second flag to clear then set the third flag and so on.

That way, the main is processing variable the ISR isn't touching.

The ISR would handle channel selection based on an internal counter, the main just needs to start the ADC for the first of the pair. The interrupt would start the second conversion of the pair but nothing beyond that. Keeping the ADC starting under the control of the main avoids issues where calculations take too long.

richardb
Posts: 306
Joined: Tue Oct 03, 2006 8:54 pm

Re: ISR for ADC's

Post by richardb » Fri Feb 19, 2016 5:59 pm

To be honest I wanted a fix for this design. As a design its pretty nice, low component count and quite flexible in what voltages it can produce, and given time I might be able to reduce the voltage ripple to a reasonable amount.

I've designed quite a few SMPS over the years this was the first digital one.:(

for now I am working on other things, as the product works but is a bit noisier than I would like.

when I get back to it I think I'll try simulating things like better resolution and cycle times to see what that does for me.

If I do a new design I will probably go back to analogue :(

or maybe not :)

PS I wouldn't recommend INA168's for current sensing.
Hmmm..

richardb
Posts: 306
Joined: Tue Oct 03, 2006 8:54 pm

Re: ISR for ADC's

Post by richardb » Fri Feb 19, 2016 6:03 pm

I just had another thought.


Could I get a pic32 with similar pinouts as a pic18f26k80? I know I should stop being lazy and look for myself but maybe one of you guys know!!!

Thanks in advance

PS obviously I was wondering if I could use the firewing compiler
Hmmm..

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

Re: ISR for ADC's

Post by Jerry Messina » Fri Feb 19, 2016 9:39 pm

Could I get a pic32 with similar pinouts as a pic18f26k80?
Not as far as I know. The pic24/33 and pic32 have different pinouts from the pic18. VDD and VSS are in different places, so even a part with PPS doesn't help.

richardb
Posts: 306
Joined: Tue Oct 03, 2006 8:54 pm

Re: ISR for ADC's

Post by richardb » Fri Feb 19, 2016 10:08 pm

Thanks jerry
Hmmm..

TonyR
Posts: 75
Joined: Fri Jan 14, 2011 11:49 pm
Location: Sydney
Contact:

Re: ISR for ADC's

Post by TonyR » Wed Feb 24, 2016 1:26 am

If you are still stuck with this I did it with PIC18FK22 and it works fine. But my ADCs are ADS1100s (16 bit - interfaced by I2C) in an ISR called at 1 second interval. Data is stored in globals and accessed by serial port at random.

Let me know and I'll help out if I can.

richardb
Posts: 306
Joined: Tue Oct 03, 2006 8:54 pm

Re: ISR for ADC's

Post by richardb » Mon Feb 29, 2016 7:41 am

Thanks tony, when I get back to it I might take you up on the offer.

Richard
Hmmm..

Post Reply