# 2018.02.20 - Reading motor phase current from the DC link current (shunt)

Following the application note:
 Linked file: AN2267 - Implementation of current regulator for BLDC motor control with ST7FMC.pdf

it should be possible to read motor phase current with “satisfactory results” from DC link current (shunt current).

The PWM interrupt code was changed so the interrupt always happen at the middle of current pulse. The ADC channel 6 reads the pulse value at start of interrupt and after all the other ADC channels are read.
Note the line debug_pin_set (); that is when ADC channel 6 read happens and will enable a digital pin so we can see on the oscilloscope.

And on oscilloscope, blue line is the digital pin signal used for debug and as we can see, the pulse (yellow line) is read by the ADC right on the middle.
Yellow line is shunt voltage (after amplification).
The next pictures show different current values and that signal is always read at middle.
Note that the firmware only reads 1 pulse every PWM cycle while there are 2 pulses every PWM cycle.

And log from real values:

Validation
Example of point on the graph:
- duty_cycle: 85 (33%)
- battery current (green line): 82
- motor phase current (purple line): 100
- lab power supply was measuring 2.5 amps

1. Battery current was 2.5 amps. The green line had a value of 83, which is a delta of 8 (83 - 75, where 75 is the ADC value for 0 amps), so every ADC step is 2.5/8 = 0.31 amps.
2. The motor phase current had a delta of 25 (100 - 75), which represents a value of 25*0.31= 7.75 amps.
3. The mathematical model says that: battery current = duty_cycle * motor phase current. As the duty_cycle was 33%, so the motor phase current should be (battery current / 0.33) = 2.5/0.33 = 7.6 amps <--- the measured values seems to be inline with the mathematical model.