1 新建工程文件夹,作为工作目录
2 进入工程文件夹,运行
mbed new .
3 设定目标板
mbed target NUCLEO_WB55RG
4 设定编译器
mbed toolchain GCC_ARM
5 创建程序main.cpp
6 编译并下载
mbed compile --flash
案例1:
#include <mbed.h>
Serial pc(SERIAL_TX, SERIAL_RX);
DigitalOut myled(LED1);
int main() {
int i = 1;
pc.printf("Hello World !\n");
while(1) {
wait(1);
pc.printf("Hello MyWorld !\n");
wait(1);
pc.printf("This program runs since %d seconds.\n", i++);
myled = !myled;
}
}
案例2:
#include "mbed.h"
I2C i2c(I2C_SDA , I2C_SCL );
Serial pc(USBTX, USBRX); // tx, rx
const int addr =0x76 <<1 ;// define the correct I2C Address 寄存器地址
int main()
{
char regaddr[1];
char readdata[8]; // room for length and 7 databytes
char writedata[9]; // room for reg address, length and 7 databytes
while (1)
{
//write ...... 写操作
regaddr[0] = 0xF4;
writedata[0] = regaddr[0]; // register address
writedata[1] = 0x15; // data
writedata[2] = 1; // length of data
// write the data
i2c.write(addr, writedata, 3); // select the register,
// write the length, write 1 databytes
wait(1);
// read .... 读操作
regaddr[0] = 0xF4;
i2c.write(addr, regaddr, 1, false); // select the register, no I2C Stop
i2c.read(addr, readdata, 8); // read the length byte and the 8 databytes
wait (1);
// print the data to the screen
pc.printf("Register 0x%x = 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x 0x%x\r\n",
regaddr[0],
readdata[0],readdata[1], readdata[2], readdata[3], readdata[4],
readdata[5], readdata[6], readdata[7] );
wait(1);
}
}