예제 #1
0
void LCD_PowerUp() {

    //Turn on the back light
    LCD_BL = 1;

    //Pause 60ms
    Wait_ms(60);

    //8-Bit, 2 Line, 5x7 Dots
    LCD_FunctionSet(1, 1, 1);

    //Display ON, Cursor OFF, Blink OFF
    LCD_DisplayOnOff(1, 0, 0);

    //Clear the Display
    LCD_ClearDisplay();

    //Pause 60ms
    Wait_ms(60);

    //8-Bit, 2 Line, 5x7 Dots
    LCD_FunctionSet(1, 1, 1);

    //Display ON, Cursor OFF, Blink OFF
    LCD_DisplayOnOff(1, 0, 0);

    //Clear the Display
    LCD_ClearDisplay();

}
예제 #2
0
파일: main.c 프로젝트: kLabUM/evaporation
/*******************************************************************************
* Function Name: main
********************************************************************************
* Summary:
*  Main function performs following functions:
*   1. Starts Character LCD and print project info
*   2. Starts SPI Master component
*   3. Configures the DMA transfer for RX and TX directions
*   4. Displays the results on Character LCD
*   
* Parameters:
*  None.
*
* Return:
*  None.
*
*******************************************************************************/
void main()
{
    uint8 i = 0u;
    CyGlobalIntEnable; 
    
    for(i=0u; i<8u; i++)
    {
        m_rxBuffer[i] = 0u;
    }
    
    LCD_Start();
    LCD_Position(0u,0u);
    LCD_PrintString("SPI Master");
    LCD_Position(1u,0u);
    LCD_PrintString("example");
    CyDelay(3000u);
    
    Dma_M_Tx_Configuration();
    Dma_M_Rx_Configuration();
    
    CyDmaChEnable(M_TxChannel, 1u); 
    CyDmaChEnable(M_RxChannel, 1u);
    
    LCD_Position(0u,0u);
    LCD_PrintString("Master Tx data:");
    LCD_Position(1u,0u);
    for(i=0u; i<8u; i++)
    {
        LCD_PrintHexUint8(m_txBuffer[i]);
    }
    
    CyDelay(5000u);
    SPIM_Start();
    
    while(!(SPIM_ReadTxStatus() & SPIM_STS_SPI_DONE));
    
    LCD_ClearDisplay();
    LCD_PrintString("Master Rx data:");
    LCD_Position(1u,0u);
    
    for(i=0u; i<8u; i++)
    {
        LCD_PrintHexUint8(m_rxBuffer[i]);
    }
    CyDelay(5000u);
    LCD_ClearDisplay();
    LCD_PrintString("SPI Master");
    LCD_Position(1u,0u);
    LCD_PrintString("example complete.");
     
    for(;;)
    {
        
    }
}
예제 #3
0
/******************************************************************************
* This function writes a string to the display
*
* Interface assumptions:
* The pstr must be zero-terminated.
*
* Return value:
* None
*
*
******************************************************************************/
void LCD_WriteString
  (
  uint8_t line,  /* IN: Line in display */
  uint8_t *pstr	 /* Pointer to text string */
  )
{
  uint8_t len;
  uint8_t x;
  uint8_t *error = "Wrong line 1 & 2";

  if( line == 2 ) {
    LCDLine(LineTwo);
  }
  else if ( line == 1 ) {
    LCDLine(LineOne);
    }
    else {
	  LCD_ClearDisplay();
	  LCDLine(LineOne);
      pstr = error;
    }

  len = GetStrlen(pstr);
  for ( x = 0; x < len; x++ ) {
    LCD_WriteChar( pstr[x] );
  }

  /* Clear the rest of the line */
  for ( ; x < gMAX_LCD_CHARS_c; x++ ) {
    LCD_WriteChar( ' ' );
  }
}
signed char GetYesNo(const char *prompt, signed char Initial) {
    signed char answer;
    signed char inp;
    LCD_ClearDisplay();
    LCD_PrintString(prompt);

    answer = Initial;
    while (1) {
        LCD_SetPosition(1, 0);
        if (answer) LCD_PrintString("<YES>\0");
        else LCD_PrintString("<NO> \0");
        inp = GetInput();
        if (inp < 0) return inp;

        switch (inp) {
            case USER_INPUT_CLICK: //The User Pressed the button to select something...
                return (answer);
                
            case USER_INPUT_CANCEL:
                return -2;
                
            case USER_INPUT_BACK:
                return -1;
                
            case USER_INPUT_INC: //Rot +
            case USER_INPUT_DEC: //Rot -
                answer = !answer;
                break;
            default:
                break;
        }
    }
    return (0);
}
signed char DisplayChoices(const char *Menu[], char ItemCount, const char *Prompt, char Initial) {
    char SelectedItem = 0;
    signed char ret = 0;
    SelectedItem = Initial;
    while (1) {
        LCD_ClearDisplay();
        LCD_PrintString(Prompt);
        LCD_SetPosition(1, 0);
        LCD_PrintChar('*');
        LCD_PrintString(Menu[SelectedItem]);

        ret = GetInput();
        if (ret < 0) return ret;

        switch (ret) {
            case USER_INPUT_CLICK:
                return (SelectedItem);
            case USER_INPUT_BACK:
                return (-1);
            case USER_INPUT_CANCEL:
                return (-2);
            case USER_INPUT_INC:
                if (SelectedItem == ItemCount) SelectedItem = ItemCount;
                else SelectedItem++;
                break;
            case USER_INPUT_DEC:
                if (SelectedItem == 0) SelectedItem = 0;
                else SelectedItem--;
                break;
        }
    }
    return (0);
}
예제 #6
0
void main_play_ttc(){
    LCD_Start();					    // initialize lcd
    LCD_ClearDisplay();
    UART_Start();                       // initialize UART
    UART_PutChar(0x81); // init connection; set to 16x12 image 
    
    struct disp_grid_81 disp; 
    disp_grid_init(&disp,0x3F); // init our display grid matrix to white  
    disp_grid_transmit(&disp);
    
    struct tic_tac_toe lolz;
 	ttc_init(&lolz,4,3);    
    disp_grid_init_ttc(&disp,lolz.grid); // init the board
    disp_grid_draw_xia(&disp,26,16,0x30); // draw xia
    disp_grid_transmit(&disp);
    
    int x,y,z; int Values;
    
    while (lolz.game_not_won == 0){
        //Values = read_from_8255(Values);
        Values = Pin3_Read();
        if (Values >= 0 && Values <= 63){ //integer value
            z = Values / 16;
            x = Values % 4; //get row value
            y = Values / 4 - z*4; // 
            LCD_ClearDisplay();
            LCD_PrintNumber(Values);
            LCD_PrintString(" x");
            LCD_PrintNumber(x);
            LCD_PrintString(" y");
            LCD_PrintNumber(y);
            LCD_PrintString(" z");
            LCD_PrintNumber(z);
        }
        if (ttc_get_grid(&lolz,x,y,z) == 0){ // has not been accessed
            ttc_step(&disp,&lolz,x,y,z); // step & print
            disp_grid_transmit(&disp);
        }
        
    }
    LCD_ClearDisplay();
    LCD_Position(0,0); //move to bot row
    LCD_PrintString("GAME OVER!");    
    
}
signed char DisplayMenu(const char *Menu[], char ItemCount, char Initial) {
    char SecondLine = 0;
    signed char SelectedItem = 0;
    signed char ret = 0;

    SelectedItem = Initial;

    while (1) {
        LCD_ClearDisplay();
        if (SecondLine) {
            LCD_SetPosition(0, 1);
            if (SelectedItem == 0) LCD_PrintString(Menu[ItemCount]);
            else LCD_PrintString(Menu[SelectedItem - 1]);
            LCD_SetPosition(1, 0);
            LCD_PrintChar('*');
            LCD_PrintString(Menu[SelectedItem]);
            LCD_SetPosition(1, 0);
        } else {
            LCD_PrintChar('*');
            LCD_PrintString(Menu[SelectedItem]);
            LCD_SetPosition(1, 1);
            if (SelectedItem == ItemCount) LCD_PrintString(Menu[0]);
            else LCD_PrintString(Menu[SelectedItem + 1]);
            LCD_SetPosition(0, 0);
        }

        ret = GetInput();
        if (ret < 0) {
            SelectedItem = ret;
            goto exit;
        }

        switch (ret) {
            case USER_INPUT_CLICK:
                goto exit;
            case USER_INPUT_BACK:
                SelectedItem = -1;
                goto exit;
            case USER_INPUT_CANCEL:
                SelectedItem = -2;
                goto exit;
            case USER_INPUT_INC:
                SecondLine = 1;
                if (SelectedItem == ItemCount) SelectedItem = ItemCount;
                else SelectedItem++;
                break;
            case USER_INPUT_DEC:
                SecondLine = 0;
                if (SelectedItem == 0) SelectedItem = 0;
                else SelectedItem--;
                break;
        }
    }
exit:
    return (SelectedItem);
}
signed char GetFloat(const char *prompt, const char *unit, double* Value, double Min, double Max, double Precision) {
    double Output;
    char WholePlaces;
    char FractionalPlaces;
    signed char ret = 0;


    if ((Max >= 10000) || (Min <= -10000)) WholePlaces = 5;
    else if (Max >= 1000 | Min <= -1000) WholePlaces = 4;
    else if (Max >= 100 | Min <= -100) WholePlaces = 3;
    else if (Max >= 10 | Min <= -10) WholePlaces = 2;
    else WholePlaces = 2;

    if (Precision >= 1.0) FractionalPlaces = 0;
    else if (Precision >= 0.1) FractionalPlaces = 1;
    else if (Precision >= 0.01) FractionalPlaces = 2;
    else if (Precision >= 0.001) FractionalPlaces = 3;
    else if (Precision >= 0.0001) FractionalPlaces = 4;


    Output = *Value;
    LCD_ClearDisplay();
    LCD_PrintString(prompt);

    LCD_SetPosition(1, 0);
    LCD_PrintFloat(Output, WholePlaces, FractionalPlaces, 1);
    LCD_PrintString(unit);

    while (1) {
        LCD_SetPosition(1, 0);
        LCD_PrintFloat(Output, WholePlaces, FractionalPlaces, 1);
        ret = GetInput();
        if (ret < 0) return ret;
        switch (ret) {
            case USER_INPUT_CLICK: //user has pressed the switch to accept.                     
                *Value = Output;
                return (0);
            case USER_INPUT_CANCEL:
                return -2;
                
            case USER_INPUT_BACK:
                return -1;
                
            case USER_INPUT_INC: //Rot+
                Output += GetRotaryMultiplier() * Precision;
                if (Output > Max) Output = Max;
                break;
            case USER_INPUT_DEC:
                Output -= GetRotaryMultiplier() * Precision;
                if (Output < Min) Output = Min;
                break;
        }
    }
    return (0);
}
예제 #9
0
void main_play_tta_ai(){
    LCD_Start();					    // initialize lcd
    LCD_ClearDisplay();
    UART_Start();                       // initialize UART
    UART_PutChar(0x81); // init connection; set to 16x12 image 
    
    struct disp_grid_81 disp; 
    disp_grid_init(&disp,0x3F); // init our display grid matrix to white  
    disp_grid_transmit(&disp);
    
    int x,y,z; uint8 Values; 
    
    struct tic_tac_ai tta;
    tta_init(&tta,4,3,true,false); //first bool for player 1, second bool for player 2
    disp_grid_init_ttc(&disp, tta.game.grid);
    disp_grid_draw_xia(&disp,26,16,0x30); // draw xia
    disp_grid_transmit(&disp);
    
    while (tta.game.game_not_won == 0){
        Values = read_from_8255(Values); //read and print
        if (Values >= 0 && Values <= 63){ //integer value
            z = Values / 16;
            x = Values % 4; //get row value
            y = Values / 4 - z*4; // 
//            LCD_ClearDisplay();
//            LCD_PrintNumber(Values);
//            LCD_PrintString(" x");
//            LCD_PrintNumber(x);
//            LCD_PrintString(" y");
//            LCD_PrintNumber(y);
//            LCD_PrintString(" z");
//            LCD_PrintNumber(z);
        }
        tta_step(&disp,&tta,x,y,z); //increment a turn
        disp_grid_transmit(&disp);
    }
    LCD_ClearDisplay();
    LCD_PrintString("GAME OVER!");   
    disp_grid_draw_win(&disp,27,9,tta.game.player); // draw tic
    disp_grid_transmit(&disp);
    
}
signed char ShowVoltage() {    
    double Batt;
    LCD_ClearDisplay();
    bLock_BatteryVoltage = 1;
    Batt = BatteryVoltage;
    bLock_BatteryVoltage = 0;
    Batt *= Config.Volts_per_Count;
    LCD_PrintString("BATTERY VOLTAGE:\0");
    LCD_SetPosition(1, 0);
    LCD_PrintFloat(Batt, 2, 2, 0);
    LCD_PrintString(" VOLTS\0");
    return GetClick();
}
예제 #11
0
void main(){
	CYGlobalIntDisable;//Disable interrupts to avoid triggering during setup.
    LCD_Start();// Start LCD.
	UART_Start();// Start UART.
	Rx_Int_Start();//Start the Interrupt.
	Rx_Int_SetVector(Rx_ISR);//Set the Vector to the ISR.
	LCD_PrintString("UART:RX ISR Demo");//Print to the LCD.
	UART_PutString("UART:RX ISR Demo");//Print to Serial Terminal.
	CyDelay(1000);
	LCD_ClearDisplay();//Clear the screen for RX Data.
	CYGlobalIntEnable;//Enable Interrupts,and let the Games begin!
	
    for(;;);
}
예제 #12
0
파일: main.c 프로젝트: atitus5/vizdude
int main()
{
    CyGlobalIntEnable; /* Enable global interrupts. */
    
    // Set up LCD screen
    LCD_Start();
	LCD_ClearDisplay();
    
    rx_isr_StartEx(rx_isr);             // start RX interrupt (look for CY_ISR with RX_INT address)
                                        // for code that writes received bytes to LCD.
    
    UART_Start();                       // initialize UART
    UART_ClearRxBuffer();
    
    // Check status message
    UART_PutString("AT\r\n");
    CyDelay(1000);\
    LCD_ClearDisplay();
    
    // Set name to VizDude
    UART_PutString("AT+NAME=VizDude\r\n");
    CyDelay(1000);\
    LCD_ClearDisplay();
    
    // Set rate to 115200 Baud, 1 stop bit, no parity
    UART_PutString("AT+UART=115200,1,0\r\n");
    CyDelay(1000);\
    LCD_ClearDisplay();

    /* Place your initialization/startup code here (e.g. MyInst_Start()) */

    for(;;)
    {
        /* Place your application code here. */
    }
}
signed char GetPresetNumber() {
    signed char inp;
    char pnum;
    signed char ptype;
    LCD_ClearDisplay();
    LCD_PrintString("PRESET SLOT:\0");
    LCD_SetPosition(1, 0);
    LCD_PrintString("PRESET 01\0");
    pnum = 1;

    while (1) {
        LCD_SetPosition(1, 7);
        LCD_PrintLong((int) pnum, 2, 0);

        ptype = GetPresetType(pnum);

        switch (ptype) {
            case 0:
                LCD_PrintString("-EMPTY    \0");
                break;
            case 1:
                LCD_PrintString("-WAYPOINT \0");
                break;
            case 2:
                LCD_PrintString("-ORBIT    \0");
                break;
        }

        inp = GetInput();
        if (inp<0) return inp;
        
        switch (inp) {
            case USER_INPUT_DEC:
                if (pnum > 1) pnum--;
                break;
            case USER_INPUT_INC:
                if (pnum < 5) pnum++;
                break;
            case USER_INPUT_CLICK:
                return (pnum);
            case USER_INPUT_BACK:
                return (-1);
            case USER_INPUT_CANCEL:
                return (-2);
        }
    }
    return (-2);
}
예제 #14
0
/*****************************************************************************
* Initialization function for the App Task. This is called during
* initialization and should contain any application specific initialization
* (ie. hardware initialization/setup, table initialization, power up
* notificaiton.
*
* Interface assumptions: None
*
* Return value: None
*
*****************************************************************************/
void MApp_init(void)
{ 
  
  /* The initial application state */
  gState = stateInit;  
  /* Reset number of pending packets */
  mcPendingPackets = 0;
  
  /* Allow sending a poll request */  
  mWaitPollConfirm = FALSE;    
  
  /* Initialize the poll interval */
  mPollInterval = mDefaultValueOfPollIntervalSlow_c;

  /* Initialize the MAC 802.15.4 extended address */
  Init_MacExtendedAddress();
  mTimer_c = TMR_AllocateTimer();
  /* register keyboard callback function */
  KBD_Init(App_HandleKeys);
  /* initialize LCD Module */
  LCD_Init();
  /* initialize LED Module */
  LED_Init();
  //WSNProject
   /*initialize the usage of power consumption*/
  PWR_CheckForAndEnterNewPowerState_Init();
  /* Initialize the UART so that we can print out status messages */
  UartX_SetBaud(gUartDefaultBaud_c);
  UartX_SetRxCallBack(UartRxCallBack);
  /* Prepare input queues.*/
  MSG_InitQueue(&mMlmeNwkInputQueue); 
  MSG_InitQueue(&mMcpsNwkInputQueue);
  /* Enable MCU interrupts */
  IntEnableAll();
  /*signal app ready*/  
  Led1Flashing();
  Led2Flashing();
  Led3Flashing();
  Led4Flashing();
    
  UartUtil_Print("\n\rPress any switch on board to start running the application.\n\r", gAllowToBlock_d);  
  LCD_ClearDisplay();
  LCD_WriteString(1,"Press any key");
  LCD_WriteString(2,"to start.");
}
예제 #15
0
void main_test_disp(){
    LCD_Start();					    // initialize lcd
    uint8 current;
    for (;;){
        LCD_ClearDisplay();
        
        LCD_Position(0,0); //move back to top row
        current = Pin0_Read(); //next, read a new value
        LCD_PrintString("P0: ");
        LCD_PrintNumber(current); //print value I am getting
        
        LCD_Position(1,0); //move to bot row
        current = Pin3_Read(); //next, read a new value
        LCD_PrintString("P3: ");
        LCD_PutChar(current); //print ascii value
        LCD_PrintString(" HEX: ");
        LCD_PrintNumber(current); //print value I am getting
        
        waiter(4);
    }
}
예제 #16
0
int main()
{
    //Blinky();
    
    CyGlobalIntEnable; /* Enable global interrupts. */

    // Enable the interrupt component connected to hallEffectSensor
    hallEffect_ISR_Start();
    hallEffect_ISR_SetVector(hallEffectSensor);
    
    // Start components
    hallTickCounter_Start();
    hallTickTimer_Start();
    PWM_GateVoltage_Start();
    
    // Start clocks
    deBounce_Start();
    PWM_clock_Start();
    //testClock_Start();
    
    // Start LCD
    LCD_Start();
    LCD_ClearDisplay();

    for(;;)
    {
        /* Place your application code here. */
        /*
        CyDelay(1000);
        Blinky();
        LCD_Position(0u,0u);
        LCD_PrintU32Number(i);
        i++;
        */
        
    }
}
예제 #17
0
파일: main.c 프로젝트: bill-he/jackbill350
/*******************************************************************************
* Function Name: main
********************************************************************************
* Summary:
*  Main function performs following functions:
*   1. Starts Character LCD and print project info
*   2. Starts SPI Master component
*   3. Configures the DMA transfer for RX and TX directions
*   4. Displays the results on Character LCD
*   
* Parameters:
*  None.
*
* Return:
*  None.
*
*******************************************************************************/
int main()
{
    uint8 i;
    
    LCD_Start();
    LCD_Position(0u,0u);
    LCD_PrintString("SPI Master");
    LCD_Position(1u,0u);
    LCD_PrintString("example");
    CyDelay(2000u);
    
    DmaTxConfiguration();
    DmaRxConfiguration();
    
    SPIM_Start();
    
    CyDmaChEnable(rxChannel, STORE_TD_CFG_ONCMPLT);
    CyDmaChEnable(txChannel, STORE_TD_CFG_ONCMPLT);

    while (0u == (SPIM_ReadTxStatus() & SPIM_STS_SPI_DONE))
    {
    }
    
    LCD_ClearDisplay();
    LCD_PrintString("Master Rx data:");
    LCD_Position(1u,0u);
    
    for(i=0u; i<BUFFER_SIZE; i++)
    {
        LCD_PrintHexUint8(rxBuffer[i]);
    }

    for(;;)
    {
    }
}
예제 #18
0
signed char GoToSleep() {
    signed char retCode = 0;
    UI_Location = UI_LOC_SLEEP;
    signed char inp;
    int bin;
    char idx;

    LCD_ClearDisplay();
    LCD_PrintString("SLEEP\0");
    LCD_DisplayOnOff(0, 0, 0);
    bPowerOff = 1;

    double Expires;
    double Time;

step1:
    ClearUI_Event = 1;
    while (ClearUI_Event);
    while (1) {
        Idle();
        inp = GetInput_nonblocking();
        if (inp<-2) {
            retCode = inp;
            goto WAKEUP;
        }
        switch (inp) {
            case USER_INPUT_CANCEL: //The user pressed cancel
                Expires = Now();
                Expires += 5000;
                bin = 0;
                goto step2;
        }
        idx = 0xff;
        while (idx--);
    }

step2:
    while (1) {
        Idle();
        inp = GetInput_nonblocking();
        if (inp<-2) {
            retCode = inp;
            goto WAKEUP;
        }
        Time = Now();
        if (Time > Expires) goto step1;

        switch (inp) {
            case USER_INPUT_INC: //The user pressed cancel
                bin++;
                if (bin > 8) goto WAKEUP;
                break;
            case USER_INPUT_DEC:
                bin = 0;
                break;
        }
        idx = 0xff;
        while (idx--);
    }
WAKEUP:
    LCD_DisplayOnOff(1, 0, 0);
    bPowerOff = 0;
    return retCode;
}
예제 #19
0
void main(void) {
    signed char ret = 0;
    signed char lastMenu = 0;
    char pnum = 0;

    SetupIO();
    MOTOR_FORWARD = 0;
    MOTOR_REVERSE = 0;

    Move_shifted_position.ul = 0;
    Move_position[0].ul = 0;
    Move_position[1].ul = 0;
    //PID_SetPoint = 0;
    char idx;
    for (idx = 0; idx < 16; idx++) RotaryDetentIntervals[idx] = 0xFF;

    LoadSettings();
    //FactoryDefault();

    SetupHardware();

    LCD_PowerUp();
    LCD_ClearDisplay();

    LCD_PrintString("CINEFLUX ORBIT\0");
    LCD_SetPosition(1, 0);
    LCD_PrintString(VER);
    Wait_ms(2000);

    const char *COMMAND_0 = "ORBIT MODE\0";
    const char *COMMAND_1 = "WAYPOINT MODE\0";
    const char *COMMAND_3 = "RUN PRESET\0";
    const char *COMMAND_2 = "REALTIME MODE\0";
    const char *COMMAND_4 = "BATTERY VOLTAGE\0";
    const char *COMMAND_5 = "GO TO SLEEP\0";
    const char *COMMAND_6 = "EXTERNAL CTRL MODE\0";
    const char*CommandMenu[7];
    CommandMenu[0] = COMMAND_0;
    CommandMenu[1] = COMMAND_1;
    CommandMenu[2] = COMMAND_2;
    CommandMenu[3] = COMMAND_3;
    CommandMenu[4] = COMMAND_4;
    CommandMenu[5] = COMMAND_5;
    CommandMenu[6] = COMMAND_6;

    bFollowMode = 1;
    LCD_ClearDisplay();
    LCD_PrintString("MOVE TO ZERO DEGREES\0");
    LCD_SetPosition(1, 0);
    LCD_PrintString("THEN CLICK...\0");
    GetClick();
    bClear_MotorPosition = 1;
    while (bClear_MotorPosition) Idle();
    Wait_ms(10);
    bFollowMode = 0;
    LCD_ClearDisplay();

    while (1) {
        UI_Location = UI_LOC_MAINMENU;
        ret = DisplayMenu(CommandMenu, 6, lastMenu);
labelProcessCommand:
        switch (ret) {
            case 0:
            case USER_INPUT_MACRO_ORBITMODE:
                lastMenu = 0;
                ret = CreateOrbitProgram();
                if (ret == -2) RealtimeMode();
                if (ret == -1) RealtimeMode();
                if (ret<-2) goto labelProcessCommand;
                break;
            case 1:
            case USER_INPUT_MACRO_WAYMODE:
                lastMenu = 1;
                ret = CreateWaypointProgram();
                if (ret<-2) goto labelProcessCommand;
                if (ret < 0) {
                    ret = RealtimeMode();
                    if (ret<-2) goto labelProcessCommand;
                }
                break;
            case 3:
                lastMenu = 3;
                ret = GetPresetNumber();
                if (ret == -1) break;
                if (ret == -2) break;
                if (ret<-2) goto labelProcessCommand;
                pnum = ret;
                ret = RunPreset(pnum);
                if (ret<-2) goto labelProcessCommand;
                break;
            case 2:
            case USER_INPUT_MACRO_REALMODE:
                lastMenu = 0;
                ret = RealtimeMode();
                if (ret<-2) goto labelProcessCommand;
                break;
            case 4:
                lastMenu = 0;
                ret = ShowVoltage();
                if (ret<-2) goto labelProcessCommand;
                break;
            case 5:
            case USER_INPUT_MACRO_SLEEP:
                lastMenu = 0;
                GoToSleep();
                break;
            case 6:
            case USER_INPUT_MACRO_EXTMODE:
                lastMenu = 0;
                ret = ExtMode();
                if (ret<-2) goto labelProcessCommand;
                break;
            case USER_INPUT_MACRO_RUNPRESET0:
                ret = RunPreset(1);
                if (ret<-2) goto labelProcessCommand;
                break;
            case USER_INPUT_MACRO_RUNPRESET1:
                ret = RunPreset(2);
                if (ret<-2) goto labelProcessCommand;
                break;
            case USER_INPUT_MACRO_RUNPRESET2:
                ret = RunPreset(3);
                if (ret<-2) goto labelProcessCommand;
                break;
            case USER_INPUT_MACRO_RUNPRESET3:
                ret = RunPreset(4);
                if (ret<-2) goto labelProcessCommand;
                break;
            case USER_INPUT_MACRO_RUNPRESET4:
                ret = RunPreset(5);
                if (ret<-2) goto labelProcessCommand;
                break;

        }
    }
}
예제 #20
0
void main(void) {
    signed char ret = 0;
    signed char lastMenu = 0;
    char pnum = 0;

    SetupIO();
    MOTOR_FORWARD = 0;
    MOTOR_REVERSE = 0;

    Move_shifted_position.ul = 0;
    Move_position[0].ul = 0;
    Move_position[1].ul = 0;
    //PID_SetPoint = 0;
    char idx;
    for (idx = 0; idx < 16; idx++) RotaryDetentIntervals[idx] = 0xFF;

    //LoadSettings();
    FactoryDefault();

    SetupHardware();

    LCD_PowerUp();
    LCD_ClearDisplay();

    LCD_PrintString("CINEFLUX ORBIT\0");
    LCD_SetPosition(1,0);    
    LCD_PrintString(VER);
    Wait_ms(2000);

    const char *COMMAND_0 = "ORBIT MODE\0";
    const char *COMMAND_1 = "WAYPOINT MODE\0";
    const char *COMMAND_3 = "RUN PRESET\0";
    const char *COMMAND_2 = "REALTIME MODE\0";
    const char *COMMAND_4 = "BATTERY VOLTAGE\0";
    const char *COMMAND_5 = "GO TO SLEEP\0";
    const char *COMMAND_6 = "EXTERNAL CTRL MODE\0";
    const char*CommandMenu[7];
    CommandMenu[0] = COMMAND_0;
    CommandMenu[1] = COMMAND_1;
    CommandMenu[2] = COMMAND_2;
    CommandMenu[3] = COMMAND_3;
    CommandMenu[4] = COMMAND_4;
    CommandMenu[5] = COMMAND_5;
    CommandMenu[6] = COMMAND_6;

    bFollowMode = 1;
    LCD_ClearDisplay();
    LCD_PrintString("MOVE TO ZERO DEGREES\0");
    LCD_SetPosition(1, 0);
    LCD_PrintString("THEN CLICK...\0");
    GetClick();
    bClear_MotorPosition = 1;
    while (bClear_MotorPosition) Idle();
    Wait_ms(10);
    bFollowMode = 0;
    LCD_ClearDisplay();

    while (1) {
        ret = DisplayMenu(CommandMenu, 6, lastMenu);
        switch (ret) {
            case 0:
                lastMenu = 0;
                if (!CreateOrbitProgram() == 0)
                    RealtimeMode();
                break;
            case 1:
                lastMenu = 1;
                if (!CreateWaypointProgram() == 0)
                    RealtimeMode();
                break;
            case 3:
                lastMenu = 2;
                ret = GetPresetNumber();
                if (ret == -1) break;
                if (ret == -2) break;
                pnum = ret;
                switch (GetPresetType(pnum)) {
                    case 1:
                        LoadPreset(pnum, (unsigned char *) &CurrentPath);
                        if (!RunWaypointProgram() == 0)
                            RealtimeMode();
                        break;
                    case 2:
                        LoadPreset(pnum, (unsigned char *) &CurrentOrbitProgram);
                        if (!RunOrbitProgram() == 0)
                            RealtimeMode();
                        break;
                }
                break;
            case 2:
                lastMenu = 2;
                RealtimeMode();
                break;
            case 4:
                lastMenu = 0;
                ShowVoltage();
                break;
            case 5:
                lastMenu = 0;
                GoToSleep();
                break;
            case 6:
                lastMenu = 0;
                ExtMode();
                break;
        }
    }
}
예제 #21
0
unsigned char ExtMode() {
    UI_Location = UI_LOC_EXTMODE;

    unsigned char PathIdx = 0;
    unsigned char PathCount = 0;
    signed int PathDistances[100];
    unsigned int PathTravelTimes[100];
    unsigned int PathDwellTimes[100];

    ExtModeActive = 1;
    signed char ret;
    unsigned char CmdID;    
    unsigned char idx;
    unsigned char State = 0;
    MULTI mTemp;

    char PrepMove_Ready = 0;
    double PrepMove_Distance, PrepMove_Speed, PrepMove_Acceleration;
    double S, D, T;

    LCD_ClearDisplay();
    LCD_PrintString("EXTERNAL MODE");

    while (1) {
        ret = GetInput_nonblocking();
        if (ret == USER_INPUT_CANCEL) {
            goto labelShutdown;
        }
        switch (State) {
            case 0: //Idle Mode - Nothing is happening...
                bMove_InProgress = 0;
                bFollowMode = 0;
                bSpeedMode = 0;
                break;
            case 1: //Stopping
                bMove_InProgress = 0;
                bFollowMode = 0;
                bSpeedMode = 1;
                Speed_SetToThis = 0;
                Speed_SetPending = 1;
                while(Speed_SetPending);
                if (!Speed_IsAccelerating) State = 0;
                break;
            case 2: //Single Move in Progress
                if (!bMove_InProgress) State = 1;
                break;
            case 3: //Moving to a Point along a Path;
                if (!bMove_InProgress) {
                    Wait_seconds_nonblocking(PathDwellTimes[PathIdx]);
                    PathIdx += 1;
                    State = 4;
                }
                break;
            case 4: //Waiting for the Dwell time to expire for this waypoint...
                if (!bWaiting) {
                    if (PathIdx == PathCount) {
                        State = 1;
                    } else {
                        D = PathDistances[PathIdx];
                        T = PathTravelTimes[PathIdx];
                        S = SpeedRequiredToMoveInTime(D, T);
                        Move(D, S);
                        State = 3;
                    }
                }
                break;
        }

        if (RX_MsgComplete) {
            ChkSum = 0;
            ReadIdx = 1;
            RX_MsgComplete = 0;
            if (MessageStream_ReadByte() == MyID) {
                CmdID = MessageStream_ReadByte();
                switch (CmdID) {
                    case CMD_PREP_MOVE:
                        mTemp.ub[0] = MessageStream_ReadByte();
                        mTemp.ub[1] = MessageStream_ReadByte();
                        mTemp.ub[2] = MessageStream_ReadByte();
                        mTemp.ub[3] = MessageStream_ReadByte();
                        PrepMove_Distance = mTemp.dbl;
                        ChkSum += mTemp.ub[3];
                        ChkSum += mTemp.ub[2];
                        ChkSum += mTemp.ub[1];
                        ChkSum += mTemp.ub[0];

                        mTemp.ub[0] = MessageStream_ReadByte();
                        mTemp.ub[1] = MessageStream_ReadByte();
                        mTemp.ub[2] = MessageStream_ReadByte();
                        mTemp.ub[3] = MessageStream_ReadByte();
                        PrepMove_Speed = mTemp.dbl;
                        ChkSum += mTemp.ub[3];
                        ChkSum += mTemp.ub[2];
                        ChkSum += mTemp.ub[1];
                        ChkSum += mTemp.ub[0];

                        mTemp.ub[0] = MessageStream_ReadByte();
                        mTemp.ub[1] = MessageStream_ReadByte();
                        mTemp.ub[2] = MessageStream_ReadByte();
                        mTemp.ub[3] = MessageStream_ReadByte();
                        PrepMove_Acceleration = mTemp.dbl;
                        ChkSum += mTemp.ub[3];
                        ChkSum += mTemp.ub[2];
                        ChkSum += mTemp.ub[1];
                        ChkSum += mTemp.ub[0];

                        if (ChkSum == MessageStream_ReadByte()) {
                            PrepMove_Ready = 1;
                            AckCmd(CmdID);
                        } else {
                            PrepMove_Ready = 0;
                            NackCmd(CmdID, 0xFA);
                        }
                        break;
                    case CMD_EXEC_MOVE:
                        if (!PrepMove_Ready) {
                            NackCmd(CmdID, 1);
                            break;
                        }
                        if (State) {
                            NackCmd(CmdID, 2);
                            break;
                        }
                        MoveAdvanced(PrepMove_Distance, PrepMove_Speed, PrepMove_Acceleration);
                        PrepMove_Ready = 0;
                        State = 2;
                        AckCmd(CmdID);

                        break;
                    case CMD_STOP:
                        if (!State == 0) State = 1;
                        AckCmd(CmdID);
                        break;
                    case CMD_STATUS:
                        TX_Idx = 0;
                        TXBuffer[TX_Idx++] = '$';
                        MessageStream_WriteByte(CmdID);

                        MessageStream_WriteByte(State);
                        ChkSum += State;

                        MessageStream_WriteByte(PrepMove_Ready);
                        ChkSum += PrepMove_Ready;

                        bLock_Motor_Position = 1;
                        mTemp.dbl = Motor_Position;
                        bLock_Motor_Position = 0;
                        mTemp.dbl *= Config.Degrees_Per_Count;
                        MessageStream_WriteByte(mTemp.ub[0]);
                        MessageStream_WriteByte(mTemp.ub[1]);
                        MessageStream_WriteByte(mTemp.ub[2]);
                        MessageStream_WriteByte(mTemp.ub[3]);
                        ChkSum += mTemp.ub[0];
                        ChkSum += mTemp.ub[1];
                        ChkSum += mTemp.ub[2];
                        ChkSum += mTemp.ub[3];

                        mTemp.dbl = 0;
                        while (!(mTemp.dbl == Move_speedQ24)) mTemp.dbl = Move_speedQ24;
                        mTemp.dbl *= INV_Conversion_Q24_500Hz;
                        mTemp.dbl *= Config.Degrees_Per_Count;
                        MessageStream_WriteByte(mTemp.ub[0]);
                        MessageStream_WriteByte(mTemp.ub[1]);
                        MessageStream_WriteByte(mTemp.ub[2]);
                        MessageStream_WriteByte(mTemp.ub[3]);
                        ChkSum += mTemp.ub[0];
                        ChkSum += mTemp.ub[1];
                        ChkSum += mTemp.ub[2];
                        ChkSum += mTemp.ub[3];

                        bLock_Tick500Hz = 1;
                        mTemp.dbl = Tick500Hz;
                        bLock_Tick500Hz = 0;
                        mTemp.dbl *= 0.002;
                        MessageStream_WriteByte(mTemp.ub[0]);
                        MessageStream_WriteByte(mTemp.ub[1]);
                        MessageStream_WriteByte(mTemp.ub[2]);
                        MessageStream_WriteByte(mTemp.ub[3]);
                        ChkSum += mTemp.ub[0];
                        ChkSum += mTemp.ub[1];
                        ChkSum += mTemp.ub[2];
                        ChkSum += mTemp.ub[3];

                        bLock_BatteryVoltage = 1;
                        mTemp.dbl = BatteryVoltage;
                        bLock_BatteryVoltage = 0;
                        mTemp.dbl *= Config.Volts_per_Count;
                        MessageStream_WriteByte(mTemp.ub[0]);
                        MessageStream_WriteByte(mTemp.ub[1]);
                        MessageStream_WriteByte(mTemp.ub[2]);
                        MessageStream_WriteByte(mTemp.ub[3]);
                        ChkSum += mTemp.ub[0];
                        ChkSum += mTemp.ub[1];
                        ChkSum += mTemp.ub[2];
                        ChkSum += mTemp.ub[3];

                        MessageStream_WriteByte(ChkSum);

                        TXBuffer[TX_Idx++] = '#';
                        idx = TX_Idx;
                        TX_Idx = 0;
                        TX_bCount = idx;
                        break;
                    case CMD_PATH_INIT:
                        if ((State == 3) || (State == 4)) {
                            NackCmd(CmdID, 1);
                            break;
                        }
                        PathIdx = 0;
                        PathCount = 0;
                        AckCmd(CmdID);
                        break;
                    case CMD_PATH_ADD:
                        if ((State == 3) || (State == 4)) {
                            NackCmd(CmdID, 1);
                            break;
                        }

                        if (PathCount > 99) {
                            NackCmd(CmdID, 2);
                            break;
                        }

                        mTemp.ub[0] = MessageStream_ReadByte();
                        mTemp.ub[1] = MessageStream_ReadByte();
                        ChkSum += mTemp.ub[0];
                        ChkSum += mTemp.ub[1];
                        PathDistances[PathCount] = mTemp.i[0];

                        mTemp.ub[0] = MessageStream_ReadByte();
                        mTemp.ub[1] = MessageStream_ReadByte();
                        ChkSum += mTemp.ub[0];
                        ChkSum += mTemp.ub[1];
                        PathTravelTimes[PathCount] = mTemp.ui[0];

                        mTemp.ub[0] = MessageStream_ReadByte();
                        mTemp.ub[1] = MessageStream_ReadByte();
                        ChkSum += mTemp.ub[0];
                        ChkSum += mTemp.ub[1];
                        PathDwellTimes[PathCount] = mTemp.ui[0];

                        if (ChkSum == MessageStream_ReadByte()) {
                            AckCmd(CmdID);
                            PathCount++;
                        } else {
                            PrepMove_Ready = 0;
                            NackCmd(CmdID, 0xFA);
                        }
                        break;
                    case CMD_PATH_RUN:
                        if (!(State == 0)) {
                            NackCmd(CmdID, 1);
                            break;
                        }
                        PathIdx = 0;
                        D = PathDistances[PathIdx];
                        T = PathTravelTimes[PathIdx];
                        S = SpeedRequiredToMoveInTime(D, T);
                        Move(D, S);
                        State = 3;
                        AckCmd(CmdID);
                        break;
                    case CMD_EXIT:
                        AckCmd(CmdID);
                        goto labelShutdown;                       
                    default:
                        NackCmd(CmdID, 0xFE);
                        break;
                }
            }
        }
    }

labelShutdown:
    bMove_InProgress = 0;
    bFollowMode = 0;
    bSpeedMode = 1;
    Speed_SetToThis = 0;
    Speed_SetPending = 1;
    while (Speed_IsAccelerating) {
        idx = 0xff;
        while (idx--);
    }
    ExtModeActive = 0;

    return 0;
}
signed char CreateOrbitProgram() {
    UI_Location = UI_LOC_ORBITSETUP;
    signed char ret;

    const char *DIRECTION_1 = "CLOCKWISE\0";
    const char *DIRECTION_0 = "COUNTER CLOCKWISE\0";
    const char*DirectionMenu[2];
    DirectionMenu[0] = DIRECTION_0;
    DirectionMenu[1] = DIRECTION_1;

    const char *SpeedMODE_0 = "MANUAL\0";
    const char *SpeedMODE_1 = "PER ORBIT\0";
    const char *SpeedMODE_2 = "FOR ALL ORBITS\0";
    const char*SpeedModeMenu[3];
    SpeedModeMenu[0] = SpeedMODE_0;
    SpeedModeMenu[1] = SpeedMODE_1;
    SpeedModeMenu[2] = SpeedMODE_2;

    const char *ENDMODE_0 = "BY ORBIT COUNT\0";
    const char *ENDMODE_1 = "BY TOTAL TIME\0";
    const char *ENDMODE_2 = "NEVER ENDING\0";
    const char*EndModeMenu[3];
    EndModeMenu[0] = ENDMODE_0;
    EndModeMenu[1] = ENDMODE_1;
    EndModeMenu[2] = ENDMODE_2;

    const char *COMMAND_0 = "RUN PROGRAM\0";
    const char *COMMAND_1 = "SAVE PRESET\0";
    const char*CommandMenu[2];
    CommandMenu[0] = COMMAND_0;
    CommandMenu[1] = COMMAND_1;

labelOrigin:
    LCD_ClearDisplay();
    LCD_PrintString("MOVE TO START...\0");
    LCD_SetPosition(1, 0);
    LCD_PrintString("THEN CLICK.\0");
    bFollowMode = 1;
    ret = GetClick();
    bFollowMode = 0;
    if (ret < 0) return ret;    
    CurrentOrbitProgram.Origin_deg = (unsigned int) GetCurrentAngle();

labelDirection:
    ret = DisplayChoices(DirectionMenu, 1, "ROTATION DIRECTION:\0", CurrentOrbitProgram.IsClockWise);
    if (ret == -1) goto labelOrigin;
    if (ret == -2) return (-2);
    if (ret < 0) return ret;
    CurrentOrbitProgram.IsClockWise = ret;

labelEndMode:
    ret = DisplayChoices(EndModeMenu, 2, "PROGRAM END MODE:\0", CurrentOrbitProgram.EndMode);
    if (ret == -1) goto labelDirection;
    if (ret == -2) return (-2);
    if (ret < 0) return ret;
    CurrentOrbitProgram.EndMode = ret;
    switch (CurrentOrbitProgram.EndMode) {
        case 0: //End based on Cycle Count.
            labelCycleCount :
            if (CurrentOrbitProgram.CycleCount_rev < 1) CurrentOrbitProgram.CycleCount_rev = 1;
            if (CurrentOrbitProgram.CycleCount_rev > 999) CurrentOrbitProgram.CycleCount_rev = 999;

            ret = GetFloat("ORBIT LIMIT:\0", "", &CurrentOrbitProgram.CycleCount_rev, 1, 999, 1.0);
            if (ret == -1) goto labelEndMode;
            if (ret == -2) return (-2);
            if (ret < 0) return ret;

labelSpeedMode:
            if (CurrentOrbitProgram.CycleCount_rev > 1) ret = DisplayChoices(SpeedModeMenu, 2, "ORBIT SPEED:\0", CurrentOrbitProgram.SpeedMode);
            else ret = DisplayChoices(SpeedModeMenu, 1, "ORBIT SPEED:\0", CurrentOrbitProgram.SpeedMode);
            if (ret == -1) goto labelCycleCount;
            if (ret == -2) return (-2);
            if (ret < 0) return ret;
            switch (ret) {
                case 0:

                    if (CurrentOrbitProgram.Speed_deg_sec < 0.01) CurrentOrbitProgram.Speed_deg_sec = 0.01;
                    ret = GetFloat("SPEED\0", "\xDF/Sec\0", &CurrentOrbitProgram.Speed_deg_sec, 0.01, 90, 0.01);
                    if (ret == -1) goto labelSpeedMode;
                    if (ret == -2) return (-2);
                    break;
                case 1:
                    if (CurrentOrbitProgram.CycleTime_sec < 4) CurrentOrbitProgram.CycleTime_sec = 4;
                    ret = GetTime("TIME PER ORBIT\0", &CurrentOrbitProgram.CycleTime_sec, 4, 86400, 0b1110);
                    if (ret == -1) goto labelSpeedMode;
                    if (ret == -2) return (-2);
                    CurrentOrbitProgram.Speed_deg_sec = 360 / CurrentOrbitProgram.CycleTime_sec;
                    break;
                case 2:
                    if (CurrentOrbitProgram.CycleTime_sec < (4 * CurrentOrbitProgram.CycleCount_rev)) CurrentOrbitProgram.CycleTime_sec = 4 * CurrentOrbitProgram.CycleCount_rev;
                    ret = GetTime("TIME FOR ALL ORBITS\0", &CurrentOrbitProgram.CycleTime_sec, 4 * CurrentOrbitProgram.CycleCount_rev, 86400, 0b1110);
                    if (ret == -1) goto labelSpeedMode;
                    if (ret == -2) return (-2);
                    CurrentOrbitProgram.Speed_deg_sec = (360 * CurrentOrbitProgram.CycleCount_rev) / CurrentOrbitProgram.CycleTime_sec;
                    break;
            }
            break;
        case 1://End based on runtime.
            labelProgramRuntime :

                    ret = GetTime("TOTAL RUNTIME:\0", &CurrentOrbitProgram.ProgramRunTime_sec, 1, 86400, 0b1110);
            if (ret == -1) goto labelEndMode;
            if (ret == -2) return (-2);
            if (ret < 0) return ret;
labelSpeedMode2:

            ret = DisplayChoices(SpeedModeMenu, 1, "ORBIT SPEED:\0", CurrentOrbitProgram.SpeedMode);
            if (ret == -1) goto labelProgramRuntime;
            if (ret == -2) return (-2);
            if (ret < 0) return ret;
            switch (ret) {
                case 0:
                    if (CurrentOrbitProgram.Speed_deg_sec < 0.01) CurrentOrbitProgram.Speed_deg_sec = 0.01;
                    ret = GetFloat("SPEED\0", "\xDF/Sec\0", &CurrentOrbitProgram.Speed_deg_sec, 0.01, 90, 0.01);
                    if (ret == -1) goto labelSpeedMode2;
                    if (ret == -2) return (-2);
                    break;
                case 1:
                    if (CurrentOrbitProgram.CycleTime_sec < 4) CurrentOrbitProgram.CycleTime_sec = 4;
                    ret = GetTime("TIME PER ORBIT:\0", &CurrentOrbitProgram.CycleTime_sec, 4, 86400, 0b1110);
                    if (ret == -1) goto labelSpeedMode2;
                    if (ret == -2) return (-2);
                    CurrentOrbitProgram.Speed_deg_sec = 360 / CurrentOrbitProgram.CycleTime_sec;
                    break;
            }
            CurrentOrbitProgram.CycleCount_rev = (CurrentOrbitProgram.Speed_deg_sec * CurrentOrbitProgram.ProgramRunTime_sec) / 360;
            break;
        case 2: //NO END AT ALL
            labelSpeedMode3 :
                    ret = DisplayChoices(SpeedModeMenu, 1, "ORBIT SPEED:\0", CurrentOrbitProgram.SpeedMode);
            if (ret == -1) goto labelEndMode;
            if (ret == -2) return (-2);
            if (ret < 0) return ret;
            switch (ret) {
                case 0:
                    if (CurrentOrbitProgram.Speed_deg_sec < 0.01) CurrentOrbitProgram.Speed_deg_sec = 0.01;
                    ret = GetFloat("SPEED\0", "\xDF/Sec\0", &CurrentOrbitProgram.Speed_deg_sec, 0.01, 90, 0.01);
                    if (ret == -1) goto labelSpeedMode3;
                    if (ret == -2) return (-2);
                    if (ret < 0) return ret;
                    break;
                case 1:
                    if (CurrentOrbitProgram.CycleTime_sec < 4) CurrentOrbitProgram.CycleTime_sec = 4;
                    ret = GetTime("TIME PER ORBIT:\0", &CurrentOrbitProgram.CycleTime_sec, 4, 86400, 0b1110);
                    if (ret == -1) goto labelSpeedMode3;
                    if (ret == -2) return (-2);
                    if (ret < 0) return ret;
                    CurrentOrbitProgram.Speed_deg_sec = 360 / CurrentOrbitProgram.CycleTime_sec;
                    break;
            }
            break;

    }

labelActions:
    ret = DisplayChoices(CommandMenu, 1, "ACTION:\0", 0);
    if (ret == -1) goto labelCycleCount;
    if (ret == -2) return (-2);
    if (ret < 0) return ret;
    if (ret == 0) {
        return RunOrbitProgram();
    }

    ret = GetPresetNumber();
    if (ret == -1) goto labelActions;
    if (ret == -2) return (-2);
    if (ret < 0) return ret;

    CurrentOrbitProgram.Type = 2;
    SavePreset(ret, (unsigned char *) &CurrentOrbitProgram);
    goto labelActions;
}
예제 #23
0
int main() 
{
     pedal_node_state = pedal_state_neutral;
    LCD_Start();
    CAN_invertor_init();
    ADC_SAR_Start();
    ADC_SAR_StartConvert();
    EEPROM_Start();
    //isr_Start();
    //Timer_Start();
    CAN_timer_Start();
    CAN_Init();
    CAN_Start();

    isr_start_StartEx(&isr_start_handler);
   	Start_Reset_Write(1); /* source of interrupt (reset) */
    isr_start_ClearPending();

    isr_neutral_StartEx(&isr_neutral_handler);
    Neutral_Reset_Write(1);
    isr_neutral_ClearPending();
    
    isr_calibration_StartEx(&isr_calibration_handler);
    
    CyGlobalIntEnable;          //enable global interrupts

    //Initialize terminal
    terminal_init();
    monitor_init();

    pedal_restore_calibration_data();               //set min and max values
    pedal_set_CAN(); //Setup tunnel from pedal control to CAN
    pedal_set_monitor(); //Setup tunnel from pedal control to USB Monitor

    // Initialize global variables
    EEPROM_ERROR_LED_Write(0);
    should_calibrate = false;

    // terminal_registerCommand("newCmd", &newCmdRout);
    sendNMT(NMT_command_startRemoteNode);
    CyDelay(1000);
    pedal_node_state = pedal_state_driving;
    for(;;){
        pedal_fetch_data();
    }
    for(;;)
    {
        CyDelay(50);

        terminal_run(); // Refresh terminal
        if (pedal_node_state == pedal_state_neutral)
        {
            if (should_calibrate)
            {
                pedal_node_state = pedal_state_calibrating;

            } else if (should_turn_to_drive) {
                pedal_node_state = pedal_state_driving;
                //Start sending can message for invertor
                CAN_invertor_resume();
            }
        } else if (pedal_node_state == pedal_state_driving) {
            if (should_turn_to_neutral) {
                pedal_node_state = pedal_state_neutral;
                //Stop sending messages for invertor
                CAN_invertor_pause();
            }
        }
        //Clear all flags after handling
        should_calibrate = false;
        should_turn_to_drive = false;
        should_turn_to_neutral = false;

        uint8_t out_of_range_flag;
        double brake_percent = 0, throttle_percent = 0;
        double brake_percent_diff = 0, throttle_percent_diff = 0;
        uint8_t torque_plausible_flag;
        uint8_t brake_plausible_flag;
        pedal_fetch_data(); //Update ADC readings
        CAN_invertor_update_pedal_state(pedal_node_state);
        monitor_update_vechicle_state(pedal_node_state); //Update vecicle state
        monitor_status_update_vehicle_state(pedal_node_state);
        switch (pedal_node_state)
        {
            case pedal_state_neutral:
                LCD_ClearDisplay();
                LCD_Position(0,0);
                LCD_PrintString("NEUTRAL");

                break;
            case pedal_state_driving:
                LCD_ClearDisplay();
                LCD_Position(0,0);
                LCD_PrintString("DRIVING");
                //out_of_range_flag = pedal_get_out_of_range_flag();
                if (out_of_range_flag != 0)
                {
                    pedal_node_state = pedal_state_out_of_range;
                    break;
                }

                torque_plausible_flag = pedal_is_pedal_reading_matched(&brake_percent_diff, &throttle_percent_diff);
                if (torque_plausible_flag != 0)
                {
                    pedal_node_state = pedal_state_discrepency;
                    break;
                }

                brake_plausible_flag = pedal_is_brake_plausible(&brake_percent, &throttle_percent);
                if (brake_plausible_flag != 0)
                {
                    pedal_node_state = pedal_state_implausible;
                    break;
                }

                break;

            case pedal_state_calibrating:
                //clock_StopBlock();      //stop clock to disable interrupt
                pedal_calibrate();
                LCD_ClearDisplay();
                //isr_ClearPending();
                //clock_Start();
                // isr_calibration_Enable();
                pedal_node_state = pedal_state_neutral;
                break;

            case pedal_state_out_of_range:
                LCD_ClearDisplay();
                LCD_Position(0,0);
                LCD_PrintString("Pedal out of");
                LCD_Position(1,0);
                LCD_PrintString("range");

                out_of_range_flag = pedal_get_out_of_range_flag();
                if (out_of_range_flag == 0)
                {
                    pedal_node_state = pedal_state_driving;
                }
                break;

            case pedal_state_discrepency:
                LCD_ClearDisplay();
                LCD_Position(0,0);
                LCD_PrintString("Pedal discrepency");
                torque_plausible_flag = pedal_is_pedal_reading_matched(&brake_percent_diff, &throttle_percent_diff);
                if (torque_plausible_flag == 0)
                {
                    pedal_node_state = pedal_state_driving;
                }
                break;

            case pedal_state_implausible:
                LCD_ClearDisplay();
                LCD_Position(0,0);
                LCD_PrintString("Pedal implausible");
                brake_plausible_flag = pedal_is_brake_plausible(&brake_percent, &throttle_percent);
                if (throttle_percent < PEDAL_BRAKE_IMPLAUSIBLE_EXIT_THROTTLE_PERCENT)
                {
                    pedal_node_state = pedal_state_driving;
                }
                break;
        }

        // CyDelay(100);
    }

    return 0;
}
예제 #24
0
파일: main.c 프로젝트: dmtucker/connect4
void LCD_UpdateStatus ( const char string[] ) {
    LCD_ClearDisplay();
    LCD_Position(0,0);
    LCD_PrintString(string);
}
signed char RunOrbitProgram() {
    signed char retCode = 0;
    UI_Location = UI_LOC_ORBITRUN;
    signed char ret;
    double EndPos;
    double OriginPos;
    double TimeRemaining;
    double DistanceRemaining;
    double MPos;
   
    LCD_ClearDisplay();
    LCD_PrintString("MOVING TO START\0");
    OriginPos=(double)CurrentOrbitProgram.Origin_deg;
    MoveToAngle(OriginPos, 90);
    while (bMove_InProgress) {
        Idle();
        ret = GetInput_nonblocking();
        if (ret == USER_INPUT_CANCEL) {
            retCode = -2;
            goto labelFinished;
        }
        if (ret < 0) {
            retCode = ret;
            goto labelFinished;
        }
    }


    double Distance = CurrentOrbitProgram.CycleCount_rev * 360;
    if (CurrentOrbitProgram.EndMode == 2) Distance = 7776000;
    if (!CurrentOrbitProgram.IsClockWise) Distance = -Distance;

    bLock_Motor_Position = 1;
    OriginPos = Motor_Position;
    bLock_Motor_Position = 0;
    OriginPos *= Config.Degrees_Per_Count;

    EndPos = OriginPos + Distance;


    LCD_ClearDisplay();
    LCD_PrintString("RUNNING: T-HH:MM:SS\0");

    if (CurrentOrbitProgram.EndMode == 2) {
        LCD_ClearDisplay();
        LCD_PrintString("ORBIT RUNNING\0");
        LCD_SetPosition(1, 0);
        LCD_PrintString("INFINITE RUNTIME\0");
    }

    double SpdConvert = 1 / CurrentOrbitProgram.Speed_deg_sec;
    ret = GetInput_nonblocking();

    Move(Distance, CurrentOrbitProgram.Speed_deg_sec);
    while (bMove_InProgress) {

        bLock_Motor_Position = 1;
        MPos = Motor_Position;
        bLock_Motor_Position = 0;
        MPos *= Config.Degrees_Per_Count;

        DistanceRemaining = EndPos - MPos;
        if (DistanceRemaining < 0) DistanceRemaining = -DistanceRemaining;
        TimeRemaining = DistanceRemaining*SpdConvert;
        DistanceRemaining *= 0.002778;

        if (!(CurrentOrbitProgram.EndMode == 2)) {
            LCD_SetPosition(0, 11);
            PrintTime(TimeRemaining, 0b1110, 0);
            LCD_SetPosition(1, 0);
            LCD_PrintFloat(DistanceRemaining, 5, 2, 0);
            LCD_PrintString(" REV REMAIN\0");
        }

        ret = GetInput_nonblocking();
        if (ret == USER_INPUT_CANCEL) {
            retCode = -2;
            goto labelFinished;
        }
        if (ret < 0) {
            retCode = ret;
            goto labelFinished;
        }

        Idle();
    }

labelFinished:
    bMove_InProgress = 0;
    bFollowMode = 0;
    bSpeedMode = 1;
    Speed_SetToThis = 0;
    Speed_SetPending = 1;
    LCD_ClearDisplay();
    LCD_PrintString("STOPPING...\0");
    while (Speed_SetPending);
    while (Speed_IsAccelerating){
        Idle();
    }
    bMove_InProgress = 0;
    bFollowMode = 0;
    bSpeedMode=0;
    return retCode;
}
예제 #26
0
파일: main.c 프로젝트: ADTL/ENC28J60-PSoC3
void main(void){
    unsigned char op = 0xFF; 
    //0xFF is the SPI command for Soft Reset.
       
    //Initialize SPIM Module on the PSoC.
    spiInit(); 
       
    //Start the LCD component.
    LCD_Start();
    LCD_PrintString("SPI Init'd.");
    CyDelay(700);
    LCD_ClearDisplay();
       
    //Send that Soft Reset Command.
    SPI_SEL(0);
        spiTxBuffer(&op,1);
    SPI_SEL(1);
       
    //Wait for the ENC chip to come back.
    CyDelay(1);
    
    LCD_PrintString("ENC Reset.");
    CyDelay(700);
    LCD_ClearDisplay();
       
    /**************************************
    We will now try to turn on the LEDs
    on the Magjack.
    Steps:
    1.Write to the PHLCON register,and turn on the LEDs.
        1.1 Since its a PHY register,Write the address of the PHLCON register to
            into the MIREGADR register.(Which is why we select Bank 2 first
            by writing to ECON1.)
        1.2 Write the lower 8 bits of data to write into the
            MIWRL register.
        1.3 Write the upper 8 bits of data to write into the
            MIWRH register. Writing to this register auto-
            matically begins the MII transaction, so it must
            be written to after MIWRL.
    2.Check the LEDs to see if they light up :-P
    3.Say so on the LCD.
    ************************************************/
       
    //Write Control Register Command for ECON1
    // 010 11111 = 0x5F
    op = 0x5F; 
    SPI_SEL(0);
        spiTxBuffer(&op,1);
            
       //Write 0x02 as we want to select bank 2.            
        op = 0x02; 
        spiTxBuffer(&op,1);
    SPI_SEL(1);
       
    //Write Control Register Command for MIREGADR Register.
    // 010 10100 = 0x54
    op = 0x54;
    SPI_SEL(0);
        spiTxBuffer(&op,1);

        //Select PHLCON Register.
        op = 0x14; 
        spiTxBuffer(&op,1);
    SPI_SEL(1);
       
    //Write Control Register Command for MIWRL Register.
    // 010 10110 = 0x56
    op = 0x56; 
    SPI_SEL(0);
    spiTxBuffer(&op,1);
       
    //Turn on LEDB.
    op = 0x80; 
    spiTxBuffer(&op,1);
    SPI_SEL(1);
    
    LCD_PrintString("LEDB ON.");
    CyDelay(700);
    LCD_ClearDisplay();
    
    //Write Control Register Command for MIWRL Register.
    // 010 10111 = 0x57
    op = 0x57; //Write to MIWRH Reg
    SPI_SEL(0);
    spiTxBuffer(&op,1);
       
    //Turn on LEDA.
    op = 0x38; 
    spiTxBuffer(&op,1);
    SPI_SEL(1);

    LCD_PrintString("LEDA ON.");
    CyDelay(700);
    LCD_ClearDisplay();

    /**************************************
    We will now try to read the Silicon Revision.
    Steps:
    1.Read from EREVID Register
        1.1 Write to ECON 1 to Select Bank 3
        1.2 Read the EREVID Register.
        (Mine gave 0x04)
    **************************************/
        op = 0x5F; //Write to ECON1 Reg
        SPI_SEL(0);
            spiTxBuffer(&op,1);
        op = 0x03; //Write 3 as we want to select bank 3.
            spiTxBuffer(&op,1);
        SPI_SEL(1);
        
        op = 0x12; //Read EREVID Register
        SPI_SEL(0);
            spiTxBuffer(&op,1);
            spiRxBuffer(&op,1);
        SPI_SEL(1);
        
        LCD_PrintString("Silicon Revision");
        LCD_Position(1,0);
        LCD_PrintInt8(op);
    for(;;);
}
예제 #27
0
void LCD_Init
  (
  void
  )
{
  LCDWaitLong( Wait5mSec ); 
  /* This function setup Bits 6-7 as outputs (EN & RS) (PTEDD) */
  Setup_EN_RS;
  /* Setup the XX Port (4-7 data bits, 3 R/W ) (PTGDD)
     data is output (default), r/w is output */
  SetupDataBit;
  /* Initialize data port */
  InitDataPort;
  /* Setup the R/W for writing (PTGD) */
  Setup_R_W_Write;
  /* Initialize EN and RS to 0 */
  Init_EN_RS;

  /* Send the reset sequence */
  LCD_Write4bits( 0x30 );
  LCDToggleEN;

  LCDWaitLong( Wait5mSec );

  LCD_Write4bits( 0x30 );
  LCDToggleEN;

  LCDWaitShort( Wait30uSec );

  LCD_Write4bits( 0x30 );
  LCDToggleEN;

  LCDWaitShort( Wait30uSec );

  LCD_Write4bits( 0x20 );
  LCDToggleEN;

  LCDWaitShort( Wait15uSec );

  /* Function, 4 bit data length */
  LCD_Write4bits( 0x20 );
  LCDToggleEN;

  LCDWaitShort( Wait15uSec );

  /* 2 lines, 5x7 dot format */
  LCD_Write4bits( 0x80 );
  LCDToggleEN;

  LCDWaitShort( Wait60uSec );

  /* Entry Mode Inc, No Shift */
  LCD_Write4bits( 0x00 );
  LCDToggleEN;
  LCDWaitShort( Wait15uSec );
  LCD_Write4bits( 0x60 );
  LCDToggleEN;
  LCDWaitShort( Wait75uSec );

  /* Display ON/OFF Control - Display On, Cursor Off, Blink Off */
  LCD_Write4bits( 0x00 );
  LCDToggleEN;
  LCDWaitShort( Wait15uSec );
  LCD_Write4bits( 0xC0 );
  LCDToggleEN;
  LCDWaitShort( Wait75uSec );

  /* Display Clear */
  LCD_ClearDisplay();

  LCDLine(LineOne);
}
예제 #28
0
void main_flashing_tta_psoc(){
    // this is the function compatible with the cap sensor input. with 8051 interrupts
    LCD_Start();					    // initialize lcd
    LCD_ClearDisplay();
    UART_Start();                       // initialize UART
    UART_PutChar(0x81); // init connection; set to 16x12 image 
    
    struct disp_grid_81 disp; 
    disp_grid_init(&disp,0x3F); // init our display grid matrix to white  
    disp_grid_transmit(&disp);
    
    struct tic_tac_ai tta;
    tta_init(&tta,4,3,false,true); //first bool for player 1, second bool for player 2. true means is AI
    disp_grid_init_ttc(&disp, tta.game.grid);
    disp_grid_draw_tic(&disp,24,1,0x27); // draw tic
    disp_grid_draw_xia(&disp,26,16,0x30); // draw xia
    disp_grid_transmit(&disp);
    
    int x,y,z, count; uint8 Values; uint8 Values_prev;
    uint8 current;
    Values_prev = 0;
    x = 0; y = 0; z = 0; //test value
    count = 0;
    
    uint8 red_flash; //the temporary location flasher
    uint8 other_val;
    other_val = disp_grid_ttc_getval(&disp,z*16 + y*4 + x); // get the nonred val
    red_flash = 0x30; //map to color red
    
    
    while (tta.game.game_not_won == 0){
        LCD_ClearDisplay();
        LCD_Position(1,0); //move to bot row
        LCD_PrintString("LAST:"); //first, we print last value
        LCD_PutChar(Values_prev); //print ascii value
        LCD_PrintString(" HEX: ");
        LCD_PrintNumber(Values_prev); //print value I am getting
        
        current = Pin3_Read(); //next, read a new value
        LCD_Position(0,0); //move back to top row
        LCD_PrintString("CURR:");
        LCD_PutChar(current); //print ascii value
        LCD_PrintString(" HEX: ");
        LCD_PrintNumber(current); //print value I am getting
        waiter(3);
        
        if (current != 255) {
            Values = current; // update values as last non
            Values_prev = Values;
        }
        
        if (Values != 0){
            if (Values == 54){
                if (tta.game.grid[z*16 + y*4 + x] == 0){ // make sure we can place there
                    tta_step(&disp,&tta,x,y,z); //increment a turn
                    disp_grid_transmit(&disp);
                    tta_step(&disp,&tta,x,y,z); //AI Increment also 
                    other_val = disp_grid_ttc_getval(&disp,z*16 + y*4 + x); // get the nonred val
                }
            }
            else{
                disp_grid_ttc_place_value(&disp,z*16 + y*4 + x,other_val);
                if (Values == 53 && y != 0){ // up
                y--;
                }
                else if (Values == 52 && y != 3){ // down
                    y++;
                }
                else if (Values == 51 && x != 0){ // left
                    x--;
                }
                else if (Values == 50 && x != 3){ // right
                    x++;
                }
                else if (Values == 49){ // level shift
                    z++;
                    z = z % 4;
                }
                other_val = disp_grid_ttc_getval(&disp,z*16 + y*4 + x); // get the nonred val
            }
            Values = 0; // reset values;
            count = 0;
        }
        
        if (count == 0){ //decide print
            count = 1;
            disp_grid_ttc_place_value(&disp,z*16 + y*4 + x,red_flash);
            disp_grid_transmit(&disp);
        }
//        else{
//            count = 0;
//            disp_grid_ttc_place_value(&disp,z*16 + y*4 + x,other_val);
//            disp_grid_transmit(&disp);
//        }
        
    }
    LCD_ClearDisplay();
    LCD_PrintString("GAME OVER!");  
    disp_grid_draw_win(&disp,27,9,tta.game.player); // draw tic
    disp_grid_transmit(&disp);
    
}
예제 #29
0
void main_flashing_tta(){
    // this is the function compatible with the cap sensor input. does not use 8051; connect directly
    LCD_Start();					    // initialize lcd
    LCD_ClearDisplay();
    UART_Start();                       // initialize UART
    UART_PutChar(0x81); // init connection; set to 16x12 image 
    
    struct disp_grid_81 disp; 
    disp_grid_init(&disp,0x3F); // init our display grid matrix to white  
    disp_grid_transmit(&disp);
    
    struct tic_tac_ai tta;
    tta_init(&tta,4,3,false,true); //first bool for player 1, second bool for player 2. true means is AI
    disp_grid_init_ttc(&disp, tta.game.grid);
    disp_grid_draw_xia(&disp,26,16,0x30); // draw xia
    disp_grid_transmit(&disp);
    
    int x,y,z, count; uint8 Values; uint8 Values_prev;
    Values_prev = Pin0_Read();
    x = 0; y = 0; z = 0; //test value
    count = 0;
    
    uint8 red_flash; //the temporary location flasher
    uint8 other_val;
    other_val = disp_grid_ttc_getval(&disp,z*16 + y*4 + x); // get the nonred val
    red_flash = 0x30; //map to color red
    
    tta_step(&disp,&tta,x,y,z); //AI Increment also 
    
    while (tta.game.game_not_won == 0){
//        Values = read_from_8255(Values); //read and print
        
        LCD_ClearDisplay();
        
        LCD_Position(0,0); //move back to top row
        Values = Pin0_Read(); //next, read a new value
        LCD_PrintString("P0: ");
        LCD_PrintNumber(Values); //print value I am getting
        
        if (Values != Values_prev){
            Values_prev = Values; //we don't want nonconsect
            if (Values != 0){
                if (Values == 32){
                    tta_step(&disp,&tta,x,y,z); //increment a turn
                    tta_step(&disp,&tta,x,y,z); //AI Increment also 
                    other_val = disp_grid_ttc_getval(&disp,z*16 + y*4 + x); // get the nonred val
                }
                else{
                    disp_grid_ttc_place_value(&disp,z*16 + y*4 + x,other_val);
                    if (Values == 16 && y != 0){ // up
                    y--;
                    }
                    else if (Values == 8 && y != 3){ // down
                        y++;
                    }
                    else if (Values == 4 && x != 0){ // left
                        x--;
                    }
                    else if (Values == 2 && x != 3){ // right
                        x++;
                    }
                    else if (Values == 1){ // level shift
                        z++;
                        z = z % 4;
                    }
                    other_val = disp_grid_ttc_getval(&disp,z*16 + y*4 + x); // get the nonred val
                }
            }
        }
        
        if (count == 0){ //decide print
            count = 1;
            disp_grid_ttc_place_value(&disp,z*16 + y*4 + x,red_flash);
        }
        else{
            count = 0;
            disp_grid_ttc_place_value(&disp,z*16 + y*4 + x,other_val);
        }
        disp_grid_transmit(&disp);
    }
    LCD_ClearDisplay();
    LCD_PrintString("GAME OVER!");   
    
}
예제 #30
0
int main()
{    
	CyGlobalIntEnable; /* Uncomment this line to enable global interrupts. */
	XBee_UART_Start();
	I2C_Start();
	LCD_Start();
	
	//	INITIALIZE VALUES
	Command_Received = 0;
	MAG_DataRdy_Flag = 0;
	ReadyForCommand_Flag = 1;
	IncomingData_Flag = 0;
	StatusError_Flag = 0;
	//WaitForDataRead_Flag = 0;
	Command_Buffer = 0;
	
	// INITIALIZE ISRs
	InitXBee_Isr();
	//InitINT1_Isr(); // IF INT1 TRIGGERS WHEN NOT IN ACTIVE MODE, MOVE INITIATION CODE FOR SETTING ACTIVE.
	//INT1_isr_Start();
	
	XBee_UART_ClearRxBuffer();
	XBee_UART_ClearTxBuffer();
	CyDelay(2000);
	LCD_ClearDisplay();
	LCD_Position(0,0);
	LCD_PrintString("MAG DRIVER:");
	LCD_Position(1,0);
	LCD_PrintString("[ERR]");
	LCD_Position(1,7);
	LCD_PrintString("[I2C]");

	uint8 status = 0;
	status = SetCtrlReg1Default();
	status |= SetCtrlReg2Default();
	if(status !=0)
	{
		LCD_ClearDisplay();
		LCD_Position(0,0);
		LCD_PrintInt8(status);
		status = 0;
	}
	uint8 pin_status = 0;
	    
    for(;;)
    {
		pin_status = INT1_Pin_Read();
		pin_status |= MAG_DataRdy_Flag;
		if(pin_status)
		{
			MAG_DataRdy_Flag = pin_status;
			if(ReadyForCommand_Flag!=0)
			{
				Command_Received = CMD_I_RM_MAGDATA;
				ReadyForCommand_Flag = 0;
			}	
		}

		if(Command_Received != 0)
		{
			LCD_ClearDisplay();
			//XBee_UART_ClearTxBuffer();
			//XBee_UART_ClearRxBuffer();			
			switch (Command_Received){
				case CMD_I_RM_MAGDATA: // CMD_I_RM_MAGDATA = 34
				{
					status = ReadMagData(Global_ReadPtr);
					if(status == 0)
					{
						CyDelay(500);
						XBee_UART_PutChar(Command_Received);
						//LED_out_Write(0);
						CyDelay(500);
						XBee_UART_PutArray(Global_ReadPtr,ARRAY_SIZE_MAG_DATA);
						CyDelay(1000);					
						//WaitForDataRead_Flag = 0;
						MAG_DataRdy_Flag = 0;
						if(Command_Buffer!=0)
						{
							Command_Received = Command_Buffer;
							Command_Buffer = 0;
							//ReadyForCommand_Flag = 0; //Dont toggle flag
						}
						else
						{
							Command_Received = 0;
							ReadyForCommand_Flag = 1;
							LED_out_Write(0);
						}
					}					
					else 
					{
						// will result is a halt of CMD processing and leaves LED lit (visual error signal)
						LCD_Position(1,0);
						LCD_PrintString("E:STA");
						CyDelay(1000);		
						StatusError_Flag = 1;						
					}
				}
				break;
				
				case CMD_I_WM_OFFSET_ALL:// CMD_I_WM_OFFSET_ALL = 1
				{
					if(IncomingData_Flag == 0) // IF THE OFFSET DATA TO WRITE HAS BEEN RECEIVED...
					{
						status = WriteOffsetCorrection(DataInPtr_Global);
						if(status == 0)
						{
							CyDelay(500);
							XBee_UART_PutChar(Command_Received); //Sends confirmation TWICE. Once after receiving CMD, and again after finishing WRITE
							LED_out_Write(0);
							Command_Received = 0;
							IncomingData_Flag = 0;
							ReadyForCommand_Flag = 1;
						}					
						else 
						{
							// will result is a halt of CMD processing and leaves LED lit (visual error signal)
							LCD_Position(1,0);
							LCD_PrintString("E:STA");
							CyDelay(1000);		
							StatusError_Flag = 1;
						}
					}
				}
				break;
				
				case CMD_I_RS_CTRL1://CMD_I_RS_CTRL1=35
				{
					status = ReadCtrlReg1(Global_ReadPtr);
					if(status == 0)
					{
						CyDelay(500);
						XBee_UART_PutChar(Command_Received);
						LED_out_Write(0);
						CyDelay(500);
						XBee_UART_PutChar(Global_ReadBuffer[0]); // Send Read Value
						Command_Received = 0;
						IncomingData_Flag = 0;
						ReadyForCommand_Flag = 1;
					}					
					else 
					{
						// will result is a halt of CMD processing and leaves LED lit (visual error signal)
						LCD_Position(1,0);
						LCD_PrintString("E:STA");
						CyDelay(1000);		
						StatusError_Flag = 1;
					}				
				}
				break;
				
				case CMD_I_RS_CTRL2://CMD_I_RS_CTRL2 = 36
				{
					status = ReadCtrlReg2(Global_ReadPtr);
					if(status == 0)
					{
						CyDelay(500);
						XBee_UART_PutChar(Command_Received);
						LED_out_Write(0);
						CyDelay(500);
						XBee_UART_PutChar(Global_ReadBuffer[0]); // Send Read Value
						Command_Received = 0;
						IncomingData_Flag = 0;
						ReadyForCommand_Flag = 1;
					}					
					else 
					{
						// will result is a halt of CMD processing and leaves LED lit (visual error signal)
						LCD_Position(1,0);
						LCD_PrintString("E:STA");
						CyDelay(1000);		
						StatusError_Flag = 1;
					}	
				}
				break;
				
				case CMD_I_RS_DRSTATUS://CMD_I_RS_DRSTATUS = 37
				{
					status = ReadDrStatus(Global_ReadPtr);
					if(status == 0)
					{
						CyDelay(500);
						XBee_UART_PutChar(Command_Received);
						LED_out_Write(0);
						CyDelay(500);
						XBee_UART_PutChar(Global_ReadBuffer[0]); // Send Read Value
						Command_Received = 0;
						IncomingData_Flag = 0;
						ReadyForCommand_Flag = 1;
					}					
					else 
					{
						// will result is a halt of CMD processing and leaves LED lit (visual error signal)
						LCD_Position(1,0);
						LCD_PrintString("E:STA");
						CyDelay(1000);		
						StatusError_Flag = 1;
					}	
				}
				break;
				
				case CMD_I_RS_SYSMOD:// CMD_I_RS_SYSMOD = 38
				{
					status = ReadSysMod(Global_ReadPtr);
					if(status == 0)
					{
						CyDelay(500);
						XBee_UART_PutChar(Command_Received);
						LED_out_Write(0);
						CyDelay(500);
						XBee_UART_PutChar(Global_ReadBuffer[0]);
						Command_Received = 0;
						IncomingData_Flag = 0;
						ReadyForCommand_Flag = 1;
					}					
					else 
					{
						// will result is a halt of CMD processing and leaves LED lit (visual error signal)
						LCD_Position(1,0);
						LCD_PrintString("E:STA");
						CyDelay(1000);		
						StatusError_Flag = 1;
					}
				}
				break;
				
				case CMD_I_RS_DIETEMP: // CMD_I_RS_DIETEMP = 39
				{
					status = ReadDieTemp(Global_ReadPtr);
					if(status == 0)
					{
						CyDelay(500);
						XBee_UART_PutChar(Command_Received);
						LED_out_Write(0);
						CyDelay(500);
						XBee_UART_PutChar(Global_ReadBuffer[0]);
						Command_Received = 0;
						IncomingData_Flag = 0;
						ReadyForCommand_Flag = 1;
					}					
					else 
					{
						// will result is a halt of CMD processing and leaves LED lit (visual error signal)
						LCD_Position(1,0);
						LCD_PrintString("E:STA");
						CyDelay(1000);		
						StatusError_Flag = 1;
					}
				}
				break;
				
				case CMD_I_RS_WHOAMI: // CMD_I_RS_WHOAMI = 40
				{
					status = ReadWhoAmI(Global_ReadPtr);
					if(status == 0)
					{
						CyDelay(500);
						XBee_UART_PutChar(Command_Received);
						LED_out_Write(0);
						CyDelay(500);
						XBee_UART_PutChar(Global_ReadBuffer[0]);
						Command_Received = 0;
						IncomingData_Flag = 0;
						ReadyForCommand_Flag = 1;
					}					
					else 
					{
						// will result is a halt of CMD processing and leaves LED lit (visual error signal)
						LCD_Position(1,0);
						LCD_PrintString("E:STA");
						CyDelay(1000);		
						StatusError_Flag = 1;
					}
				}
				break;
				
				case CMD_I_WS_CTRL1_DEFAULT: // CMD_I_WS_CTRL1_DEFAULT = 41
				{
					status = SetCtrlReg1Default();
					if(status == 0) //if the write was a success
					{
						CyDelay(500);
						XBee_UART_PutChar(Command_Received);//Returns command received from MATLAB as confirmation
						LED_out_Write(0); //Turns off LED
						Command_Received = 0; //Clears the Command
						IncomingData_Flag = 0;
						ReadyForCommand_Flag = 1; // Sets state as READY for next command
					}					
					else 
					{
						// will result is a halt of CMD processing and leaves LED lit (visual error signal)
						LCD_Position(1,0);
						LCD_PrintString("E:STA");
						CyDelay(1000);		
						StatusError_Flag = 1;
					}
				}
				break;
				
				case CMD_I_WS_CTRL1_MODSTANDBY: // CMD_I_WS_CTRL1_MODSTANDBY = 42
				{
					status = SetStandbyMode();
					if(status == 0) //if the write was a success
					{
						CyDelay(500);
						XBee_UART_PutChar(Command_Received);//Returns command received from MATLAB as confirmation
						LED_out_Write(0); //Turns off LED
						Command_Received = 0; //Clears the Command
						IncomingData_Flag = 0;
						ReadyForCommand_Flag = 1; // Sets state as READY for next command
					}					
					else 
					{
						// will result is a halt of CMD processing and leaves LED lit (visual error signal)
						LCD_Position(1,0);
						LCD_PrintString("E:STA");
						CyDelay(1000);		
						StatusError_Flag = 1;
					}
				}
				break;
				
				case CMD_I_WS_CTRL1_MODSINGLE: // CMD_I_WS_CTRL1_MODSINGLE = 43
				{
					status = SetSingleMeasurmentMode();
					if(status == 0) //if the write was a success
					{
						CyDelay(500);
						XBee_UART_PutChar(Command_Received);//Returns command received from MATLAB as confirmation
						LED_out_Write(0); //Turns off LED
						Command_Received = 0; //Clears the Command
						IncomingData_Flag = 0;
						ReadyForCommand_Flag = 1; // Sets state as READY for next command
					}					
					else 
					{
						// will result is a halt of CMD processing and leaves LED lit (visual error signal)
						LCD_Position(1,0);
						LCD_PrintString("E:STA");
						CyDelay(1000);		
						StatusError_Flag = 1;
					}
				}
				break;
				
				//IF INT1 CONTINUES TO TRIGGER WHEN NOT IT ACTIVE MODE, DEACTIVATE/ACTIVATE ISR WHEN CHANGING MODES				
				case CMD_I_WS_CTRL1_MODACTIVE: // CMD_I_WS_CTRL1_MODACTIVE = 44
				{
					status = SetContinuousMode();
					if(status == 0) //if the write was a success
					{
						CyDelay(500);
						XBee_UART_PutChar(Command_Received);//Returns command received from MATLAB as confirmation
						LED_out_Write(0); //Turns off LED
						Command_Received = 0; //Clears the Command
						IncomingData_Flag = 0;
						ReadyForCommand_Flag = 1; // Sets state as READY for next command
					}					
					else 
					{
						// will result is a halt of CMD processing and leaves LED lit (visual error signal)
						LCD_Position(1,0);
						LCD_PrintString("E:STA");
						CyDelay(1000);		
						StatusError_Flag = 1;
					}
				}
				break;
				
				case CMD_I_WS_CTRL1_MODTRIGGER: // CMD_I_WS_CTRL1_MODTRIGGER = 45
				{
					status = SetTriggerMeasurmentMode();
					if(status == 0) //if the write was a success
					{
						CyDelay(500);
						XBee_UART_PutChar(Command_Received);//Returns command received from MATLAB as confirmation
						LED_out_Write(0); //Turns off LED
						Command_Received = 0; //Clears the Command
						IncomingData_Flag = 0;
						ReadyForCommand_Flag = 1; // Sets state as READY for next command
					}					
					else 
					{
						// will result is a halt of CMD processing and leaves LED lit (visual error signal)
						LCD_Position(1,0);
						LCD_PrintString("E:STA");
						CyDelay(1000);		
						StatusError_Flag = 1;
					}
				}
				break;
				
				case CMD_I_WS_CTRL1_ENFAST: // CMD_I_WS_CTRL1_ENFAST = 46
				{
					status = SetFastReadOn();
					if(status == 0) //if the write was a success
					{
						CyDelay(500);
						XBee_UART_PutChar(Command_Received);//Returns command received from MATLAB as confirmation
						LED_out_Write(0); //Turns off LED
						Command_Received = 0; //Clears the Command
						IncomingData_Flag = 0;
						ReadyForCommand_Flag = 1; // Sets state as READY for next command
					}					
					else 
					{
						// will result is a halt of CMD processing and leaves LED lit (visual error signal)
						LCD_Position(1,0);
						LCD_PrintString("E:STA");
						CyDelay(1000);		
						StatusError_Flag = 1;
					}
				}
				break;
				
				case CMD_I_WS_CTRL1_NENFAST: //CMD_I_WS_CTRL1_NENFAST = 47
				{
					status = SetFastReadOff();
					if(status == 0) //if the write was a success
					{
						CyDelay(500);
						XBee_UART_PutChar(Command_Received);//Returns command received from MATLAB as confirmation
						LED_out_Write(0); //Turns off LED
						Command_Received = 0; //Clears the Command
						IncomingData_Flag = 0;
						ReadyForCommand_Flag = 1; // Sets state as READY for next command
					}					
					else 
					{
						// will result is a halt of CMD processing and leaves LED lit (visual error signal)
						LCD_Position(1,0);
						LCD_PrintString("E:STA");
						CyDelay(1000);		
						StatusError_Flag = 1;
					}
				}
				break;
				
				case CMD_I_WS_CTRL2_DEFAULT: //CMD_I_WS_CTRL2_DEFAULT = 48
				{
					status = SetCtrlReg2Default();
					if(status == 0) //if the write was a success
					{
						CyDelay(500);
						XBee_UART_PutChar(Command_Received);//Returns command received from MATLAB as confirmation
						LED_out_Write(0); //Turns off LED
						Command_Received = 0; //Clears the Command
						IncomingData_Flag = 0;
						ReadyForCommand_Flag = 1; // Sets state as READY for next command
					}					
					else 
					{
						// will result is a halt of CMD processing and leaves LED lit (visual error signal)
						LCD_Position(1,0);
						LCD_PrintString("E:STA");
						CyDelay(1000);		
						StatusError_Flag = 1;
					}
				}
				break;
				
				case CMD_I_WS_CTRL2_ENAUTORESET: // CMD_I_WS_CTRL2_ENAUTORESET = 49
				{
					status = SetAutoResetOn();
					if(status == 0) //if the write was a success
					{
						CyDelay(500);
						XBee_UART_PutChar(Command_Received);//Returns command received from MATLAB as confirmation
						LED_out_Write(0); //Turns off LED
						Command_Received = 0; //Clears the Command
						IncomingData_Flag = 0;
						ReadyForCommand_Flag = 1; // Sets state as READY for next command
					}					
					else 
					{
						// will result is a halt of CMD processing and leaves LED lit (visual error signal)
						LCD_Position(1,0);
						LCD_PrintString("E:STA");
						CyDelay(1000);		
						StatusError_Flag = 1;
					}
				}
				break;
				
				case CMD_I_WS_CTRL2_NENAUTORESET: // CMD_I_WS_CTRL2_NENAUTORESET = 50
				{
					status = SetAutoResetOff();
					if(status == 0) //if the write was a success
					{
						CyDelay(500);
						XBee_UART_PutChar(Command_Received);//Returns command received from MATLAB as confirmation
						LED_out_Write(0); //Turns off LED
						Command_Received = 0; //Clears the Command
						IncomingData_Flag = 0;
						ReadyForCommand_Flag = 1; // Sets state as READY for next command
					}					
					else 
					{
						// will result is a halt of CMD processing and leaves LED lit (visual error signal)
						LCD_Position(1,0);
						LCD_PrintString("E:STA");
						CyDelay(1000);		
						StatusError_Flag = 1;
					}
				}
				break;
				
				case CMD_I_WS_CTRL2_ENUSEROFFSET: // CMD_I_WS_CTRL2_ENUSEROFFSET = 51
				{
					status = SetUserCorrectedData();
					if(status == 0) //if the write was a success
					{
						CyDelay(500);
						XBee_UART_PutChar(Command_Received);//Returns command received from MATLAB as confirmation
						LED_out_Write(0); //Turns off LED
						Command_Received = 0; //Clears the Command
						IncomingData_Flag = 0;
						ReadyForCommand_Flag = 1; // Sets state as READY for next command
					}					
					else 
					{
						// will result is a halt of CMD processing and leaves LED lit (visual error signal)
						LCD_Position(1,0);
						LCD_PrintString("E:STA");
						CyDelay(1000);		
						StatusError_Flag = 1;						
					}
				}
				break;
				
				case CMD_I_WS_CTRL2_NENUSEROFFSET: // CMD_I_WS_CTRL2_NENUSEROFFSET = 52
				{
					status = SetRawData();
					if(status == 0) //if the write was a success
					{
						CyDelay(500);
						XBee_UART_PutChar(Command_Received);//Returns command received from MATLAB as confirmation
						LED_out_Write(0); //Turns off LED
						Command_Received = 0; //Clears the Command
						IncomingData_Flag = 0;
						ReadyForCommand_Flag = 1; // Sets state as READY for next command
					}					
					else 
					{
						// will result is a halt of CMD processing and leaves LED lit (visual error signal)
						StatusError_Flag = 1;
						LCD_Position(1,0);
						LCD_PrintString("E:STA");
						CyDelay(1000);		
						StatusError_Flag = 1;						
					}
				}
				break;				
				
				case CMD_I_WS_CTRL2_RESETMAG: // CMD_I_WS_CTRL2_RESETMAG = 53
				{
					status = ResetMag();
					if(status == 0) //if the write was a success
					{
						CyDelay(1000); //LET MAGNETOMETER RESET PROCEDURE FINISH
						XBee_UART_PutChar(Command_Received);//Returns command received from MATLAB as confirmation
						LED_out_Write(0); //Turns off LED
						Command_Received = 0; //Clears the Command
						IncomingData_Flag = 0;
						ReadyForCommand_Flag = 1; // Sets state as READY for next command
					}					
					else 
					{
						// will result is a halt of CMD processing and leaves LED lit (visual error signal)
						//LCD_ClearDisplay();
						LCD_Position(1,0);
						LCD_PrintString("E:STA");
						CyDelay(1000);		
						StatusError_Flag = 1;
					}
				}
				break;
				
				case CMD_I_RESET_ALL://54
				{
					// RESET I2C
					uint8 i2c_status = I2C_MasterClearStatus();
					//LCD_ClearDisplay();
					LCD_Position(1,7);
					LCD_PrintInt8(i2c_status);
					
					I2C_MasterClearReadBuf();
					I2C_MasterClearWriteBuf();
					
					// Reset MAG CTRL REGISTERS
					status = SetCtrlReg1Default();
					status |= SetCtrlReg2Default();
					if(status == 0) //if the write was a success
					{
						CyDelay(500);
						XBee_UART_PutChar(Command_Received);//Returns command received from MATLAB as confirmation
						LED_out_Write(0); //Turns off LED
						Command_Received = 0; //Clears the Command
						IncomingData_Flag = 0;
						ReadyForCommand_Flag = 1; // Sets state as READY for next command
					}
					else 
					{
						// will result is a halt of CMD processing and leaves LED lit (visual error signal)
						//LCD_ClearDisplay();
						LCD_Position(1,0);
						LCD_PrintString("E:STA");
						CyDelay(1000);		
						StatusError_Flag = 1;
					}					
					
				}
				break;
				
				default: //handles Set Sampling/Data rate CMDs, and Out of range errors
				{
					if((Command_Received >= (STARTOFRANGE_SET_SAMPLING_AND_RATE))&&(Command_Received < (STARTOFRANGE_SET_SAMPLING_AND_RATE + RANGESIZE_SET_SAMPLING_AND_RATE)))
					{
						uint8 offset = Command_Received-STARTOFRANGE_SET_SAMPLING_AND_RATE;
						offset *=DELTAVALS_SET_SAMP_AND_RATE;
						status = SetOverSampleAndDataRate(offset);
						if(status == 0) //if the write was a success
						{
							CyDelay(500);
							XBee_UART_PutChar(Command_Received);//Returns command received from MATLAB as confirmation
							LED_out_Write(0); //Turns off LED
							Command_Received = 0; //Clears the Command
							IncomingData_Flag = 0;
							ReadyForCommand_Flag = 1; // Sets state as READY for next command
						}		
						else 
						{
							// will result is a halt of CMD processing and leaves LED lit (visual error signal)
							StatusError_Flag = 1;
							//LCD_ClearDisplay();
							LCD_Position(1,0);
							LCD_PrintString("E:STA");
							CyDelay(1000);
						}
					}
					else // ERROR: CMD VALUE OUT OF RANGE (executed if not a CMD to set sampling/data rate)
					{
						//CLEAR EVERYTHING
						XBee_UART_ClearTxBuffer();
						XBee_UART_ClearRxBuffer();
						I2C_MasterClearReadBuf();
						I2C_MasterClearWriteBuf();

						Command_Received = 0; //Clears the Command
						IncomingData_Flag = 0;
						ReadyForCommand_Flag = 1; // Sets state as READY for next command						
						XBee_UART_PutChar(CMD_O_CMDVALUEOUTOFRANGE); //Send error msg
						//LCD_ClearDisplay();
						LCD_Position(1,0);
						LCD_PrintString("E:RAN");
						CyDelay(1000);
						LED_out_Write(0); //Turns off LED
					}
				}			
			} //END OF SWITCH-CASE
		}//end of IF statement encasing switch-case
		
		/*else // if(Command_Received == 0)
		{
			if((MAG_DataRdy_Flag==1) && (WaitForDataRead_Flag==0))
			{
				//May change CMD_O_MAGDATARDY to simply be the same as CMD_I_RM_MAGDATA.
				//XBee_UART_ClearTxBuffer();
				//LCD_ClearDisplay();
				LCD_Position(1,0);
				LCD_PrintString("DRDY2");
				XBee_UART_PutChar(CMD_O_MAGDATARDY);// CMD_O_MAGDATARDY = 55
				WaitForDataRead_Flag = 1; //Prevents constant resending of notification to MATLAB
				INT1_isr_Disable();
			}
		}*/
	}//END OF FOR LOOP	
		
}//END OF MAIN