STM32F103 ADC采样 +1.3“ IIC SH1106动态曲线描点显示示例
- ✨显示效果

📑功能描述
🎉通过ADC采样,获取通道4的电压值,将获取的数据通过1.3寸SH1106 OLED 屏幕曲线来直观的显示出来。可以应用于PTC加热台或其他应用场景里面。每采满123个数据点就清零,重新从坐标原地开始显示,实现的动态值在屏幕上方显示出来,可以根据需求自行调整采样频率,采样范围:0-4095。
接线说明
GND---电源地
VCC 3.3v电源
SCL -> PA5(SCL)
SDA -> PA7(SDA)
ADC采样引脚----PA4
🧧工程代码说明
- 📌通过上一篇的《STM32F103+1.3“SH1106 IIC内容滚动显示示例》显示添加ADC功能部分实现。
- 采用标准库开发,欢迎大家移植到自己有需求的应用工程当中。
- 采用DMA通道ADC数据存储器。
- 电压采用区间是0-3.3V,如果需要在其他范围内使用,可以通过运放比例电路将信号转换后接入,保证输入电压在单片机可承受电压范围之内。
⛳ADC功能实现代码
void ADC_DMA_Init(void){
DMA_InitTypeDef DMA_InitStructure;
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADC_DMA_IN4;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = 1;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel1, &DMA_InitStructure);
DMA_Cmd(DMA1_Channel1, ENABLE);
}
void ADC_GPIO_Init(void){
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC,ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
GPIO_InitStructure.GPIO_Pin = ADC_CH4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(ADCPORT, &GPIO_InitStructure);
}
void ADC_Configuration(void){
ADC_InitTypeDef ADC_InitStructure;
ADC_GPIO_Init();
ADC_DMA_Init();
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = ENABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 1, ADC_SampleTime_28Cycles5);
ADC_DMACmd(ADC1, ENABLE);
ADC_Cmd(ADC1, ENABLE);
ADC_ResetCalibration(ADC1);
while(ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1));
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
}
main代码
#include "stm32f10x_adc.h"
#include "stm32f10x_dma.h"
#include "oled_font.h"
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "draw_api.h"
#define ADCPORT GPIOA
#define ADC_CH4 GPIO_Pin_4
#define ADC_CH5 GPIO_Pin_5
#define ADC_CH6 GPIO_Pin_6
#define ADC_CH7 GPIO_Pin_7
u8 flag1s;
u16 ADC_DMA_IN4;
#define ADC1_DR_Address ((uint32_t)0x4001244C)
void ADC_DMA_Init(void);
void ADC_GPIO_Init(void);
void ADC_Configuration(void);
int main(void)
{
char str[5]="";
u16 value;
u8 i=0;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
ADC_DMA_Init();
delay_init();
uart_init(115200);
InitGraph();
ADC_Configuration();
while(1)
{
str[0]=ADC_DMA_IN4/1000+0x30;
str[1]=ADC_DMA_IN4%1000/100+0x30;
str[2]=ADC_DMA_IN4%100/10+0x30;
str[3]=ADC_DMA_IN4%10+0x30;
str[4]='\n';
SetFontSize(1);
DrawString(58,0,str);
value = ADC_DMA_IN4*60/4096;
DrawFastHLine(0,60,128);
DrawFastVLine(5,0,64);
DrawLine(5,0,0,6);
DrawLine(5,0,9,4);
DrawLine(123,56,128,60);
DrawLine(123,64,128,60);
DrawPixel(5+i,60-value);
UpdateScreen();
DelayMs(800);
i++;
if(i == 127)ClearScreen();
i %=127;
}
}
void ADC_DMA_Init(void){
DMA_InitTypeDef DMA_InitStructure;
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADC_DMA_IN4;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = 1;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel1, &DMA_InitStructure);
DMA_Cmd(DMA1_Channel1, ENABLE);
}
void ADC_GPIO_Init(void){
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC,ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
GPIO_InitStructure.GPIO_Pin = ADC_CH4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(ADCPORT, &GPIO_InitStructure);
}
void ADC_Configuration(void){
ADC_InitTypeDef ADC_InitStructure;
ADC_GPIO_Init();
ADC_DMA_Init();
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = ENABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 1, ADC_SampleTime_28Cycles5);
ADC_DMACmd(ADC1, ENABLE);
ADC_Cmd(ADC1, ENABLE);
ADC_ResetCalibration(ADC1);
while(ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1));
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
}
🌿工程源码
链接:https:
提取码:wpme