I know you want to change the voltage and measure the current. The problem is there is no current limit. Current can get very large.
SO
My plan is to set the current and measure the voltage.
I=0, V=0
I=2mA, v=30mV
I=2mA, V=50mV
......
I=500mA, V=540mV
The "PWM" is a output from the micro and goes from 0 to 100% or (0 to 5V).
The 0 to 5V is divided by 10 to get 0 to 500mV. This voltage is across R1 and sets the current.
You will get 2mA/step.
"ADC" is a input to the micro. You need to subtract the voltage across R1 to get a good value.
Voltage across D1 = ADC-(PWM/10)