原理图所示:
BAT_DET 接到PB0 引脚,VSYS 是直流3.7V的电压。再来看下103的adc转换和引脚GPIO的关系
我们直接选用ADC1 根据上表格只能使用通道8
下面给我常用的ADC1寄存器配置和使用实验
#include "ADC.h" void ADCInit(void)
{ ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB |RCC_APB2Periph_ADC1, ENABLE ); //ʹÄÜADC1ͨµÀʱÖÓ RCC_ADCCLKConfig(RCC_PCLK2_Div6); //ÉèÖÃADC·ÖƵÒò×Ó6 72M/6=12,ADC×î´óʱ¼ä²»Äܳ¬¹ý14M //PB0 ×÷ΪģÄâͨµÀÊäÈëÒý½Å GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; //Ä£ÄâÊäÈëÒý½Å
GPIO_Init(GPIOB, &GPIO_InitStructure); ADC_DeInit(ADC1); //¸´Î»ADC1,½«ÍâÉè ADC1 µÄÈ«²¿¼Ä´æÆ÷ÖØÉèΪȱʡֵ ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //ADC¹¤×÷ģʽ:ADC1ºÍADC2¹¤×÷ÔÚ¶ÀÁ¢Ä£Ê½
ADC_InitStructure.ADC_ScanConvMode = DISABLE; //Ä£Êýת»»¹¤×÷ÔÚµ¥Í¨µÀģʽ
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; //Ä£Êýת»»¹¤×÷ÔÚµ¥´Îת»»Ä£Ê½
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //ת»»ÓÉÈí¼þ¶ø²»ÊÇÍⲿ´¥·¢Æô¶¯
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //ADCÊý¾ÝÓÒ¶ÔÆë
ADC_InitStructure.ADC_NbrOfChannel = ; //˳Ðò½øÐйæÔòת»»µÄADCͨµÀµÄÊýÄ¿
ADC_Init(ADC1, &ADC_InitStructure); //¸ù¾ÝADC_InitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèADCxµÄ¼Ä´æÆ÷ ADC_Cmd(ADC1, ENABLE); //ʹÄÜÖ¸¶¨µÄADC1 ADC_ResetCalibration(ADC1); //ʹÄܸ´Î»Ð£×¼ while(ADC_GetResetCalibrationStatus(ADC1)); //µÈ´ý¸´Î»Ð£×¼½áÊø ADC_StartCalibration(ADC1); //¿ªÆôADУ׼ while(ADC_GetCalibrationStatus(ADC1)); //µÈ´ýУ׼½áÊø } u16 GetADCValue(u8 chose)
{
ADC_RegularChannelConfig(ADC1, chose, , ADC_SampleTime_239Cycles5 ); //ADC1,ADCͨµÀ,²ÉÑùʱ¼äΪ239.5ÖÜÆÚ ADC_SoftwareStartConvCmd(ADC1, ENABLE); //ʹÄÜÖ¸¶¨µÄADC1µÄÈí¼þת»»Æô¶¯¹¦ÄÜ while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC ));//µÈ´ýת»»½áÊø return ADC_GetConversionValue(ADC1); //·µ»Ø×î½üÒ»´ÎADC1¹æÔò×éµÄת»»½á¹û }
C文件
#ifndef __ADC_H
#define __ADC_H #include "sys.h"
void ADCInit(void);
u16 GetADCValue(u8 chose); #endif
H文件
#include "sys.h"
#include "usart.h"
#include "delay.h"
#include "ADC.h" #define LED0 PAout(8)// DS0
#define LED1 PBout(1)// DS1
#define LED2 PBout(3)// DS1
#define LED3 PAout(11)// DS1 int main(void)
{ u16 WADC;
float fadc;
Stm32_Clock_Init();
delay_init();
uart_init();
ADCInit();
while()
{
WADC = GetADCValue(ADC_Channel_8);
fadc =(float)WADC*(3.3/);
printf("V1-2 adc : %f \r\n",fadc);
delay_ms();
}
return ; }
main