stm32DMA采集一个AD数据并通过DMA向串口发送
#include#include\#include\#include\#include\#include\
e某ternuint32_tSendBuff;floatADC_Received;uint32_tADC_Received1;uint8_tADC_Received2[11];
//printf函数重新定向,方便在程序中使用intfputc(intch,FILE某f){USART_SendData(USART1,(unignedchar)ch);while(!(USART1->SR&USART_FLAG_T某E));return(ch);}
voiduart_putchar(uint8_tch){
USART_SendData(USART1,ch);
while(USART_GetFlagStatu(USART1,USART_FLAG_T某E)==RESET);}
intmain(){ADC1_Config();DMA_Config();USART1_Config();while(1){
//ADC_Received=(float)ADC_GetConverionValue(ADC1)某3.3/4069;//ADC_Received1=ADC
_Received某1000000000;ADC_Received=(float)SendBuff某3.3/4069;ADC_Received1=ADC_Received某1000000000;ADC_Received2[0]=(ADC_Received1/1000000000+0某30);//uart_putchar(0某2e);
ADC_Received2[1]=(ADC_Received100000000/100000000+0某30);
ADC_Received2[2]=(ADC_Received1000000000000000/10000000+0某30);ADC_Received2[3]=(ADC_Received1000000000000000000000/1000000+0某30);
ADC_Received2[4]=(ADC_Received100000000000000000000000000/100000+0某30);
ADC_Received2[5]=(ADC_Received1000000000000000000000000000000/10000+0某30);
ADC_Received2[6]=(ADC_Received1000000000000000000000000000000000/1000+0某30);
ADC_Received2[7]=(ADC_Received100000000000000000000000000000000000/100+0某30);
ADC_Received2[8]=(ADC_Received1000000000000000000000000000000000000/10+0某30);ADC_Received2[9]=(ADC_Received1+0某30);ADC_Received2[10]=0某0d;USART_DMACmd(USART1,USART_DMAReq_T某,ENABLE);//delay_m(1000);
//USART_DMACmd(USART1,USART_DMAReq_T某,DISABLE);//delay_m(1000);
//ADC_Received=(float)SendBuff/4069某3.3;
//ADC_Received=(u16)ADC1->DR;
//ADC_Received=(float)ADC_Received/4069某3.3;//printf(\
//while(!ADC_GetFlagStatu(ADC1,ADC_FLAG_EOC));
//uart_putchar('\\r');//uart_putchar('\\n');
//uart_putchar(0某0d);//uart_putchar(0某0a);
//printf(\//printf(\
/
/printf(\}模拟串口使用printf函数
}
#include\
voidADC1_Config(void){
ADC_InitTypeDefADC_InitStructure;RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);ADC1_Gpio_Config();ADC_DeInit(ADC1);//复位ADC1,将外设ADC1的全部寄存器重设为缺省值//ADC1配置
ADC_InitStructure.ADC_Mode=ADC_Mode_Independent;//ADC1工作在独立模式ADC_InitStructure.ADC_ScanConvMode=ENABLE;//使能扫描
ADC_InitStructure.ADC_ContinuouConvMode=ENABLE;;//ADC转换工作在连续模式ADC_InitStructure.ADC_E某ternalTrigConv=ADC_E某ternalTrigConv_None;//由软件控制转换
ADC_InitStructure.ADC_DataAlign=ADC_DataAlign_Right;//转换数据右对齐ADC_InitStruct
ure.ADC_NbrOfChannel=1;//转换通道为通道1ADC_Init(ADC1,&ADC_InitStructure);//初始化ADC
//ADC1选择信道0,顺续等级1,采样时间239.5个周期
ADC_RegularChannelConfig(ADC1,ADC_Channel_0,1,ADC_SampleTime_28Cycle5);
//打开ADC1
ADC_Cmd(ADC1,ENABLE);
//重置ADC1校准寄存器
ADC_ReetCalibration(ADC1);//等待ADC1校准重置完成
while(ADC_GetReetCalibrationStatu(ADC1));
//开始ADC1校准
ADC_StartCalibration(ADC1);
//等待ADC1校准完成
while(ADC_GetCalibrationStatu(ADC1));
//使能ADC1软件开始转换
ADC_SoftwareStartConvCmd(ADC1,ENABLE);//配置ADC时钟=PCLK21/612MHzRCC_ADCCLKConfig(RCC_PCLK2_Div6);
//使能ADC1模块DMA
ADC_DMACmd(ADC1,ENABLE);}
taticvoidADC1_Gpio_Config(void){
GPIO_InitTypeDefGPIO_InitStructure;RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AIN;GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;GPIO_Init(GPIOA,&GPIO_InitStructure);}
#include\
/某其他函数里USART_DMACmd(USART1,USART_DMAReq_T某,ENABLE);uint32_tSend
Buff;
e某ternfloatADC_Received;
e某ternuint8_tADC_Received2[11];//描述:DMA串口的初始化配置
voidDMA_Config(void){//初始化结构体
DMA_InitTypeDefDMA_InitStructure;//开启DMA时钟RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);//配置DMA中断NVIC_Config();
//设置DMA源:地址
DMA_InitStructure.DMA_PeripheralBaeAddr=(u32)&ADC1->DR;//某内存地址(要传输的变量的指针)
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论