01 |
#include <stm32f10x_lib.h> |
02 |
#include "system.h" |
03 |
#include "usart.h" |
04 |
#include "adc.h" |
05 |
#include "stdio.h" |
06 |
07 |
#define LED1 PAout(4) |
08 |
#define LED2 PAout(5) |
09 |
10 |
#define VREF 3.3 //参考电压 |
11 |
void Gpio_Init( void ); |
12 |
13 |
int main( void ) |
14 |
{ |
15 |
u16 ADValue; |
16 |
float temp; |
17 |
18 |
Rcc_Init(9); //系统时钟设置 |
19 |
Usart1_Init(72,9600); //设置串口时钟和波特率 |
20 |
21 |
Adc1_Init(8,7); //使用8通道采样,采样时间系数为7(111),据手册可得采样时间为 (239.5+12.5)/12= 21 (us) |
22 |
Gpio_Init(); |
23 |
24 |
while (1){ |
25 |
|
26 |
ADValue = Get_Adc(ADC_1,8); |
27 |
temp = ( float )VREF*(ADValue/4096); //ADC精度为12位精度,即达到 VREF电压时为 2^12 = 4096 |
28 |
29 |
printf ( "\r\n ADValue = %.2fv\r\n" ,temp); |
30 |
31 |
LED2 = !LED2; |
32 |
33 |
delay(100000); //延时100ms |
34 |
35 |
} |
36 |
} |
37 |
38 |
39 |
void Gpio_Init( void ) |
40 |
{ |
41 |
RCC->APB2ENR|=1<<2; //使能PORTA时钟 |
42 |
RCC->APB2ENR|=1<<3; //使能PORTB时钟 |
43 |
|
44 |
45 |
GPIOA->CRL&=0xFF0FFFF0; |
46 |
GPIOA->CRL|=0xFF3FFFF0; // PA0设置为模拟输入,PA4推挽输出 |
47 |
48 |
GPIOB->CRL&=0xFFFFFFF0; |
49 |
GPIOB->CRL|=0xFFFFFFF0; // PB0设置为模拟输入 |
50 |
51 |
|
52 |
//USART1 串口I/O设置 |
53 |
54 |
GPIOA -> CRH&=0xFFFFF00F; //设置USART1 的Tx(PA.9)为第二功能推挽,50MHz;Rx(PA.10)为浮空输入 |
55 |
GPIOA -> CRH|=0x000008B0; |
56 |
} |
01 |
#include <stm32f10x_lib.h> |
02 |
#include "adc.h" |
03 |
04 |
05 |
//ADC1采样初始化 |
06 |
//独立工做模式 |
07 |
//参数说明: |
08 |
// ADC_CH_x 选择使用通道(0~17),目前暂支持0~15通道 |
09 |
// ADC_CH_SMP 设定采样周期(0~7) |
10 |
//采样周期算法: |
11 |
12 |
void Adc1_Init(u8 ADC_CH_x,u8 ADC_CH_SMP) |
13 |
{ |
14 |
RCC -> APB2ENR |= 1<<9; //开启ADC1时钟 |
15 |
RCC -> APB2RSTR |= 1<<9; //复位ADC1 |
16 |
RCC -> APB2RSTR &= ~(1<<9); //ADC1复位结束 |
17 |
18 |
RCC -> CFGR &= ~(3<<14); //分频因子清零 |
19 |
RCC -> CFGR |= 2<<14; //设定分频因数为2,PCLK2 6分频后做为ADC时钟 |
20 |
21 |
ADC1 -> CR1 &= 0xF0FFFF; //工做模式清零 |
22 |
ADC1 -> CR1 |= 0<<16; //设定为独立模式 |
23 |
ADC1 -> CR1 &= ~(1<<8); //非扫描工做模式 |
24 |
ADC1 -> CR2 &= ~(1<<1); //关闭连续转换 |
25 |
26 |
ADC1 -> CR2 &= ~(7<<17); //清除规则通道启动事件 |
27 |
ADC1 -> CR2 |= 7<<17; //设定规则通道启动事件为软件启动(SWSTART) |
28 |
29 |
ADC1 -> CR2 |= 1<<20; //使用外部事件触发 SWSTART |
30 |
ADC1 -> CR2 &= ~(1<<11); //设置对齐模式为右对齐 |
31 |
32 |
ADC1 -> SQR1 &= ~(0xF<<20); //清零规则序列的数量 |
33 |
ADC1 -> SQR1 |= 15<<20; //设置规则序列的数量为16 |
34 |
35 |
ADC1 -> SMPR2 &= 0x00000000; //清零通道采样时间 |
36 |
ADC1 -> SMPR1 &= 0xFF000000; |
37 |
38 |
if (ADC_CH_x <= 9 ){ |
39 |
ADC1 -> SMPR2 |= 7<<(ADC_CH_x*3); //设置通道x采样时间,提升采样时间能够提升采样精度 |
40 |
} |
41 |
42 |
if (ADC_CH_x > 9 ){ |
43 |
ADC1 -> SMPR1 |= 7<<((ADC_CH_x-10)*3); |
44 |
} |
45 |
|
46 |
47 |
ADC1 -> CR2 |= 1<<0; //开启AD转换 |
48 |
ADC1 -> CR2 |= 1<<3; //使能复位校准,由硬件清零 |
49 |
while ((ADC1 -> CR2)& (1<<3)); //等待校准结束 |
50 |
ADC1 -> CR2 |= 1<<2; //开启AD校准,由硬件清零 |
51 |
while ((ADC1 -> CR2)& (1<<2)); //等待校准结束 |
52 |
53 |
} |
54 |
55 |
//取得数模转换的值 |
56 |
//参数说明:(参数定义于adc.h) |
57 |
// ADC_x (0~3),选择数模转换器 |
58 |
// ADC_CH_x (0~15),选择通道 |
59 |
u16 Get_Adc(u8 ADC_x,u8 ADC_CH_x) |
60 |
{ |
61 |
u16 data = 0; |
62 |
63 |
switch (ADC_x) |
64 |
{ |
65 |
case 1 : { |
66 |
67 |
ADC1 -> SQR3 &= 0xFFFFFFE0; //清除通道选择 |
68 |
ADC1 -> SQR3 |= ADC_CH_x; //选择通道 |
69 |
ADC1 -> CR2 |= 1<<22; //开启AD转换 |
70 |
while (!(ADC1 -> SR & 1<<1)); //等待转换结束 |
71 |
72 |
data = ADC1->DR; |
73 |
break ; |
74 |
} |
75 |
case 2 : { break ;} |
76 |
case 3 : { break ;} |
77 |
} |
78 |
79 |
return data; |
80 |
} |
1 |
#include <stm32f10x_lib.h> |
2 |
3 |
#define ADC_1 0x01 |
4 |
#define ADC_2 0x02 |
5 |
#define ADC_3 0x03 |
6 |
7 |
void Adc1_Init(u8 ADC_CH_x,u8 ADC_CH_SMP); |
8 |
u16 Get_Adc(u8 ADC_x,u8 ADC_CH_x); |
001 |
#include "stm32f10x.h" |
002 |
#include "stdio.h" |
003 |
004 |
005 |
#define PRINTF_ON 1 |
006 |
#define VREF 3.3 // 参考电压 |
007 |
008 |
009 |
void RCC_Configuration( void ); |
010 |
void GPIO_Configuration( void ); |
011 |
void USART_Configuration( void ); |
012 |
void ADC_Configuration( void ); |
013 |
014 |
015 |
int main( void ) |
016 |
{ |
017 |
float ADValue = 0.00; |
018 |
u32 delayTime = 0; |
019 |
020 |
RCC_Configuration(); |
021 |
GPIO_Configuration(); |
022 |
USART_Configuration(); |
023 |
ADC_Configuration(); |
024 |
025 |
while (1) |
026 |
{ |
027 |
if (delayTime++ >=2000000) |
028 |
{ |
029 |
delayTime = 0; |
030 |
ADValue = VREF*ADC_GetConversionValue(ADC1)/0x0fff; |
031 |
printf ( "\r\n ADValue = %.2fv\r\n" ,ADValue); |
032 |
|
033 |
} |
034 |
} |
035 |
} |
036 |
037 |
|
038 |
void GPIO_Configuration( void ) |
039 |
{ |
040 |
GPIO_InitTypeDef GPIO_InitStructure; |
041 |
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 ; |
042 |
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; |
043 |
GPIO_Init(GPIOA , &GPIO_InitStructure); |
044 |
045 |
046 |
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; |
047 |
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; |
048 |
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; |
049 |
GPIO_Init(GPIOA , &GPIO_InitStructure); |
050 |
051 |
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; |
052 |
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; |
053 |
GPIO_Init(GPIOA , &GPIO_InitStructure); |
054 |
} |
055 |
056 |
void ADC_Configuration( void ) |
057 |
{ |
058 |
ADC_InitTypeDef ADC_InitStructure; |
059 |
060 |
RCC_ADCCLKConfig(RCC_PCLK2_Div4); //配置ADC时钟分频 |
061 |
062 |
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; |
063 |
ADC_InitStructure.ADC_ScanConvMode = ENABLE; |
064 |
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; |
065 |
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; |
066 |
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; |
067 |
ADC_InitStructure.ADC_NbrOfChannel = 1; |
068 |
ADC_Init(ADC1,&ADC_InitStructure); |
069 |
|
070 |
ADC_RegularChannelConfig(ADC1,ADC_Channel_8,1,ADC_SampleTime_55Cycles5); |
071 |
ADC_Cmd(ADC1,ENABLE); |
072 |
ADC_ResetCalibration(ADC1); |
073 |
while (ADC_GetResetCalibrationStatus(ADC1)); |
074 |
ADC_StartCalibration(ADC1); |
075 |
while (ADC_GetCalibrationStatus(ADC1)); |
076 |
ADC_SoftwareStartConvCmd(ADC1,ENABLE); |
077 |
078 |
079 |
} |
080 |
081 |
082 |
void RCC_Configuration( void ) |
083 |
{ |
084 |
/* 定义枚举类型变量 HSEStartUpStatus */ |
085 |
ErrorStatus HSEStartUpStatus; |