Readadc while PWM is running

manie

Senior Member
Since the Mosfet problem has been settled (NOT blowing up) I've moved on in that project. Now passing decent amps with (fairly) cool Mfets...

In comes Readadc10 or Readadc. I'm reading the current that results from a certain PWM duty cycle. What happens is that while the 28x1 goes off reading the analogue from the Closed Loop Hall sensor, the PWM duty changes, this is also visible on the scope. I will take pics tommorow and post. After completing the Readadc(10), the scope shows the correct duty at that moment, untill the next Readadc(10) cycle. It is as if the actual pulse from PWM is chopped to what appears to be about 70% of the then set duty.

Example: Say period is 100ms, duty at 50%. When reading analogue the total period seems the same but smaller chops of not 50ms(50% duty) but more like 30ms with the same "off" or low period as normal. A picture will show this so I will post some....

Any ideas ?
Manie
 

manie

Senior Member
PS to above: The bit value obtained is also quite eratic, as if the actual readadc takes place at the top of the square wave (full on), or in the trough (low) or somewhere along the rising or falling edge. Very disturbing.

My clamp style multimeter (0 - 1000A AC/DC) shows this Amperage undulation clearly also, so the readadc(10) does effect the total "on" time of the Mfets. Don't know the reason but it does represent a big problem..
Manie
 

BeanieBots

Moderator
Are you running with clock higher than 4Mhz?
Some commands drop the clock back to 4Mhz during their execution and then back after completion. This would upset PWMout.

EDIT: You can edit and/or delete your own posts.
 

hippy

Ex-Staff (retired)
I couldn't see any double post to delete ... ;-)

By PWM do you mean using PWMOUT or something else ? You mention a period of "100ms" which is far slower than normal PWMOUT operation would give so it is hard to guess what the issue may be without knowing more about your code.

There was an issue identified with the use of variables in odd numbered word variables for the 28X1 which we are investigating. Is that the same case here ? Make sure you are not using w1, w3, w5 etc and the associated byte variables, b2, b3 , b6, b7 etc.

What firmware version are you using ?
 
Last edited:

womai

Senior Member
Are you using hardware serial input (hserin) in your program. There is a know issue with hserin, it messes up the PWM frequency (and maybe the duty cycle?). From what I know from Rev-Ed this looks like a bug in the PIC16F886's silicon.

Wolfgang
 

BCJKiwi

Senior Member
Not clear where the Readadc is being done.
If this is on an output from the pwm, then the value being read will be varying form 0 to max much faster then readadc can track. The solution is to smooth the pwmout with an RC or similar on the readadc input.
 

Dippy

Moderator
BCJ: "Not clear where the Readadc is being done."

Manie said "...goes off reading the analogue from the Closed Loop Hall sensor.."

So, hopefully we can assume he's not ReadADCing the PWM pin??? Please confirm.

If he is, then odd things are bound to happen and I will say "Doh!"..

But I think a schematic of those two sections are needed to show PICAXE connections.
AND a listing of that part of code.
On the face of it, and assuming he's not trying to read the PWM pin, then I can't imagine how the ADC reading can conflict with the PWM as they are independent peripherals in the PIC.

Many of us have made Switchers or motor controllers using PIC/PICAXE and if this problem was common then it would have shown up years ago.
Maybe 4 possibilities.
1. Faulty code
2. Faulty circuit
3. Faulty wiring - hefty switching can cause funnies
4. Faulty PIC (so, try another 28X1)
In the unlikely even that its a firmware issue then it needs sorting, but I would find that hard to believe.

I completely agree with BCJ about RCing things.
In a recent-ish design I did with an 8 Amp Switcher, I had to use RC right by the IC pins to smooth out voltage sensing returns which were ADCd.

Tangent:-
Switcher designs produce piles of harmonics which can be 'excited' by poor circuit design, cheapo components and inappropriate PCB layout. Lengths,widths and proximities of tracks can(will) have a big effect - especially where power tracks are in close proximity to signal.
Tracks have all the annoying LCRs but these effects can be minimised by using un-common sense.
Chuck in some poor grounding techniques and you have a recipe for lousy performance.
Sometimes even little things like 45o track bends vs right-angle... I bet only Westy and Andrew knew about that.

Anyway, this isn't a lesson in PCB layout.
Post your code and a schematic.
(BB, don't you dare mention....;) )
 

manie

Senior Member
Thanks so far, I'mm not home for the weekend so will get something together and post by Mondy evening or so. In the meantime some clarification:

Variables: declared as below.(I use convention where Word Var's end in "W" and Byte var's end in "B".)

Symbol thw=w8 'b0/1 readadc thr
symbol bvw=w0 'b2/3 readadc V
symbol shw=w2 'b4/5 readadc shunt (or Hall Effect Sensor)
symbol dcw=w3 'b6/7 duty cycle

Pulse command as below:

PWMOUT 1,249,dcw '(where dcw is the variable duty).
notes:

Chip running in native 4mHz
PWM pin connected directly to a "master" TC4420 Mfet driver chip which in turn drives the other six (parallelled) TC4420's (each driving its own Mfet).
I (at last) get a good square wave pulse on the Mfet Gates with very nice fast rising/falling edges shown on the CRT.

Reading analogue: The only way I get a reasonably accurate reading is by looping and averaging as below:

do until cnt2b=10 'read Hall-mV output, untill 10 readings made
readadc10 1, shw
if shw>505 then 'Hall Sensor ref.V=2499mV=zero base=505
cnt2b= cnt2b+1
acw1=acw1+shw 'add for average
debug shw
end if
pause 15 'pause between gets better result
loop
shw=acw1/cnt2b


The closed loop Hall Effect output is connected to a LM324 quad op-amp input 3(pin10), with unity gain (invert/non-invert outputs of op-amp - pins 8/9 connected together, which now feeds analogue input 1 on the 28x1.

When pulsing the power via the Mfets, the Hall Sensor produces a "ringing" sound which goes "chirpp..chirpp..chirpp..chiiiiirrrrrpp" in rythm with the CRT picture of the chopped up HIGH part of the duty cycle.

In fact, looking at the mV output from the Hall Sensor, it varied between 2.846V to 2.970V ! This gave me between 53A and 72A on the multi-meter and between 18A and 115A calculated by the program !

So how confused do you think I am????????
Manie
 
Last edited:

jglenn

Senior Member
Have you looked at the hall signal on a scope? If you want an average reading, add an RC filter, maybe a 1K and a 10uF, a time constant of .1 sec would work too.
 

manie

Senior Member
Jglenn: As much as 10uF !!?? The PWM is running at 4kHz, period is 250nS... surely the RC time constant must be shorter ?? Would you place this RC circuit between Hall Sensor output and 0V (C=====>>R in series) ?? eg.

Hall Output--------------------->>Op-amp input
|
|
C1
|
R1
|
0V
 

BCJKiwi

Senior Member
Generally a "low pass filter";

Code:
pwm or similar source --------- R ------+------ Output
                                        |
                                        C
                                        |
                                        0V
 

BCJKiwi

Senior Member
Well there are plenty of complicated formulas but if you have a scope, trial and error works well! All depends on voltage/current/shape of source waveform so a bit tricky for the crystal ball at this distance!;)

However, others may have a better idea (or crystal ball).

On reflection, you may get a startng point from Post #8 in this thread http://www.picaxeforum.co.uk/showthread.php?t=10139 which uses a pwmout output to directly control a FET and utilises an R-C to generate a 'clean analogue' signal on the FET gate. Your mileage will vary as all the elements in the control loop are different.
 
Last edited:

Dippy

Moderator
My crystal ball has been sent back.
So, you're going to put this RC between sensor and Op/amp aren't you.
And so are you making a very low pass filter with the op/amp?

Have you got a scope? No, oh dear. Yes, oh good.
Well, its still going to be guesswork for values as I assume you don't know the output impedance of your sensor?

Back to the PWM glitch.
Are you absolutely sure that it was only when you included the ReadADC command that it started?
So, in your example code you can take out the ReadADC line and PWM is happy?

Could it be that you are getting some big switcher-related nasties getting back to PICAXE and upsetting it? Maybe R/Cing all inputs may be needed. Or a re-design of your circuit/pcb to reduce the spikies in the first place.
Is this your first 'power' switcher circuit? Yes?
 

BeanieBots

Moderator
Just gone through all of this again to see if there is anything 'silly' going on.
Have you tried it without the 'debug' statement?
Take it out and then look at the PWM with a 'scope and see if ReadADC is still having any effect.
 

jglenn

Senior Member
Manie: You said you are using the hall sensor to measure load current, but sometimes the readadc happens when the fet is off, sometimes when it is on, or during switching. This is what is causing your reading variation. If you want to read the current when the fet is on, it should be prevented from reading at any other time.

I assumed you wanted to read average current, hence the suggested RC filter. The 10uF with 1K series R is one I've used with success with noisy sensors, it will filter about anything. But of course you cannot read fast events this way. I suggested a high speed current limit circuit in the past, this is almost always used in switching power supplies and drivers. There are even fets with it built in, along with overtemp shutdown. So in general, you want FAST for shutdowns, SLOW for average current measurements.
 

manie

Senior Member
Jglenn/BB/BCJ and Dippy, There are a number of points mentioned I will have to look at before I have better answers. Like remove the "debug", which is quick. Like I said, even the mV from the Hall varies up and down, so that must be influenced by the PWM duty variation. At any one point, the duty is "fixed" for that particular reading and is then adjusted to alter the required amps up/down. Will get back to you guys as soon as I've some better answers. Thanks so far.
Manie

Edit: Attached is a pic of the switching (general) when full PWM duty cycle is actually "on". I'll try and get a pic of the "broken/chattering" signal.
 

Attachments

Last edited:
Top