|
用的是YL-69 4线的土壤湿度检测,连接的A0口模拟输入adc检测, 电路如下
问题如图,检测出的结果上面的数据为直接得到ADC返回电压 adc代码中 Get_Adc(u8 ch) 函数的返回值
下面的数据为 取10次的平均电压后在进行公式计算得到的结果,公式为 shidu1=(4092-temp_val)/3292*100; 但是得到的数据只有240、40来回变(Get_Adc_Average(u8 ch,u8 times)该函数返回值)
ps 我按上面公式反着来计算也达不到一样的值啊
ADC的代码
- #include "adc.h"
- #include "delay.h"
- int shidu1;
- void Adc_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- ADC_InitTypeDef ADC_InitStruct;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_ADC1,ENABLE);
- /*PA_1éèÖÃÎaÄ£Äaêäèë*/
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- RCC_ADCCLKConfig(RCC_PCLK2_Div6);
-
- ADC_DeInit(ADC1);//¸′λADC
-
- //3õê¼»ˉADC2Îêy
- ADC_InitStruct.ADC_ContinuousConvMode=DISABLE;
- ADC_InitStruct.ADC_DataAlign=ADC_DataAlign_Right;
- ADC_InitStruct.ADC_ExternalTrigConv=ADC_ExternalTrigConv_None;
- ADC_InitStruct.ADC_Mode=ADC_Mode_Independent;
- ADC_InitStruct.ADC_NbrOfChannel=1;
- ADC_InitStruct.ADC_ScanConvMode=DISABLE;
-
- ADC_Init(ADC1, &ADC_InitStruct);
-
- ADC_Cmd(ADC1,ENABLE);
-
- ADC_ResetCalibration(ADC1);
-
- while(ADC_GetResetCalibrationStatus(ADC1));
-
- ADC_StartCalibration(ADC1);
-
- while(ADC_GetCalibrationStatus(ADC1));
-
- }
- u16 Get_Adc(u8 ch)
- {
-
- ADC_RegularChannelConfig(ADC1,ch,1, ADC_SampleTime_239Cycles5);
-
- ADC_SoftwareStartConvCmd(ADC1,ENABLE);
-
- while(!ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC));
-
- return ADC_GetConversionValue(ADC1);
- }
- u16 Get_Adc_Average(u8 ch,u8 times)
- {
- u32 temp_val=0;
- u8 t;
- for(t=0;t<times;t++)
- {
- temp_val+=Get_Adc(ch);
- delay_ms(1);
- }
- temp_val=temp_val/times;
- shidu1=(4092-temp_val)/3292*100;
- return shidu1;
- }
复制代码
主函数代码
- #include"stm32f10x.h"
- #include "delay.h"
- #include "led.h"
- #include "i2c.h"
- #include "oled.h"
- #include "GUI.h"
- #include "dht11.h"
- #include "SGP30.h"
- #include "adc.h"
- int shidu;
- int main(void)
- {
- u8 soliHumi,s1;
-
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2)
-
- delay_init()
- LED_Init()
- IIC_Init()
- OLED_Init()
- OLED_Clear(0)
- OLED_display_string_8x16(0,0,"SYSTEM INIT");
- OLED_display_string_8x16(0,16,"OLED INIT");
-
- Adc_Init();
- OLED_display_string_8x16(0,64,"YL-69 INIT");
- delay_ms(50);
- OLED_Clear(0);
- OLED_display_string_8x16(0,82,"soilhumi1:");
- OLED_display_string_8x16(0,98,"soilhumi2:");
-
- while(1) {
- shidu = Get_Adc_Average(ADC_Channel_1,10);
- s1 = Get_Adc_Average(ADC_Channel_1,10);
- soliHumi = Get_Adc(ADC_Channel_1);
- OLED_modify_num(40,82,soliHumi,16);
- OLED_modify_num(40,98,s1,16);
- //LED0=!LED0;
- delay_ms(100);
- }
- }
复制代码 |
|