stm32AD转换
#include "bsp_wt_adc.h"
#include "lcd.h"
__IO uint16_t ADC_ConvertValue[8]={0};
__IO uint16_t ADC_AvgValue[8]={0};
float Waveguide_Water_Temp = 0.0;
float Waveguide_Water_Flow = 0.0;
float Sample_Watertemp = 0.0;
float Sample_Waterflow = 0.0;
float Cavity_Watertemp = 0.0;
float Cavity_Waterflow = 0.0;
float M_Changer_Watertemp = 0.0;
float M_Changer_Waterflow = 0.0;
u16 Alarm_Msg_Error = 0;
void Waterflow_Sw_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd (RCC_AHB1Periph_GPIOF, ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_Init(GPIOF, &GPIO_InitStructure);
GPIO_SetBits(GPIOF, GPIO_Pin_13);
}
void ADC_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_6 | GPIO_Pin_7 ;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 ;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_Init(GPIOF, &GPIO_InitStructure);
GPIO_ResetBits(GPIOF, GPIO_Pin_13);
}
void ADC_DMA_Mode_Init(void)
{
DMA_InitTypeDef DMA_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
// ------------------DMA Init ½á¹¹Ìå²ÎÊý ³õʼ»¯--------------------------
// ADC1ʹÓÃDMA2£¬Êý¾ÝÁ÷0£¬Í¨µÀ0£¬Õâ¸öÊÇÊÖ²á¹Ì¶¨ËÀµÄ
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 , ENABLE); // ¿ªÆôADCʱÖÓ
DMA_InitStructure.DMA_PeripheralBaseAddr = ((u32)ADC1 0x4c); // ÍâÉè»ùַΪ£ºADC Êý¾Ý¼Ä´æÆ÷µØÖ·
DMA_InitStructure.DMA_Memory0BaseAddr = (u32)ADC_ConvertValue; // ´æ´¢Æ÷µØÖ·£¬Êµ¼ÊÉϾÍÊÇÒ»¸öÄÚ²¿SRAMµÄ±äÁ¿
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; // Êý¾Ý´«Êä·½ÏòΪÍâÉèµ½´æ´¢Æ÷
DMA_InitStructure.DMA_BufferSize = 1; // »º³åÇø´óСΪ£¬Ö¸Ò»´Î´«ÊäµÄÊý¾ÝÁ¿
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; // ´æ´¢Æ÷Êý¾Ý´óСҲΪ°ë×Ö£¬¸úÍâÉèÊý¾Ý´óСÏàͬ
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High; // DMA ´«ÊäͨµÀÓÅÏȼ¶Îª¸ß£¬µ±Ê¹ÓÃÒ»¸öDMAͨµÀʱ£¬ÓÅÏȼ¶ÉèÖò»Ó°Ïì// Ñ»·´«Êäģʽ
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; // ½ûÖ¹DMA FIFO £¬Ê¹ÓÃÖ±Á¬Ä£Ê½
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; // FIFO ´óС£¬FIFOģʽ½ûֹʱ£¬Õâ¸ö²»ÓÃÅäÖÃ
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_InitStructure.DMA_Channel = DMA_Channel_0; // Ñ¡Ôñ DMA ͨµÀ£¬Í¨µÀ´æÔÚÓÚÁ÷ÖÐ
DMA_Init(DMA2_Stream0, &DMA_InitStructure); //³õʼ»¯DMAÁ÷£¬Á÷Ï൱ÓÚÒ»¸ö´óµÄ¹ÜµÀ£¬¹ÜµÀÀïÃæÓкܶàͨµÀ
DMA_Cmd(DMA2_Stream0, ENABLE);// ʹÄÜDMAÁ÷
// -------------------ADC Common ½á¹¹Ìå ²ÎÊý ³õʼ»¯------------------------
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent; // ¶ÀÁ¢ADCģʽ
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2; // ʱÖÓΪfpclk x·ÖƵ
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; // ½ûÖ¹DMAÖ±½Ó·ÃÎÊģʽ
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles; // ²ÉÑùʱ¼ä¼ä¸ô
ADC_CommonInit(&ADC_CommonInitStructure);
// -------------------ADC Init ½á¹¹Ìå ²ÎÊý ³õʼ»¯--------------------------
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; // ADC ·Ö±æÂÊ
ADC_InitStructure.ADC_ScanConvMode = ENABLE; // ɨÃèģʽ£¬¶àͨµÀ²É¼¯ÐèÒª
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; // Á¬Ðøת»»
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; //½ûÖ¹Íⲿ±ßÑØ´¥·¢
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1; //ʹÓÃÈí¼þ´¥·¢£¬Íⲿ´¥·¢²»ÓÃÅäÖã¬×¢Ê͵ô¼´¿É
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //Êý¾ÝÓÒ¶ÔÆë
ADC_InitStructure.ADC_NbrOfConversion = 8;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 1, ADC_SampleTime_56Cycles);
ADC_RegularChannelConfig(ADC1, ADC_Channel_3, 2, ADC_SampleTime_56Cycles);
ADC_RegularChannelConfig(ADC1, ADC_Channel_6, 3, ADC_SampleTime_56Cycles);
ADC_RegularChannelConfig(ADC1, ADC_Channel_7, 4, ADC_SampleTime_56Cycles);
ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 5, ADC_SampleTime_56Cycles);
ADC_RegularChannelConfig(ADC1, ADC_Channel_15, 6, ADC_SampleTime_56Cycles);
ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 7, ADC_SampleTime_56Cycles);
ADC_RegularChannelConfig(ADC1, ADC_Channel_9, 8, ADC_SampleTime_56Cycles);
ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE); // ʹÄÜDMAÇëÇó after last transfer (Single-ADC mode)
ADC_DMACmd(ADC1, ENABLE); // ʹÄÜADC DMA
ADC_Cmd(ADC1, ENABLE); // ʹÄÜADC
ADC_SoftwareStartConv(ADC1); // ¿ªÊ¼adcת»»£¬Èí¼þ´¥·¢
}
void ADC_Init_Config(void)
{
ADC_GPIO_Init();
ADC_DMA_Mode_Init();
}
void ADC_Calculate(void)
{
float Adc_Wave_WaterFlow = 0.0;
float Adc_Wave_WaterTemp = 0.0;
float Adc_Sample_Watertemp = 0.0;
float Adc_Sample_Waterflow = 0.0;
float Adc_Cavity_Watertemp = 0.0;
float Adc_Cavity_Waterflow = 0.0;
float Adc_M_Changer_Watertemp = 0.0;
float Adc_M_Changer_Waterflow = 0.0;
static u16 Adc_count = 0,ErrorWave_WaterFlow=0,ErrorWave_WaterTemp = 0;
static u16 ErrorSample_Watertemp = 0,ErrorSample_Waterflow = 0;
static u16 ErrorCavity_Watertemp = 0,ErrorCavity_Waterflow = 0;
static u16 ErrorM_Changer_Watertemp = 0,ErrorM_Changer_Waterflow = 0;
static vu16 max[4] = {0}, min[4] = {0}, sum[4] = {0};
int i = 0;
if(Adc_count == 0)
{
for(i=0;i<8;i )
{
min[i]=max[i]=sum[i]=ADC_ConvertValue[i];
}
}
else
{
for(i = 0; i < 8; i )
{
sum[i] = ADC_ConvertValue[i];
if(max[i] <= ADC_ConvertValue[i]) /* ±£´æ×î´óÖµ */
max[i] = ADC_ConvertValue[i];
if(min[i] >= ADC_ConvertValue[i]) /* ±£´æ×îСֵ */
min[i] = ADC_ConvertValue[i];
}
}
Adc_count ;
if(Adc_count == 10)
{
sum[i] -= min[i] max[i]; /* ³ýÈ¥×î´óÖµºÍ×îСֵ */
ADC_AvgValue[i] = sum[i] >> 3;
}
Adc_Wave_WaterFlow = (float)(ADC_AvgValue[0] * 3.3f/4096);
Waveguide_Water_Flow = (float)(Adc_Wave_WaterFlow * 4.00f - 4.00 )*10;
Adc_Sample_Waterflow = (float)(ADC_AvgValue[1] * 3.3f/4096);
Sample_Waterflow = (float)(Adc_Sample_Waterflow * 4.00f - 4.00 )*10;
Adc_Cavity_Waterflow = (float)(ADC_AvgValue[2] * 3.3f/4096);
Cavity_Waterflow = (float)(Adc_Cavity_Waterflow * 4.00f - 4.00 )*10;
Adc_M_Changer_Waterflow = (float)(ADC_AvgValue[3] * 3.3f/4096);
M_Changer_Waterflow = (float)(Adc_M_Changer_Waterflow * 4.00f - 4.00 )*10;
Adc_Wave_WaterTemp = (float)(ADC_AvgValue[4]* 3.3f/4096);
Waveguide_Water_Temp = (float)(Adc_Wave_WaterTemp * 25.00f - 25.00f )*10;
Adc_Sample_Watertemp = (float)(ADC_AvgValue[5]* 3.3f/4096);
Sample_Watertemp = (float)(Adc_Sample_Watertemp * 25.00f - 25.00f )*10;
Adc_Cavity_Watertemp = (float)(ADC_AvgValue[6]* 3.3f/4096);
Cavity_Watertemp = (float)(Adc_Cavity_Watertemp * 25.00f - 25.00f )*10;
Adc_M_Changer_Watertemp =(float)(ADC_AvgValue[7]* 3.3f/4096);
M_Changer_Watertemp = (float)(Adc_M_Changer_Watertemp * 25.00f - 25.00f )*10;
if(Waveguide_Water_Flow < Alarm_Waveguide_Flow)
{
ErrorWave_WaterFlow ;
if(ErrorWave_WaterFlow > 1000)
{
Alarm_Msg_Error = 0x0040;
ErrorWave_WaterFlow = 1001;
//alarm led
}
}
else if(Sample_Waterflow < Alarm_Sample_Waterflow)
{
ErrorSample_Waterflow ;
if(ErrorSample_Waterflow > 1000)
{
Alarm_Msg_Error = 0x0041;
ErrorSample_Waterflow = 1001;
//alarm led
}
}
else if(Cavity_Waterflow < Alarm_Cavity_Waterflow)
{
ErrorCavity_Waterflow ;
if(ErrorCavity_Waterflow > 1000)
{
Alarm_Msg_Error = 0x0042;
ErrorCavity_Waterflow = 1001;
//alarm led
}
}
else if(M_Changer_Waterflow < Alarm_M_Changer_Waterflow)
{
ErrorM_Changer_Waterflow ;
if(ErrorM_Changer_Waterflow > 1000)
{
Alarm_Msg_Error = 0x0043;
ErrorCavity_Waterflow = 1001;
//alarm led
}
}
else if(Waveguide_Water_Temp > Alarm_Waveguide_Temp)
{
ErrorWave_WaterTemp ;
if(ErrorWave_WaterTemp > 1000)
{
Alarm_Msg_Error = 0X0080;
ErrorWave_WaterTemp = 1001;
//alarm led
}
}
else if(Sample_Watertemp > Alarm_Sample_Watertemp)
{
ErrorSample_Watertemp ;
if(ErrorSample_Watertemp > 1000)
{
Alarm_Msg_Error = 0X0081;
ErrorSample_Watertemp = 1001;
//alarm led
}
}
else if(Cavity_Watertemp > Alarm_Cavity_Watertemp)
{
ErrorCavity_Watertemp ;
if(ErrorCavity_Watertemp > 1000)
{
Alarm_Msg_Error = 0X0081;
ErrorCavity_Watertemp = 1001;
//alarm led
}
}
else if(M_Changer_Watertemp > Alarm_M_Changer_Watertemp)
{
ErrorM_Changer_Watertemp ;
if(ErrorM_Changer_Watertemp > 1000)
{
Alarm_Msg_Error = 0X0081;
ErrorM_Changer_Watertemp = 1001;
//alarm led
}
}
}
评论