基stm32f103的AD显示在OLCD代码
时间: 2023-05-30 12:03:23 浏览: 95
以下代码是基于STM32F103的ADC和OLED显示屏的基础上,实现将ADC采集到的数据显示在OLED屏幕上的示例代码:
```C
#include "stm32f10x.h"
#include "oled.h"
#include "delay.h"
ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
void ADC_Configuration(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_ADC1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
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_1, 1, ADC_SampleTime_1Cycles5);
ADC_Cmd(ADC1, ENABLE);
ADC_ResetCalibration(ADC1);
while(ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1));
}
int main(void)
{
u16 ad_value;
char str[16];
OLED_Init();
ADC_Configuration();
while(1)
{
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));
ad_value = ADC_GetConversionValue(ADC1);
sprintf(str, "ADC:%d", ad_value);
OLED_ShowString(0, 0, str, 1);
delay_ms(1000);
}
}
```
代码中首先进行了ADC的配置,设置了GPIOA1为模拟输入,ADC1的采样模式为单次采样。在主函数中,循环读取ADC采样到的数据,将其转换成字符串格式,并显示在OLED屏幕上。为了避免显示过快,添加了1秒的延时。
阅读全文