Current stepping via transistor DAC
This commit is contained in:
72
src/main.rs
72
src/main.rs
@@ -35,9 +35,6 @@ pub static BOOT2: [u8; 256] = rp2040_boot2::BOOT_LOADER_GENERIC_03H;
|
|||||||
/// if your board has a different frequency
|
/// if your board has a different frequency
|
||||||
const XTAL_FREQ_HZ: u32 = 12_000_000u32;
|
const XTAL_FREQ_HZ: u32 = 12_000_000u32;
|
||||||
|
|
||||||
// below this we won't be able to turn on the transistor (0.6v)
|
|
||||||
const MIN_DUTY: u16 = 6000;
|
|
||||||
|
|
||||||
#[rp2040_hal::entry]
|
#[rp2040_hal::entry]
|
||||||
fn main() -> ! {
|
fn main() -> ! {
|
||||||
// Grab our singleton objects
|
// Grab our singleton objects
|
||||||
@@ -79,33 +76,19 @@ fn main() -> ! {
|
|||||||
.clock_divider(47999, 0)
|
.clock_divider(47999, 0)
|
||||||
.start_paused();
|
.start_paused();
|
||||||
|
|
||||||
let mut pwm_slices = hal::pwm::Slices::new(pac.PWM, &mut pac.RESETS);
|
|
||||||
|
|
||||||
let pwm = &mut pwm_slices.pwm2;
|
|
||||||
// info!("PWM Top: {}", pwm.get_top());
|
|
||||||
pwm.set_ph_correct();
|
|
||||||
pwm.enable();
|
|
||||||
|
|
||||||
let pwm_channel = &mut pwm.channel_a;
|
|
||||||
pwm_channel.output_to(pins.gpio20);
|
|
||||||
pwm_channel.set_duty_cycle(MIN_DUTY);
|
|
||||||
|
|
||||||
// calibrate
|
|
||||||
delay.delay_ms(2000);
|
|
||||||
|
|
||||||
// let mut led_pin = pins.gpio25.into_push_pull_output();
|
// let mut led_pin = pins.gpio25.into_push_pull_output();
|
||||||
|
|
||||||
let mut duty = MIN_DUTY;
|
let mut tick = 0u8;
|
||||||
|
|
||||||
let mut power = 0;
|
|
||||||
|
|
||||||
let mut tick = 19u8;
|
|
||||||
|
|
||||||
let mut peak_v = 0u32;
|
let mut peak_v = 0u32;
|
||||||
|
|
||||||
loop {
|
let mut current_pins = [
|
||||||
pwm_channel.set_duty_cycle(duty);
|
pins.gpio16.into_push_pull_output().into_dyn_pin(),
|
||||||
|
pins.gpio17.into_push_pull_output().into_dyn_pin(),
|
||||||
|
pins.gpio18.into_push_pull_output().into_dyn_pin(),
|
||||||
|
];
|
||||||
|
|
||||||
|
loop {
|
||||||
delay.delay_ms(500);
|
delay.delay_ms(500);
|
||||||
adc_fifo.clear();
|
adc_fifo.clear();
|
||||||
adc_fifo.resume();
|
adc_fifo.resume();
|
||||||
@@ -116,42 +99,17 @@ fn main() -> ! {
|
|||||||
|
|
||||||
adc_fifo.pause();
|
adc_fifo.pause();
|
||||||
|
|
||||||
let mut samps = [0u16; 8];
|
for (i, cp) in current_pins.iter_mut().enumerate() {
|
||||||
|
let mask = 0b1 << i;
|
||||||
|
|
||||||
for i in 0..8 {
|
if mask & tick > 0 {
|
||||||
samps[i] = adc_fifo.read();
|
cp.set_high();
|
||||||
|
} else {
|
||||||
|
cp.set_low();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let avg = avg_fifo(&samps, false);
|
tick = (tick + 1) % 8;
|
||||||
let voltage = (avg * 3300) / 4095;
|
|
||||||
|
|
||||||
let duty_as_pct = (duty - MIN_DUTY) as u32 * 100;
|
|
||||||
let duty_as_pct = duty_as_pct / (0xFFFF - MIN_DUTY) as u32;
|
|
||||||
|
|
||||||
let new_power = (voltage * (1 + voltage / 100)) * duty_as_pct;
|
|
||||||
|
|
||||||
tick = (tick + 1) % 20;
|
|
||||||
|
|
||||||
if tick == 0 {
|
|
||||||
info!(
|
|
||||||
"{} duty, {}mV, {} power, {} peak",
|
|
||||||
duty_as_pct, voltage, new_power, peak_v
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if duty_as_pct == 0 || voltage > peak_v {
|
|
||||||
peak_v = voltage;
|
|
||||||
}
|
|
||||||
|
|
||||||
duty = duty.wrapping_add(1000);
|
|
||||||
duty = MIN_DUTY.max(duty);
|
|
||||||
// if voltage > (90 * peak_v) / 100 || new_power >= power {
|
|
||||||
// duty = duty.saturating_add(33);
|
|
||||||
// } else {
|
|
||||||
// duty = MIN_DUTY.max(duty - 32);
|
|
||||||
// }
|
|
||||||
|
|
||||||
power = new_power;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user