找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2546|回復: 0
打印 上一主題 下一主題
收起左側(cè)

stm32單片機與YL-69土壤濕度檢測模塊的程序調(diào)試問題

[復制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:597761 發(fā)表于 2022-4-4 21:41 | 只看該作者 |只看大圖 回帖獎勵 |倒序瀏覽 |閱讀模式
用的是YL-69 4線的土壤濕度檢測,連接的A0口模擬輸入adc檢測,  電路如下


問題如圖,檢測出的結(jié)果上面的數(shù)據(jù)為直接得到ADC返回電壓 adc代碼中 Get_Adc(u8 ch) 函數(shù)的返回值
下面的數(shù)據(jù)為 取10次的平均電壓后在進行公式計算得到的結(jié)果,公式為  shidu1=(4092-temp_val)/3292*100;   但是得到的數(shù)據(jù)只有240、40來回變(Get_Adc_Average(u8 ch,u8 times)該函數(shù)返回值
ps 我按上面公式反著來計算也達不到一樣的值啊
           
ADC的代碼
  1. #include "adc.h"
  2. #include "delay.h"
  3. int shidu1;

  4. void Adc_Init(void)
  5. {
  6.         GPIO_InitTypeDef  GPIO_InitStructure;
  7.         ADC_InitTypeDef                ADC_InitStruct;
  8.         
  9.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_ADC1,ENABLE);
  10. /*PA_1éèÖÃÎaÄ£Äaêäèë*/
  11.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;  
  12.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  13.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  14.         GPIO_Init(GPIOA, &GPIO_InitStructure);        

  15.         RCC_ADCCLKConfig(RCC_PCLK2_Div6);
  16.         
  17.         ADC_DeInit(ADC1);//¸′λADC
  18.         
  19.         //3õê¼»ˉADC2Îêy
  20.         ADC_InitStruct.ADC_ContinuousConvMode=DISABLE;
  21.         ADC_InitStruct.ADC_DataAlign=ADC_DataAlign_Right;
  22.         ADC_InitStruct.ADC_ExternalTrigConv=ADC_ExternalTrigConv_None;
  23.         ADC_InitStruct.ADC_Mode=ADC_Mode_Independent;
  24.         ADC_InitStruct.ADC_NbrOfChannel=1;
  25.         ADC_InitStruct.ADC_ScanConvMode=DISABLE;
  26.         
  27.         ADC_Init(ADC1, &ADC_InitStruct);
  28.         
  29.         ADC_Cmd(ADC1,ENABLE);
  30.         
  31.         ADC_ResetCalibration(ADC1);
  32.          
  33.         while(ADC_GetResetCalibrationStatus(ADC1));
  34.         
  35.         ADC_StartCalibration(ADC1);

  36.         while(ADC_GetCalibrationStatus(ADC1));
  37.                

  38. }

  39. u16  Get_Adc(u8 ch)
  40. {
  41.         
  42.         ADC_RegularChannelConfig(ADC1,ch,1, ADC_SampleTime_239Cycles5);
  43.         
  44.         ADC_SoftwareStartConvCmd(ADC1,ENABLE);
  45.         
  46.         while(!ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC));
  47.         
  48.         return ADC_GetConversionValue(ADC1);
  49. }

  50. u16 Get_Adc_Average(u8 ch,u8 times)
  51. {
  52.         u32 temp_val=0;
  53.         u8 t;
  54.         for(t=0;t<times;t++)
  55.         {
  56.                 temp_val+=Get_Adc(ch);
  57.                 delay_ms(1);
  58.         }
  59.         temp_val=temp_val/times;
  60.         shidu1=(4092-temp_val)/3292*100;
  61.         return shidu1;
  62. }

復制代碼

主函數(shù)代碼
  1. #include"stm32f10x.h"
  2. #include "delay.h"
  3. #include "led.h"
  4. #include "i2c.h"
  5. #include "oled.h"
  6. #include "GUI.h"
  7. #include "dht11.h"
  8. #include "SGP30.h"
  9. #include "adc.h"

  10. int shidu;

  11. int main(void)
  12. {
  13.         u8 soliHumi,s1;
  14.         
  15.         NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2)
  16.         
  17.         delay_init()
  18.         LED_Init()
  19.         IIC_Init()
  20.         OLED_Init()
  21.         OLED_Clear(0)
  22.         OLED_display_string_8x16(0,0,"SYSTEM INIT");
  23.         OLED_display_string_8x16(0,16,"OLED INIT");
  24.         
  25.         Adc_Init();
  26.         OLED_display_string_8x16(0,64,"YL-69 INIT");
  27.         delay_ms(50);
  28.         OLED_Clear(0);

  29.         OLED_display_string_8x16(0,82,"soilhumi1:");
  30.         OLED_display_string_8x16(0,98,"soilhumi2:");
  31.         
  32.         while(1) {
  33.                 shidu = Get_Adc_Average(ADC_Channel_1,10);
  34.                 s1 = Get_Adc_Average(ADC_Channel_1,10);
  35.                 soliHumi = Get_Adc(ADC_Channel_1);
  36.                 OLED_modify_num(40,82,soliHumi,16);
  37.                 OLED_modify_num(40,98,s1,16);
  38.                 //LED0=!LED0;
  39.                 delay_ms(100);
  40.         }
  41. }
復制代碼
分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復 返回頂部 返回列表