10 years, 1 month ago.

NUCLEO newbie. STM32401 & Timer interrupts..?

Hi all! My ame is Steve. I heard about the Nucleo boards a few days ago (STM32, Arduino shield compatible Mbed and only £7.32!!!!!) and decided I wanted one as I already have used the STM32F4 discovery and think it is an amazing development board. I'm not an experienced MCU programmer but am learning both as a mature undergrad student in my final year of an Audio systems BSc AND as a synth DIY/ noise experimentalist (which I have been doing for the last 20 years or so and counting.)

)
So I ordered an STM32F401 Nucleo and 3 of the L152RE boards (they have DAC capabilities, the 401 does not.) I am still waiting for the L152RE boards to arrive but am attempting to use PWM to output a sine wave just out of interest. I have been playing around with the example PWM code and tried to change it to output a sine wave from a wavetable of 255 values I generated using a python script. The code is here:

wavetable PWM sine

#include "mbed.h"

PwmOut mypwm(PB_3);

DigitalOut myled(LED1);

uint8_t sine[] = {127, 134, 142, 150, 158, 166, 173, 181, 188, 195, 201, 207, 213, 219, 224, 229, 234, 238, 241, 245, 247, 250, 251, 252, 253, 254, 253, 252, 251, 250, 247, 245, 241, 238, 234, 229, 224, 219, 213, 207, 201, 195, 188, 181, 173, 166, 158, 150, 142, 134, 127, 119, 111, 103, 95, 87, 80, 72, 65, 58, 52, 46, 40, 34, 29, 24, 19, 15, 12, 8, 6, 3, 2, 1, 0, 0, 0, 1, 2, 3, 6, 8, 12, 15, 19, 24, 29, 34, 40, 46, 52, 58, 65, 72, 80, 87, 95, 103, 111, 119, 126, 134, 142, 150, 158, 166, 173, 181, 188, 195, 201, 207, 213, 219, 224, 229, 234, 238, 241, 245, 247, 250, 251, 252, 253, 254, 253, 252, 251, 250, 247, 245, 241, 238, 234, 229, 224, 219, 213, 207, 201, 195, 188, 181, 173, 166, 158, 150, 142, 134, 127, 119, 111, 103, 95, 87, 80, 72, 65, 58, 52, 46, 40, 34, 29, 24, 19, 15, 12, 8, 6, 3, 2, 1, 0, 0, 0, 1, 2, 3, 6, 8, 12, 15, 19, 24, 29, 34, 40, 46, 52, 58, 65, 72, 80, 87, 95, 103, 111, 119, 126, 134, 142, 150, 158, 166, 173, 181, 188, 195, 201, 207, 213, 219, 224, 229, 234, 238, 241, 245, 247, 250, 251, 252, 253, 254, 253, 252, 251, 250, 247, 245, 241, 238, 234, 229, 224, 219, 213, 207, 201, 195, 188, 181, 173, 166, 158, 150, 142, 134, 127, 119, 111, 103, 95,};

int t;
int freq;

int main() 
{
    mypwm.period_us(2);
    
    
    while(1) {
      for (t=0;t<255;t++)
    {
    
    mypwm.pulsewidth_us(sine[t]);
  }
    }
}

I filtered the output from pin 3 with an RC filter and the result is what you see in the picture. A sine wave that slowly decreases in amplitude. Could anyone please explain to me why it is happeining and also, would anyone kow how to use timer interrupts using mbed and the nucleo boards so I could set up a timer overflow interrupt at 44.1 KHz and output the wave in the interrupt routine? I am very new to mbed but knwo how to do all of this on Arduino/AVR.

/media/uploads/mubase/imag006.bmp

Are there any comprehensive tutorials for mbed and Nucleo? Thanks. :) Steve.

Hello,

what tutorials do you have in mind? Look at handbook/cookbook for more information. Most of libraries are generic (if not, then rewrite it and ask for pull request), with written home pages (some kind of a guide). That should be enough to get you started. If you find some inconsistencies or missing documentation, you can add it there by yourself.

posted by Martin Kojtal 01 Mar 2014

1 Answer

10 years, 1 month ago.

Timer interrupt is extremely easy on mbed, see: https://mbed.org/handbook/Ticker

Pwm has one downside, it is limitted on 1us accuracy. So with your period set at 2us, there isn't much space left for modulation (read: none). Additionally you set a period of 2us, with a pulsewidth of 100-250 us. That makes no sense.

But while Pwm can be faster than the mbed lib does (for the LPC1768, LPC11u24 and KL25Z for example there is a library which allows full access to the pwm peripheral), in the end it is not suitable to make audio with 44kHz sample rate. Either lower sample rate, or use a DAC.

Accepted Answer