diff options
| author | Grant Miller <[email protected]> | 2024-09-06 13:53:49 -0500 |
|---|---|---|
| committer | Grant Miller <[email protected]> | 2024-09-06 13:53:49 -0500 |
| commit | 1a8977db7837a5635d3c5e0ae8213d1b3181ffb7 (patch) | |
| tree | 5bceb855c421f6572aa1909d997bd6d9dda3b895 /examples | |
| parent | cfc76cec711447300e2d957439a32d6ad418a58c (diff) | |
Update examples
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/stm32f4/src/bin/pwm.rs | 18 | ||||
| -rw-r--r-- | examples/stm32f4/src/bin/ws2812_pwm.rs | 4 | ||||
| -rw-r--r-- | examples/stm32g0/src/bin/input_capture.rs | 8 | ||||
| -rw-r--r-- | examples/stm32g0/src/bin/pwm_input.rs | 9 | ||||
| -rw-r--r-- | examples/stm32g4/src/bin/pwm.rs | 18 | ||||
| -rw-r--r-- | examples/stm32h7/src/bin/pwm.rs | 18 |
6 files changed, 37 insertions, 38 deletions
diff --git a/examples/stm32f4/src/bin/pwm.rs b/examples/stm32f4/src/bin/pwm.rs index 8844a9f0e..0cd65a258 100644 --- a/examples/stm32f4/src/bin/pwm.rs +++ b/examples/stm32f4/src/bin/pwm.rs | |||
| @@ -15,22 +15,22 @@ async fn main(_spawner: Spawner) { | |||
| 15 | let p = embassy_stm32::init(Default::default()); | 15 | let p = embassy_stm32::init(Default::default()); |
| 16 | info!("Hello World!"); | 16 | info!("Hello World!"); |
| 17 | 17 | ||
| 18 | let ch1 = PwmPin::new_ch1(p.PE9, OutputType::PushPull); | 18 | let ch1_pin = PwmPin::new_ch1(p.PE9, OutputType::PushPull); |
| 19 | let mut pwm = SimplePwm::new(p.TIM1, Some(ch1), None, None, None, khz(10), Default::default()); | 19 | let mut pwm = SimplePwm::new(p.TIM1, Some(ch1_pin), None, None, None, khz(10), Default::default()); |
| 20 | let max = pwm.get_max_duty(); | 20 | let mut ch1 = pwm.ch1(); |
| 21 | pwm.enable(Channel::Ch1); | 21 | ch1.enable(); |
| 22 | 22 | ||
| 23 | info!("PWM initialized"); | 23 | info!("PWM initialized"); |
| 24 | info!("PWM max duty {}", max); | 24 | info!("PWM max duty {}", ch1.max_duty_cycle()); |
| 25 | 25 | ||
| 26 | loop { | 26 | loop { |
| 27 | pwm.set_duty(Channel::Ch1, 0); | 27 | ch1.set_duty_cycle_fully_off(); |
| 28 | Timer::after_millis(300).await; | 28 | Timer::after_millis(300).await; |
| 29 | pwm.set_duty(Channel::Ch1, max / 4); | 29 | ch1.set_duty_cycle_fraction(1, 4); |
| 30 | Timer::after_millis(300).await; | 30 | Timer::after_millis(300).await; |
| 31 | pwm.set_duty(Channel::Ch1, max / 2); | 31 | ch1.set_dutycycle_fraction(1, 2); |
| 32 | Timer::after_millis(300).await; | 32 | Timer::after_millis(300).await; |
| 33 | pwm.set_duty(Channel::Ch1, max - 1); | 33 | ch1.set_duty_cycle(ch1.max_duty_cycle() - 1); |
| 34 | Timer::after_millis(300).await; | 34 | Timer::after_millis(300).await; |
| 35 | } | 35 | } |
| 36 | } | 36 | } |
diff --git a/examples/stm32f4/src/bin/ws2812_pwm.rs b/examples/stm32f4/src/bin/ws2812_pwm.rs index cbaff75fc..7a9fa302b 100644 --- a/examples/stm32f4/src/bin/ws2812_pwm.rs +++ b/examples/stm32f4/src/bin/ws2812_pwm.rs | |||
| @@ -61,7 +61,7 @@ async fn main(_spawner: Spawner) { | |||
| 61 | // construct ws2812 non-return-to-zero (NRZ) code bit by bit | 61 | // construct ws2812 non-return-to-zero (NRZ) code bit by bit |
| 62 | // ws2812 only need 24 bits for each LED, but we add one bit more to keep PWM output low | 62 | // ws2812 only need 24 bits for each LED, but we add one bit more to keep PWM output low |
| 63 | 63 | ||
| 64 | let max_duty = ws2812_pwm.get_max_duty() as u16; | 64 | let max_duty = ws2812_pwm.max_duty_cycle(); |
| 65 | let n0 = 8 * max_duty / 25; // ws2812 Bit 0 high level timing | 65 | let n0 = 8 * max_duty / 25; // ws2812 Bit 0 high level timing |
| 66 | let n1 = 2 * n0; // ws2812 Bit 1 high level timing | 66 | let n1 = 2 * n0; // ws2812 Bit 1 high level timing |
| 67 | 67 | ||
| @@ -84,7 +84,7 @@ async fn main(_spawner: Spawner) { | |||
| 84 | let pwm_channel = Channel::Ch1; | 84 | let pwm_channel = Channel::Ch1; |
| 85 | 85 | ||
| 86 | // make sure PWM output keep low on first start | 86 | // make sure PWM output keep low on first start |
| 87 | ws2812_pwm.set_duty(pwm_channel, 0); | 87 | ws2812_pwm.channel(pwm_channel).set_duty(0); |
| 88 | 88 | ||
| 89 | // flip color at 2 Hz | 89 | // flip color at 2 Hz |
| 90 | let mut ticker = Ticker::every(Duration::from_millis(500)); | 90 | let mut ticker = Ticker::every(Duration::from_millis(500)); |
diff --git a/examples/stm32g0/src/bin/input_capture.rs b/examples/stm32g0/src/bin/input_capture.rs index 69fdae96d..bc814cb13 100644 --- a/examples/stm32g0/src/bin/input_capture.rs +++ b/examples/stm32g0/src/bin/input_capture.rs | |||
| @@ -47,10 +47,10 @@ async fn main(spawner: Spawner) { | |||
| 47 | unwrap!(spawner.spawn(blinky(p.PB1))); | 47 | unwrap!(spawner.spawn(blinky(p.PB1))); |
| 48 | 48 | ||
| 49 | // Connect PB1 and PA8 with a 1k Ohm resistor | 49 | // Connect PB1 and PA8 with a 1k Ohm resistor |
| 50 | let ch1 = PwmPin::new_ch1(p.PA8, OutputType::PushPull); | 50 | let ch1_pin = PwmPin::new_ch1(p.PA8, OutputType::PushPull); |
| 51 | let mut pwm = SimplePwm::new(p.TIM1, Some(ch1), None, None, None, khz(1), Default::default()); | 51 | let mut pwm = SimplePwm::new(p.TIM1, Some(ch1_pin), None, None, None, khz(1), Default::default()); |
| 52 | pwm.enable(Channel::Ch1); | 52 | pwm.ch1().enable(); |
| 53 | pwm.set_duty(Channel::Ch1, 50); | 53 | pwm.ch1().set_duty_cycle(50); |
| 54 | 54 | ||
| 55 | let ch1 = CapturePin::new_ch1(p.PA0, Pull::None); | 55 | let ch1 = CapturePin::new_ch1(p.PA0, Pull::None); |
| 56 | let mut ic = InputCapture::new(p.TIM2, Some(ch1), None, None, None, Irqs, khz(1000), Default::default()); | 56 | let mut ic = InputCapture::new(p.TIM2, Some(ch1), None, None, None, Irqs, khz(1000), Default::default()); |
diff --git a/examples/stm32g0/src/bin/pwm_input.rs b/examples/stm32g0/src/bin/pwm_input.rs index 152ecda86..983705e2f 100644 --- a/examples/stm32g0/src/bin/pwm_input.rs +++ b/examples/stm32g0/src/bin/pwm_input.rs | |||
| @@ -43,11 +43,10 @@ async fn main(spawner: Spawner) { | |||
| 43 | 43 | ||
| 44 | unwrap!(spawner.spawn(blinky(p.PB1))); | 44 | unwrap!(spawner.spawn(blinky(p.PB1))); |
| 45 | // Connect PA8 and PA6 with a 1k Ohm resistor | 45 | // Connect PA8 and PA6 with a 1k Ohm resistor |
| 46 | let ch1 = PwmPin::new_ch1(p.PA8, OutputType::PushPull); | 46 | let ch1_pin = PwmPin::new_ch1(p.PA8, OutputType::PushPull); |
| 47 | let mut pwm = SimplePwm::new(p.TIM1, Some(ch1), None, None, None, khz(1), Default::default()); | 47 | let mut pwm = SimplePwm::new(p.TIM1, Some(ch1_pin), None, None, None, khz(1), Default::default()); |
| 48 | let max = pwm.get_max_duty(); | 48 | pwm.ch1().set_duty_cycle_fraction(1, 4); |
| 49 | pwm.set_duty(Channel::Ch1, max / 4); | 49 | pwm.ch1().enable(); |
| 50 | pwm.enable(Channel::Ch1); | ||
| 51 | 50 | ||
| 52 | let mut pwm_input = PwmInput::new(p.TIM2, p.PA0, Pull::None, khz(1000)); | 51 | let mut pwm_input = PwmInput::new(p.TIM2, p.PA0, Pull::None, khz(1000)); |
| 53 | pwm_input.enable(); | 52 | pwm_input.enable(); |
diff --git a/examples/stm32g4/src/bin/pwm.rs b/examples/stm32g4/src/bin/pwm.rs index d4809a481..3833be58f 100644 --- a/examples/stm32g4/src/bin/pwm.rs +++ b/examples/stm32g4/src/bin/pwm.rs | |||
| @@ -15,22 +15,22 @@ async fn main(_spawner: Spawner) { | |||
| 15 | let p = embassy_stm32::init(Default::default()); | 15 | let p = embassy_stm32::init(Default::default()); |
| 16 | info!("Hello World!"); | 16 | info!("Hello World!"); |
| 17 | 17 | ||
| 18 | let ch1 = PwmPin::new_ch1(p.PC0, OutputType::PushPull); | 18 | let ch1_pin = PwmPin::new_ch1(p.PC0, OutputType::PushPull); |
| 19 | let mut pwm = SimplePwm::new(p.TIM1, Some(ch1), None, None, None, khz(10), Default::default()); | 19 | let mut pwm = SimplePwm::new(p.TIM1, Some(ch1_pin), None, None, None, khz(10), Default::default()); |
| 20 | let max = pwm.get_max_duty(); | 20 | let mut ch1 = pwm.ch1(); |
| 21 | pwm.enable(Channel::Ch1); | 21 | ch1.enable(); |
| 22 | 22 | ||
| 23 | info!("PWM initialized"); | 23 | info!("PWM initialized"); |
| 24 | info!("PWM max duty {}", max); | 24 | info!("PWM max duty {}", ch1.max_duty_cycle()); |
| 25 | 25 | ||
| 26 | loop { | 26 | loop { |
| 27 | pwm.set_duty(Channel::Ch1, 0); | 27 | ch1.set_duty_cycle_fully_off(); |
| 28 | Timer::after_millis(300).await; | 28 | Timer::after_millis(300).await; |
| 29 | pwm.set_duty(Channel::Ch1, max / 4); | 29 | ch1.set_duty_cycle_fraction(1, 4); |
| 30 | Timer::after_millis(300).await; | 30 | Timer::after_millis(300).await; |
| 31 | pwm.set_duty(Channel::Ch1, max / 2); | 31 | ch1.set_dutycycle_fraction(1, 2); |
| 32 | Timer::after_millis(300).await; | 32 | Timer::after_millis(300).await; |
| 33 | pwm.set_duty(Channel::Ch1, max - 1); | 33 | ch1.set_duty_cycle(ch1.max_duty_cycle() - 1); |
| 34 | Timer::after_millis(300).await; | 34 | Timer::after_millis(300).await; |
| 35 | } | 35 | } |
| 36 | } | 36 | } |
diff --git a/examples/stm32h7/src/bin/pwm.rs b/examples/stm32h7/src/bin/pwm.rs index 1e48ba67b..1d0b89e97 100644 --- a/examples/stm32h7/src/bin/pwm.rs +++ b/examples/stm32h7/src/bin/pwm.rs | |||
| @@ -37,22 +37,22 @@ async fn main(_spawner: Spawner) { | |||
| 37 | let p = embassy_stm32::init(config); | 37 | let p = embassy_stm32::init(config); |
| 38 | info!("Hello World!"); | 38 | info!("Hello World!"); |
| 39 | 39 | ||
| 40 | let ch1 = PwmPin::new_ch1(p.PA6, OutputType::PushPull); | 40 | let ch1_pin = PwmPin::new_ch1(p.PA6, OutputType::PushPull); |
| 41 | let mut pwm = SimplePwm::new(p.TIM3, Some(ch1), None, None, None, khz(10), Default::default()); | 41 | let mut pwm = SimplePwm::new(p.TIM3, Some(ch1_pin), None, None, None, khz(10), Default::default()); |
| 42 | let max = pwm.get_max_duty(); | 42 | let mut ch1 = pwm.ch1; |
| 43 | pwm.enable(Channel::Ch1); | 43 | ch1.enable(); |
| 44 | 44 | ||
| 45 | info!("PWM initialized"); | 45 | info!("PWM initialized"); |
| 46 | info!("PWM max duty {}", max); | 46 | info!("PWM max duty {}", ch1.max_duty_cycle()); |
| 47 | 47 | ||
| 48 | loop { | 48 | loop { |
| 49 | pwm.set_duty(Channel::Ch1, 0); | 49 | ch1.set_duty_cycle_fully_off(); |
| 50 | Timer::after_millis(300).await; | 50 | Timer::after_millis(300).await; |
| 51 | pwm.set_duty(Channel::Ch1, max / 4); | 51 | ch1.set_duty_cycle_fraction(1, 4); |
| 52 | Timer::after_millis(300).await; | 52 | Timer::after_millis(300).await; |
| 53 | pwm.set_duty(Channel::Ch1, max / 2); | 53 | ch1.set_dutycycle_fraction(1, 2); |
| 54 | Timer::after_millis(300).await; | 54 | Timer::after_millis(300).await; |
| 55 | pwm.set_duty(Channel::Ch1, max - 1); | 55 | ch1.set_duty_cycle(ch1.max_duty_cycle() - 1); |
| 56 | Timer::after_millis(300).await; | 56 | Timer::after_millis(300).await; |
| 57 | } | 57 | } |
| 58 | } | 58 | } |
