Browse over 10,000 Electronics Projects

Development Board for the STM32F042

Development Board for the STM32F042

The transistor switch

This test is closely related to the basic blinky except this time I connected an external LED and series resistor to the LD (load) pin and change the code to control the blinking to use pin PA7.

It’s working as expected which validates that the NPN transistor is connected up correctly.

What about the external crystal?

I’m not done with the blinkenled just yet. Before I can move on I need to know if the external 8MHz crystal is working which means configuring it as the MCU clock source. Luckily this is really simple.

If you look inside the System.c startup file you’ll see that ST have provided a simple #define to select the clock source. To configure the HSE as the clock source we merely have to change the code to read like this:

//#define PLL_SOURCE_HSI   // HSI (~8MHz) used to clock the PLL, and the PLL is used as system clock source
#define PLL_SOURCE_HSE           // HSE (8MHz) used to clock the PLL, and the PLL is used as system clock source
//#define PLL_SOURCE_HSE_BYPASS  // HSE bypassed with an external clock (8MHz, coming from ST-Link) used to clock
                                 // the PLL, and the PLL is used as system clock source

Testing a timer

The pin that I’ve attached to the LED, PB1, is also the GPIO output for channel 4 of timer 3. It’s a simple task to set up that timer in PWM mode and then to create a quick demo that pulses the LED as smooth ‘heartbeat’.



Advertisement1


void run() {

  Timer3<
    Timer3InternalClockFeature,       // the timer clock source is APB1
    TimerChannel4Feature<>,           // we're going to use channel 4
    Timer3GpioFeature<                // we want to output something to GPIO
      TIMER_REMAP_NONE,               // the GPIO output will not be remapped
      TIM3_CH4_OUT                    // we will output channel 4 to GPIO
    >
  > timer;

  /*
   * Set an up-timer up to tick at 12MHz with an auto-reload value of 1999
   * The timer will count from 0 to 1999 inclusive then reset back to 0.
   */

  timer.setTimeBaseByFrequency(12000000,1999);
  timer.initCompareForPwmOutput();
  timer.enablePeripheral();

  /*
   * It's all running automatically now, use the main CPU to vary the duty cycle up
   * to 100% and back down again
   */

  for(;;) {

    // fade up to 100% in 4ms steps

    for(int8_t i=0;i<=100;i++) {
      timer.setDutyCycle(i);
      MillisecondTimer::delay(4);
    }

    // fade down to 0% in 4ms steps

    for(int8_t i=100;i>=0;i--) {
      timer.setDutyCycle(i);
      MillisecondTimer::delay(4);
    }
  }
}

This worked as planned and you can see the pulsating heartbeat LED in the video linked in at the end of this article.

Pages: 1 2 3 4 5 6 7 8 9 10 11

 


Top