Ryota Маэда
/
AstroC_planetarium_master
投影機本体のプログラム
Fork of AstroC_planetarium_master by
main.cpp
- Committer:
- Raccoon
- Date:
- 2017-09-23
- Revision:
- 0:ee25c3c95837
- Child:
- 1:b6a142877de4
File content as of revision 0:ee25c3c95837:
#include "mbed.h" #define STEPPER_DELTA_T 100 /*ステッピングモーター*/ Ticker step_timer; DigitalOut APHASE(p5); DigitalOut AENBL(p6); DigitalOut BPHASE(p7); DigitalOut BENBL(p8); /*MCP4922*/ Ticker spi_timer; SPI spi(p11, p12, p13);//mosi(SDO), miso, sclk DigitalOut CS(p14); DigitalOut LDAC(p17); /*RGB_LED_3*/ Ticker rgb_timer; BusOut signal(p24, p25, p26); PwmOut R_led(p23); PwmOut G_led(p22); PwmOut B_led(p21); /*シリアル通信(xbee)*/ Serial xbee(p28, p27); // tx, rx /*受信データ格納*/ char data[6] = {0}; /*ステッピングモーター*/ void stepper(void) { static unsigned int cnt = 0; static int i = 1; static unsigned int delay = 1000; /*速度制限:MAX周期1500[us](脱調防止)*/ delay = (255 - data[0]) + 15; /*ステッピングモーターの制御*/ if((++cnt > delay) && data[0]!=0) { AENBL = 1; BENBL = 1; switch(i) { case 1: APHASE = 1; break; case 2: BPHASE = 1; break; case 3: APHASE = 0; break; case 4: BPHASE = 0; break; } if(++i==5)i = 1; cnt = 0; } else if(data[0]==0) { AENBL = 0; BENBL = 0; } } /*spiでの書き込み用関数*/ void spi_write(unsigned int config, unsigned int mask, unsigned int value) { unsigned int spi_data; spi_data = config | (mask & value); CS = 0; LDAC = 1; spi.write(spi_data); CS = 1; LDAC = 0; } /*SPI通信でMCP4922(DAC)を動かすための関数*/ void light_bulb(void) { static unsigned char ss = 0; //MCP4922へ送信するデータ static unsigned int mask = 0b0000111111111111; unsigned int config; unsigned int value; static unsigned int EX_MAX = 2510;//2.0[V] static unsigned int Milky_MAX = 3150;//2.5[V] /*前回の電圧の値と違う場合のみ実行される*/ if(ss==0 /*&& data[1]!=old_data[0]*/) { /*EX球(2V2A)用*/ config = 0b0111000000000000; value = (data[1] * EX_MAX) / 255; spi_write(config, mask, value); ss = 1; } else if(ss==1 /*&& data[2]!=old_data[1]*/) { /*天の川(2.5V2A)用*/ config = 0b1111000000000000; value = (data[2] * Milky_MAX) / 255; spi_write(config, mask, value); ss = 0; } } /*Hueの値deg(0~180)を受け取るとRGBに変換して反映させる*/ void HSB2RGB(float deg) { /*リセット*/ R_led = 0.0; G_led = 0.0; B_led = 0.0; if(0 < deg && deg <= 60) { R_led = 1.0 - (deg / 60); G_led = deg/60; B_led = 0.0; } else if(60 < deg && deg <= 120) { deg = deg - 60; R_led = 0.0; G_led = 1.0 - (deg / 60); B_led = deg/60; } else if(120 < deg && deg <= 180) { deg = deg - 120; R_led = deg/60; G_led = 0.0; B_led = 1.0 - (deg / 60); } } /*RGB_LEDをダイナミック点灯するための関数*/ void RGB_LED(void){ static uint8_t dig; uint8_t binary = 0b00001; HSB2RGB((float)data[3]*180/255); /*switch(dig){ case 0: signal = 0b00001; break; case 1: signal = 0b00010; break; case 2: signal = 0b00100; break; }*/ signal = binary << dig; if(++dig == 3)dig = 0; } /*シリアル通信受信(xbee)*/ void receve(void) { static int sw = 0; /*送信側から送信開始の合図で'R'が送られてくる*/ /*'R'を受信したら、データ受信開始*/ /*5個のデータが順番に受信されるので、順番に配列に代入する*/ if((sw == 0) && (xbee.getc() == 'R')) { sw = 1; } else if(1 <= sw && sw <= 6) { data[sw-1] = xbee.getc(); if(++sw > 6)sw = 0; } } int main(){ /*ステッピングモーター*/ AENBL = 1; APHASE = 0; BENBL = 1; BPHASE = 0; step_timer.attach_us(&stepper, STEPPER_DELTA_T);//100[us],10000[Hz] /*spi通信(MCP4922)*/ CS = 1; LDAC = 1; spi.format(16, 0); spi.frequency(1000000); spi_timer.attach_us(&light_bulb, 10000); /*RGB_LED_3*/ R_led.period(0.00004); G_led.period(0.00004); B_led.period(0.00004); rgb_timer.attach_us(&RGB_LED, 5000); /*シリアル通信(xbee)*/ xbee.baud(9600); xbee.format(8, Serial::None, 1); xbee.attach(&receve, Serial::RxIrq);//受信割り込み while(1)__WFI(); }