/*Requires Delay after call*/
uint8 MagReadByte(uint8 registerAddress, uint8 *readPtr)
{
	/* Pointer to the register address */
    //uint8 *writePtr = &registerAddress;/* changed writeptr to &registerAddress*/
	uint8 i2c_status = I2C_MasterClearStatus();
	//LCD_ClearDisplay();
	LCD_Position(1,7);
	LCD_PrintInt8(i2c_status);
	
	I2C_MasterClearReadBuf();
	I2C_MasterClearWriteBuf();
	
    /* Start the I2C transmission for a read */
    uint8 status = I2C_MasterWriteBuf(SLAVE_ADDRESS, &registerAddress, 1, I2C_MODE_NO_STOP);
    /*wait for the tranmission to finish */
    while (I2C_MasterStatus() && !I2C_MSTAT_WR_CMPLT){}
    /* Needed because of some bug in the psoc I2C tramission */
    CyDelay(1);
	/* read a byte using I2C */
	//return I2C_MasterReadBuf(SLAVE_ADDRESS, readPtr, 1, I2C_MODE_REPEAT_START);
	
	//or TO ENSURE READ IS COMPLETE BEFORE ADDITIONAL CODE EXECUTED
	status |= I2C_MasterReadBuf(SLAVE_ADDRESS,readPtr , 1, I2C_MODE_REPEAT_START);
	while (I2C_MasterStatus() && !I2C_MSTAT_RD_CMPLT){}
    CyDelay(1); //Needed because of some bug in the psoc I2C tramission
	return status;
}
/**
 * @brief Write multiple bytes of data to the MAG3110 in sequential memory
 * @details User supplies the register that the write will start on. User also supplues a buffer of data to write
 *          to the MAG3110 and the size of the buffer as well.
 *
 * @param firstRegister Register the sequential write will start on
 * @param bufferPtr Pointer that points to the buffer of data to write
 * @param size umber of elements that are in the buffer
 * @return Error status of the function
 */
uint8 MagWriteMultipleBytes(uint8 firstRegister, uint8 *bufferPtr, uint8 size)
{
    /* 	Create a new buffer that has that has the firstRegistsre as first element and the other elements as the data pointed to 
		in bufferPtr */
	uint8 i2c_status = I2C_MasterClearStatus();
	//LCD_ClearDisplay();
	LCD_Position(1,7);
	LCD_PrintInt8(i2c_status);
	I2C_MasterClearReadBuf();
	I2C_MasterClearWriteBuf();
	
    /* Pinter to the write buffer that will be written */
    

    /* Set the first element of the buffer equal to the first register we will write to */
    //*(Global_WritePtr) = firstRegister;
    /* Tranfers the data from the buffer that was passed to the function to the buffer that is to be written */
	//uint8 i = 1;
    //for(i=1;i<=size;i++)
	//{
	//	*(Global_WritePtr+i) = bufferPtr[i-1];
    //}
	
	/* Set the first element of the buffer equal to the first register we will write to */
	uint8 j;
	for(j = size;j>0;j--)
	{
		bufferPtr[j] = bufferPtr[j-1];
	}
	bufferPtr[0] = firstRegister;

    /* Write multiple bytes to the MAG3110 */
    uint8 status = I2C_MasterWriteBuf(SLAVE_ADDRESS, bufferPtr, (size+1), I2C_MODE_COMPLETE_XFER);
    while (I2C_MasterStatus() && !I2C_MSTAT_WR_CMPLT){}
    /* Needed because of some bug in the psoc I2C tramission */
    CyDelay(1);
	//functioncomplete_flag = 1;/*set Flag for write completion*/
	return status;
}
/* 
 * @brief Write a byte to the MAG3110
 * @details Takes a register address that we are going to write to and a byte of datat that we will write to the register
 *
 * @param registerAddress Address of the register on the MAG3110
 * @param data Byte that will be written to the MAG3110
 *
 * @return Error status of the function
 */
uint8 MagWriteByte(uint8 registerAddress, uint8 value)
{
    //functioncomplete_flag = 0;/*set Flag for function Incomplete*/
	I2C_MasterClearReadBuf();
	I2C_MasterClearWriteBuf();
	uint8 i2c_status = I2C_MasterClearStatus();
	//LCD_ClearDisplay();
	LCD_Position(1,7);
	LCD_PrintInt8(i2c_status);
	
	/* Buffer that holds the data to be written */
    uint8 writeBuffer[2] = {registerAddress, value};
    /* Pointer to the buffer that will be written */
    uint8 *writePtr = writeBuffer;
    /* Write to MAG3110 */
    uint8 status = I2C_MasterWriteBuf(SLAVE_ADDRESS, writePtr, 2, I2C_MODE_COMPLETE_XFER);
	/*Wait for transmission to finish*/
	while (I2C_MasterStatus() && !I2C_MSTAT_WR_CMPLT){}
    CyDelay(1);
	//functioncomplete_flag = 1;/*set Flag for function complete*/
	return status;
}
/**
 * @brief Read multiple bytes from the MAG3110
 * @details User supplies the register that we are going to read from and the buffer that we are going to place the data 
 *          that we read from the MAG3110. REQUIRES DELAY AFTER CALL
 *      
 * @param registerAddress Address of the first register we are going to read
 * @param readPtr Pointer to the buffer that the data that we read will be placed  
 * @param size Number of bytes that we are going to read from the MAG3110
 * @return Error status of the function
 */
uint8 MagReadMultipleByte(uint8 registerAddress, uint8 *readPtr,uint8 size)
{
	uint8 i2c_status = I2C_MasterClearStatus();
	LCD_Position(1,7);
	LCD_PrintInt8(i2c_status);
	I2C_MasterClearReadBuf();
	I2C_MasterClearWriteBuf();
	
    /* We start by writng the slave address and the register we are going to start the rea from*/
    uint8 status = I2C_MasterWriteBuf(SLAVE_ADDRESS, &registerAddress, 1, I2C_MODE_NO_STOP);
    /*wait for the tranmission to finish */
    while (I2C_MasterStatus() && !I2C_MSTAT_RD_CMPLT){}
    /* Needed because of some bug in the psoc I2C tramission */
    CyDelay(1);
    
	
	/* read data from the MAG I2C */
	//return I2C_MasterReadBuf(SLAVE_ADDRESS,readPtr , size, I2C_MODE_REPEAT_START);
	//or TO ENSURE READ IS COMPLETE BEFORE ADDITIONAL CODE EXECUTED
	status |= I2C_MasterReadBuf(SLAVE_ADDRESS,readPtr , size, I2C_MODE_REPEAT_START);
	while (I2C_MasterStatus() && !I2C_MSTAT_RD_CMPLT){}
    CyDelay(1); //Needed because of some bug in the psoc I2C tramission
	return status;
}
Example #5
0
/*******************************************************************************
* Function Name: I2C_Init
********************************************************************************
*
* Summary:
*  Initializes I2C registers with initial values provided from customizer.
*
* Parameters:
*  None
*
* Return:
*  None
*
* Global variables:
*  None
*
* Reentrant:
*  No
*
*******************************************************************************/
void I2C_Init(void) 
{
    #if(I2C_FF_IMPLEMENTED)
        I2C_CFG_REG  = I2C_DEFAULT_CFG;
        I2C_XCFG_REG = I2C_DEFAULT_XCFG;

        #if(CY_PSOC5A)
            I2C_CLKDIV_REG  = LO8(I2C_DEFAULT_DIVIDE_FACTOR);
        #else
            I2C_CLKDIV1_REG = LO8(I2C_DEFAULT_DIVIDE_FACTOR);
            I2C_CLKDIV2_REG = HI8(I2C_DEFAULT_DIVIDE_FACTOR);
        #endif /* (CY_PSOC5A) */

    #else
        uint8 enableInterrupts;

        I2C_CFG_REG      = I2C_DEFAULT_CFG;      /* control  */
        I2C_INT_MASK_REG = I2C_DEFAULT_INT_MASK; /* int_mask */

        /* Enable interrupts from block */
        enableInterrupts = CyEnterCriticalSection();
        I2C_INT_ENABLE_REG |= I2C_INTR_ENABLE; /* aux_ctl */
        CyExitCriticalSection(enableInterrupts);

        #if(I2C_MODE_MASTER_ENABLED)
            I2C_MCLK_PRD_REG = I2C_DEFAULT_MCLK_PRD;
            I2C_MCLK_CMP_REG = I2C_DEFAULT_MCLK_CMP;
         #endif /* (I2C_MODE_MASTER_ENABLED) */

        #if(I2C_MODE_SLAVE_ENABLED)
            I2C_PERIOD_REG = I2C_DEFAULT_PERIOD;
        #endif  /* (I2C_MODE_SLAVE_ENABLED) */

    #endif /* (I2C_FF_IMPLEMENTED) */

    #if(I2C_TIMEOUT_ENABLED)
        I2C_TimeoutInit();
    #endif /* (I2C_TIMEOUT_ENABLED) */

    /* Disable Interrupt and set vector and priority */
    CyIntDisable    (I2C_ISR_NUMBER);
    CyIntSetPriority(I2C_ISR_NUMBER, I2C_ISR_PRIORITY);
    #if(I2C_INTERN_I2C_INTR_HANDLER)
        (void) CyIntSetVector(I2C_ISR_NUMBER, &I2C_ISR);
    #endif /* (I2C_INTERN_I2C_INTR_HANDLER) */


    /* Put state machine in idle state */
    I2C_state = I2C_SM_IDLE;

    #if(I2C_MODE_SLAVE_ENABLED)
        /* Reset status and buffers index */
        I2C_SlaveClearReadBuf();
        I2C_SlaveClearWriteBuf();
        I2C_slStatus = 0u; /* Reset slave status */

        /* Set default address */
        I2C_SlaveSetAddress(I2C_DEFAULT_ADDR);
    #endif /* (I2C_MODE_SLAVE_ENABLED) */

    #if(I2C_MODE_MASTER_ENABLED)
        /* Reset status and buffers index */
        I2C_MasterClearReadBuf();
        I2C_MasterClearWriteBuf();
        (void) I2C_MasterClearStatus();
    #endif /* (I2C_MODE_MASTER_ENABLED) */
}
Example #6
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