mbed Application Board
.
■AnalogIn
アナログ入力端子の電圧を読み取ります。
Example
0(0V)~1.0(3.3V)の数値(float)が0.3以上であればHIGHそうでなければLOWを出力し、アナログ入力から読み取った値を表示します。
main.cpp
#include "mbed.h" AnalogIn ain(p19); // AnalogInのインスタンスであるainを生成 DigitalOut dout(p25); // DigitalOutのインスタンスであるdoutを生成 int main(void) { while(1) { // 0.3 * VCCより大きい場合は1、そうでなければ0を出力 if(ain > 0.3f) { dout = 1; } else { dout = 0; } // パーセンテージと16ビットの値を表示 // percentage: 20.537% // normalized: 0x3293 printf("percentage: %3.3f%%\n", ain.read() * 100.0f); printf("normalized: 0x%04X \n", ain.read_u16()); wait(0.2f); } }
■DigitalIn
デジタル入力ピンの値を読み取るために使用します。
Example
ボタンが押されたらLEDを点灯します。
main.cpp
#include "mbed.h" DigitalIn enable(p14); BusOut leds(LED1,LED2,LED3,LED4); int main() { while(1) { if (enable) { leds=0xf; // ボタンが押されたらLED1〜LED4を点灯 } else { leds=0x0; // LED1〜LED4を消灯 } wait(0.1); // チャタリング対策 } }
■I2C
I2CインターフェイスはI2Cマスタ機能を提供します。I2Cマスタは、I2Cスレーブとデータを交換することを可能にする2線式シリアル・プロトコルです。センサー、シリアルメモリ、その他モジュールなどのI2Cデバイスと通信するために使用します。
Example
x,y,z軸の加速度を5秒間隔で表示します。
main.cpp
#include "mbed.h" #include "MMA7660.h" DigitalOut connectionLed(LED1); MMA7660 MMA(p28, p27); Serial pc(USBTX, USBRX); int main() { if (MMA.testConnection()) // MMA7660が通信可能かテストする connectionLed = 1; while(1) { /* x,y,z軸の加速度を表示 x: -0.188 g y: -0.094 g z: 0.985 g */ pc.printf("x: %1.3f g \n", MMA.x()); pc.printf("y: %1.3f g \n", MMA.y()); pc.printf("z: %1.3f g \n", MMA.z()); pc.printf("\n"); wait(5); } }
■LCD
C12832(128x32 LCD)にテキストを表示します。
Example
LM75Bから読み取った温度を1秒間隔でLCDに表示します。
main.cpp
#include "mbed.h" #include "LM75B.h" #include "C12832.h" C12832 lcd(p5, p7, p6, p8, p11); LM75B sensor(p28,p27); Serial pc(USBTX,USBRX); int main () { // LM75Bが通信可能か確認する if (sensor.open()) { printf("Device detected!\n"); while (1) { lcd.cls(); // ディスプレイのバッファをクリア lcd.locate(0,3); // 座標をセット lcd.printf("Temp = %.3f\n", (float)sensor); // 温度を表示する wait(1.0); } } else { error("Device not detected!\n"); } }
■Ticker
ティッカーは指定されたタイミングで関数を呼び出す定期的な割り込みに使用されます。
Example
2秒間隔でLEDが点滅を繰り返します。5秒毎に割り込みが入りLCDに温度(100回の平均値)が表示されます。
main.cpp
#include "mbed.h" #include "C12832.h" #include "LM75B.h" #define COUNT 100 //測定回数 #define INTERVAL 5.0 //測定間隔(秒) C12832 lcd(p5, p7, p6, p8, p11); LM75B temp(p28,p27); DigitalOut led1(LED1); Ticker input; void get_temp() { float ans ; int i ; ans = 0 ; lcd.cls(); // ディスプレイのバッファをクリア for (i=0 ; i < COUNT ; i++) { ans = ans + temp; // 温度を積算する } float avg = ans / COUNT; // 100回の平均値を返す lcd.locate(0,5); // 座標をセット lcd.printf("Temp = %.3f\n", avg); // 温度を表示する } int main () { // LM75Bが通信可能か確認する if (temp.open()) { lcd.locate(0,5); // 座標をセット lcd.printf("Device detected!\n"); input.attach(&get_temp, INTERVAL); // 5.0秒毎にget_temp関数を実行する while (1) { // 無限ループ led1 = !led1; wait(2.0); } } else { error("Device not detected!\n"); } }
■Ethernet
Ethernetインターフェイスは、イーサネット接続を介してインターネットに接続するためのAPIを提供します。
Example
HTTPクライアントプログラムの簡単な例です。Ethernetインターフェイスを使用し、TCPソケット経由でHTTPトランザクションを実行するために使用します。
main.cpp
#include "mbed.h" #include "TCPSocketConnection.h" #include "EthernetInterface.h" EthernetInterface eth; TCPSocketConnection socket; int main() { printf("Example network-socket HTTP client\n"); // Brings up the network interface eth.init(); // インターフェースの初期化 eth.connect(); // インターフェースを起動 const char *ip = eth.getIPAddress(); // IPアドレスを取得 const char *mac = eth.getMACAddress(); // MACアドレスを取得 printf("IP address is: %s\n", ip ? ip : "No IP"); printf("MAC address is: %s\n", mac ? mac : "No MAC"); socket.connect("mbed.org", 80); // サーバーへ接続する char sbuffer[] = "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\r\n\r\n"; // 送信するHTTPリクエスト int scount = socket.send(sbuffer, sizeof sbuffer); // TCPソケットでデータを送信する printf("sent %d [%.*s]\r\n", scount, strstr(sbuffer, "\r\n")-sbuffer, sbuffer); char rbuffer[17]; int rcount = socket.receive(rbuffer, sizeof rbuffer); // HTTPレスポンスを受信する printf("recv %d [%.*s]\r\n", rcount, strstr(rbuffer, "\r\n")-rbuffer, rbuffer); // Close the socket to return its memory and bring down the network interface socket.close(); // ソケットを閉じる eth.disconnect(); // インターフェースを停止 printf("Done\n"); }
Please log in to post comments.