T
//******************************************************************************
// Measure Light
//******************************************************************************
uint16_t MeasureLight(void)
{
uint16_t x;
Init_Normal_ADC();
ADC_StartConversion(0x05); // Port A.5
while (!ADC_IsConversionDone());
x = ADC_GetConversionResult();
return x;
}
//******************************************************************************
// The random.c DALI library change the ADC settings, so we need to restore them
// after a random command. Also this function initialize ADC the first time
//******************************************************************************
void Init_Normal_ADC()
{
// ADGO stop; ADFM right; ADON enabled; ADCONT disabled; ADCS FOSC;
ADCON0 = 0x84;
ADCON1 = 0x00;
ADCON2 = 0x00;
ADCON3 = 0x00;
ADSTAT = 0x00;
// ADCCS FOSC/2;
ADCLK = 0x00;
// ADNREF VSS; ADPREF VDD;
ADREF = 0x00;
// CHANNEL --> ANA0;
ADPCH = 0x00;
// PRECHARGE TIME --> 0;
ADPRE = 0x00;
// ACQUISITION TIME --> 0;
ADACQ = 0x00;
// ADDITIONAL CAPACITOR --> 0
ADCAP = 0x00;
// AUTO conversion disabled;
ADACT = 0x00;
// ADRPT 0;
ADRPT = 0x00;
// ADLTHL 0;
ADLTHL = 0x00;
// ADLTHH 0;
ADLTHH = 0x00;
// ADUTHL 0;
ADUTHL = 0x00;
// ADUTHH 0;
ADUTHH = 0x00;
// ADSTPTL 0;
ADSTPTL = 0x00;
// ADSTPTH 0;
ADSTPTH = 0x00;
// ADC VALUE REGISTER L
ADRESL = 0x00;
// ADC VALUE REGISTER H
ADRESH = 0x00;
}
//******************************************************************************
// Start ADC conversion
//******************************************************************************
void ADC_StartConversion(uint8_t channel)
{
// Set CHANNEL
ADPCH = channel;
// ADRESL 0x0;
ADRESL = 0x00;
// ADRESH 0x0;
ADRESH = 0x00;
// Acquisition time delay
__delay_us(ACQ_US_DELAY);
// Turn on the ADC module
ADCON0bits.ADON = 1;
// Start the conversion
ADCON0bits.ADGO = 1;
}
//******************************************************************************
// Conversion DONE
//******************************************************************************
uint8_t ADC_IsConversionDone()
{
// Start the conversion
return (!ADCON0bits.ADGO);
}
//******************************************************************************
// Get result
//******************************************************************************
uint16_t ADC_GetConversionResult(void)
{
// Conversion finished, return the result
return ((ADRESH << 8) + ADRESL);
}
Thankyou Andre, this is a brilliant finding.....of course, you are right, the acquisition delay should be commenced just after the adc conversion is started.the delay ACQ_US_DELAY should supposedly be placed after the start of the conversion itself,
this uses an "addition" to combine both bytes.
Be sure it treats both bytes as UINT8-
I assume a logical "OR" is a sefer way to combine both.
return ((ADRESH << 8) + ADRESL);
Thanks, we have probed it with scope and we do see the light sensor output correctly going up and down with the light level over the 10ms mains half periods......but we didnt find exact value, and of course, the microcontroller adc pin's leakage current could have meant that the voltage levels on the 82k resistor were too high.Perhaps probing with an oscilloscope this point would bring to light something else you did not realize on this sensor.
Thanks, but please i refer to the SFH5711 datasheet (in above posts)...the output of the SFH5711 light sensor is a current source, so its extremely high impedance, and the impedance "seen" by the ADC input will be the 82k of the resistor that we placed at the output of the light sensor IC (SFH5711). [schematic in post #8 above]The amplifier probably has low output impedance.
The 82k is not the source impedance...it will be much lower.
I think this behaviour is very unlikely to be caused by any mistreatment...so this harps back to the suspicion that the ADC module in a certain percentage of our microcontrollers is actually broken, and has the next most significant bit of ADRES register permanenetly set high.
Thanks, do you mean you doubt that the micro's ADC really is damaged?I think this behaviour is very unlikely to be caused by any mistreatment.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?