Beispiel #1
0
//Enables the peripherals associated with the strain amplifier and
//sets the default values.
//Make sure that you initialize I2C1 first!
void init_strain(void)
{
	int i = 0, j = 0;
	
	//Array prep:
	//=-=-=-=-=-=
	init_adc_delsig_dma_array();
	
	//Peripherals:
	//=-=-=-=-=-=
	
	//16-bits ADC:
	ADC_DelSig_1_Start();
	//ADC_DelSig_1_IRQ_Enable();
	dma_2_config();
	isr_delsig_StartEx(isr_delsig_Interrupt_InterruptCallback);
	
	//Defaults:
	//=-=-=-=-=-=
	for(i = 0; i < STRAIN_CHANNELS; i++)
	{
		strain[i].offset = STRAIN_DEFAULT_OFFSET;

		for(j = 0; j < STRAIN_BUF_LEN; j++)
		{
			strain[i].strain_raw[j] = 0;
		}
		strain[i].strain_filtered = 0;
		strain_config(i, strain[i].offset);
	}
	
	ADC_DelSig_1_StartConvert();
}
Beispiel #2
0
/*******************************************************************************
* Function Name: main
********************************************************************************
*
* Summary:
*  main() performs following functions:
*  1: Initializes the LCD
*  2: Starts ADC
*  3: Starts ADC converstion.
*  4: Gets the converted result and displays it in LCD.
*
* Parameters:
*  None.
*
* Return:
*  None.
*
*******************************************************************************/
int main()
{
    int16 output;

    /* Start the components */

    LCD_Start();
    ADC_DelSig_1_Start();

    /* Start the ADC conversion */
    ADC_DelSig_1_StartConvert();

    /* Display the value of ADC output on LCD */
    LCD_Position(0u, 0u);
    LCD_PrintString("ADC_Output");

    for(;;)
    {
        if(ADC_DelSig_1_IsEndConversion(ADC_DelSig_1_RETURN_STATUS))
        {
            output = ADC_DelSig_1_GetResult16();
            
            /* Saturate ADC result to positive numbers. */
            if(output < 0)
            {
                output = 0;
            }
            LCD_Position(1u, 0u);
            LCD_PrintInt16(output);
        }
    }
}
Beispiel #3
0
/*******************************************************************************
* 初始化函数
********************************************************************************/
void init()
{
    CyGlobalIntEnable; //全局中断开启
    
    ADC_DelSig_1_Start();  /* 配置并开启ADC */
    ADC_DelSig_1_StartConvert(); /* 开始进行转换 */
    
    Uart_Rx_ISR_StartEx(RxInterruptHandler);      /* 开启 Uart Rx 中断 并连接到 RxInterruptHandler */
    Uart_Tx_ISR_StartEx(TxInterruptHandler);      /* 开启 Uart Tx 并连接到 TxInterruptHandler */
    UART_Start();     /* 开启 UART */
    
    Uart_Rx_ISR_1_StartEx(Rx_1_InterruptHandler);      /* 开启 Uart Rx 中断 并连接到 RxInterruptHandler */
    Uart_Tx_ISR_1_StartEx(Tx_1_InterruptHandler);      /* 开启 Uart Tx 并连接到 TxInterruptHandler */
    UART_1_Start();     /* 开启 UART1 */
    
    Uart_Rx_ISR_2_StartEx(Rx_1_InterruptHandler);      /* 开启 Uart Rx 中断 并连接到 RxInterruptHandler */
    Uart_Tx_ISR_2_StartEx(Tx_1_InterruptHandler);      /* 开启 Uart Tx 并连接到 TxInterruptHandler */
    UART_2_Start();     /* 开启 UART2 */
    
    
    Timer_ISR_StartEx(TimerInterruptHandler); /* 开启 Timer 中断并连接到 TimerInterruptHandler */
    Timer_Start();          /* 开启定时器 */
    
    LCD_Char_1_Start(); /* 初始化并清除LCD */
    //LCD_Char_1_PrintString("init");
}
Beispiel #4
0
//Case 3: Strain Gauge DelSig ADC, SAR ADC
void main_fsm_case_3(void)
{
	//Start a new conversion
	ADC_DelSig_1_StartConvert();
	
	//Filter the previous results
	strain_filter_dma();
}
Beispiel #5
0
/*******************************************************************************
* Function Name: ADC_DelSig_1_Wakeup
********************************************************************************
*
* Summary:
*  Restores the user configuration and enables the power to the block.
*
* Parameters:
*  None
*
* Return:
*  None
*
* Global variables:
*  ADC_DelSig_1_backup:  The structure field 'enableState' is used to
*  restore the enable state of block after wakeup from sleep mode.
*
*******************************************************************************/
void ADC_DelSig_1_Wakeup(void) 
{
    /* Restore the configuration */
    ADC_DelSig_1_RestoreConfig();

    /* Enables the component operation */
    if(ADC_DelSig_1_backup.enableState != ADC_DelSig_1_DISABLED)
    {
        ADC_DelSig_1_Enable();
        if((ADC_DelSig_1_backup.enableState & ADC_DelSig_1_STARTED) != 0u)
        {
            ADC_DelSig_1_StartConvert();
        }
    } /* Do nothing if component was disable before */
}
/******************************************************************************* 
* Function Name: ADC_DelSig_1_Wakeup
********************************************************************************
*
* Summary:
*  Restores the user configuration and enables the power to the block.
*  
* Parameters:  
*  void
*
* Return: 
*  void
*
* Global variables:
*  ADC_DelSig_1_backup:  The structure field 'enableState' is used to 
*  restore the enable state of block after wakeup from sleep mode.
*
*******************************************************************************/
void ADC_DelSig_1_Wakeup(void) 
{
    /* Restore the configuration */
    ADC_DelSig_1_RestoreConfig();

    /* Enable's the component operation */
    if(ADC_DelSig_1_backup.enableState == ADC_DelSig_1_ENABLED)
    {
        ADC_DelSig_1_Enable();

        /* Start the conversion only if conversion is not triggered by the hardware */
        if(!(ADC_DelSig_1_DEC_CR_REG & ADC_DelSig_1_DEC_XSTART_EN))
        {
            ADC_DelSig_1_StartConvert();
        }

    } /* Do nothing if component was disable before */
}
Beispiel #7
0
void main()
{
    /* Place your initialization/startup code here (e.g. MyInst_Start()) */
	Opamp_1_Start();
	Opamp_3_Start();
    Clock_1_Start(); 
	ADC_DelSig_1_Start();
	ADC_DelSig_1_StartConvert();
	VDAC8_1_Start();
	VDAC8_2_Start();
   
    CyGlobalIntEnable;  /* Uncomment this line to enable global interrupts. */
    
    isr_StartEx(filterVDAC);
    for(;;)
    {
		/* Place your application code here. */
		
    }
}
/*main*/
void main(void)
{   
    /*Preliminary parts not important*/  
    LCD_Char_1_Start();
    ADC_DelSig_1_Start();
    ADC_DelSig_1_StartConvert();

    Configure_DMA(); 
    isr_1_StartEx(Buffer_complete);
    isr_2_StartEx(LPF_buffer_complete);
    
    ADC_DelSig_1_SetCoherency(ADC_DelSig_1_COHER_MID);   
    
    Filter_SetDalign(Filter_STAGEA_DALIGN,Filter_ENABLED);
    Filter_SetDalign(Filter_HOLDA_DALIGN,Filter_ENABLED);
    Filter_SetCoherency(Filter_STAGEA_COHER,Filter_KEY_MID);
    Filter_SetCoherency(Filter_HOLDA_COHER,Filter_KEY_MID);
    Filter_SetCoherency(Filter_CHANNEL_A,Filter_KEY_MID);  
    
    Filter_SetDalign(Filter_STAGEB_DALIGN,Filter_ENABLED);
    Filter_SetDalign(Filter_HOLDB_DALIGN,Filter_ENABLED);
    Filter_SetCoherency(Filter_STAGEB_COHER,Filter_KEY_MID);
    Filter_SetCoherency(Filter_HOLDB_COHER,Filter_KEY_MID);
    Filter_SetCoherency(Filter_CHANNEL_B,Filter_KEY_MID);

    CyGlobalIntEnable;

    Filter_Start();

    /*Writes ADC values to ADC_samples array*/
    while(1){
    if (isr_BC_flag==1){        
        arm_cfft_q15(&arm_cfft_sR_q15_len256, Buffer_samples, 0, 1); 
        arm_cmplx_mag_q15(Buffer_samples, magoutput, fftlength); 
        CyDmaChEnable(DMA_2_Chan, 1);
        
        isr_BC_flag=0;
        isr_1_ClearPending();
    }
}
}
Beispiel #9
0
void main()
{
    uint8 state, var_mask, ADC_in, auto_mode, continous_mode;
    uint8  DigitsPrecision, MaximumWeight;
    int16 MaxAvgReading = null, \
          MinAvgReading = null, \ 
          ScaledRange = null; 
  //uint8 Digit[5], ScaledRange, LastFractionResult, Remainder;

    
    /* Start the components */
    ADC_DelSig_1_Start();
    //ADC_DelSig_1_IRQ_Start();  //disable for manual polling instead so can single step
    
    CYGlobalIntEnable;   //enable for ADC and USB interrupts     

    
    /* Start the ADC conversion */
    ADC_DelSig_1_StartConvert();
    ADC_DelSig_1_IRQ_Disable();  //disable for manual polling instead so can single step
    
    
    /* Start USBFS Operation with 5V operation */
    USBFS_1_Start(0u, USBFS_1_5V_OPERATION); //USBFS_1_5V_OPERATION);

    /* Wait for Device to enumerate i.e. detects USB settings from host PC */
    while(USBFS_1_GetConfiguration() != 0u);

    /* Enumeration is done, enable OUT endpoint for receive data from Host */
    USBFS_1_EnableOutEP(OUT_EP);
    
    state=0u;
    for(;;) // this is infinite state loop
    { 
        if(state==0u)
        {
           /* Check that configuration is changed (there is only one configured on the USBFS )*/
           if(USBFS_1_IsConfigurationChanged() != 0u)
           {
              /* Re-enable endpoint when device is configured */
              if(USBFS_1_GetConfiguration() != 0u)
              {
                 USBFS_1_EnableOutEP(OUT_EP);
              }
           }
        
           /* GET MODE STATE Read USB PC host application*/
           if(USBFS_1_GetEPState(OUT_EP) == USBFS_1_OUT_BUFFER_FULL)
           {
              /* Read received bytes count */
              length = USBFS_1_GetEPCount(OUT_EP);

              /* Unload the OUT buffer */
              USBFS_1_ReadOutEP(OUT_EP, &in_buffer[0u], length);   
              state= in_buffer[MODE_BYTE];
              var_mask=in_buffer[SET_VARS_MASK_BYTE];

              if(var_mask & CONTINOUS_MODE_MASK_BIT)
                continous_mode = TRUE;
              else
                continous_mode = FALSE;
            }
        }
        
        if(state & INIT_SCALE_VARS)
        {
            if(var_mask & MAX_WEIGHT_VAR_MASK_BIT)
                MaximumWeight=in_buffer[MAX_WEIGHT_VAR_BYTE];
            if(MaximumWeight<MIN_WEIGHT_SCALE)
                MaximumWeight=MIN_WEIGHT_SCALE;
            if(MaximumWeight>MAX_WEIGHT_SCALE)
                MaximumWeight=MAX_WEIGHT_SCALE;           
            if(var_mask & PRECISION_VAR_MASK_BIT)
                DigitsPrecision=in_buffer[PRECISION_VAR_BYTE];
            if(DigitsPrecision<MIN_PRECISION_DIGITS)
                DigitsPrecision=MIN_PRECISION_DIGITS;
            if(DigitsPrecision>MAX_PRECISION_DIGITS)
                DigitsPrecision=MAX_PRECISION_DIGITS;
            if(var_mask & AUTO_MODE_MASK_BIT)
                auto_mode=TRUE;
            else
                auto_mode=FALSE;
            
            state=0u;
        }
         
        if(state & GET_SCALE_LOW_LIMIT)
        {             
            MinAvgReading = get_adc_average(NUMBER_SAMPLES);
            clear_buffer(out_buffer,BUF_SIZE);
            out_buffer[0u]=(uint8)MinAvgReading;
            //respond with min avg adc value (this requires host request)                       
            SendOutData(out_buffer); 
            state=0u;
        }
        
        if(state & GET_SCALE_HIGH_LIMIT)
        {   
            MaxAvgReading = get_adc_average(NUMBER_SAMPLES);            
            clear_buffer(out_buffer,BUF_SIZE);
            out_buffer[0u]=(uint8)MaxAvgReading;
            //respond with max avg adc value (this requires host request)                      
            SendOutData(out_buffer);  
            state=0u;
        }        
        
        if(state & SEND_SCALE_WEIGHT)
        {   
           do
           {
                //this flag tests if the ADC interrupt occurred meaning data is ready 
                 
                adc_flag=Status_Reg_1_Read();  //note the interrupt is disabled so read EOC bit instead
                if(adc_flag==1u)
                {
                    ADC_in=ADC_DelSig_1_GetResult8(); //read adc byte
                    adc_flag=0u;
                    
                    clear_buffer(out_buffer,BUF_SIZE);
                    
                    if(auto_mode==FALSE)
                        calculate_fixed_weight(ADC_in, out_buffer);
                    if(auto_mode==TRUE)
                        if( (MaxAvgReading == null) || (MinAvgReading == null) )
                          // Send Error Message
                          continue;
                        ScaledRange = (MaxAvgReading - MinAvgReading) / MaximumWeight;
                        calculate_scaled_weight(ADC_in, out_buffer, ScaledRange);
                    
                    //send weight data (this requires host request)                       
                    SendOutData(out_buffer);
                
                              
                }
            }while(USBFS_1_GetEPState(OUT_EP) != USBFS_1_OUT_BUFFER_FULL \
                   && continous_mode == TRUE);
            
        state=0u;  

        }

 
        

    }
}
Beispiel #10
0
uint8 campbell_cond_br_read(float* Rs){
    
    uint8 i, chan = 0u;
    
    AMux_1_Select(chan);
    // if i == 5, Mux could not be reset to 0

    ADC_DelSig_1_StartConvert();    
    CyDelay(1u);     
    
    for (i = 0u; i < 100; i++) {
        
        if(ADC_DelSig_1_IsEndConversion(ADC_DelSig_1_RETURN_STATUS)){
            
            if (chan == 0)
            {                
            
                ADC_DelSig_1_StopConvert(); // Stop the conversion so signals don't 
                                        // get mixed when switching the mux
                chan = 1u;
                AMux_1_Select(chan);
                res1 = ADC_DelSig_1_GetResult16();
    			dvpp = ADC_DelSig_1_CountsTo_Volts(res1) ;

                CyDelay(1u);
                ADC_DelSig_1_StartConvert();  
                CyDelay(49u);
            }
            else {            
                ADC_DelSig_1_StopConvert(); // Stop the conversion so signals don't 
                                        // get mixed when switching the mux
                AMux_1_DisconnectAll();
                
                res2 = ADC_DelSig_1_GetResult16();
        		vpp1 = (ADC_DelSig_1_CountsTo_Volts(res2)/2);
                vpp2 = vpp1 - dvpp;
                
                break;
            }                                                       
        }
        CyDelay(1u); 
    }
    
    
    //*Rs_half = 2*(vpp2-(vpp1)/2)/(vpp1);    // BR_HALF: Voltage divider between Cond LO and GND
    //*Rs_full = 2*(vpp2-(vpp1)/2)/(vpp1);    // BR_FULL: Reversed Differential measurement bewteen Cond HI and cond LO
    
    //*Rs_half = vpp2/vpp1;    // BR_HALF: Voltage divider between Cond LO and GND
    *Rs = vpp2/vpp1;    // BR_FULL: Reversed Differential measurement bewteen Cond HI and cond LO
    
    // ADC range is configured as min(v1) +/- 6.114
    // Readings are expected to be between min(v1) to +6.114
    // and misreading min(v1) as max(v1) results in overflow (eg values like 7.8 and 11)
    if (vpp1 > 6.114 || vpp2 > 6.114) {                                            
        return 0u;
    // Range of excitation signal is 2-2.5 VAC        
    } else if (vpp1 > 2.6) {
        return 0u;
    // vpp2 should always be less than vpp1        
    } else if (*Rs > 1) {
        return 0u;        
    } else if (*Rs < 0) {
        return 0u;
    } else {
        return 1u;
    }
}
Beispiel #11
0
uint8 campbell_temp_br_read(float* VsVx){
    
    uint8 i, chan = 2u;
    float mult = 1.0, ratio = 0.0, R = 0.0;
    
    if (! campbell_temp_start()) {
        return 0u;
    }
    
    AMux_1_Select(chan);
    ADC_DelSig_1_StartConvert();    
    CyDelay(100u);     
    
    for (i = 0u; i < 100; i++) {
        
        if(ADC_DelSig_1_IsEndConversion(ADC_DelSig_1_RETURN_STATUS)){
            
            if (chan == 2)
            {                
            
                ADC_DelSig_1_StopConvert(); // Stop the conversion so signals don't 
                                        // get mixed when switching the mux
                
                
                res1 = ADC_DelSig_1_GetResult16();
    			Vx = ADC_DelSig_1_CountsTo_Volts(res1) ;
                mult = 8/Vx;                // See Sec 12 http://s.campbellsci.com/documents/au/manuals/cs547a.pdf
                                            // Multiplier scales Vx to 8000 mV
                chan = 3u;
                AMux_1_Select(chan);  
                //ADC_DelSig_1_SelectConfiguration(2u, 1u);
                CyDelay(1u);
                ADC_DelSig_1_StartConvert(); 
                CyDelay(99u);                 
            }
            else {            
                ADC_DelSig_1_StopConvert(); // Stop the conversion so signals don't 
                                        // get mixed when switching the mux
                AMux_1_DisconnectAll();
                
                res2 = ADC_DelSig_1_GetResult16();
        		Vs = ADC_DelSig_1_CountsTo_Volts(res2) ;                
                break;
            }                    
        }
        CyDelay(1u); 
    }
        
    if (! campbell_temp_stop()) {
        return 0u;
    }     
    
    //*VsVx = mult * VsVx * 8000;   
    
    ratio = Vs/Vx;                          // Added to adjust for using 100 kOhm instead of 1 kOhm
    R = (100000 - (ratio*349000)) / ratio;  // Added to adjust for using 100 kOhm instead of 1 kOhm  
    *VsVx = 1000 / (R + 250000) * 8000;     // Now that Rs is known, use original equation
                                            // note: Datasheet says 800, but 8000 yields correct results

        // According to Therm107 Datasheet, http://s.campbellsci.com/documents/au/manuals/107.pdf
    // VsVx*8000 should range between 0 and 30
    if (*VsVx > 0 && *VsVx < 30) {                                            
        return 1u;      // Result is valid
    } else {
        return 0u;
    }
}
Beispiel #12
0
void main()
{
	CYGlobalIntEnable; /* Enable global interrupts */
	
	ADC_DelSig_1_Start();/* Configure and power up ADC */
	LCD_Char_1_Start(); /* Initialize and clear the LCD */
	
	/* Move the cursor to Row 0 Column 0 */
	LCD_Char_1_Position(ROW_0,COLUMN_0); 
	/* Print Label for the pot voltage raw count */
	LCD_Char_1_PrintString("TEMP NOW:    C");
	
	LCD_Char_1_Position(ROW_1,COLUMN_0);
	LCD_Char_1_PrintString("TEMP SET:    C");
	
	ADC_DelSig_1_StartConvert(); /* Force ADC to initiate a conversion */
	
	/* Start capsense and initialize baselines and enable scan */
	CapSense_Start();
	CapSense_InitializeAllBaselines();
	CapSense_ScanEnabledWidgets();

    /* CyGlobalIntEnable; */ /* Uncomment this line to enable global interrupts. */
	//Start the pwm;
	PWM_1_Start();
    for(;;)
    {
		/* If scanning is completed update the baseline count and check if sensor is active */
		while(CapSense_IsBusy());
		
		/* Update baseline for all the sensors */
		CapSense_UpdateEnabledBaselines();
		CapSense_ScanEnabledWidgets();
		
		/* Test if button widget is active */
		stateB_1 = CapSense_CheckIsWidgetActive(CapSense_BUTTON0__BTN);
		stateB_2 = CapSense_CheckIsWidgetActive(CapSense_BUTTON1__BTN);
	
        /* Wait for end of conversion */
		ADC_DelSig_1_IsEndConversion(ADC_DelSig_1_WAIT_FOR_RESULT);
		/* Get converted result */
		voltageRawCount = ADC_DelSig_1_GetResult16();
		//Change voltageRawCount to Temperature;
		temp = voltageRawCount / 3.870 * 0.1017 + 0.5;
		cold = (9999 - (temp > temp_set ? temp - temp_set : 0) * 50);
		if(cold < 1000)
			cold = 1000;
		if(cold > 9999)
			cold = 9999;
		//Change the pwm;
		PWM_1_WriteCompare(cold);
		/* Set range limit */
		if (temp > 0x7FFF)
		{
			temp = 0;
		}
		else
		{
		/* Continue on */
		}
		if(show < 10)
		{
			show++;
		}
		else
		{
			show = 0;
			UpdateDisplay(temp, 0); /* Print result on LCD */
			UpdateButtonState(stateB_1, stateB_2);
		}
    }
}
Beispiel #13
0
int main(void)
{
	//Local variables:
	uint8 i = 0;
	unsigned char result = 0;
	uint8 toggle_wdclk = 0;	
	uint8 cmd_ready_485_1 = 0, cmd_ready_usb = 0;
	static uint8 new_cmd_led = 0;
	uint16 safety_delay = 0;
	uint8 i2c_time_share = 0;
	
	//Power on delay with LEDs
	power_on();	     

	//Initialize all the peripherals
	init_peripherals();
	
	//Start with an empty buffer
	flexsea_clear_slave_read_buffer();
	
	//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
	//Blocking Test code - enable one and only one for special 
	//debugging. Normal code WILL NOT EXECUTE when this is enabled!
	//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
	//strain_test_blocking();
	//safety_cop_comm_test_blocking();
	//imu_test_code_blocking();
	//motor_fixed_pwm_test_code_blocking(141);
	//wdclk_test_blocking();
	//timing_test_blocking();
	//test_current_tracking_blocking();
	//test_pwm_pulse_blocking();
	//test_uart_dma_xmit();
	//motor_cancel_damping_test_code_blocking();
	//csea_knee_up_down_test_demo();
	//motor_stepper_test_blocking_1(80);
	//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
	//Non-Blocking Test code
	//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
	#ifdef USE_SPI_COMMUT
		
	motor_stepper_test_init(0);
	//Note: deadtime is 55, small PWM values won't make it move.
	//Starting at 0, GUI will change that when it wants.
	
	#endif	//USE_SPI_COMMUT	
	//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=	
	
	//Special code for the ExoBoots:
	#ifdef PROJECT_EXOCUTE
	init_exo();
	#endif

	//Main loop
	while(1)
	{
		if(t1_new_value == 1)
		{
			//If the time share slot changed we run the timing FSM. Refer to
			//timing.xlsx for more details. 't1_new_value' updates at 10kHz,
			//each slot at 1kHz.
			
			t1_new_value = 0;
			
			//Timing FSM:
			switch(t1_time_share)
			{
				//Case 0: I2C
				case 0:
					i2c_time_share++;
					i2c_time_share %= 4;
				
					#ifdef USE_I2C_INT
				
					//Subdivided in 4 slots.
					switch(i2c_time_share)
					{
						//Case 0.0: Accelerometer
						case 0:
						
							#ifdef USE_IMU							
							get_accel_xyz();
							i2c_last_request = I2C_RQ_ACCEL;
							#endif 	//USE_IMU
						
							break;
						
						//Case 0.1: Gyroscope
						case 1:
							
							#ifdef USE_IMU							
							get_gyro_xyz();		
							i2c_last_request = I2C_RQ_GYRO;
							#endif 	//USE_IMU
							
							break;
						
						//Case 0.2: Safety-Cop
						case 2:
							
							safety_cop_get_status();
							i2c_last_request = I2C_RQ_SAFETY;
							break;
						
						//Case 0.3: Free
						case 3:
							//I2C RGB LED
							
							//minm_test_code();
							update_minm_rgb();		//ToDo: That's EXT_I2C, not INT
							
							break;
						
						default:
							break;
					}
					
					#endif //USE_I2C_INT
					
					#ifdef USE_SPI_COMMUT
						
					angle = as5047_read_single(AS5047_REG_ANGLEUNC);
					
					#endif	//USE_SPI_COMMUT
				
					break;
				
				//Case 1:	
				case 1:
					break;
				
				//Case 2:	
				case 2:
					break;
				
				//Case 3: Strain Gauge DelSig ADC, SAR ADC
				case 3:
					
					//Start a new conversion
					ADC_DelSig_1_StartConvert();
					
					//Filter the previous results
					strain_filter_dma();					
					
					break;
				
				//Case 4: User Interface	
				case 4:
					
					//Alive LED
					alive_led();
					
					//UI RGB LED:
					
					if(safety_delay > SAFETY_DELAY)
					{
						//status_error_codes(safety_cop.status1, safety_cop.status2, &eL0, &eL1, &eL2); 
					}
					else
					{
						safety_delay++;
					}
					
					//Display temperature status on RGB	
					overtemp_error(&eL1, &eL2);					//Comment this line if safety code is problematic
					rgb_led_ui(eL0, eL1, eL2, new_cmd_led);		//ToDo add more error codes
					if(new_cmd_led)
					{
						new_cmd_led = 0;
					}
					
					break;
				
				//Case 5: Quadrature encoder & Position setpoint
				case 5:
					
					#ifdef USE_QEI1
				
					//Refresh encoder readings
					encoder_read();
						
					#endif	//USE_QEI1		
					
					#ifdef USE_TRAPEZ	
				
					if((ctrl.active_ctrl == CTRL_POSITION) || (ctrl.active_ctrl == CTRL_IMPEDANCE))
					{	
						//Trapezoidal trajectories (can be used for both Position & Impedance)				
						ctrl.position.setp = trapez_get_pos(steps);	//New setpoint
						ctrl.impedance.setpoint_val = trapez_get_pos(steps);	//New setpoint
					}
					
					#endif	//USE_TRAPEZ	
			
					break;
				
				//Case 6: P & Z controllers, 0 PWM	
				case 6:
					
					#ifdef USE_TRAPEZ	
					
					if(ctrl.active_ctrl == CTRL_POSITION)
					{
						motor_position_pid(ctrl.position.setp, ctrl.position.pos);
					}
					else if(ctrl.active_ctrl == CTRL_IMPEDANCE)
					{
						//Impedance controller
						motor_impedance_encoder(ctrl.impedance.setpoint_val, ctrl.impedance.actual_val);
					}
					
					#endif	//USE_TRAPEZ
					
					//If no controller is used the PWM should be 0:
					if(ctrl.active_ctrl == CTRL_NONE)
					{
						motor_open_speed_1(0);
					}
					
					break;
				
				case 7:
					
					#ifdef USE_SPI_COMMUT
						
					//Stepper test code:
					motor_stepper_test_runtime(10);
					
					#endif	//USE_SPI_COMMUT					
					
					break;
				
				//Case 8: SAR ADC filtering
				case 8:

					filter_sar_adc();
					
					break;
				
				//Case 9: User functions	
				case 9:
					
					//ExoBoot code - 1kHz
					#ifdef PROJECT_EXOCUTE
						
					exo_fsm();	
						
					#endif
					
					break;
				
				default:
					break;
			}
			
			//The code below is executed every 100us, after the previous slot. 
			//Keep it short!
			
			//BEGIN - 10kHz Refresh
			
			//RS-485 Byte Input
			#ifdef USE_RS485			
		
			//get_uart_data();	//Now done via DMA
			
			if(data_ready_485_1)
			{
				data_ready_485_1 = 0;
				//Got new data in, try to decode
				cmd_ready_485_1 = unpack_payload_485_1();
			}
				
			#endif	//USE_RS485
			
			//USB Byte Input
			#ifdef USE_USB			
		
			get_usb_data();
			
			if(data_ready_usb)
			{
				data_ready_usb = 0;
				//Got new data in, try to decode
				cmd_ready_usb = unpack_payload_usb();
				
				eL1 = 1;
			}

			#endif	//USE_USB
			
			//FlexSEA Network Communication
			#ifdef USE_COMM
				
			//Valid communication from RS-485 #1?
			if(cmd_ready_485_1 != 0)
			{
				cmd_ready_485_1 = 0;
				
				//Cheap trick to get first line	//ToDo: support more than 1
				for(i = 0; i < PAYLOAD_BUF_LEN; i++)
				{
					tmp_rx_command_485_1[i] = rx_command_485_1[0][i];
				}
				
				//payload_parse_str() calls the functions (if valid)
				result = payload_parse_str(tmp_rx_command_485_1);
				
				//LED:
				if(result == PARSE_SUCCESSFUL)
				{
					//Green LED only if the ID matched and the command was known
					new_cmd_led = 1;
				}
			}

			//Valid communication from USB?
			if(cmd_ready_usb != 0)
			{
				cmd_ready_usb = 0;
				
				//Cheap trick to get first line	//ToDo: support more than 1
				for(i = 0; i < PAYLOAD_BUF_LEN; i++)
				{
					tmp_rx_command_usb[i] = rx_command_usb[0][i];
				}
				
				//payload_parse_str() calls the functions (if valid)
				result = payload_parse_str(tmp_rx_command_usb);
				
				//LED:
				if(result == PARSE_SUCCESSFUL)
				{
					//Green LED only if the ID matched and the command was known
					new_cmd_led = 1;
				}
			}

			
			#endif	//USE_COMM	
			
			//END - 10kHz Refresh
		}
		else
		{
			//Asynchronous code goes here.
			
			//WatchDog Clock (Safety-CoP)
			toggle_wdclk ^= 1;
			WDCLK_Write(toggle_wdclk);
		}
	}
}