[Solved-I guess :)] Issue by reading current feedback from a H-bridge MC33887 - Page 1
Forum section: electronics / projects

author: Fab6657
author_rank: Contributor
timestamp: March 21, 2015, 08:23:12 am
content: Hello to alli like to ask you because i am facing to a problem by reading the currentfrom a H-bridge MC33887.The MC33887 is interfacing from a Microchip 18F26K22. A 12V motoris driven, PWM works fine, reverve, bridge enable and fault feedback also.My problem is at the ADC conversion from the current feedback pin.The current according the datasheet is 1/375 of the output current of the bridge.This feedback current is passing a 150R to elevate to voltage.The voltage is reading by the ADC channel 2 (pin RA2 of the pic).By reading the voltage on the resistor (resistor 150R and capacity 1µF)with a voltmeter, the voltage is stable and according to the 1/375 ratio.But the problem is by the value converted by the ADC.The value is the right value but it make springs as so a lot of noiseare reading. Current go from 0 to 1.8A, so voltage on the ADCis from 0V to 0,72V.Until yet i have not found the cause. Do you already facing this ?Could be the read voltage to low for the ADV (Vref = 5V from power supply).I guess the registry settings from the PIC are correct, but a mistake can happen.The LCD is displaying the current, and display hops in the value.The display value basically is the right, but make springs.....Thank's for help,Greetings from France,Fabrice

author: Fab6657
author_rank: Contributor
timestamp: March 21, 2015, 03:10:15 pm
content: Hi,me again, searching through the problems.Timer0 interrupts are use to display the time. One idea perhaps, that the Timerinterrupt generate this issue. So i deactivate General Interrupt before measureand activate again just after. The problem is still the same.What could be else ? I search .....Thanks for ideas,Fabrice

author: H.O
author_rank: Frequent Contributor
timestamp: March 21, 2015, 04:33:18 pm
content: If you feed a stable voltage into the ADC does it produce a stable reading on the display?Have you looked at the deeback voltage with an oscilloscope? It's quite possible that the voltage is that noisy and that the multimeter just filters it out.If you're not triggering the A to D conversion from the PWM module then it mightdo a conversion at the beginning of the cycle, Another one at the end of the cycle and yet another somewhere in the middle.If the current feedback is for display purposes only (and not for closed loop current control) then take 10 or 50 or 100 readings and average them, see if that makes it any better,

author: Fab6657
author_rank: Contributor
timestamp: March 21, 2015, 05:26:52 pm
content: Hi,thanks for feedback,Quote from: H.O on March 21, 2015, 04:33:18 pmi found in the meantime with the oscilloscope that the signal is a 4Khz signaluntil 0.6V. Over this value from the h-bridge feedback the signal is stable.The problem is that is near the highest value that the h-bridge should work in this application.I must check the PWM value in the program, but i think this is the half of my PWM setting.My school is far behind, could that be a reaction of the both input diode inthe AN converter from the PIC ?The current feedback is for displaying but also for control, and action ifcurrent is too high in the application (not for the spec of the h-bridge).One way could be to add an non inverting AOP to adjust in a higher rangethe voltage? Your Idea with the average value is also a way, but i make ameasure each second, and i cannot wait 10 or more seconds.Thanks, i continue my investigationsFabrice

author: Fab6657
author_rank: Contributor
timestamp: March 21, 2015, 06:28:29 pm
content: Hi again,i guess, the reason why after 0.6V the signal is stable is perhapsbecause the PWM reach or is egal to 100%So the current should be also stable.But how i can measure an average value of the current ?Like you give me the tip with more measure pointsand calculate his average ?May be this is the simplest way, and without no more components.Ok, i search a way to code this in the microcontroller...I continue my learning phase....Fabrice

author: Fab6657
author_rank: Contributor
timestamp: March 22, 2015, 05:06:16 am
content: Hi,back to school for meI completely forgot that is PWM current.So the initial question wasAnyway, that help me to found out a way to calculate the average value.Found on a another forum :And even better yet, you can use exponential smoothing.New average = alpha*(New Sample) + (1-alpha)*(Old average)alpha is a parameter. Must be less than 1. The less it is the more smoothing you exert.This way you do not need to keep track of your past samples. You only need to store the new average. Calculation can be done real quick in integers, like for alpha = 0.125Code (C):average = (sample + 7*average + 4)>>3;I will try today.Soon new feedbackFabrice

author: Fab6657
author_rank: Contributor
timestamp: March 22, 2015, 06:08:15 am
content: Hello,i made some tests with the adc routine. It is written with MikroBasic from MikroElektronika and run on a PIC 18F26K22 :Code:sub procedure AmpPump()AmperePumpRead = ADC_read(2)AmperePump = (AmperePumpRead + (7*AmperePump) + 4) >>3 ' Calculate the average value (see: http://www.electro-tech-online.com/threads/average-voltage.140560/#post-1174006)if UpdateLCDSecondsFlag = 1 then ' Refresh the screen each secondtlong1 = (((AmperePump * 375)*5)/150) ' Adapt from readed ADC value to Amperech1 = (tlong1 / 1000) mod 10 ' Extract unit from the valueLcd_Chr(2,7,48+ch1) ' Display unitLcd_Chr_CP(".") ' Display the "." between unit and 1/10 unitch1 = (tlong1 / 100) mod 10 ' Extract 1/10 from the valueLcd_Chr_CP(48+ch1) ' Display the 1/10 unitch1 = (tlong1 / 10) mod 10 ' Extract 1/10O from the value tlong1Lcd_Chr_CP(48+ch1) ' Display the 1/100 unitLcd_Chr_CP("A") ' Display current symbol after last characterend ifend subIt is much better, but not so average as i want (just when PWM reach 90%)Ideas how to improve the code ?Nice dayFabrice

author: Fab6657
author_rank: Contributor
timestamp: March 22, 2015, 08:35:49 am
content: Hello,i write the routine in this way, it is much better :Code:sub procedure AmpPump()AmperePumpRead = ADC_read(2)'AmperePump = (AmperePumpRead + (7*AmperePump) + 4) >>3 ' Calculate the average value (see: http://www.electro-tech-online.com/threads/average-voltage.140560/#post-1174006)AmperePump = ( AmperePumpRead + (15*AmperePump)) / 16 ' Calculate the average value (see:http://www.chris15.com/pages/ponder.php)if UpdateLCDSecondsFlag = 1 then ' Refresh the screen each secondtlong1 = (((AmperePump * 375)*5)/150) ' Adapt from readed ADC value to Amperech1 = (tlong1 / 1000) mod 10 ' Extract unit from the valueLcd_Chr(2,7,48+ch1) ' Display unitLcd_Chr_CP(".") ' Display the "." between unit and 1/10 unitch1 = (tlong1 / 100) mod 10 ' Extract 1/10 from the valueLcd_Chr_CP(48+ch1) ' Display the 1/10 unitch1 = (tlong1 / 10) mod 10 ' Extract 1/100 from the value tlong1Lcd_Chr_CP(48+ch1) ' Display the 1/100 unitLcd_Chr_CP("A") ' Display current symbol after last characterend ifend subIf could be improve, please tell me. Could be probably improved...Kind regardsFabrice

author: Zero999
author_rank: Super Contributor
timestamp: March 22, 2015, 10:51:36 am
content: You could certainly make that code much faster by eliminating some of that division which will be very slow as it'll be done with software, rather than hardware.

author: H.O
author_rank: Frequent Contributor
timestamp: March 22, 2015, 01:11:14 pm
content: QuoteWell, yes of course. If the dutycycle is 100% then there's no risk of the A to D conversion being done while the motor is not actually driven. As you lower the dutycycle more and more of the conversions will be performed while the motors isn't driven.How often do you make a conversion? You said once a second previously but from your code I suspect you might be doing the conversion at a faster rate and then only update the display once a second?Anyway, if the PWM frequency is 5kHz (period of 200us) then, when you need a new reading, take 10 (or as many as you can/like/want) readings evenly spaced over a period of 200us. That will ensure you get the average current over the whole PWM period. Or, if you're PIC supports it (which I don't Think the 26K22 does but you verify), use the special event trigger to trig the A to D conversion from the PWM module so that the conversion always happens at the same "place" relative to the PWM period.

author: Fab6657
author_rank: Contributor
timestamp: March 22, 2015, 02:35:09 pm
content: Hi,thank's for feedback.Quote from: Hero999 on March 22, 2015, 10:51:36 amCan you advice me more what you mean. If i understand, the /16 division should be keep left, and i place a other division (or shift) later perhaps after an iteration of 16 measure or more ?Quote from: H.O on March 22, 2015, 01:11:14 pmThe conversion is made in the main loop, so often like the cpu have resources. Timer interrupt will happen for increment the second counter.At each second a flag is set and the LCD display is refreshed with the actual calculate value from the following formula:AmperePump = ( AmperePumpRead + (15*AmperePump)) / 16According the datasheet of the PIC (and if i no made mistakes), with a 16Mhz internal clock, the ADC need 125ns.I have no idea how much the conversion is done, but i can set a value with increment at each conversion and display it.Thanks again for the helpFabrice

author: Zero999
author_rank: Super Contributor
timestamp: March 22, 2015, 07:35:40 pm
content: If the compiler is smart then it should replace the /16 with a shift.The conversion to a string will take a long time, with all of those divide and modulo operators. Again it depends on how smart the compiler is.Multiply is faster than divide or modulo (especially as the MCU you're using has a hardware multiplier) so you could replace the modulo with a multiply but it will give the digits from low to high.Code:'Where number is the value you want to covert to a stringdoshadownumber = numbernumber = number \ 10digit = shadownumber - number * 10PRINT digit ' Get your MCU to write the digit to the LCD or store it first.loop until number = 0Another thing you could to is avoid division and multiplication altogether by subtracting in a 1, 2, 5 sequence (5000, 2000, 1000, then 500, 200, 100 etc.) and recording each subtraction, until you get to zero. I'll provide a more detailed explanation if you need.If it's already fast enough as it is then don't worry but it's good to bear all of this in mind in future.

author: Fab6657
author_rank: Contributor
timestamp: March 23, 2015, 04:47:19 am
content: Hello,thank's for information, i will (try) keep in mind.Kind regards,Fabrice

author: nuno
author_rank: Frequent Contributor
timestamp: March 23, 2015, 09:09:35 pm
content: If your PWM frequency is too low "compared" to the motor's winding inductance, the motor's current ripple can be very high and you can read very different "consecutive" values with the ADC. The worst condition for a motor driver is to drive it with a stalled shaft.

author: Fab6657
author_rank: Contributor
timestamp: March 24, 2015, 03:39:07 am
content: Hi Nuno,another thing to consider. An other question, it is possiblethat the current is linear according to the PWM ?PWM 100%, max current,PWM 50%, half maw currentor are the rules (if they exists) differents ?ThanksFabrice

author: nuno
author_rank: Frequent Contributor
timestamp: March 24, 2015, 09:02:17 am
content: Current doesn't depend only on PWM duty cycle, it's more complex than that, rotor speed matters, PWM frequency, ... and it may also be limited to a maximum value by the controller. This is a good source of info about h-bridges:http://www.modularcircuits.com/blog/articles/h-bridge-secrets/

author: Fab6657
author_rank: Contributor
timestamp: March 24, 2015, 06:15:29 pm
content: Quote from: nuno on March 24, 2015, 09:02:17 amNice article and good information's for me, even if math are not the best for me.But is not complex math for a lot.ThanksFabrice