ブログ用

Dependencies:   SB1602E X_NUCLEO_IKS01A1-f255a2c75ecb mbed

角度と温度、湿度をLCDに表示

基本的にブログ用のコードで https://goo.gl/EMTAQB に簡単な説明がある。 ここでは詳細について書いておく。

必要機材

  • NUCLEO F401RE
  • X-NUCLEO-IKS01A1
  • AQM0802A-RN-GBW(秋月コード:M-09109)

つなぎ方

/media/uploads/bean1310/p7102948.jpg LCDの電源(赤いコード)とタクトスイッチにつながる(抵抗は必要ないか)白いコードをX-NUCLEO-IKS01A1のCN8の3V3のピンにつなぐ。LCDの青いコードをCN5のD15, 黄色いコードをD14に。緑のコードをCN6のGNDにそれぞれ接続。
タクトスイッチに繋がっている黒いコードはCN5のD10に接続。
それ以外は外部電源でプログラムに必須というわけではないから説明は省く。

Revision:
0:5b16bcf60d96
Child:
1:24590aa69d29
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sun Jul 10 06:45:50 2016 +0000
@@ -0,0 +1,90 @@
+#include "mbed.h"
+#include "x_nucleo_iks01a1.h"
+#include "SB1602E.h"
+#include <math.h>
+
+#define xMax 979
+#define xMin -1034
+#define yMax 996
+#define yMin -1001
+
+static X_NUCLEO_IKS01A1 *mems_expansion_board = X_NUCLEO_IKS01A1::Instance(D14, D15);
+static MotionSensor *accelerometer = mems_expansion_board->GetAccelerometer();
+static TempSensor *temp_sensor = mems_expansion_board->ht_sensor;
+static HumiditySensor *humidity_sensor = mems_expansion_board->ht_sensor;
+SB1602E lcd(D14, D15);
+//Serial pc(USBTX, USBRX);
+DigitalIn angSet(D10, PullDown);
+
+int angle(int max, int min, int32_t now);//角度をint型で返す。
+void lcdSet();                           //lcdの液晶設定をクリアにする。
+void accelerometerMode();                //angle関数を含み、lcdに表示する関数
+void tempMode();                         //気温, 湿度をlcdに表示する関数
+
+int main() {
+    
+    int mode = 0, i;
+    
+    do{
+        
+        printf("N\r\n");
+        lcdSet();
+        if(mode == 0){
+            accelerometerMode();
+        }else if(mode == 1){
+            tempMode();
+        }
+    
+        if(angSet == 1 && mode == 0){
+            mode = 1;
+            for(i = 0; i < 4; i++){
+                lcdSet();
+                wait(0.25);
+                lcd.printf(0, "Mode");
+                lcd.printf(1, "TEMP");
+                wait(0.25);
+            }
+        }else if(angSet == 1 && mode == 1){
+            mode = 0;
+            for(i = 0; i < 4; i++){
+                lcdSet();
+                wait(0.25);
+                lcd.printf(0, "Mode");
+                lcd.printf(1, "ACCEL");
+                wait(0.25);
+            }
+        }
+        wait(0.05);
+    }while(1);
+}
+
+int angle(int max, int min, int32_t now){/*<http://deviceplus.jp/hobby/entry017/>*/
+    return (int)(((((int)now - min) / ((max - min) / 180.0)) - 90) + 0.5);
+}
+
+void lcdSet(){
+    lcd.clear();//LCDの表示をクリアに。
+    lcd.contrast(0x32);//コントラストの設定。
+}
+
+void accelerometerMode(){
+
+    int32_t axes[3];
+    
+    accelerometer->Get_X_Axes(axes);
+    
+    lcd.printf(0, "x:%d", angle(xMax, xMin, axes[0]));
+    lcd.printf(1, "y:%d", -angle(yMax, yMin, axes[1]));  
+    
+}
+
+void tempMode(){
+    float temp, humid;
+    
+    temp_sensor -> GetTemperature(&temp);
+    humidity_sensor -> GetHumidity(&humid);
+    lcd.printf(0, "%2.1f['C]", temp);
+    lcd.printf(1, "%2d[%%]", (int)humid);
+    
+}
+            
\ No newline at end of file