test2017-1-13

Dependencies:   AT24C1024 DMX-STM32 mbed

main.cpp

Committer:
tomo370
Date:
2017-01-13
Revision:
0:d94213e24c2e
Child:
1:cf2f5992241c

File content as of revision 0:d94213e24c2e:

//#define


#include "mbed.h"
#include "AT24C1024.h"
#include "AccelStepper.h"
#include "DMX.h"

Timer t;
DigitalOut dmxEnable(D8);
DMX dmx(PA_9, PA_10);

AccelStepper stepper1(1, D9, D6); // step, direction
AccelStepper stepper2(1, D10, D7); // step, direction

DigitalOut stepper1Enable(D2);
DigitalOut stepper1MS1(D11);
DigitalOut stepper1MS2(D12);
DigitalIn stepper1MS3(A4);

DigitalOut stepper2Enable(D3);
DigitalOut stepper2MS1(A5);
DigitalOut stepper2MS2(A6);
DigitalOut stepper2MS3(A7);

DigitalIn EndSW4(A3); //CN7
DigitalIn EndSW3(A2); //CN6
DigitalIn EndSW2(A1); //CN5
DigitalIn EndSW1(A0); //CN4
DigitalOut myLED(LED3); //動作確認用LED

long dt;
unsigned int dmxInHighBit;
unsigned int dmxInLowBit;
int pastPosition;

//map関数です
long map(long x, long in_min, long in_max, long out_min, long out_max)
{
    return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

void checkSW()
{
    if(EndSW4.read() == 0) myLED = 1;
    else {
        stepper1.stop();
        stepper1.setCurrentPosition(0);
        myLED =0;
    }
}

void silence()
{
    DigitalIn stepper1MS1(D11);
    DigitalIn stepper1MS2(D12);
}

void run()
{
    DigitalOut stepper1MS1(D11);
    DigitalOut stepper1MS2(D12);
    stepper1MS1 = 1;
    stepper1MS2 = 1;
}

void init()
{
    stepper1.setMaxSpeed(2000);
    stepper1.setAcceleration(20000);
    stepper1.moveTo(-16000);

    while(stepper1.currentPosition() != 0) {
        stepper1.run();
        checkSW();
    }
    stepper1.moveTo(500);
    while(stepper1.distanceToGo() != 0) {
        stepper1.run();
    }
    silence();


}



int main()
{

    stepper1MS1 = 1;
    stepper1MS2 = 1;
    //stepper1MS3 = 0;
    stepper1Enable = 0;

    t.start();

    dmxEnable = 0;  //0:receiver 1:sender

    stepper1.setCurrentPosition(-1);

    init();

    stepper1.setMaxSpeed(25000);
    stepper1.setAcceleration(30000);
    stepper1.setCurrentPosition(0);

    //run();

    while(1) {

        
        dmxInHighBit = dmx.get(0);
        dmxInLowBit = dmx.get(1);
        dmxInHighBit = dmxInHighBit << 8;
        dt = (long)(dmxInHighBit + dmxInLowBit);
        //if(unsigned (stepper1.distanceToGo()) > 0)
//            run();
//        else
//            silence();
        dt = map(dt, 0, 0xFFFF, 0, 32000); 
        //dt = dt*100;

        //printf("%d\n", dt);
        //wait(1);
        stepper1.moveTo(dt);//
//        if(unsigned ((stepper1.distanceToGo() - stepper1.currentPosition())) > 100)
//            run();
        checkSW();

        stepper1.run();
        

            
//        if (stepper1.distanceToGo() == 0)
//            silence();
//        else
//            run();
    }
}


//    printf("\r\n*** AT24C1024 Test ***\r\n");
//
//    // Byte Read/Write
//    //
//    uint8_t dt = 0x55;
//    printf("Byte Write: %02x\r\n", dt);
//    at24c1024.write(0, dt);     // write addr=0 data=dt
//    wait_ms(5);
//    dt = at24c1024.read(0);     // read addr=0
//    printf("Byte Read: %02x\r\n", dt);
//
//    // Page Read/Write
//    //
//    uint8_t eep_buf[258];
//    for (int i = 0; i < 256; i++) {
//        eep_buf[i] = i;
//    }
//    printf("Page Write: 0..255\r\n");
//    AT24C_STATUS status = at24c1024.write_page(0x1ff00, eep_buf, sizeof(eep_buf));
//    wait_ms(5);
//    printf("Status: %d\r\n", status);
//    printf("Page Read:\r\n");
//    status = at24c1024.read_page(0x1ff00, eep_buf, sizeof(eep_buf));
//    printf("Status: %d\r\n", status);
//    for (int i = 0; i < sizeof(eep_buf); i++) {
//        printf("%d ", eep_buf[i]);
//    }
//    printf("\r\n");


//I2C         i2c(D4, D5);      // SDA, SCL
//AT24C1024   at24c1024(i2c);     // Atmel 1Mbit EE-PROM

//    printf("\r\n*** AT24C1024 Test ***\r\n");
//
//    // Byte Read/Write
//    //
//    uint8_t dt = 0x22;
//    printf("Byte Write: %02x\r\n", dt);
//    at24c1024.write(0, dt);     // write addr=0 data=dt
//    wait_ms(10);
//    dt = at24c1024.read(0);     // read addr=0
//    printf("Byte Read: %02x\r\n", dt);
//
//    // Page Read/Write
//    //
//    uint8_t eep_buf[258];
//    for (int i = 0; i < 256; i++) {
//        eep_buf[i] = i;
//    }
//    printf("Page Write: 0..255\r\n");
//    AT24C_STATUS status = at24c1024.write_page(0x1ff00, eep_buf, sizeof(eep_buf));
//    wait_ms(5);
//    printf("Status: %d\r\n", status);
//    printf("Page Read:\r\n");
//    status = at24c1024.read_page(0x00, eep_buf, sizeof(eep_buf));
//    printf("Status: %d\r\n", status);
//    for (int i = 0; i < sizeof(eep_buf); i++) {
//        printf("%02x ", eep_buf[i]);
//    }
//    printf("\r\n");
//}