On-chip ADC conversion experiments stm32

Principle shown below:

BAT_DET to pin PB0, VSYS dc voltage of 3.7V. Let's look at the relationship between the conversion adc 103 and the GPIO pins

We only use the direct selection ADC1 channel 8 on table

I used the following to configure and use experimental ADC1 register

 1 #include "ADC.h"
 2 
 3 
 4 void ADCInit(void)
 5 {
 6     
 7     ADC_InitTypeDef ADC_InitStructure; 
 8     GPIO_InitTypeDef GPIO_InitStructure;
 9 
10     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB |RCC_APB2Periph_ADC1, ENABLE );    //ʹÄÜADC1ͨµÀʱÖÓ
11     
12     RCC_ADCCLKConfig(RCC_PCLK2_Div6); //ÉèÖÃADC·ÖƵÒò×Ó6 72M/6=12,ADC×î´óʱ¼ä²»Äܳ¬¹ý14M
13 
14     //PB0 ×÷ΪģÄâͨµÀÊäÈëÒý½Å 
15     
16     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
17     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;        //Ä£ÄâÊäÈëÒý½Å
18     GPIO_Init(GPIOB, &GPIO_InitStructure);    
19 
20     ADC_DeInit(ADC1);  //¸´Î»ADC1,½«ÍâÉè ADC1 µÄÈ«²¿¼Ä´æÆ÷ÖØÉèΪȱʡֵ
21     
22     ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;    //ADC¹¤×÷ģʽ:ADC1ºÍADC2¹¤×÷ÔÚ¶ÀÁ¢Ä£Ê½
23     ADC_InitStructure.ADC_ScanConvMode = DISABLE;    //Ä£Êýת»»¹¤×÷ÔÚµ¥Í¨µÀģʽ
24     ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;    //£ Ey × Somodari: en: Ä »» ¹¤ I × × ÷ ¥ OUm Somodari »»: en: Ä £ E 
to 25      ADCInitStructureADCExternalTrigConv = ADCExternalTrigConvNone;    // × Somodari »» OEEiþø »· ¢ ¥ ECIa Æo 
26,      ADCInitStructureADCDataAlign = ADCDataAlignRight;    // ADCEyYOOOÆe 
27,      ADCInitStructureADCNbrOfChannel = 1 ;    // EÐoøÐÐæOo × Somodari »» mAADCImAmAEyA 
28,      ADCInit (ADC1, & ADCInitStructure);    // uYADCInitStructOÐOmAIEyoE »IaEeADCxmAAæÆ ÷    
29,   
the 30-      ADCCmd (ADC1, account is created!);    // EAUOmAADC1 
31,      
a 32-      ADCResetCalibration (ADC1);    //ʹÄܸ´Î»Ð£×¼  
33      
34     while(ADC_GetResetCalibrationStatus(ADC1));    //µÈ´ý¸´Î»Ð£×¼½áÊø
35     
36     ADC_StartCalibration(ADC1);     //¿ªÆôADУ׼
37  
38     while(ADC_GetCalibrationStatus(ADC1));     //µÈ´ýУ׼½áÊø
39     
40 }
41 
42 u16 GetADCValue(u8 chose)
43 {
44     ADC_RegularChannelConfig(ADC1, chose, 1, ADC_SampleTime_239Cycles5 );    //ADC1,ADCͨµÀ,²ÉÑùʱ¼äΪ239.5ÖÜÆÚ                      
45   
46     ADC_SoftwareStartConvCmd(ADC1, ENABLE);        //ʹÄÜÖ¸¶¨µÄADC1µÄÈí¼þת»»Æô¶¯¹¦ÄÜ    
47      
48     while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC ));//µÈ´ýת»»½áÊø
49 
50     return ADC_GetConversionValue(ADC1);    //·µ»Ø×î½üÒ»´ÎADC1¹æÔò×éµÄת»»½á¹û
51     
52 }
C file
1 #ifndef __ADC_H
2 #define __ADC_H    
3 
4 #include "sys.h"
5 void ADCInit(void);
6 u16  GetADCValue(u8 chose); 
7 
8 #endif 
H file
 1                         #include "sys.h"
 2                         #include "usart.h"
 3                         #include "delay.h"
 4                         #include "ADC.h"
 5                         
 6 #define LED0 PAout(8)// DS0
 7 #define LED1 PBout(1)// DS1    
 8 #define LED2 PBout(3)// DS1
 9 #define LED3 PAout(11)// DS1    
10 
11 
12 
13                     int main(void)
14                     {    
15                         
16                         u16 WADC;
17                         float fadc;            
18                         Stm32_Clock_Init(9); 
19                         delay_init(72); 
20                         uart_init(); 
21               ADCInit();
22                         while(1)
23                         {                
24                          WADC =    GetADCValue(ADC_Channel_8);
25                          fadc =(float)WADC*(3.3/4096);
26                          printf("V1-2 adc : %f \r\n",fadc);
27                          delay_ms(1000);                
28                         }
29                         return 0;
30                         
31                     }
32                         
33                         
34                         
35                 
main

 

Guess you like

Origin www.cnblogs.com/tengye/p/11511816.html