当前位置:网站首页>STM32---ADC
STM32---ADC
2022-07-05 08:16:00 【chen_ bx】
STM32---ADC
ADC Read the port voltage value
Preface
routine : adopt ADC Read PA1 The port collects the voltage value Print through serial port
ADC Input range 2.4V~3.6V Connecting too much voltage will burn the port
Reference self punctual atom
10X series ADC Channel pin relationship


Code
//main.c Code
#include "stm32f10x.h"
#include "usart.h"
#include <stdio.h>
#include "adc.h"
#include "delay.h"
int main(void)
{
u16 adcx;
float temp;
Usart_Init();
delay_init();
Adc_Cfg();
printf("hello,ADC!");
delay_ms(1000);
while(1){
adcx=Get_Adc_Average(ADC_Channel_1,10);
printf("adcx:%d",adcx);
temp=(float)adcx*(3.3/4096);
printf("V:%f",temp);
}
}
//adc.c Code
#include "adc.h"
#include "delay.h"
static void Adc_GPIO_Cfg(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_ADC1,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AIN;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
RCC_ADCCLKConfig(RCC_PCLK2_Div6);// frequency division
ADC_DeInit(ADC1);
}
void Adc_Cfg(void)
{
Adc_GPIO_Cfg();
ADC_InitTypeDef ADC_InitStructure;
ADC_InitStructure.ADC_ContinuousConvMode=DISABLE;
ADC_InitStructure.ADC_DataAlign=ADC_DataAlign_Right;
ADC_InitStructure.ADC_ExternalTrigConv=ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_Mode=ADC_Mode_Independent;
ADC_InitStructure.ADC_NbrOfChannel=1;
ADC_InitStructure.ADC_ScanConvMode=DISABLE;
ADC_Init(ADC1,&ADC_InitStructure);
ADC_Cmd(ADC1,ENABLE);
ADC_ResetCalibration(ADC1);
while(ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1));
}
// get ADC value
//ch: Value channel 0~3
u16 Get_Adc(u8 ch)
{
// Set the specified ADC The rule group channel for , A sequence , Sampling time
ADC_RegularChannelConfig(ADC1, ch, 1, ADC_SampleTime_239Cycles5 ); //ADC1,ADC passageway , The sampling time is 239.5 cycle
ADC_SoftwareStartConvCmd(ADC1, ENABLE); // Enable to designate ADC1 Software conversion start function
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC ));// Wait for the conversion to finish
return ADC_GetConversionValue(ADC1); // Go back to the last time ADC1 Conversion result of rule group
}
u16 Get_Adc_Average(u8 ch,u8 times)
{
u32 temp_val=0;
u8 t;
for(t=0;t<times;t++)
{
temp_val+=Get_Adc(ch);
delay_ms(5);
}
return temp_val/times;
}
//adc.h Code
#ifndef _ADC_H
#define _ADC_H
#include "stm32f10x.h"
void Adc_Cfg(void);
u16 Get_Adc(u8 ch);
u16 Get_Adc_Average(u8 ch,u8 times);
#endif
Serial port print results

adcx:3891
V:3.134V
The port is floating input The read value is 3.134V
PA1 Port connection GND After the ground
adcx:0
V:0V
The port read value is 0V

PA1 Port connection 3.3V after
adcx:4095
V:3.299V
The port read value is 3.299V
ADC Read the internal temperature of the chip
Preface
routine :ADC Read the internal temperature of the chip and output the read temperature value through the serial port
Reference self punctual atom
Block diagram of internal temperature sensor


Precautions for use of internal temperature sensor




Code
//main.c Code
#include "stm32f10x.h"
#include "usart.h"
#include <stdio.h>
#include "adc.h"
#include "delay.h"
int main(void)
{
float temp;
Usart_Init();
delay_init();
Adc_Cfg();
printf("hello,ADC!");
delay_ms(1000);
while(1){
temp=Get_Temprate();
printf("temp:%f\n",temp);
}
}
//adc.c Code
#include "adc.h"
#include "delay.h"
static void Adc_GPIO_Cfg(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_ADC1,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AIN;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
RCC_ADCCLKConfig(RCC_PCLK2_Div6);// frequency division
ADC_DeInit(ADC1);
}
void Adc_Cfg(void)
{
Adc_GPIO_Cfg();
ADC_InitTypeDef ADC_InitStructure;
ADC_InitStructure.ADC_ContinuousConvMode=DISABLE;
ADC_InitStructure.ADC_DataAlign=ADC_DataAlign_Right;
ADC_InitStructure.ADC_ExternalTrigConv=ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_Mode=ADC_Mode_Independent;
ADC_InitStructure.ADC_NbrOfChannel=1;
ADC_InitStructure.ADC_ScanConvMode=DISABLE;
ADC_Init(ADC1,&ADC_InitStructure);
ADC_TempSensorVrefintCmd(ENABLE); // Turn on the internal temperature sensor
ADC_Cmd(ADC1,ENABLE);
ADC_ResetCalibration(ADC1);
while(ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1));
}
// get ADC value
//ch: Value channel 0~3
u16 Get_Adc(u8 ch)
{
// Set the specified ADC The rule group channel for , A sequence , Sampling time
ADC_RegularChannelConfig(ADC1, ch, 1, ADC_SampleTime_239Cycles5 ); //ADC1,ADC passageway , The sampling time is 239.5 cycle
ADC_SoftwareStartConvCmd(ADC1, ENABLE); // Enable to designate ADC1 Software conversion start function
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC ));// Wait for the conversion to finish
return ADC_GetConversionValue(ADC1); // Go back to the last time ADC1 Conversion result of rule group
}
u16 Get_Adc_Average(u8 ch,u8 times)
{
u32 temp_val=0;
u8 t;
for(t=0;t<times;t++)
{
temp_val+=Get_Adc(ch);
delay_ms(5);
}
return temp_val/times;
}
// Get the temperature value
// Return value : Temperature value ( Expanded 100 times , Company :℃.)
double Get_Temprate(void) // Obtain the temperature value of the internal temperature sensor
{
u32 adcx;
double result;
double temperate;
adcx=Get_Adc_Average(ADC_Channel_16,20); // Read channel 16,20 Average the times
temperate=(float)adcx*(3.3/4096); // Voltage value
result=(1.43-temperate)/0.0043+25; // Convert to temperature value
return result;
}
//adc.h Code
#ifndef _ADC_H
#define _ADC_H
#include "stm32f10x.h"
void Adc_Cfg(void);
u16 Get_Adc(u8 ch);
u16 Get_Adc_Average(u8 ch,u8 times);
double Get_Temprate(void);
#endif
Serial port print results

边栏推荐
- Sql Server的存儲過程詳解
- DokuWiki deployment notes
- MySQL之MHA高可用集群
- FIO测试硬盘性能参数和实例详细总结(附源码)
- Void* C is a carrier for realizing polymorphism
- Process communication mode between different hosts -- socket
- 【云原生 | 从零开始学Kubernetes】三、Kubernetes集群管理工具kubectl
- go依赖注入--google开源库wire
- After installing the new version of keil5 or upgrading the JLINK firmware, you will always be prompted about the firmware update
- Stm32--- systick timer
猜你喜欢

Carrier period, electrical speed, carrier period variation
![C WinForm [get file path -- traverse folder pictures] - practical exercise 6](/img/8b/1e470de4e4ecd4fd1bb8e5cf23f466.jpg)
C WinForm [get file path -- traverse folder pictures] - practical exercise 6

Management and use of DokuWiki
![[tutorial 19 of trio basic from introduction to proficiency] detailed introduction of trio as a slave station connecting to the third-party bus (anybus PROFIBUS DP...)](/img/54/2fe86f54af01f10de93818103f2154.jpg)
[tutorial 19 of trio basic from introduction to proficiency] detailed introduction of trio as a slave station connecting to the third-party bus (anybus PROFIBUS DP...)

After installing the new version of keil5 or upgrading the JLINK firmware, you will always be prompted about the firmware update

Working principle and type selection of common mode inductor

Several important parameters of LDO circuit design and type selection

Compilation warning solution sorting in Quartus II

Talk about the function of magnetic beads in circuits

VESC Benjamin test motor parameters
随机推荐
Take you to understand the working principle of lithium battery protection board
My-basic application 1: introduction to my-basic parser
UE像素流,来颗“减肥药”吧!
Basic embedded concepts
Circleq of linked list
Management and use of DokuWiki
Shell script realizes the reading of serial port and the parsing of message
Improve lighting C program
Google sitemap files for rails Projects - Google sitemap files for rails projects
Various types of questions judged by prime numbers within 100 (C language)
Charge pump boost principle - this article will give you a simple understanding
My-basic application 2: my-basic installation and operation
Some tips for using source insight (solve the problem of selecting all)
STM32 outputs 1PPS with adjustable phase
Array integration initialization (C language)
Bluetooth hc-05 pairing process and precautions
Soem EtherCAT source code analysis I (data type definition)
Working principle and type selection of common mode inductor
Briefly talk about the identification protocol of mobile port -bc1.2
Ble encryption details