|
#define TEMP110_CAL_ADDR ((uint16_t*) ((uint32_t) 0x1FFFF7C2))
#define TEMP30_CAL_ADDR ((uint16_t*) ((uint32_t) 0x1FFFF7B8))
#define VDD_CALIB ((uint16_t) (330))
#define VDD_APPLI ((uint16_t) (300))
#define ADC_CHANNEL_NUMBERS 8 //ADCͨµÀÊý
void AdcConfiguration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA|RCC_AHBPeriph_GPIOB|RCC_AHBPeriph_DMA1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOB, &GPIO_InitStructure);
ADC_DeInit(ADC1);
ADC_StructInit(&ADC_InitStructure);
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward; // ADC_ScanDirection_Upward;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_ChannelConfig(ADC1, ADC_Channel_0, ADC_SampleTime_239_5Cycles); /* Convert the ADC1 Channel with 239.5 Cycles as sampling time */
ADC_ChannelConfig(ADC1, ADC_Channel_1, ADC_SampleTime_239_5Cycles);
ADC_ChannelConfig(ADC1, ADC_Channel_4, ADC_SampleTime_239_5Cycles);
ADC_ChannelConfig(ADC1, ADC_Channel_5, ADC_SampleTime_239_5Cycles);
ADC_ChannelConfig(ADC1, ADC_Channel_6, ADC_SampleTime_239_5Cycles);
ADC_ChannelConfig(ADC1, ADC_Channel_7, ADC_SampleTime_239_5Cycles);
ADC_ChannelConfig(ADC1, ADC_Channel_9, ADC_SampleTime_239_5Cycles);
/*************************************************************************/
//ADC_ChannelConfig(ADC1, ADC_Channel_TempSensor, ADC_SampleTime_239_5Cycles);
ADC_ChannelConfig(ADC1, ADC_Channel_16, ADC_SampleTime_239_5Cycles);
//ADC_ChannelConfig(ADC1, ADC_Channel_Vrefint, ADC_SampleTime_7_5Cycles);
ADC_TempSensorCmd(ENABLE);
//ADC_VrefintCmd(ENABLE);
/*************************************************************************/
ADC_GetCalibrationFactor(ADC1); /* ADC Calibration */
ADC_Cmd(ADC1, ENABLE); /* Enable ADCperipheral[PerIdx] */
while (!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADRDY)); /* Wait the ADCEN falg */
/*--------DMAÅäÖÃ-----------*/
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&ADC1->DR;// ADC1_DR_ADDRESS;
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&AdcValue;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
//BufferSize=2£¬ÒòΪADCת»»ÐòÁÐÓÐ2¸öͨµÀ
//Èç´ËÉèÖã¬Ê¹ÐòÁÐ1½á¹û·ÅÔÚAD_Value[0]£¬ÐòÁÐ2½á¹û·ÅÔÚAD_Value[1]
DMA_InitStructure.DMA_BufferSize = ADC_SAMPLE_TIMES*ADC_CHANNEL_NUMBERS;//7¸öͨµÀ£¬Ã¿¸öͨµÀ30¸öÖµ
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
//Ñ»·Ä£Ê½¿ªÆô£¬BufferдÂúºó£¬×Ô¶¯»Øµ½³õʼµØÖ·¿ªÊ¼´«Êä
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel1, &DMA_InitStructure);
//ÅäÖÃÍê³Éºó£¬Æô¶¯DMAͨµÀ
DMA_Cmd(DMA1_Channel1, ENABLE);
DMA_ITConfig(DMA1_Channel1, DMA_IT_TC, ENABLE); //ʹÄÜDMA´«ÊäÍê³ÉÖжÏ
ADC_DMARequestModeConfig(ADC1, ADC_DMAMode_Circular); /* Enable ADC_DMA */
ADC_DMACmd(ADC1, ENABLE);
/*--------DMAÖжÏÅäÖÃ----------*/
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure); // Enable Interrupt
ADC_StartOfConversion(ADC1); /* ADC1 regular Software Start Conv */
while(ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC)==RESET);
}
void ADC_GetValue(void)
{
u32 sum = 0;
u8 count,i;
// float AdcPortValut[ADC_CHANNEL_NUMBERS];
for(i=0;i<ADC_CHANNEL_NUMBERS;i++)
{
for ( count=0;count<ADC_SAMPLE_TIMES;count++)
{
sum += AdcValue[count];
AdcValue[count] = 0;
}
AdcValueAfterFilter=sum/ADC_SAMPLE_TIMES;
sum=0;
}
}
void TemperatureCalculate(void)
{
TemperatureSensor= (((int32_t) AdcValueAfterFilter[7] * VDD_APPLI / VDD_CALIB)- (int32_t) *TEMP30_CAL_ADDR );
TemperatureSensor= TemperatureSensor* (int32_t)(110 - 30);
TemperatureSensor= TemperatureSensor/ (int32_t)(*TEMP110_CAL_ADDR- *TEMP30_CAL_ADDR);
//TemperatureSensor= TemperatureSensor+ 30;
}
屏蔽这行,上电运行,算出来的温度是37度 差的很多呀,最后一行该不该屏蔽呢??
//TemperatureSensor= TemperatureSensor+ 30;
|
上一篇: 最全PID控制算法的C语言实现下一篇: 常用PID 算法原理、调试经验以及代码 适用于初学者
|