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

Dependencies:   mbed

/media/uploads/Raccoon/controller.jpg

Committer:
Raccoon
Date:
Tue Dec 26 01:26:52 2017 +0000
Revision:
2:6383151eb64b
Parent:
1:a213bf47cc4d
minor version update

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Raccoon 0:a17db953c0b2 1 #include "mbed.h"
Raccoon 0:a17db953c0b2 2 #define MAX 4
Raccoon 1:a213bf47cc4d 3
Raccoon 0:a17db953c0b2 4 Serial xbee(PA_9, PA_10); // tx, rx
Raccoon 0:a17db953c0b2 5 AnalogIn ain_0(PA_5);
Raccoon 0:a17db953c0b2 6 AnalogIn ain_1(PA_6);
Raccoon 0:a17db953c0b2 7 AnalogIn ain_2(PA_4);
Raccoon 0:a17db953c0b2 8 AnalogIn ain_3(PA_7);
Raccoon 0:a17db953c0b2 9
Raccoon 0:a17db953c0b2 10 DigitalIn sw_0(PB_3);
Raccoon 0:a17db953c0b2 11 DigitalIn sw_1(PB_5);
Raccoon 0:a17db953c0b2 12 DigitalIn sw_2(PB_4);
Raccoon 0:a17db953c0b2 13
Raccoon 0:a17db953c0b2 14 DigitalOut myled(PA_8);
Raccoon 0:a17db953c0b2 15
Raccoon 0:a17db953c0b2 16 Ticker adc_timer;
Raccoon 0:a17db953c0b2 17 Ticker Tr_timer;
Raccoon 1:a213bf47cc4d 18 uint8_t data[MAX];
Raccoon 0:a17db953c0b2 19
Raccoon 2:6383151eb64b 20 /*符号化(*/
Raccoon 1:a213bf47cc4d 21 uint8_t ChannelCoding(float data_f, int index)
Raccoon 0:a17db953c0b2 22 {
Raccoon 1:a213bf47cc4d 23 uint8_t tmp;
Raccoon 1:a213bf47cc4d 24 tmp = data_f * 31;
Raccoon 1:a213bf47cc4d 25 return ((tmp << 3) | index);
Raccoon 0:a17db953c0b2 26 }
Raccoon 0:a17db953c0b2 27
Raccoon 1:a213bf47cc4d 28 /*送信*/
Raccoon 1:a213bf47cc4d 29 void transmit(void)
Raccoon 0:a17db953c0b2 30 {
Raccoon 1:a213bf47cc4d 31 static int i = 0;
Raccoon 1:a213bf47cc4d 32 xbee.putc(data[i]);
Raccoon 1:a213bf47cc4d 33 if(++i >= MAX)i=0;
Raccoon 0:a17db953c0b2 34 }
Raccoon 0:a17db953c0b2 35
Raccoon 1:a213bf47cc4d 36 /*受信*/
Raccoon 0:a17db953c0b2 37 void receve(void)
Raccoon 0:a17db953c0b2 38 {
Raccoon 1:a213bf47cc4d 39 myled = 1;
Raccoon 0:a17db953c0b2 40 }
Raccoon 0:a17db953c0b2 41
Raccoon 0:a17db953c0b2 42 void adcRead(void){
Raccoon 0:a17db953c0b2 43 /*ステッピングモーター*/
Raccoon 1:a213bf47cc4d 44 data[0] = ChannelCoding(ain_0, 0);
Raccoon 0:a17db953c0b2 45 /*EX球*/
Raccoon 1:a213bf47cc4d 46 data[1] = ChannelCoding(ain_1, 1);
Raccoon 1:a213bf47cc4d 47 /*室内灯*/
Raccoon 1:a213bf47cc4d 48 data[2] = ChannelCoding(ain_2, 2);
Raccoon 0:a17db953c0b2 49
Raccoon 1:a213bf47cc4d 50 data[3] = ChannelCoding(ain_3, 3);
Raccoon 0:a17db953c0b2 51 }
Raccoon 0:a17db953c0b2 52
Raccoon 0:a17db953c0b2 53 int main() {
Raccoon 2:6383151eb64b 54 for(int i=0; i<MAX; i++)data[i] = ChannelCoding(0.0f, i);
Raccoon 1:a213bf47cc4d 55 myled = 0;
Raccoon 0:a17db953c0b2 56 sw_0.mode(PullUp);
Raccoon 0:a17db953c0b2 57 sw_1.mode(PullUp);
Raccoon 0:a17db953c0b2 58 sw_2.mode(PullUp);
Raccoon 0:a17db953c0b2 59 xbee.baud(9600);
Raccoon 0:a17db953c0b2 60 xbee.format(8, Serial::None, 1);
Raccoon 0:a17db953c0b2 61 xbee.attach(&receve, Serial::RxIrq);//受信割り込み
Raccoon 1:a213bf47cc4d 62 Tr_timer.attach_us(&transmit, 20000);//送信
Raccoon 2:6383151eb64b 63 adc_timer.attach_us(&adcRead, 20000);//ADC
Raccoon 0:a17db953c0b2 64 while(1)__WFI();
Raccoon 0:a17db953c0b2 65 }