新手入门
- 积分
- 14
- 金钱
- 14
- 注册时间
- 2023-11-22
- 在线时间
- 3 小时
|
1金钱
stm32F103中ADC多通道转换使用DMA,串口调试助手没有数据传输,大佬可以帮我看看我的代码有什么问题吗?
main.c
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "usart3.h"
#include "esp8266.h"
#include "timer.h"
#include "dht11.h"
#include "lcd.h"
#include "bh1750.h"
#include "ADC.h"
#include "stm32f10x.h"
float c;
float x;
float soil_T;
float soil_H;
float soil_PH;
float CO2;
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
delay_init(); //延时函数初始化
uart_init(115200); //串口初始化为115200
My_ADC_Init();
while (1)
{ soil_T=(float) ADC_Value[0]/4096*3.3; // 读取转换的AD值 //PA0-PA4,ADC1
printf("\r\n ADC value_1 = 0x%04X \r\n", ADC_Value[0]); //数字量
printf("\r\n soil_T = %f V \r\n",soil_T);
soil_H =(float) ADC_Value[1]/4096*3.3; // 读取转换的AD值
printf("\r\n ADC value_2 = 0x%04X \r\n", ADC_Value[1]);
printf("\r\n soil_H = %f V \r\n",soil_H);
soil_PH =(float) ADC_Value[2]/4096*3.3; // 读取转换的AD值
printf("\r\n ADC value_3 = 0x%04X \r\n", ADC_Value[2]);
printf("\r\n soil_PH = %f V \r\n",soil_PH);
CO2 =(float) ADC_Value[3]/4096*3.3; // 读取转换的AD值
printf("\r\n ADC value_4 = 0x%04X \r\n", ADC_Value[3]);
printf("\r\n CO2 = %f V \r\n",CO2);
delay_ms(1000);
} }
adc.c
#include "ADC.h"
uint16_t ADC_Value[5];
void My_ADC_Init()
{
GPIO_InitTypeDef GPIO_InitStruct;
ADC_InitTypeDef ADC_InitStruct;
DMA_InitTypeDef DMA_InitStructure;
//1、使能时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//GPIOA时钟使能
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);//DMA1时钟使能
//2、选择ADC分频
RCC_ADCCLKConfig(RCC_PCLK2_Div6);//时钟为12MHZ
//3、初始化GPIO
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AIN;//模拟输入
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStruct);
//4、选择ADC通道
ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_239Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 2, ADC_SampleTime_239Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_2, 3, ADC_SampleTime_239Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_3, 4, ADC_SampleTime_239Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 5, ADC_SampleTime_239Cycles5);
//5、初始化ADC
ADC_InitStruct.ADC_Mode = ADC_Mode_Independent;//ADC工作模式。独立工作模式
ADC_InitStruct.ADC_ScanConvMode = ENABLE; //扫描模式(多个通道)
ADC_InitStruct.ADC_ContinuousConvMode =ENABLE;//连续转换
//不使用外部触发,使用内部软件触发
ADC_InitStruct.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right;//右对齐
ADC_InitStruct.ADC_NbrOfChannel =5;//需要扫描的通道数
ADC_Init(ADC1, &ADC_InitStruct);//ADC初始化
//6、初始化DMA
//DMA外设地址,ADC数据储存地址
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR;
//外设非增量模式,只读取ADC储存地址里面的
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
//外设数据长度:16位
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)ADC_Value;//存储器地址
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//存储器增量模式
//接收到数据长度:16位
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;//外设到储存器(数据传输方向)
DMA_InitStructure.DMA_BufferSize = 5;//数据传输量
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;// 循环扫描
DMA_InitStructure.DMA_Priority = DMA_Priority_Medium;//中等优先级
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //DMA通道x没有设置为内存到内存传输
DMA_Init(DMA1_Channel1, &DMA_InitStructure);//初始化DMA
//6、使能DMA
DMA_Cmd(DMA1_Channel1,ENABLE);
//7、使能ADC触发DMA
ADC_DMACmd(ADC1,ENABLE);
//8、使能ADC
ADC_Cmd(ADC1, ENABLE);
//9、校准ADC数据
ADC_ResetCalibration(ADC1);//开始复位校准
while(ADC_GetResetCalibrationStatus(ADC1) == SET);//获取ADC重置校准寄存器的状态
ADC_StartCalibration(ADC1);//开始指定ADC的校准状态
while(ADC_GetCalibrationStatus(ADC1) == SET);//获取指定ADC的校准状态
//使能指定的ADC1的软件转换启动功能 (连续转换时,初始化一次就行了)
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
}
|
|