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

Dependencies:   mbed

/media/uploads/Raccoon/controller.jpg

main.cpp

Committer:
Raccoon
Date:
2017-12-26
Revision:
1:a213bf47cc4d
Parent:
0:a17db953c0b2
Child:
2:6383151eb64b

File content as of revision 1:a213bf47cc4d:

#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;
uint8_t data[MAX];

/*情報源符号化(data-5bit, index-3bitにして符号化)*/
uint8_t ChannelCoding(float data_f, int index)
{
    uint8_t tmp;
    tmp = data_f * 31;
    return ((tmp << 3) | index);
}

/*送信*/
void transmit(void)
{
    static int i = 0;
    xbee.putc(data[i]);
    if(++i >= MAX)i=0;
}

/*受信*/
void receve(void)
{
    myled = 1;
}
    
void adcRead(void){
    /*ステッピングモーター*/
    data[0] = ChannelCoding(ain_0, 0);
    /*EX球*/
    data[1] = ChannelCoding(ain_1, 1);
    /*室内灯*/
    data[2] = ChannelCoding(ain_2, 2);
    
    data[3] = ChannelCoding(ain_3, 3);
}

int main() {
    for(int i=0; i<MAX; i++)data[i] = ChannelCoding(0.0f, i);//配列dataを初期化
    myled = 0;
    sw_0.mode(PullUp);
    sw_1.mode(PullUp);
    sw_2.mode(PullUp);
    xbee.baud(9600);
    xbee.format(8, Serial::None, 1);
    xbee.attach(&receve, Serial::RxIrq);//受信割り込み
    Tr_timer.attach_us(&transmit, 20000);//送信
    adc_timer.attach_us(&adcRead, 20000);//ADC用
    while(1)__WFI();
}