采集到的数据一直是 4095 不知道哪里出问题了 找了一天没找出来 来此求助 希望大虾们提点提点!!!
附上代码:
#include "stm32f10x.h"
#include "lcd.h"
#include "delay.h"
#include "PIN9.h"
#include "adc.h"
u8 ADC_Value[5] = {0};
u8 ah,al,comm = 0;
u16 ad;
u32 tempu32 = 0;
u8 gototime = 0;
void ADC_GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
/* 使能 ADC1 and GPIOC clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOC, ENABLE);
RCC_ADCCLKConfig(RCC_PCLK2_Div6); //72M/6=12,ADC最大时间不能超过14M
/* 配置Pc0 pc1 pc2为模拟输入 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/* 配置ADC1, 不用DMA, 用软件自己触发 */
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //ADC1工作模式:独立模式
ADC_InitStructure.ADC_ScanConvMode = DISABLE; //单通道模式
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; //单次转换
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //转换由软件触发启动
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //ADC1数据右对齐
ADC_InitStructure.ADC_NbrOfChannel = 1; //顺序进行规则转换的ADC通道的数目
ADC_Init(ADC1, &ADC_InitStructure); //根据ADC_InitStruct中指定的参数,初始化外设ADC1的寄存器
// ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 1, ADC_SampleTime_239Cycles5); //ADC1,ADC通道1,规则采样顺序值为1,采样时间为239.5周期
// ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 1, ADC_SampleTime_239Cycles5); //ADC1,ADC通道1,规则采样顺序值为1,采样时间为239.5周期
ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 1, ADC_SampleTime_239Cycles5); //ADC1,ADC通道1,规则采样顺序值为1,采样时间为239.5周期
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE); //使能ADC1
/* Enable ADC1 reset calibaration register */
ADC_ResetCalibration(ADC1); //重置ADC1的校准寄存器
/* Check the end of ADC1 reset calibration register */
while(ADC_GetResetCalibrationStatus(ADC1)); //获取ADC1重置校准寄存器的状态,设置状态则等待
/* Start ADC1 calibaration */
ADC_StartCalibration(ADC1); //开始ADC1的校准状态
/* Check the end of ADC1 calibration */
while(ADC_GetCalibrationStatus(ADC1)); //等待校准完成
/* Start ADC1 Software Conversion */
ADC_SoftwareStartConvCmd(ADC1, ENABLE); //使能ADC1的软件转换启动功能
}
//数组清零
void ADC_Value_Clear(void)
{
u8 i;
for(i=0;i<5;i++)
{
ADC_Value=0x20; //空格键
}
}
//检测ADC
void ADC_Detection(void)
{
while(Status==SET)Status=ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC);
AD_num=ADC_GetConversionValue(ADC1);
ADC_SoftwareStartConvCmd(ADC1, ENABLE); //软件启动ADC转换
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC )); //等待转换结束
ad = 0;
ad = ADC_GetConversionValue(ADC1); //读取ADC值
delay_ms(1);
// ad &= 0x0fff ;
// return ad
}
void Disp_DWQ(u8 x , u8 y )
{
u16 temp;
u8 temp0,temp1,temp2,temp3,temp4;
// 1K-10K电阻
u8 i;
ADC_Value_Clear(); //清空数据
ADC_Detection(); //检测AD值
temp=ad; //备份
temp0=temp/10000;
temp1=temp%10000/1000;
temp2=temp%1000/100;
temp3=temp%100/10;
temp4=temp%10;
ADC_Value[0]=temp0+48;
ADC_Value[1]=temp1+48;
ADC_Value[2]=temp2+48;
ADC_Value[3]=temp3+48;
ADC_Value[4]=temp4+48;
if(x==1) x=2;
else if(x==2)x=1;
// ERR=PIN9_Detection_D(); //检测单头9芯
Write12864(0x80+8*x+y,0); //合并生成坐标地址写入LCD12864
for(i=0;i<5;i++)
{
// if(Pin_Num!=0x20)
Write12864(ADC_Value,1);
// delay_ms(1);
}
// Write12864(0x80+8*(x+2)+y,0); //合并生成坐标地址写入LCD12864
// Write12864(ERR+48,1); //下两行显示错误号
}
|