コントローラーのプログラム

Dependencies:   mbed

/media/uploads/Raccoon/controller.jpg

Revision:
0:a17db953c0b2
Child:
1:a213bf47cc4d
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu Dec 21 13:03:34 2017 +0000
@@ -0,0 +1,95 @@
+#include "mbed.h"
+#define MAX 4
+ 
+Serial xbee(PA_9, PA_10); // tx, rx
+AnalogIn ain_0(PA_5);
+AnalogIn ain_1(PA_6);
+AnalogIn ain_2(PA_4);
+AnalogIn ain_3(PA_7);
+
+DigitalIn sw_0(PB_3);
+DigitalIn sw_1(PB_5);
+DigitalIn sw_2(PB_4);
+
+DigitalOut myled(PA_8);
+
+Ticker adc_timer;
+Ticker Tr_timer;
+Ticker led_timer;
+char data[MAX] = {0};
+unsigned int sw = 0;
+volatile int RECEVED = 0;
+
+void transmit(void)
+{
+    if(sw == 0)
+    {
+        xbee.putc('R');
+    }
+    else if(1 <= sw && sw <= MAX)
+    { 
+        xbee.putc(data[sw-1]);
+    }
+}
+
+void switching(void)
+{
+    if(sw == 0)
+    {
+        sw = 1;
+    }
+    else if(sw > 0)
+    {
+        if(++sw > MAX){
+            sw = 0;
+        }
+    }
+}
+
+void receve(void)
+{
+    RECEVED = 1;
+}
+    
+void adcRead(void){
+    /*ステッピングモーター*/
+    data[0] = ain_0 * 255;
+    
+    /*EX球*/
+    data[1] = ain_1 * 255;
+    
+    /*天の川*/
+    data[2] = ain_2 * 255;
+    
+    data[3] = ain_3 * 255;
+}
+
+void led_off(void)
+{
+    static int cnt = 0;
+    if(RECEVED == 1)
+    {
+        myled = 1;
+        RECEVED = 0;
+        cnt = 0;
+    }
+    else if(++cnt > 50)
+    {
+        myled = 0;
+        RECEVED = 0;
+    }
+}
+
+int main() {
+    sw_0.mode(PullUp);
+    sw_1.mode(PullUp);
+    sw_2.mode(PullUp);
+    xbee.baud(9600);
+    xbee.format(8, Serial::None, 1);
+    xbee.attach(&switching, Serial::TxIrq);//送信完了時に割り込み
+    xbee.attach(&receve, Serial::RxIrq);//受信割り込み
+    Tr_timer.attach_us(&transmit, 30000);//送信
+    adc_timer.attach_us(&adcRead, 20000);//ADC用
+    led_timer.attach_us(&led_off, 10000);//通信確認led用
+    while(1)__WFI();
+}
\ No newline at end of file