💾
EC
  • Introduction
  • EC Course
    • Syllabus
    • Preparation for EC
    • Tutorial
      • Tutorial: arduino-stm32
        • Tutorial: arduino-stm32 Installation
        • Tutorial: arduino-stm32 Part1
        • Tutorial: arduino-stm32 Part2
      • Tutorial: MDK uVision
        • Tutorial: Installing MDK uVision
        • Tutorial: Create a Project with uVision
        • Tutorial: Adding library header in uVision
        • Tutorial: Re-using Project Configuration
        • Debugging in uVision
      • Tutorial: PlatformIO in CLion
      • Tutorial: PlatformIO in VSCode
      • Tutorial: Repository Management
      • Tutorial: Managing library header files
      • Tutorial: PinName Configuration
      • Tutorial: Bitwise Macro
      • Tutorial: Custom initialization
      • Tutorial: Documentation
      • Tutorial: Creating Application API
      • Tutorial: 7-Segment Display
      • Tutorial: DC motor driver connection
      • Tutorial: USART with TeraTerm
      • Tutorial: Finite State Machine programming
      • Tutorial: Bluetooth
      • Tutorial: Zigbee with Nucleo board
    • LAB
      • LAB Report Template
      • LAB: Smart mini-fan with STM32-duino
      • LAB: Portable Fan with mbed
      • LAB: GPIO Digital InOut
      • LAB: GPIO Digital InOut 7-segment
      • LAB: EXTI & SysTick
      • LAB: Timer & PWM
      • LAB: Stepper Motor
      • LAB: Input Capture - Ultrasonic
      • LAB: USART - LED, Bluetooth
      • LAB: ADC - IR reflective sensor
      • LAB: Line Tracing RC Car
    • Sample code
      • Code Templates
    • Hardware
      • Nucleo-F411RE
      • LAB Hardware
        • Electronic Chips
        • HUINS Embedded Kit
    • Projects
      • Line Tracing Car Project
      • Design Project
        • Past Projects
      • Project Grading Criteria
    • Study Resource for MCU
      • Hexa-Decimal Table
      • Bitwise Op for Register
      • System Clock
      • Timer
      • USART
      • ADC
  • STM32 M4 Programming
    • Documentation
      • C++ API Docs
    • Firmware Library
      • PinName Configuration
      • GPIO Digital
      • RCC
      • EXTI_SysTick
      • TIMER
      • USART
    • Troubleshooting
    • mbed for STM32
      • Tutorial: mbed-Part 1
      • Tutorial: mbed - Part 2
      • Tutorial: mbed - Part 3
      • Using mbed API on uVision
    • mbed OS
  • Other Programming
    • Arduino
    • Socket Programming
      • Window Socket Programming
      • Arduino WiFi
    • Cube-MX
    • Github
    • Markdown
      • Example: API documentation
    • MATLAB
  • C Programming
    • C-Programming Lessons
      • Installing Visual Studio Community
        • Visual Studio Community 2022
      • Installing VS Code(Mac/Linux)
      • Creating Header Lib
      • Pointer
      • Array
      • 2D Array
      • Structure
      • Dynamic Alloc
      • Bitwise Operation
  • Numerical Programming
    • Syllabus
    • Preparation for NP
    • Math Review
    • Tutorial
      • TA Session Video
      • Tutorial: NP Library Header Files
      • Tutorial - Sine Taylor
      • Tutorial: Passing a Function, Function callback
      • Tutorial: Nonlinear solver
      • Tutorial: Differentiation
      • Tutorial: Integration
      • Tutorial: Matrix Structure
      • Tutorial: Eigenvalue problem
      • Tutorial: ODE-IVP
      • Tutorial: Curve Fitting
      • Tutorial: Create Github Repos of NP lib
      • Tutorial: Version Control in Github
      • Tutorial: Documentation with Markdown
      • Exercise: Version Control and Documentation
    • Example: MATLAB
    • Example: NP Library
    • Assignment
      • Assignment Factorial and Power
      • Assignment: Version Control and Documentation
    • Problem Bank
Powered by GitBook
On this page
  • Preparation
  • GPIO Digital In/Out
  • mbed class
  • External Interrupt
  • mbed class
  • Exercise
  • Ticker (SysTick interrupt)
  • mbed class
  • Exercise
  • ****

Was this helpful?

  1. STM32 M4 Programming
  2. mbed for STM32

Tutorial: mbed - Part 2

PreviousTutorial: mbed-Part 1NextTutorial: mbed - Part 3

Last updated 2 years ago

Was this helpful?

Preparation

MCU board: Nucleo-F401RE

GPIO Digital In/Out

We are going to create a simple program that turns LED(LD2) on and off by pressing the user button(BT1).

mbed class

Look up for DigitalOut and DigitalIn in mbed documentation for the fulll list of methods

Create a **** new program named as ‘TU_mbed_GPIO_LED_button’.

Write the following source code on ‘main.cpp’

#include "mbed.h"

DigitalIn  button(USER_BUTTON);
DigitalOut led(LED1);

int main() {
    while(1) {
        if(!button) led = 1;
        else led = 0;
    }
}

Click on Compile button. Then, the binary files will be created and downloaded. Copy the binary file to MCU board via USB cable.

Push the reset button(black) and check the performance. The LED(LD2) should be turned on when the button is pressed.

External Interrupt

We are going to create a simple program that turns LED(LD2) on triggered by External Interrupt of user button(BT1).

mbed class

Create new program as ‘TU_mbed_ExtIn’.

Write the following code on ‘mbed’ complier.

We have created user defined functions of void pressed() and void released().

#include "mbed.h"

InterruptIn button(USER_BUTTON); 
DigitalOut  led(LED1);

void pressed()
{
    led = 1; 
}

void released(){
    led = 0;
}

int main()
{
    button.fall(&pressed);
    button.rise(&released);
    while (1);
}

Click on Compile button. Then, the binary files will be created and downloaded. Copy the binary file to MCU board via USB cable.

Whenever the user button(BT1) is pressed (at fall), then the LED should be ON. When the button is released then the LED should be off.

Exercise

The experiment kit has IR motion sensor(HD-SEN0018) that detects a motion of an object nearby. It is often used in automatic lighting system at the front door. It is connected to PinName D5 as DigitalIn

  • Use External interrupt to get the digital in data from the motion sensor

  • When the userbutton is pressed, it should turn-off the LED.

  • Hint:

InterruptIn motion(D5)
// InterruptIN for button input

void motionDetected(void)
{
    // code to light up the LED
}

//  Interrupt handler function for Button to turn off LED
// Refer to void pressed(void)

int main(void)
{
    // other codes
    while(1){
    // other codes
    motion.rise(&motionDetected);
    // other codes
}

Ticker (SysTick interrupt)

We are going to create a simple program that uses System Timer Tick Interrupt that occurs periodically. Lets turn LED on and off at 1 sec of period.

mbed class

Use the Ticker interface to set up a recurring interrupt; it calls a function repeatedly and at a specified rate.

Create new program as ‘TU_mbed_SysTick’.

tick.attach( ) makes periodic interrupt of second unit.

You can make LED blink every second, even though there is no infinite loop in main(). This is also called as the ‘ SysTIck interrupt’.

#include "mbed.h"

Ticker     tick;
DigitalOut led(LED1);

void INT(){
    led = !led;      
}
int main(void){
    tick.attach(&INT, 1); // 1초마다 LED blink

    while(1);
}

Click on Compile button. Then, the binary files will be created and downloaded. Copy the binary file to MCU board via USB cable.

LED(LD2) should blink every second.

Exercise

This experiment kit has a digital buzzer (MCKPI-G1410).

It is connected at DigitalOut PinName PA_13

  • Buzz the sound for about 1second that repeats for every 3 seconds.

You can also may use wait(sec)

To use the buzzer, square digital signals such as

while(1){
    buzzer=1;
    wait(0.01);
    buzzer=0;
    wait(0.01);
}

****

InterruptIn
Ticker
DigitalOut
Digital In
Refer to HUINS mbed experiment kit: Pin Map