Professional Documents
Culture Documents
Developed
by mbed.org
or its
contributors
Device vendor’s
library
Device register
level
Library Architecture
Design Overview
The mbed library provides abstractions for the microcontroller (MCU)
hardware (in particular drivers for the MCU peripherals) and it is divided in the
following software layers and APIs:
MCU register
void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct)
{
… …
access
GPIOx->MODER &= ~(GPIO_MODER_MODER0 << (pinpos * 2));
GPIOx->MODER |= (((uint32_t)GPIO_InitStruct->GPIO_Mode) << (pinpos * 2));
… …
}
ARM CMSIS for
ARM CMSIS library for core registers core register
configuration
C++ in the SDK
int main() {
while(1) {
myled = 1; // LED is ON Operator overloading
wait(0.2); // 200 ms (look at the definition,
myled = 0; // LED is OFF ctrl + click)
wait(1.0); // 1 sec
}
}
The MCU can never exit from the Main function
Program your board
Connect the board to your PC
DigitalOut myled(LED1);
int main() {
printf("RTC example\n");
set_time(1429284734); // Set RTC time to Fri, 17 Apr 2015 15:32:14 UTC
printf("Date and time are set.");
while(1) {
time_t seconds = time(NULL);
printf("Time as a basic string = %s\r\n", ctime(&seconds));
myled = !myled;
wait(1);
}
}
InterruptIn mybutton(USER_BUTTON);
DigitalOut myled(LED1);
int main()
{
mybutton.fall(&pressed); // Attach interrupt on falling edge
while (1) {
myled = !myled;
wait(delay);
}
}
Periodic interrupt
#include "mbed.h"
void toggle_led() {
led1 = !led1;
}
int main() {
// Init the ticker with the address of the function (toggle_led) to be
attached and the interval (100 ms)
toggle_led_ticker.attach(&toggle_led, 0.1);
while (true) {
// Do other things...
}
}
Serial using Interrupts
#include "mbed.h"
void callback() {
// Note: you need to actually read from the serial to clear the RX interrupt
int i = 0;
pc.scanf("%d",&i);
pc.printf("Hello Interrupt: %d \r\n ", i);
}
int main() {
pc.attach(&callback);
Interrupt does not block main
while(1) { execution and is called only
myled = 1; // LED is ON when new data on serial port
wait(0.2); // 200 ms are present
myled = 0; // LED is OFF
wait(1.0); // 1 sec
}
}
Analog to Digital Conversion:
Connect the potentiometer to the board:
10K potentiometer
External pin connected to 3.3V and GND
Central pin connected to A0
Analog to Digital Converter
#include "mbed.h"
AnalogIn analog_value(A0);
DigitalOut led(LED1);
int main() {
float meas;
printf("\nAnalogIn example\n");
while(1) {
meas = analog_value.read(); // Converts the analog input value (value from 0.0 to 1.0)
meas = meas * 3300; // Change the value to be in the 0 to 3300 range
printf("measure = %.0f mV\n", meas);
if (meas > 2000) { // If the value is greater than 2V then switch the LED on
led = 1;
}
else {
led = 0;
}
wait(0.2); // 200 ms
}
}
Pulse Width Modulation
#include "mbed.h"
InterruptIn my_button(USER_BUTTON);
PwmOut my_pwm(LED1);
void pressed() {
if (my_pwm.read() == 0.25) {
my_pwm.write(0.75);
}
else {
my_pwm.write(0.25);
}
}
int main()
{
// Set PWM
my_pwm.period_ms(10);
my_pwm.write(0.25); //Set the Duty cycle
// Set button
my_button.fall(&pressed);
while (1) {
c:\work\temp> cd blinky\