stm32,DMA采集一个AD数据,并通过DMA向串口发送
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
//delay_ms(1000);
//ADC_Received = (float) SendBuff/4069*3.3;
//ADC_Received = (u16)ADC1 -> DR;
//ADC_Received = (float)ADC_Received/4069*3.3;
//printf("\r\n v = %f V \r\n",ADC_Received);
//开启DMA时钟
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
//配置DMA中断
NVIC_Config();
//设置DMA源:地址
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&ADC1 -> DR;
//*内存地址(要传输的变量的指针)
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&SendBuff;
//外设作为数据传输的来源
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
//指定DMA通道的DMA缓存的大小,单位为数据单位。
//*外设数据单位数据宽度为16位
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
//*内存数据单位数据宽度为16位
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
ADC_Received2[5]=(ADC_Received1%1000000000%100000000%10000000%1000000%100000/10000 + 0x30);
ADC_Received2[6]=(ADC_Received1%1000000000%100000000%10000000%1000000%100000%10000/1000 + 0x30);
ADC_Received2[7]=(ADC_Received1%1000000000%100000000%10000000%1000000%100000%10000%1000/100 + 0x30);
ADC_Received2[8]=(ADC_Received1%1000000000%100000000%10000000%1000000%100000%10000%1000%100/10 + 0x30);
return (ch);
}
void usart_putchar(uint8_t ch)
{
USART_SendData(USART1,ch);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE) == RESET);
}
int main()
{
ADC1_Config();
ADC_Received2[9]=(ADC_Received1%10 + 0x30);
ADC_Received2[10]=0x0d;
USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE);
//delay_ms(1000);
//USABaidu NhomakorabeaT_DMACmd(USART1, USART_DMAReq_Tx, DISABLE);
uint32_t ADC_Received1;
uint8_t ADC_Received2[11];
//printf函数重新定向,方便在程序中使用
int fputc(int ch, FILE *f)
{
USART_SendData(USART1, (unsigned char) ch);
while (!(USART1->SR & USART_FLAG_TXE));
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_Init(GPIOA , &GPIO_InitStructure);
}
#include "DMA1.h"
/*其他函数里USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE);*/
//*优先级:高
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
//*禁止内存到内存的传输
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
//*配置DMA1的1通道
DMA_Init(DMA1_Channel1, &DMA_InitStructure);
ADC_Received1 = ADC_Received * 1000000000;
ADC_Received2[0]=(ADC_Received1/1000000000 + 0x30);
//usart_putchar(0x2e);
ADC_Received2[1]=(ADC_Received1%1000000000/100000000 + 0x30);
ADC_Received2[2]=(ADC_Received1%1000000000%100000000/10000000 + 0x30);
ADC_Received2[3]=(ADC_Received1%1000000000%100000000%10000000/1000000 + 0x30);
ADC_Received2[4]=(ADC_Received1%1000000000%100000000%10000000%1000000/100000 + 0x30);
//****************************************************///
//****************************************************///
//设置DMA源:地址
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&USART1 -> DR;
DMA_InitStructure.DMA_BufferSize = 1;
//*外设地址不增
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
//*内存地址不增
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;
ADC1_Gpio_Config();
ADC_DeInit(ADC1); //复位ADC1,将外设ADC1的全部寄存器重设为缺省值
// ADC1配置
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //ADC1工作在独立模式
ADC_InitStructure.ADC_ScanConvMode = ENABLE;//使能扫描
//*DMA模式:一次传输/循环
//DMA_Mode_Circular工作在循环缓存模式
// DMA_Mode_Normal工作在正常缓存模式
// DMA_InitStructure.DMA_Mode = DMA_Mode_Normal ;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
//打开ADC1
ADC_Cmd(ADC1, ENABLE);
//重置ADC1校准寄存器
ADC_ResetCalibration(ADC1);
//等待ADC1校准重置完成
while(ADC_GetResetCalibrationStatus(ADC1));
//开始ADC1校准
ADC_StartCalibration(ADC1);
//使能ADC1模块DMA
ADC_DMACmd(ADC1, ENABLE);
}
static void ADC1_Gpio_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;;//ADC转换工作在连续模式
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;//由软件控制转换
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//转换数据右对齐
//使能DMA
DMA_Cmd (DMA1_Channel1,ENABLE);
//配置DMA发送完成后产生中断
//DMA_ITConfig(DMA1_Channel1,DMA_IT_TC,ENABLE);
//****************************************************///
//while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC ));
//usart_putchar('\r');
//usart_putchar('\n');
//usart_putchar(0x0d);
//usart_putchar(0x0a);
//printf("\r");
uint32_t SendBuff;
extern float ADC_Received;
extern uint8_t ADC_Received2[11];
//描述:DMA串口的初始化配置
void DMA_Config(void)
{
//初始化结构体
DMA_InitTypeDef DMA_InitStructure;
//等待ADC1校准完成
while(ADC_GetCalibrationStatus(ADC1));
//使能ADC1软件开始转换
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
//配置ADC时钟=PCLK2 1/6 12MHz
RCC_ADCCLKConfig(RCC_PCLK2_Div6);
#include <stm32f10x.h>
#include "ADC1.h"
#include "DMA1.h"
#include "USART1.h"
#include "time.h"
#include "stdio.h"
extern uint32_t SendBuff;
float ADC_Received;
//*内存地址(要传输的变量的指针)
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)ADC_Received2;
DMA_Config();
USART1_Config();
while(1)
{
//ADC_Received = (float)ADC_GetConversionValue(ADC1)*3.3/4069;
//ADC_Received1 = ADC_Received * 1000000000;
ADC_Received = (float)SendBuff*3.3/4069;
//printf("\n");
//printf("\r\n V = %fv\r\n",ADC_Received);
}
}
#include "ADC1.h"
void ADC1_Config(void)
{
ADC_InitTypeDef ADC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
ADC_InitStructure.ADC_NbrOfChannel = 1;//转换通道为通道1
ADC_Init(ADC1, &ADC_InitStructure); //初始化ADC
//ADC1选择信道0,顺续等级1,采样时间239.5个周期
ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_28Cycles5);
//ADC_Received = (float) SendBuff/4069*3.3;
//ADC_Received = (u16)ADC1 -> DR;
//ADC_Received = (float)ADC_Received/4069*3.3;
//printf("\r\n v = %f V \r\n",ADC_Received);
//开启DMA时钟
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
//配置DMA中断
NVIC_Config();
//设置DMA源:地址
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&ADC1 -> DR;
//*内存地址(要传输的变量的指针)
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&SendBuff;
//外设作为数据传输的来源
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
//指定DMA通道的DMA缓存的大小,单位为数据单位。
//*外设数据单位数据宽度为16位
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
//*内存数据单位数据宽度为16位
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
ADC_Received2[5]=(ADC_Received1%1000000000%100000000%10000000%1000000%100000/10000 + 0x30);
ADC_Received2[6]=(ADC_Received1%1000000000%100000000%10000000%1000000%100000%10000/1000 + 0x30);
ADC_Received2[7]=(ADC_Received1%1000000000%100000000%10000000%1000000%100000%10000%1000/100 + 0x30);
ADC_Received2[8]=(ADC_Received1%1000000000%100000000%10000000%1000000%100000%10000%1000%100/10 + 0x30);
return (ch);
}
void usart_putchar(uint8_t ch)
{
USART_SendData(USART1,ch);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE) == RESET);
}
int main()
{
ADC1_Config();
ADC_Received2[9]=(ADC_Received1%10 + 0x30);
ADC_Received2[10]=0x0d;
USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE);
//delay_ms(1000);
//USABaidu NhomakorabeaT_DMACmd(USART1, USART_DMAReq_Tx, DISABLE);
uint32_t ADC_Received1;
uint8_t ADC_Received2[11];
//printf函数重新定向,方便在程序中使用
int fputc(int ch, FILE *f)
{
USART_SendData(USART1, (unsigned char) ch);
while (!(USART1->SR & USART_FLAG_TXE));
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_Init(GPIOA , &GPIO_InitStructure);
}
#include "DMA1.h"
/*其他函数里USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE);*/
//*优先级:高
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
//*禁止内存到内存的传输
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
//*配置DMA1的1通道
DMA_Init(DMA1_Channel1, &DMA_InitStructure);
ADC_Received1 = ADC_Received * 1000000000;
ADC_Received2[0]=(ADC_Received1/1000000000 + 0x30);
//usart_putchar(0x2e);
ADC_Received2[1]=(ADC_Received1%1000000000/100000000 + 0x30);
ADC_Received2[2]=(ADC_Received1%1000000000%100000000/10000000 + 0x30);
ADC_Received2[3]=(ADC_Received1%1000000000%100000000%10000000/1000000 + 0x30);
ADC_Received2[4]=(ADC_Received1%1000000000%100000000%10000000%1000000/100000 + 0x30);
//****************************************************///
//****************************************************///
//设置DMA源:地址
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)&USART1 -> DR;
DMA_InitStructure.DMA_BufferSize = 1;
//*外设地址不增
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
//*内存地址不增
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;
ADC1_Gpio_Config();
ADC_DeInit(ADC1); //复位ADC1,将外设ADC1的全部寄存器重设为缺省值
// ADC1配置
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //ADC1工作在独立模式
ADC_InitStructure.ADC_ScanConvMode = ENABLE;//使能扫描
//*DMA模式:一次传输/循环
//DMA_Mode_Circular工作在循环缓存模式
// DMA_Mode_Normal工作在正常缓存模式
// DMA_InitStructure.DMA_Mode = DMA_Mode_Normal ;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
//打开ADC1
ADC_Cmd(ADC1, ENABLE);
//重置ADC1校准寄存器
ADC_ResetCalibration(ADC1);
//等待ADC1校准重置完成
while(ADC_GetResetCalibrationStatus(ADC1));
//开始ADC1校准
ADC_StartCalibration(ADC1);
//使能ADC1模块DMA
ADC_DMACmd(ADC1, ENABLE);
}
static void ADC1_Gpio_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;;//ADC转换工作在连续模式
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;//由软件控制转换
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//转换数据右对齐
//使能DMA
DMA_Cmd (DMA1_Channel1,ENABLE);
//配置DMA发送完成后产生中断
//DMA_ITConfig(DMA1_Channel1,DMA_IT_TC,ENABLE);
//****************************************************///
//while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC ));
//usart_putchar('\r');
//usart_putchar('\n');
//usart_putchar(0x0d);
//usart_putchar(0x0a);
//printf("\r");
uint32_t SendBuff;
extern float ADC_Received;
extern uint8_t ADC_Received2[11];
//描述:DMA串口的初始化配置
void DMA_Config(void)
{
//初始化结构体
DMA_InitTypeDef DMA_InitStructure;
//等待ADC1校准完成
while(ADC_GetCalibrationStatus(ADC1));
//使能ADC1软件开始转换
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
//配置ADC时钟=PCLK2 1/6 12MHz
RCC_ADCCLKConfig(RCC_PCLK2_Div6);
#include <stm32f10x.h>
#include "ADC1.h"
#include "DMA1.h"
#include "USART1.h"
#include "time.h"
#include "stdio.h"
extern uint32_t SendBuff;
float ADC_Received;
//*内存地址(要传输的变量的指针)
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)ADC_Received2;
DMA_Config();
USART1_Config();
while(1)
{
//ADC_Received = (float)ADC_GetConversionValue(ADC1)*3.3/4069;
//ADC_Received1 = ADC_Received * 1000000000;
ADC_Received = (float)SendBuff*3.3/4069;
//printf("\n");
//printf("\r\n V = %fv\r\n",ADC_Received);
}
}
#include "ADC1.h"
void ADC1_Config(void)
{
ADC_InitTypeDef ADC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
ADC_InitStructure.ADC_NbrOfChannel = 1;//转换通道为通道1
ADC_Init(ADC1, &ADC_InitStructure); //初始化ADC
//ADC1选择信道0,顺续等级1,采样时间239.5个周期
ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_28Cycles5);