现象:用沾水的纸巾擦拭土壤传感器表面
ADC单通道的配置,里面有各种各样的注释,这里话了
主函数:
#include "stm32f10x.h" #include "oled.h" #include "ADC.h" uint16_t ADValue=0,V=0; int main() { OLED_Init(); MADC_Init(); OLED_ShowString(1,1,"ADValue:"); OLED_ShowString(3,1,"V:"); while(1) { ADValue=ADC_GetValue(); V=ADValue/4095.0*3.3*100; OLED_ShowNum(1,9,ADValue,4); OLED_ShowNum(3,3,V/100,1); OLED_ShowString(3,4,"."); OLED_ShowNum(3,5,V0,2); } }
ADC.c
#include "ADC.h" #include "Delay.h" // Device header void MADC_Init(void) { GPIO_InitTypeDef GPIO_InitStruct; //定义GPIO结构体变量的初始化 ADC_InitTypeDef ADC_InitStruct;//定义ADC结构体变量的初始化 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_ADC1,ENABLE);//开启GPIO和ADC的时钟 RCC_ADCCLKConfig(RCC_PCLK2_Div6);//设置ADC分频因子时钟,6分频 72M/6=12,ADC最大时间不超过144M GPIO_InitStruct.GPIO_Pin=GPIO_Pin_6;//ADC通道6 GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AIN; ///模拟输入 GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz; GPIO_Init(GPIOA,&GPIO_InitStruct); /* 选择ADC,通道数、规则组(通道在哪里)、转换周期 */ // ADC_RegularChannelConfig(ADC1,ADC_Channel_6,1,ADC_SampleTime_55Cycles5)//选择输入通道 ADC_RegularChannelConfig(ADC1,ADC_Channel_7,1,ADC_SampleTime_55Cycles5); ADC_InitStruct.ADC_Mode = ADC_Mode_Independent;//ADC工作模式。独立工作模式 ADC_InitStruct.ADC_ScanConvMode = DISABLE; //非扫描模式 ADC_InitStruct.ADC_ContinuousConvMode = DISABLE;///单次转换 ADC_InitStruct.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;//不使用外部触发,使用内部软件触发 ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right;//右对齐 ADC_InitStruct.ADC_NbrOfChannel = 2.///需要扫描的通道数 ADC_Init(ADC1, &ADC_InitStruct);//ADC初始化 ADC_Cmd(ADC1, ENABLE);//开启AD转换器 ADC_ResetCalibration(ADC1)//开始复位校准 while(ADC_GetResetCalibrationStatus(ADC1);//获取ADC重置校准寄存器状态 ADC_StartCalibration(ADC1);//开始指定ADC的校准状态 while(ADC_GetCalibrationStatus(ADC1);//获取指定ADC的校准状态 } /******************************************************************************* * 函 数 名 : ADC_GetValue * 函数功能 : 获取ADC的转换值 *******************************************************************************/ u16 ADC_GetValue() { ADC_SoftwareStartConvCmd(ADC1, ENABLE);//使能指定ADC1软件转换启动功能 while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC ) == RESET);//等待转换标志位置 return ADC_GetConversionValue(ADC1);//阅读ADC转换函数并返回a }
ADC.h
#ifndef __ADC_H_ #define __ADC_H_ #include "stm32f10x.h" // Device header void MADC_Init(void); u16 ADC_GetValue(); #endif