I'm having problems when I use the ADC0_SOURCE__AD5B channel in the below structure.
The code works if I use the ADC0_SOURCE_AD3. I know that the muxsel must be set to B for the AD5B channel.
Is there any known issues with this channel?
Hi Jeff,
I find a disorder in your header file:
// 3.3V Current Sensor const ADC_INIT_CHANNEL_STRUCT ADC0_I_SENSE_3_3V_Channel = { ADC0_SOURCE_AD3, // 3.3V Current Sensor,physical ADC ADC_CHANNEL_MEASURE_ONCE | ADC_CHANNEL_START_NOW, NUM_SAMPLES_IN_ONE_RUN, // number of samples in one run sequence START_OFFSET, // PERIOD, // period in us SCALE_RANGE, // scale range of result (not used now) CIRCULAR_BUFFER_SIZE, // circular buffer size (sample count) ADC_PDB_TRIGGER, // logical trigger ID that starts this ADC channel NULL, // no lwevent used 0 // lwevent bits };
// 5V Voltage Sensor const ADC_INIT_CHANNEL_STRUCT ADC0_VOLT_SENSE_5V_Channel = { ADC0_SOURCE_AD1, // 3.3V Voltage Sensor,physical ADC channel ADC_CHANNEL_MEASURE_ONCE | ADC_CHANNEL_START_NOW, NUM_SAMPLES_IN_ONE_RUN, // number of samples in one run sequence PERIOD, // period in us START_OFFSET, // SCALE_RANGE, // scale range of result (not used now) CIRCULAR_BUFFER_SIZE, // circular buffer size (sample count) ADC_PDB_TRIGGER, // logical trigger ID that starts this ADC channel NULL, // no lwevent used 0 // lwevent bits };
// AC Current Sensor const ADC_INIT_CHANNEL_STRUCT ADC0_I_SENSE_AC_Channel = { #if 0 ADC0_SOURCE_AD5B, // not working // AC current Sensor,physical ADC channel #else ADC0_SOURCE_AD3, // !!!!! using working channel here even thou it is not the correct channel.!!!!! #endif ADC_CHANNEL_MEASURE_ONCE | ADC_CHANNEL_START_NOW, NUM_SAMPLES_IN_ONE_RUN, // number of samples in one run sequence START_OFFSET, // PERIOD, // period in us SCALE_RANGE, // scale range of result (not used now) CIRCULAR_BUFFER_SIZE, // circular buffer size (sample count) ADC_PDB_TRIGGER, // logical trigger ID that starts this ADC channel NULL, // no lwevent used 0 // lwevent bits };
Why ADC0_VOLT_SENSE_5V_Channel, the defination of PERIOD, START_OFFSET is not like other channel?
Whether it should be :
// 5V Voltage Sensor
const ADC_INIT_CHANNEL_STRUCT ADC0_VOLT_SENSE_5V_Channel =
{
ADC0_SOURCE_AD1, // 3.3V Voltage Sensor,physical ADC channel
ADC_CHANNEL_MEASURE_ONCE | ADC_CHANNEL_START_NOW,
NUM_SAMPLES_IN_ONE_RUN, // number of samples in one run sequ
START_OFFSET, //
PERIOD, // period in us
SCALE_RANGE, // scale range of result (not used now)
CIRCULAR_BUFFER_SIZE, // circular buffer size (sample count)
ADC_PDB_TRIGGER, // logical trigger ID that starts this ADC channel
NULL, // no lwevent used
0 // lwevent bits
};
I am not familiar with your mqx code, I just find the definiation has the difference, I don't know whether it has problem when you call : VOLT_SENSE_5V_INDEX
Besides, please also check the PORTx_PCRn[MUX], when the problem happens.
Have a great day,
Jingjing
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------
The above shows a break point set when the channel fails the fopen. ADC_Channel_PTR = _vector_table(0x0):
Here is the same break point but this is for the working case. ADC_Channel_PTR = 0x72183C30
The only difference between the two cases is that the failed case does not execute the below code:
if (channelIndex == I_SENSE_AC_INDEX)
{
fclose(m_ADC0);
InitADCModule_ADC0();
}
I got the code to work but I shouldn't have had to add the code that I did.
I added the following code to the end of the void CADCModule::ADC_ReadChannel(BYTE channelIndex) function.
if (channelIndex == I_SENSE_AC_INDEX)
{
fclose(m_ADC0);
InitADCModule_ADC0();
}
Without this code the next measured channel on the ADC0 channel would fail to open.
Why does the ADC0_SOURCE_AD5B channel measurement cause the next channel to fail to open.
case VOLT_SENSE_5V_INDEX:
ADC_Channel_PTR = fopen(ADC0 "5V Voltage Sensor", (const char *)&ADC0_VOLT_SENSE_5V_Channel);
channel = 0;
break;
All the other channels work as they should.
Hi Jeff,
Could you tell me what the fopen function retrun when you call ADC_ReadChannel, channelIndex= I_SENSE_AC_INDEX in your code?
What the ADC_Channel_PTR data value?
void CADCModule::ADC_ReadChannel(BYTE channelIndex) { MQX_FILE_PTR ADC_Channel_PTR; // numBytesRead = 0; BYTE channel;
switch (channelIndex) { case I_SENSE_3_3V_INDEX: ADC_Channel_PTR = fopen(ADC0 "3V Current Sensor", (const char *)&ADC0_I_SENSE_3_3V_Channel); channel = 0; break; case I_SENSE_5V_INDEX: ADC_Channel_PTR = fopen(ADC1 "5V Current Sensor", (const char *)&ADC1_I_SENSE_5V_Channel); channel = 1; break; case I_SENSE_AC_INDEX: ADC_Channel_PTR = fopen(ADC0 "AC Current Sensor", (const char *)&ADC0_I_SENSE_AC_Channel); channel = 0; break; case VOLT_SENSE_5V_INDEX: ADC_Channel_PTR = fopen(ADC0 "5V Voltage Sensor", (const char *)&ADC0_VOLT_SENSE_5V_Channel); channel = 0; break; case VOLT_SENSE_3_3V_INDEX: ADC_Channel_PTR = fopen(ADC1 "3V Voltage Sensor", (const char *)&ADC1_VOLT_SENSE_3_3V_Channel); channel = 1; break; case TEMP_SENSE_INDEX: ADC_Channel_PTR = fopen(ADC1 "Temp Sensor", (const char *)&ADC1_TEMP_SENSE_Channel); channel = 1; break; default: ADC_Channel_PTR = NULL; channel = 0; break; }
if (ADC_Channel_PTR == NULL)
{
printf("Cannot open ADC Channel\n");
fclose(ADC_Channel_PTR);
if (channel == 0)
{
fclose(m_ADC0);
InitADCModule_ADC0();
}
else if (channel == 1)
{
fclose(m_ADC1);
InitADCModule_ADC1();
}
}
Please do the debug step by step in your code when you are calling ADC_ReadChannel, whether other ADC channel will make ADC_Channel_PTR == NULL? I find your code when ADC_Channel_PTR == NULL, then will do ADC channel fclose and init.
Have a great day,
Jingjing
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------
Hello Jeff,
- When you use the AD5B channel, please show us the ADC channel initialize code ,
the pin initialize code .
- Also please check whether it is right about the related registers .
- Then please be sure which pin do you used ?
Hope it helps
Alice