/******************************************************************************* * Function Name : ADC_IRQHandler * Description : This function handles ADC global interrupt request. * Input : None * Output : None * Return : None *******************************************************************************/ void ADC_IRQHandler(void) { /* Disable ADC1 end of conversion interrupt */ ADC1_ITConfig(ADC1_FLAG_END_OF_CONVERSION, DISABLE); if (ADC1_GetFlagStatus(ADC1_IT_END_OF_CONVERSION) == SET) { /* Read value of the ADC1_RUSULT register and clear (automatically) * ADC1_IT_END_OF_CONVERSION flag */ ADC1_Value = ADC1_GetResult(); } if (ADC1_GetFlagStatus(ADCx_FLAG_OVERWRITE) == SET) { ADC1_ClearOverwriteFlag(); } }
// Convert to level: [0..100] uint8_t als_read() { uint8_t level; // Wait convert finished while(ADC1_GetFlagStatus(ADC1_FLAG_EOC) == RESET); // Get value uint16_t adc_value = ADC1_GetConversionValue(); // Clear flag ADC1_ClearFlag(ADC1_FLAG_EOC); // Start next conversion ADC1_StartConversion(); // [0..1023], reversed scale down to [100..0] if( adc_value >= 1000 ) { level = 0; } else { level = 100 - adc_value / 10; } return level; }
/******************************************************************************* * Function Name : ADC_IRQHandler * Description : This function handles ADC global interrupt request. * Input : None * Output : None * Return : None *******************************************************************************/ void ADC_IRQHandler(void) { if(ADC1_GetFlagStatus(ADCx_IT_OUT_OF_RANGE) == SET) { tmp = MDR_ADC->ADC1_RESULT & 0x0FFF; if(tmp > H_Level){ /* Turns LED1 On */ Display_PutChar(1, 0); Display_PutDigit(2, 1); } else{ Display_PutChar(1, MINUS); Display_PutDigit(2, 1); } } else{ Display_PutChar(0, 0); Display_PutChar(1, 0); Display_PutDigit(2, 0); } /* Clear ADC1 OUT_OF_RANGE interrupt bit */ MDR_ADC->ADC1_STATUS = (ADCx_IT_END_OF_CONVERSION | ADCx_IT_OUT_OF_RANGE)<<2; }