STM32F030学习笔记(1):ADC单/双通道采集详解

 1.实现单通道

        如 pa0 pa1 pa2 温度传感器

        1.实现单通道步骤

                1):配置RCC   GPIOA,UART1,ADC1使能

                        RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA,ENABLE);
                        RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);

                        RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);

                2):配置GPIO 

                        GPIO_InitTypeDef GPIO_InitStruct={
                                .GPIO_Pin = GPIO_Pin_9 ,//串口发送引脚,方便调试
                                .GPIO_Mode = GPIO_Mode_AF,
                                .GPIO_Speed = GPIO_Speed_Level_3,
                                .GPIO_OType = GPIO_OType_PP,
                                .GPIO_PuPd = GPIO_PuPd_NOPULL,
                            };
                        GPIO_Init(GPIOA,&GPIO_InitStruct);

                        GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_1);//配置9脚位复用

                        GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0;//传感器TAM引脚
                        GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AIN;//模拟输入
                        GPIO_InitStruct.GPIO_Speed = GPIO_Speed_Level_3;
                        GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;

                        GPIO_Init(GPIOA,&GPIO_InitStruct);

                3):配置UART1

                        USART_InitTypeDef USART_InitStruct={
                                .USART_BaudRate = 115200,//波特率
                                .USART_WordLength = USART_WordLength_8b,//数据位 8
                                .USART_StopBits = USART_StopBits_1,//停止位 1
                                .USART_Parity = USART_Parity_No,//校验位 无
                                .USART_Mode = USART_Mode_Rx | USART_Mode_Tx,//rx接收,tx发送
                                .USART_HardwareFlowControl = USART_HardwareFlowControl_None,//无流控
                       };
                      USART_Init(USART1,&USART_InitStruct);
                      USART_Cmd(USART1,ENABLE);//使能串口

                4):配置ADC1

                        //adc时钟初始化
                        RCC_ADCCLKConfig(RCC_ADCCLK_PCLK_Div4);

                        //adc1 初始化
                        ADC_InitTypeDef ADC_InitStruct={
                                .ADC_Resolution = ADC_Resolution_12b, //配置数字信号值的分辨率为12位
                                .ADC_ContinuousConvMode = DISABLE, //配置不要进行持续的模数转换  DISABLE   持续转换 ENABLE
                                .ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None, //启用软件触发,禁止外部启用模数转换
                                .ADC_DataAlign = ADC_DataAlign_Right, //右对齐
                                .ADC_ScanDirection = ADC_ScanDirection_Backward, //配置adc扫描方式 向下扫描
                            };
                            ADC_Init(ADC1,&ADC_InitStruct);
                             //校准ADC1
                            ADC_GetCalibrationFactor(ADC1);
                            //使能adc1
                            ADC_Cmd(ADC1,ENABLE);
                            //等待adc1启用
                            while(ADC_GetFlagStatus(ADC1,ADC_FLAG_ADEN) == RESET);
                            //开始校准ADC1 开始转换
                            ADC_StartOfConversion(ADC1);

完整代码:

   串口部分

uart.h
---------------------//分隔符,不要复制
#ifndef __UART_H_
#define __UART_H_

void uart_init(void);
void uart_tx_data(uint16_t a);
void uart_tx_char(char a);
void delay(void);

#endif
--------------------//分隔符,不要复制
uart.c
--------------------//分隔符,不要复制
#include "stm32f0xx.h"                  // Device header
#include "uart.h"

void uart_init()
{
	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA,ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
		
	GPIO_InitTypeDef GPIO_InitStruct={
		.GPIO_Pin = GPIO_Pin_9 ,//| GPIO_Pin_10,
		.GPIO_Mode = GPIO_Mode_AF,
		.GPIO_Speed = GPIO_Speed_Level_3,
		.GPIO_OType = GPIO_OType_PP,
		.GPIO_PuPd = GPIO_PuPd_NOPULL,
	};
	GPIO_Init(GPIOA,&GPIO_InitStruct);
	
	//GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10;
	//GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;
	//GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
	//GPIO_Init(GPIOA,&GPIO_InitStruct);
	
	GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_1);
	//GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_1);
	
//UART1_init
	USART_InitTypeDef USART_InitStruct={
		.USART_BaudRate = 115200,
		.USART_WordLength = USART_WordLength_8b,
		.USART_StopBits = USART_StopBits_1,
		.USART_Parity = USART_Parity_No,
		.USART_Mode = USART_Mode_Rx | USART_Mode_Tx,
		.USART_HardwareFlowControl = USART_HardwareFlowControl_None,
	};
	USART_Init(USART1,&USART_InitStruct);
	USART_Cmd(USART1,ENABLE);
}
//延时
void delay()
{
	int i=0x4C4B40;
	while(i--);
}
//串口发送 接收到的ADC采集到的数据
void uart_tx_data(uint16_t a)
{
	USART_SendData(USART1,a);
	while(USART_GetFlagStatus(USART1,USART_FLAG_TC) == RESET);
}
//串口发送一个字符
void uart_tx_char(char a)
{
	USART_SendData(USART1,a);
	while(USART_GetFlagStatus(USART1,USART_FLAG_TC) == RESET);
}
---------------------------//分隔符,不要复制


ADC部分

adc.h
-----------------------------------//分隔符,不要复制
#ifndef __ADC_H_
#define __ADC_H_

void adc_init(void);
uint32_t get_val(uint32_t ADC_Channel);

#endif
-----------------------------------//分隔符,不要复制
adc.h
-----------------------------------//分隔符,不要复制
#include "stm32f0xx.h"                  // Device header
#include "adc.h"

void adc_init(void)
{
	//系统时钟初始化  gpioa adc1
	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA,ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);
	//adc时钟初始化 
	RCC_ADCCLKConfig(RCC_ADCCLK_PCLK_Div4);
	//gpioa 初始化
	GPIO_InitTypeDef GPIO_InitStruct={
		.GPIO_Pin = GPIO_Pin_0,//| GPIO_Pin_1 | GPIO_Pin_2,
		.GPIO_Mode = GPIO_Mode_AN,
		.GPIO_Speed = GPIO_Speed_Level_3,
		.GPIO_PuPd = GPIO_PuPd_NOPULL,
	};
	GPIO_Init(GPIOA,&GPIO_InitStruct);
	//adc1 初始化
	ADC_InitTypeDef ADC_InitStruct={
		.ADC_Resolution = ADC_Resolution_12b, //配置数字信号值的分辨率为12位
		.ADC_ContinuousConvMode = DISABLE, //配置不要进行持续的模数转换 DISABLE   持续转换 ENABLE 
		.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None, //启用软件触发,禁止外部启用模数转换
		.ADC_DataAlign = ADC_DataAlign_Right, //右对齐
		.ADC_ScanDirection = ADC_ScanDirection_Backward, //配置adc扫描方式 向下扫描
	};
	ADC_Init(ADC1,&ADC_InitStruct);
	//校准ADC1 
	ADC_GetCalibrationFactor(ADC1);
	//启用adc1
	ADC_Cmd(ADC1,ENABLE);
	//等待adc1启用
	while(ADC_GetFlagStatus(ADC1,ADC_FLAG_ADEN) == RESET);
	//开始校准ADC1 开始转换
	ADC_StartOfConversion(ADC1);
}
//采集数据 ADC_Channel 填 ADC_Channel_0
uint32_t get_val(uint32_t ADC_Channel)
{
	//配置adc1的采样时长
	ADC_ChannelConfig(ADC1,ADC_Channel,ADC_SampleTime_239_5Cycles);
	//开启多通道
	ADC1->CHSELR=(uint32_t)ADC_Channel;
	//开始转换
	ADC_StartOfConversion(ADC1);
	//等待转换完成
	while(ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC) == RESET);
	//返回转换数据
	return ADC_GetConversionValue(ADC1);
}
-----------------------------------//分隔符,不要复制

主函数

#include "stm32f0xx.h" // Device header
#include "uart.h"
#include "adc.h"
//发送高八位数据
uint16_t get_h_data(uint16_t a)
{
	uint16_t h_data;
	h_data = (a>>8) & (0xff);
	return h_data;
}
//发送低八位数据
uint16_t get_l_data(uint16_t a)
{
	uint16_t l_data;
	l_data = a & (0xff);
	return l_data;
}

int main(void)
{
	uint16_t a;
	uart_init();
	adc_init();
	
	while (1)
	{
		delay();
		//  TAM端
		a = get_val(ADC_Channel_0);
		uart_tx_data(get_h_data(a));
		uart_tx_data(get_l_data(a));
		delay();
	}
}

 

30为 ‘\0’的16进制    01 B3 为采集到的值(16进制) 

        

        

物联沃分享整理
物联沃-IOTWORD物联网 » STM32F030学习笔记(1):ADC单/双通道采集详解

发表评论