/******************************************************************************* * Function Name: USBFS_1_GetCount ******************************************************************************** * * Summary: * This function returns the number of bytes that were received from the PC. * * Parameters: * None. * * Return: * Returns the number of received bytes. * * Global variables: * USBFS_1_cdc_data_out_ep: CDC OUT endpoint number used. * *******************************************************************************/ uint16 USBFS_1_GetCount(void) { uint8 bytesCount = 0u; if (USBFS_1_EP[USBFS_1_cdc_data_out_ep].apiEpState == USBFS_1_OUT_BUFFER_FULL) { bytesCount = USBFS_1_GetEPCount(USBFS_1_cdc_data_out_ep); } return(bytesCount); }
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; } } }