blippy
Posts: 201
Joined: Fri Nov 03, 2017 3:07 pm

Code snippet: PWM generated at a specified frequency

Sun Feb 07, 2021 11:07 pm

I'm exploring PWM with the Pico. The good news is that you can set both the duty cycle and frequency of PWM. Here's an example which sets a 60% duty cycle on a 440Hz frequency on GPIO0:

Code: Select all

#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h"
#include "hardware/clocks.h"
#include "hardware/pwm.h"


typedef uint16_t u16;
typedef uint32_t u32;


int main() 
{
	// set your desired inputs here
	const u32 f_pwm = 440; // frequency we want to generate
	const u16 duty = 60; // duty cycle, in percent

	gpio_set_function(0, GPIO_FUNC_PWM); // Tell GPIO 0 it is allocated to the PWM
	uint slice_num = pwm_gpio_to_slice_num(0); // get PWM slice for GPIO 0 (it's slice 0)


	// set frequency
	// determine top given Hz - assumes free-running counter rather than phase-correct
	u32 f_sys = clock_get_hz(clk_sys); // typically 125'000'000 Hz
	float divider = f_sys / 1'000'000UL;  // let's arbitrarily choose to run pwm clock at 1MHz
	pwm_set_clkdiv(slice_num, divider); // pwm clock should now be running at 1MHz
	u32 top =  1'000'000UL/f_pwm -1; // TOP is u16 has a max of 65535, being 65536 cycles
	pwm_set_wrap(slice_num, top);

	// set duty cycle
	u16 level = (top+1) * duty / 100 -1; // calculate channel level from given duty cycle in %
	pwm_set_chan_level(slice_num, 0, level); 
	
	pwm_set_enabled(slice_num, true); // let's go!

	for(;;);
	return 0;
}

Hope that's useful to folks.

I see in the documentation that it's possible to double-buffer the duty cycle, too. I plan to exploit that capability. I'm impressed with the Pico's level of documentation.

Actually, you know what would be good? A Pico cookbook.

User avatar
nick.mccloud
Posts: 1280
Joined: Sat Feb 04, 2012 4:18 pm

Re: Code snippet: PWM generated at a specified frequency

Mon Feb 08, 2021 12:03 pm

blippy wrote:
Sun Feb 07, 2021 11:07 pm
Hope that's useful to folks.
Saves me a job, thank you. Let the tunes begin.
Actually, you know what would be good? A Pico cookbook.
Perhaps a community effort on Git?
Pico/RP2040 ≠ Arduino
Pico = hot rod kit car, Arduino = hot rod kit car wrapped in cotton wool with buoyancy aids & parachute

blippy
Posts: 201
Joined: Fri Nov 03, 2017 3:07 pm

Re: Code snippet: PWM generated at a specified frequency

Mon Feb 08, 2021 5:02 pm

nick.mccloud wrote:
Mon Feb 08, 2021 12:03 pm
blippy wrote:
Sun Feb 07, 2021 11:07 pm
Hope that's useful to folks.
Saves me a job, thank you. Let the tunes begin. At the back of my mind is a whole synthesiser idea. My knowledge of electronics and DSP is woefully lacking, however, so there's a huge learning-curve on my behalf.

I have since learned that changes to the frequency and duty cycle of PWM waves are, in fact, double-buffered; which is exactly what I was hoping for.

I asked the team to incorporate this into the SDK, which they have done.

I think I'd hate to be on the engineering team with an awkward customer like me around ;)

I guess it can be difficult, knowing which user suggestion is valuable, and which is a bit ... um .. dumbass.

I'm kinda excited by this whole Pico thing, though, because if the documentation blows all the other MCUs out of the market, then it offers a USP (Unique Selling Point) for the Pico.

Glad you liked it.
blippy wrote:
Sun Feb 07, 2021 11:07 pm

Actually, you know what would be good? A Pico cookbook.
Perhaps a community effort on Git?
Yeah. A wiki would work, too.

User avatar
nick.mccloud
Posts: 1280
Joined: Sat Feb 04, 2012 4:18 pm

Re: Code snippet: PWM generated at a specified frequency

Mon Feb 08, 2021 6:08 pm

blippy wrote:
Mon Feb 08, 2021 5:02 pm
nick.mccloud wrote:
Mon Feb 08, 2021 12:03 pm
Perhaps a community effort on Git?
Yeah. A wiki would work, too.
A wiki on GitHub?
Pico/RP2040 ≠ Arduino
Pico = hot rod kit car, Arduino = hot rod kit car wrapped in cotton wool with buoyancy aids & parachute

User avatar
HermannSW
Posts: 5298
Joined: Fri Jul 22, 2016 9:09 pm
Location: Eberbach, Germany

Re: Code snippet: PWM generated at a specified frequency

Mon Feb 08, 2021 7:01 pm

nick.mccloud wrote:
Mon Feb 08, 2021 6:08 pm
blippy wrote:
Mon Feb 08, 2021 5:02 pm
nick.mccloud wrote:
Mon Feb 08, 2021 12:03 pm
Perhaps a community effort on Git?
Yeah. A wiki would work, too.
A wiki on GitHub?
A PicoWikipedia?
https://hermann-sw.github.io/planar_graph_playground
https://stamm-wilbrandt.de/en#raspcatbt
https://github.com/Hermann-SW/memrun
https://github.com/Hermann-SW/Raspberry_v1_camera_global_external_shutter
https://stamm-wilbrandt.de/en/Raspberry_camera.html

Return to “SDK”