cancel
Showing results for 
Search instead for 
Did you mean: 

ADC1 and internal temperature sensor ?

antonius
Senior
Posted on January 28, 2014 at 08:13

Guys,

Am I right on initializing ADC ?

It's running only one time and then freeze, I put it on forever loop, but stopping, never looping again, any wrong configuration here ?

thanks

      //read internal temperature sensor begin

        RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);

        //ADC1 configuration

        //select independent conversion mode (single)

        ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;

        //We will convert single channel only

        ADC_InitStructure.ADC_ScanConvMode = DISABLE;

        //we will convert one time

        ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;

        //select no external triggering

        ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;

        //right 12-bit data alignment in ADC data register

        ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;

        //single channel conversion

        ADC_InitStructure.ADC_NbrOfChannel = 1;

        //load structure values to control and status registers

        ADC_Init(ADC1, &ADC_InitStructure);

        //wake up temperature sensor

        ADC_TempSensorVrefintCmd(ENABLE);

        //ADC1 channel16 configuration

        //we select 41.5 cycles conversion for channel16

        //and rank=1 which doesn't matter in single mode

        ADC_RegularChannelConfig(ADC1, ADC_Channel_16, 1, ADC_SampleTime_41Cycles5);

        //Enable ADC1

        ADC_Cmd(ADC1, ENABLE);

        //Enable ADC1 reset calibration register

        ADC_ResetCalibration(ADC1);

        //Check the end of ADC1 reset calibration register

        while(ADC_GetResetCalibrationStatus(ADC1));

        //Start ADC1 calibration

        ADC_StartCalibration(ADC1);

        //Check the end of ADC1 calibration

        while(ADC_GetCalibrationStatus(ADC1));

        //Start ADC1 Software Conversion

        ADC_SoftwareStartConvCmd(ADC1, ENABLE);

        //wait for conversion complete

        while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC)){}

        //read ADC value

        AD_value=ADC_GetConversionValue(ADC1);

        //clear EOC flag

        ADC_ClearFlag(ADC1, ADC_FLAG_EOC);

        printf(''\r\n ADC value: %d \r\n'', AD_value);

        TemperatureC = (uint16_t)((V25-AD_value)/Avg_Slope+25);

        printf(''Temperature: %d%cC\r\n'', TemperatureC, 176);

        

      //read internal temperature sensor end

4 REPLIES 4
raptorhal2
Lead
Posted on January 28, 2014 at 15:49

There is no forever loop here.

Is it reading one time correctly ?

Cheers, Hal

antonius
Senior
Posted on January 29, 2014 at 00:54

Yes it's reading it correctly,

I put     while(1){}    at the end,

but it's reading only one time, the next one is empty, do you have idea what the cause is ?

thanks

Posted on January 29, 2014 at 03:07

A more considered presentation might look like this :

void TempSensorInit(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
//ADC1 configuration
//select independent conversion mode (single)
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
//We will convert single channel only
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
//we will convert one time
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
//select no external triggering
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
//right 12-bit data alignment in ADC data register
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
//single channel conversion
ADC_InitStructure.ADC_NbrOfChannel = 1;
//load structure values to control and status registers
ADC_Init(ADC1, &ADC_InitStructure);
//wake up temperature sensor
ADC_TempSensorVrefintCmd(ENABLE);
//ADC1 channel16 configuration
//we select 5 cycles conversion for channel16
//and rank=1 which doesn't matter in single mode
ADC_RegularChannelConfig(ADC1, ADC_Channel_16, 1, ADC_SampleTime_41Cycles5);
//Enable ADC1
ADC_Cmd(ADC1, ENABLE);
//Enable ADC1 reset calibration register
ADC_ResetCalibration(ADC1);
//Check the end of ADC1 reset calibration register
while(ADC_GetResetCalibrationStatus(ADC1));
//Start ADC1 calibration
ADC_StartCalibration(ADC1);
//Check the end of ADC1 calibration
while(ADC_GetCalibrationStatus(ADC1));
}
void TempSensorRead(void)
{
//Start ADC1 Software Conversion
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
//wait for conversion complete
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC)){}
//read ADC value
AD_value=ADC_GetConversionValue(ADC1);
//clear EOC flag
ADC_ClearFlag(ADC1, ADC_FLAG_EOC);
printf(''

 ADC value: %d 

'', AD_value);
TemperatureC = (uint16_t)((V25-AD_value)/Avg_Slope+25);
printf(''Temperature: %d%cC

'', TemperatureC, 176);
} 
main(void)
{
//..
TempSensorInit();
while(1)
{
TempSensorRead();
}
}

You'll need to add back your variables, etc. Try to post sufficiently complete code that demonstrates your problem, not random chunks with a poorly described context and use case.
Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
antonius
Senior
Posted on January 29, 2014 at 08:24

Thanks for the idea...