consept_meter

Dependencies:   mbed mbed-rtos

main.cpp

Committer:
shindo
Date:
2012-11-06
Revision:
0:134db3267830
Child:
1:39b4d6e71f76

File content as of revision 0:134db3267830:

#include "mbed.h"

DigitalIn busy1(p26);
DigitalIn flag1(p25);
DigitalOut cs1(p8);
DigitalOut stck1(p28);
DigitalOut stby1(p27);

DigitalIn busy2(p22);
DigitalIn flag2(p21);
DigitalOut cs2(p19);
DigitalOut stck2(p24);
DigitalOut stby2(p23);

DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);
DigitalOut led4(LED4);

SPI spi(p5, p6, p7); // mosi, miso, sclk

void writeCommand(int port, unsigned char data)
{
    if (port == 1) {
        cs1 = 0;
        spi.write(data);
        cs1 = 1;
    } else {
        cs2 = 0;
        spi.write(data);
        cs2 = 1;
    }
}

unsigned char readCommand(int port)
{
    unsigned char result = 0;

    if (port == 1) {
        cs1 = 0;
        result = spi.write(0);
        cs1 = 1;
    } else {
        cs2 = 0;
        result = spi.write(0);
        cs2 = 1;
    }

    return result;
}

void initMotor()
{
    spi.format(8,0);
    busy1.mode(PullUp);
    flag1.mode(PullUp);
    busy2.mode(PullUp);
    flag2.mode(PullUp);
    stby1 = 1;
    stby2 = 1;
    for (int port = 1; port <= 2; port++) {

        writeCommand(port, 0x08);
        writeCommand(port, 0x00);

        writeCommand(port, 0x09);
        writeCommand(port, 0xff);

        writeCommand(port, 0x0a);
        writeCommand(port, 0xff);

        writeCommand(port, 0x0b);
        writeCommand(port, 0xff);

        writeCommand(port, 0x0c);
        writeCommand(port, 0xff);

        writeCommand(port, 0x13);
        writeCommand(port, 0x0f);

        writeCommand(port, 0x14);
        writeCommand(port, 0x7f);
    }
}
void MAXspeed_port_1_A()
{
    writeCommand(1, 0x07);
    writeCommand(1, 0x00);
    writeCommand(1, 0x32);
}
void Step_Startspeed_port_1_A()
{
    writeCommand(1, 0x05);
    writeCommand(1, 0x00);
    writeCommand(1, 0x23);
}
void Step_Stopspeed_port_1_A()
{
    writeCommand(1, 0x06);
    writeCommand(1, 0x00);
    writeCommand(1, 0x15);
}
void CW_port_1_A()
{
    writeCommand(1, 0x51);
    writeCommand(1, 0x00);
    writeCommand(1, 0x25);
    writeCommand(1, 0x00);
}
void CCW_port_1_A()
{
    writeCommand(1, 0x50);
    writeCommand(1, 0x00);
    writeCommand(1, 0x10);
    writeCommand(1, 0x00);
}
void CCW_port_1_A2()
{
    writeCommand(1, 0x50);
    writeCommand(1, 0x00);
    writeCommand(1, 0x40);
    writeCommand(1, 0x00);
}
//----------------------------------------------
void MAXspeed_port_2_A()
{
    writeCommand(2, 0x07);
    writeCommand(2, 0x00);
    writeCommand(2, 0x32);
}
void Step_Startspeed_port_2_A()
{
    writeCommand(2, 0x05);
    writeCommand(2, 0x00);
    writeCommand(2, 0x23);
}
void Step_Stopspeed_port_2_A()
{
    writeCommand(2, 0x06);
    writeCommand(2, 0x00);
    writeCommand(2, 0x15);
}
void CW_port_2_A()
{
    writeCommand(2, 0x51);
    writeCommand(2, 0x00);
    writeCommand(2, 0x25);
    writeCommand(2, 0x00);
}
void CCW_port_2_A()
{
    writeCommand(2, 0x50);
    writeCommand(2, 0x00);
    writeCommand(2, 0x10);
    writeCommand(2, 0x00);
}
void CCW_port_2_A2()
{
    writeCommand(2, 0x50);
    writeCommand(2, 0x00);
    writeCommand(2, 0x40);
    writeCommand(2, 0x00);
}
//-----------------------------------
void stertup1 ()
{
    CW_port_1_A();
    CW_port_2_A();
}
void stertup2 ()
{
    CCW_port_1_A();
    CCW_port_2_A();
}
void power_up_p1_p2 ()
{
    writeCommand(1, 0x51);
    writeCommand(1, 0x00);
    writeCommand(1, 0x40);
    writeCommand(1, 0x00);
    writeCommand(2, 0x51);
    writeCommand(2, 0x00);
    writeCommand(2, 0x40);
    writeCommand(2, 0x00);
    wait_ms (630);
    writeCommand(2, 0xb8);
    wait_ms (175);
    writeCommand(1, 0xb8);
}
void on ()
{
    CW_port_2_A();
    wait_ms(200);
    writeCommand(2, 0xb8);
}
void reb1 ()
{
    CW_port_2_A();
    wait_ms(331);
    writeCommand(2, 0xb0);
    CCW_port_2_A();
    wait_ms(1400);
    writeCommand(2, 0xb0);
    wait_ms(300);
}
void reb2 ()
{
    writeCommand(2, 0x51);
    writeCommand(2, 0x00);
    writeCommand(2, 0x30);
    writeCommand(2, 0x00);
    wait_ms(60);
    writeCommand(2, 0xb8);
    writeCommand(2, 0x50);
    writeCommand(2, 0x00);
    writeCommand(2, 0x30);
    writeCommand(2, 0x00);
    wait_ms(60);
    writeCommand(2, 0xb8);
    writeCommand(2, 0x51);
    writeCommand(2, 0x00);
    writeCommand(2, 0x30);
    writeCommand(2, 0x00);
    wait_ms(60);
    writeCommand(2, 0xb8);
    writeCommand(2, 0x50);
    writeCommand(2, 0x00);
    writeCommand(2, 0x30);
    writeCommand(2, 0x00);
    wait_ms(60);
    writeCommand(2, 0xb8);
    writeCommand(2, 0x51);
    writeCommand(2, 0x00);
    writeCommand(2, 0x30);
    writeCommand(2, 0x00);
    wait_ms(60);
}
void reb3 ()
{
    writeCommand(2, 0x51);
    writeCommand(2, 0x00);
    writeCommand(2, 0x15);
    writeCommand(2, 0x00);
    wait_ms(500);

    writeCommand(2, 0x50);
    writeCommand(2, 0x00);
    writeCommand(2, 0x03);
    writeCommand(2, 0x00);
    wait_ms(4000);
    writeCommand(2, 0xb0);
}
void type1_tacho ()
{
    wait_ms(2000);

    writeCommand(2, 0x51);  //---1---
    writeCommand(2, 0x00);
    writeCommand(2, 0x10);
    writeCommand(2, 0x00);
    wait_ms(500);

    writeCommand(2, 0xb0);
    wait_ms(100);

    writeCommand(2, 0x51);
    writeCommand(2, 0x00);
    writeCommand(2, 0x25);
    writeCommand(2, 0x00);
    wait_ms(380);
    reb2 ();

    writeCommand(2, 0x50);
    writeCommand(2, 0x00);
    writeCommand(2, 0x20);
    writeCommand(2, 0x00);
    wait_ms(500);
    writeCommand(2, 0xb0);

    writeCommand(2, 0x51);
    writeCommand(2, 0x00);
    writeCommand(2, 0x08);
    writeCommand(2, 0x00);
    wait_ms(2050);

    writeCommand(2, 0x50);
    writeCommand(2, 0x00);
    writeCommand(2, 0x20);
    writeCommand(2, 0x00);
    wait_ms(450);
    writeCommand(2, 0xb8);
    wait_ms(200);
    writeCommand(2, 0x51);//---2---
    writeCommand(2, 0x00);
    writeCommand(2, 0x05);
    writeCommand(2, 0x00);
    wait_ms(1550);

    writeCommand(2, 0x50);
    writeCommand(2, 0x00);
    writeCommand(2, 0x20);
    writeCommand(2, 0x00);
    wait_ms(430);
    writeCommand(2, 0xb8);
    wait_ms(200);
    writeCommand(2, 0x51);//---3---
    writeCommand(2, 0x00);
    writeCommand(2, 0x04);
    writeCommand(2, 0x00);
    wait_ms(2000);

    writeCommand(2, 0x50);
    writeCommand(2, 0x00);
    writeCommand(2, 0x20);
    writeCommand(2, 0x00);
    wait_ms(400);
    writeCommand(2, 0xb8);
    wait_ms(200);
    writeCommand(2, 0x51);//---4---
    writeCommand(2, 0x00);
    writeCommand(2, 0x02);
    writeCommand(2, 0x00);
    wait_ms(4000);

    writeCommand(2, 0x50);
    writeCommand(2, 0x00);
    writeCommand(2, 0x20);
    writeCommand(2, 0x00);
    wait_ms(300);
    writeCommand(2, 0xb8);
    wait_ms(200);
    writeCommand(2, 0x51);//---5---
    writeCommand(2, 0x00);
    writeCommand(2, 0x01);
    writeCommand(2, 0x50);
    wait_ms(3000);
    writeCommand(2, 0x51);//---5---
    writeCommand(2, 0x00);
    writeCommand(2, 0x01);
    writeCommand(2, 0x00);
    wait_ms(1500);

    writeCommand(2, 0x50);
    writeCommand(2, 0x00);
    writeCommand(2, 0x20);
    writeCommand(2, 0x00);
    wait_ms(300);
    writeCommand(2, 0xb8);
    wait_ms(200);
    writeCommand(2, 0x51);//---6---
    writeCommand(2, 0x00);
    writeCommand(2, 0x01);
    writeCommand(2, 0x50);
    wait_ms(3000);
    writeCommand(2, 0x51);//---6---
    writeCommand(2, 0x00);
    writeCommand(2, 0x01);
    writeCommand(2, 0x00);
    wait_ms(2500);
}
void type1_speed ()
{
    writeCommand(1, 0x51);
    writeCommand(1, 0x00);
    writeCommand(1, 0x04);
    writeCommand(1, 0xb0);
    wait_ms(1500);
    writeCommand(1, 0x51);
    writeCommand(1, 0x00);
    writeCommand(1, 0x03);
    writeCommand(1, 0xe8);
    wait_ms(2000);
    writeCommand(1, 0x51);
    writeCommand(1, 0x00);
    writeCommand(1, 0x02);
    writeCommand(1, 0x58);
    wait_ms(2500);
    writeCommand(1, 0x51);
    writeCommand(1, 0x00);
    writeCommand(1, 0x01);
    writeCommand(1, 0x90);
    wait_ms(3000);
    writeCommand(1, 0x51);
    writeCommand(1, 0x00);
    writeCommand(1, 0x01);
    writeCommand(1, 0x2c);
    wait_ms(3500);
    writeCommand(1, 0x51);
    writeCommand(1, 0x00);
    writeCommand(1, 0x00);
    writeCommand(1, 0xfa);
    wait_ms(4500);
    writeCommand(1, 0x51);
    writeCommand(1, 0x00);
    writeCommand(1, 0x00);
    writeCommand(1, 0x78);
    wait_ms(5200);
    writeCommand(1, 0xb0);
}

int main()
{
    initMotor();
    MAXspeed_port_1_A();
    Step_Startspeed_port_1_A();
    Step_Stopspeed_port_1_A();

    MAXspeed_port_2_A();
    Step_Startspeed_port_2_A();
    Step_Stopspeed_port_2_A();

    writeCommand(2, 0x83);
    writeCommand(1, 0x83);

    int i = 0;
    while(1) {
        if (i == 0) {
            stertup1 ();
            wait_ms(200);
            stertup2 ();
            wait_ms(4000);
        } else if (i == 1) {
            power_up_p1_p2 ();
            wait_ms(1000);
        } else if (i == 2) {
            CCW_port_1_A2();
            CCW_port_2_A2();
            wait_ms(1000);
        } else if (i == 3) {
            on ();
            wait_ms(2000);
        } else if (i == 4) {
            reb1 ();
        } else if (i == 5) {

            //type1_demo ();
            type1_speed ();
            type1_tacho ();

        } else if (i == 6) {
            writeCommand(1, 0x50);
            writeCommand(1, 0x00);
            writeCommand(1, 0x01);
            writeCommand(1, 0x2c);

            writeCommand(2, 0x50);
            writeCommand(2, 0x00);
            writeCommand(2, 0x03);
            writeCommand(2, 0x00);
            wait_ms(5000);
            reb3 ();
            reb3 ();
            reb3 ();
            reb3 ();
            reb3 ();


            writeCommand(2, 0x50);
            writeCommand(2, 0x00);
            writeCommand(2, 0x03);
            writeCommand(2, 0x00);
            wait_ms(1800);
            writeCommand(1, 0xb8);
            writeCommand(2, 0xb8);
            wait(10);

        } else if (i == 8) {
            CCW_port_1_A();
            CCW_port_2_A();
            wait_ms(5000);
            i = -1;
        }
        wait_ms(200);

        i++;

    }

}