Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Current sensing / A/D issues with PIC18f4550

Status
Not open for further replies.

ninjathegreat

New Member
I've been trying to get current information from a DC motor that I'm running using the PWM (P1A single mode). I have the L298N connected, and a .5 ohm current sensing resistor connected to pin 15.

The problem I have is that the USART output looks like this:

24 24 25 23 0 0 0 23 26 0 0 25 26 26 0 0 0 .....

Why do I get the 0s? Am I making some error?

I've connected pin 15 of the L298N to AN0 (pin2). this is how my MPLABC code looks like (relevant portions only):

main(){
TRISA = 0xff;
.
.
.
.
while(1){
testADC();
}
}

testADC(){

float i;
char *c;

i = getCurrent();
atoi((int) i, c);
putsUSART(c);
Delay10kTCYx(3);
}

float getCurrent(){
float i;
unsigned int v;

ADCON1 = 0x0e; // reference: VDD and VSS, AN0 analog
ADCON0 = 0x00 // Channel 0 (AN0)
ADCON2 = 0x80 // right justified, 0 TAD, 2*TOSC conversion

ADCON0bits.ADON = 1; // turn on A/D
ADCON0bits.GO_DONE = 1; // start conversion

while (ADCON0bits.GO_DONE); // wait for completion

v = ADRESH * 256+ADRESL; //voltage in A/D levels
i = v*2*.0048*100 // current in mA, .5 ohm current sense resistor

return (i);
}

Appreciate suggestions,
thanks
N.

P.S. I am not doing the interrupt thingy now, that's the next step...
 
Assuming that you are getting the correct values from the ADC sensing the drop across the sense resistor, your results look about right. Is 26 mA what you expected to be seeing?

You said you were driving the motor with PWM. Think about the definition of PWM - Pulse Width Modulation. It might help to draw the wave form of the voltage to the motor. Think about when you you are sampling the voltage across the sense resistor. What is the current (and thus voltage across the sense resistor) when there is no voltage supplied to the motor? Then think about when your getcurrent() routine runs. Don't you want it to run only when you are applying voltage to the motor? Your code has no way of doing that.
 
Last edited:
Utilizing a shell sort will get rid of anomalies and is a lot more accurate then using averages, consider the following 10 samples;

24
25
0
25
24
0
22
0
24
26
24​

The average would be the sum of all samples / the number of samples leaving you with 194 / 10 = 19.4 which is clearly not the result your looking for.

Shell sorting utilises shift samples stored in an Array into sequential order, and then extracts the median (middle value) eg,


0
0
0
22
24
24
24
25
25
26

The answer would be the middle value, in this case sample #5, and that is 24


Sure it takes slightly longer to sample, and the more you sample the more accurate it becomes depending on your application, but the anomalies are gone.

**broken link removed**has this routine built into its ADC.bas library, its called ADC.ReadMedian(Channel)
 
Thanks philba and gramo.

Yes, I think the PWM might be the problem. One of the old ADC (in MPLAB C18) functions used to have a PWM trigger - that does not seem to exist now.

I did discover that I am not getting the right ADC results, though. my mistake - It's supposed to be around 230 mA; which can be explained by that missing zero in the multiplier :).

One of the things that's happened since I posted the message is that the whole system is now haywire. I'm sensing current from pin 15 of the L298N. A .5 ohm, 12W current sensing resistor is connect to ground and a wire goes to ANO from pin 15, too.

technically, with this code, I should get a fair estimate of the current through the device. Well, I don't - no idea what the reason is. May be the PWM is messing things up.

Any idea how I can use the rising edge of the PWM to trigger ADC conversion?

I also want to make sure that I'm doing the right thing with reading the result of the conversion...

If not, I will have to go with gramo's note on medians...

To explain the application, I'm measuring the current through the motor terminals, and I need to get the motor to stop when it hits a physical limit switch and the load lead sto a jump in the current...

P.S. I won't be available from tomorrow evening till Wednesday - give you all some time to help me out! :) Have a great long weekend!
 
You don't say which PWM you are using (Standard or Enhanced) but I believe you can get timer2 interrupts in both cases. You can use that to start the ADC process.

At the worst case, you could run the PWM output pin into an interrupt pin.
 
There are some points to consider though;

If you interrupt drive your sampling intervals, then you will be performing an ADC sample at the exact same time after every interrupt, and as your target device has inductive properties and is PWM driven, who's to say that the current draw is at its maximum when the sample begins?

You could very well be creating a routine that is very inaccurate :rolleyes:

I was thinking about this fact earlier, and taking the median might not be the best method either.

Practical testing would be the best solution without going through the math :eek:
 
Last edited:
Use a resistor coming from the high side of the 0.5R sense and a capacitor between the other side of this resistor and ground.

This will smooth out most of the spikes and average your current reading.

Its standard practice when sensing PWM currents.
 
Use a resistor coming from the high side of the 0.5R sense and a capacitor between the other side of this resistor and ground.

This will smooth out most of the spikes and average your current reading.

Its standard practice when sensing PWM currents.

What kind of RC components should I use? The current sensing resistor is 12W; should I use RC components of similar power ratings...

Thanks!
 
I'm afraid I'm not really an analogue kind of guy so trying to choose the right components might be a bit hit and miss.

You don't need high power rating stuff for the smoothing side.

As a suggestion maybe try a 10k resistor and a 0.1uf capacitor to start with.
 
Still need a wee bit of help. I have the RC in parallel with the current sense resistor; I still am not getting the right values - one thing is I do get some voltage when there is no input, which is understandable from the RC point o view... any help?
 
Have a look at the datasheets for the L297 and L298 (I think) from ST - they have a similar RC system for the current sensing on their Stepper drivers.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top